CS353 Midterm Slides
CS353 Midterm Slides
CS353 Midterm Slides
26/12/2021
College of Computing and Informatics
Module 1
Fundamental of Algorithms
Week Learning Outcomes
Classify the types of common problems and various types of data structures
4
Topic Outline
5
The definition of algorithm
What is an algorithm
Algorithm
Input
Computer output
7
The Basic blocks of an algorithm
8
Characteristics of an algorithm
9
Steps for writing an algorithm
1. An algorithm is a procedure. It has two parts; the first part is head and the second
part is body.
2. The Head section consists of keyword Algorithm and Name of the algorithm with
parameter list.
E.g. Algorithm name1(p1, p2,…,p3)
10
Steps for writing an algorithm
4. The compound statements may be enclosed with { and } brackets. if, for, while
can be closed by endif, endfor, endwhile respectively. Proper indention is must
for block.
5. Comments are written using // at the beginning.
6. The identifier should begin by a letter and not by digit. It contains alpha numeric
letters after first letter. No need to mention data types.
7. The left arrow “←” used as assignment operator. E.g. v←10
8. Boolean operators (TRUE, FALSE), Logical operators (AND, OR, NOT) and
Relational operators (<,<=, >, >=,=, ≠, <>) are also used.
9. Input and Output can be done using read and write.
11
Example
Consider Euclid’s algorithm for computing gcd(m, n) in simple steps
Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2.
Step 2 Divide m by n and assign the value of the remainder to r.
Step 3 Assign the value of n to m and the value of r to n. Go to Step 1.
14
Understanding The problem
This is the first step in designing of algorithm.
In this step :
15
Decision making
16
Decision making
• An algorithm used to solve the problem exactly and produce correct result is called an
exact algorithm.
• If the problem is so complex and not able to get exact solution, then we have to choose
an algorithm called an approximation algorithm.
Examples: extracting square roots, solving nonlinear equations, and evaluating definite
integrals.
17
Decision making
The Decision making is done on the following:
18
Methods of Specifying an Algorithm
Algorithm Spesifications
( a) ( b) ( c)
Natural Language Pseudocode Flowchart
Pseudocode is the option that are most widely used nowadays for specifying algorithms.
19
Methods of Specifying an Algorithm
a. Natural Language
• It is very simple and easy to specify an algorithm
Drawbacks:
many times provides a brief specification.
20
Methods of Specifying an Algorithm
b. Pseudocode
• Pseudocode is a mixture of a natural language and programming language constructs.
• Pseudocode is usually more precise than natural language.
For Assignment operation left arrow “←”, for comments two slashes “//”,if condition,
for, while loops are used.
Example:
ALGORITHM Sum(a,b)
//Problem Description: This algorithm performs addition of two numbers
//Input: Two integers a and b
//Output:( a)
Addition of two integers
c←a+b
Natural Language
return c
23
Proving an Algorithm’s Correctness
• The notion of correctness for approximation algorithms is less straightforward than it is for
exact algorithms. The error produced by the algorithm should not exceed a predefined
limit.
24
Analyzing an Algorithm
25
Coding an Algorithm
• It is very essential to write an optimized code (efficient code) to reduce the burden of
compiler.
26
Important Problem Types
Important Problem Types
28
Important Problem Types
(i) Sorting
• The sorting problem is to rearrange the items of a given list in nondecreasing
(ascending) order.
• Sorting can be done on numbers, characters, strings or records.
• To sort student records in alphabetical order of names or by student number or by
student grade-point average. Such a specially chosen piece of information is called a
key.
• An algorithm is said to be in-place if it does not require extra memory, E.g., Quick sort.
• A sorting algorithm is called stable if it preserves the relative order of any two equal
elements in its input.
29
Important Problem Types
(ii) Searching
• The searching problem deals with finding a given value, called a search key, in a given
set.
E.g., Ordinary Linear search and fast binary search.
30
Important Problem Types
32
Activity !!!
In groups , state the
basic types of data
structure and define
their features ?
33
Summary
• Algorithms for the same problem can be based on very different ideas and can solve
the problem with dramatically different speeds.
• Input, output, definiteness, finiteness and efficiency are the main characteristics of any
algorithms
• We introduced the major steps for writing an algorithms and also the steps for
designing and analysis an algorithms in details.
• Sorting problems , Searching problems , String matching problems Graph problems ,
Combinatorial problems , Geometric problems and Numerical problems
• Algorithm design techniques (or “strategies” or “paradigms”) are general
approaches to solving problems algorithmically, applicable to a variety of problems
from different areas of computing.
34
List of Readings
➢ Required Reading :
Chapter 1 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
➢ Recommending Reading :
Chapter 1 from Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, Clifford Stein ( 2022 ). Introduction to Algorithms. MIT Press.
.Fourth edition
35
Thank You
36
ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
College of Computing and Informatics
Module 2
Algorithms Analysis Methods
Week Learning Outcomes
Explain the growth function Asymptotic notation, functions, and running times
40
Topic Outline
41
The Analysis Framework
Analysis of algorithms
• Issues:
• correctness
• time efficiency
• space efficiency
• optimality
• Approaches:
• empirical analysis – less useful
• theoretical analysis – most important
43
The Analysis Framework
Time efficiency also can be called time complexity indicating how fast the algorithm
runs.
Space efficiency, also called space complexity indicating the amount of memory
units required by the algorithm as well as the space needed for its input and output.
44
Theoretical analysis of time efficiency
T(n) ≈ copC(n)
Number of times
running time execution time basic operation is
for basic operation executed
45
Measuring an Input’s Size
• An algorithm’s efficiency is defined as a function of some parameter n indicating
the algorithm’s input size.
.
• For the problem of evaluating a polynomial 𝑝 𝑥 = 𝑎𝑛 𝑥 𝑛 + … . +𝑎0 of degree n, the
size of the parameter will be the polynomial’s degree or the number of its
coefficients, which is larger by 1 than its degree.
• In computing the product of two n ✕ n matrices, the choice of a parameter indicating
an input size does matter.
• Consider a spell-checking algorithm. If the algorithm examines individual characters
of its input, then the size is measured by the number of characters.
• In measuring input size for algorithms solving problems such as checking primality
of a positive integer n. The input is just one number.
• The input size by the number b of bits in the n’s binary representation is
𝑏 = ( log 2 𝑛) + 1 .
46
Units for Measuring Running Time
• Some standard unit of time measurement such as a second, or millisecond, and so on can
be used to measure the running time of a program after implementing the algorithm.
Drawbacks
1. Dependence on the speed of a particular computer.
2. Dependence on the quality of a program implementing the algorithm.
3. The compiler used in generating the machine code.
4. The difficulty of clocking the actual running time of the program.
• One possible approach is to count the number of times each of the algorithm’s operations
is executed. This approach is excessively difficult.
• The most important operation (+, -, *, /) of the algorithm, called the basic operation.
Computing the number of times the basic operation is executed is easy. The total running
time is determined by basic operations count.
47
Input size and basic operation examples
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
48
Orders of Growth
• A difference in running times on small inputs is not what really distinguishes efficient
algorithms from inefficient ones.
• For example, the greatest common divisor of two small numbers, it is not immediately
clear how much more efficient Euclid’s algorithm is compared to the other algorithms, the
difference in algorithm efficiencies becomes clear for larger numbers only.
49
Orders of Growth
• For large values of n, it is the function’s order of growth that counts As shown in table
below, which contains values of a few functions particularly important for analysis of
algorithms
50
Worst-case, Best-case, and Average-case Efficiencies
• Consider Sequential Search algorithm some search key K
ALGORITHM SequentialSearch(A[0..n - 1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n - 1] and a search key K
//Output: The index of the first element in A that matches K or -1 if there are no
// matching elements
i ←0
while i < n and A[i] ≠ K do Clearly, the running time of this algorithm can be quite
i ←i + 1 different for the same list size n.
if i < n return i
else return -1
In the worst case, there is no matching of elements or the first matching element
can found at last on the list.
In the best case, there is matching of elements at first on the list. 51
Worst-case Efficiency
• The worst-case efficiency of an algorithm is its efficiency for the worst case input of size
𝑛.
• The algorithm runs the longest among all possible inputs of that size.
• For the input of size 𝑛, the running time is 𝐶𝑤𝑜𝑟𝑠𝑡 𝑛 = 𝑛 .
• The worst-case analysis provides very important information about an algorithm’s
efficiency by bounding its running time from above it guarantees that for any
instance of size n, the running time will not exceed 𝐶𝑤𝑜𝑟𝑠𝑡 𝑛 , its running time on
the worst-case inputs
52
Best-case Efficiency
• The best-case efficiency of an algorithm is its efficiency for the best case input of size 𝑛.
• The algorithm runs the fastest among all possible inputs of that size 𝑛.
• In sequential search, If we search a first element in list of size 𝑛. (i.e. first element equal
to a search key), then the running time is 𝐶𝑏𝑒𝑠𝑡 𝑛 = 1
53
Average-case Efficiency
• The Average case efficiency lies between best case and worst case.
• To analyze the algorithm’s average case efficiency, we must make some assumptions
about possible inputs of size 𝑛.
54
Average case efficiency
In the case of a successful search, the probability of the first match occurring in the 𝑖𝑡ℎ
position of the list is 𝑝/𝑛 for every 𝑖, and the number of comparisons made by the
algorithm in such a situation is obviously 𝑖.
In the case of an unsuccessful search, the number of comparisons will be 𝑛 with the
probability of such a search being (1 − 𝑝). Therefore,
55
Amortized Efficiency
• In some situations ,a single operation can be expensive, but the total time for an
entire sequence of 𝑛 such operations is always significantly better than the worst-
case efficiency of that single operation multiplied by 𝑛.
• “Amortize” the high cost of such a worst-case occurrence over the entire sequence in
a manner similar to the way a business would amortize the cost of an expensive item
over the years of the item’s productive life.
• This sophisticated approach was discovered by Robert Tarjan, who used it, among
other applications, in developing an interesting variation of the classic binary search
tree
56
Key Points of the Analysis Framework
• Both time and space efficiencies are measured as functions of the algorithm’s input size.
• Time efficiency is measured by counting the number of times the algorithm’s basic
operation is executed.
• Space efficiency is measured by counting the number of extra memory units consumed by
the algorithm.
• The efficiencies of some algorithms may differ significantly for inputs of the same size.
• For such algorithms, we need to distinguish between the worst-case, average-case, and
best-case efficiencies.
• The framework’s primary interest lies in the order of growth of the algorithm’s running time
(extra memory units consumed) as its input size goes to infinity.
57
Asymptotic Notations and Basic Efficiency Classes
Asymptotic notation
• Asymptotic notation is a notation, which is used to take meaningful statement about the
efficiency of a program.
• The efficiency analysis framework concentrates on the order of growth of an algorithm’s
basic operation count as the principal indicator of the algorithm’s efficiency.
• To compare and rank such orders of growth, computer scientists use three notations, they
are:
O - Big oh notation
Ω - Big omega notation
Θ - Big theta notation
59
Asymptotic notation
• Let 𝑡(𝑛) and 𝑔(𝑛) can be any nonnegative functions defined on the set of natural
numbers.
• The algorithm’s running time 𝑡(𝑛) usually indicated by its basic operation count 𝐶(𝑛), and
𝑔(𝑛), some simple function to compare with the count.
Example 1:
𝑶(𝒈(𝒏)) is the set of all functions with a lower or same order of growth as 𝑔(𝑛) (to within a
constant multiple, as 𝑛 goes to infinity).
Thus, to give a few examples, the following assertions are all true:
60
Asymptotic notation
Example 1:
• The second notation, Ω (𝒈(𝒏)), stands for the set of all functions with a higher or same
order of growth as 𝒈(𝒏) (to within a constant multiple, as 𝑛 goes to infinity).
For example,
• The third notian ⍬(𝑔(𝑛)) is the set of all functions that have the same order of
growth as 𝑔(𝑛) (to within a constant multiple, as n goes to infinity). Thus, every
quadratic function
is in ⊝ (𝑛2 )
61
O-notation
O = Asymptotic upper bound = Useful for worst case analysis = Loose bound
62
O-notation
Example :
formally prove that 100 𝑛 + 5 ∈ 𝑂(𝑛2 )
100 𝑛 + 5 ≤ 100 𝑛 + 𝑛 ∀ 𝑛 ≥ 5
= 101 𝑛 ≤ 101𝑛2
as values of the constants c and n0 required by the definition, we can take 101 and 5,
respectively
• the definition gives us a lot of freedom in choosing specific values for constants c
and n0.
• For example, we could also reason that
100𝑛 + 5 ≤ 100𝑛 + 5𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑛 ≥ 1 = 105𝑛
i.e., 100𝑛 + 5 ≤ 105𝑛
i.e., 𝑡(𝑛) ≤ 𝑐𝑔(𝑛)
∴ 100 𝑛 + 5 ∈ 𝑂(𝑛) with c=105 and n0=1
63
Ω-notation
64
Ω-notation
Example
Prove the assertions
𝑛3 + 10 𝑛2 + 4 𝑛 + 2 ∈ Ω (𝑛2 ).
Proof:
𝑛3 + 10 𝑛2 + 4 𝑛 + 2 ≥ 𝑛2 (for all n ≥ 0)
65
⍬-notation
• Where 𝑡(𝑛) and 𝑔(𝑛) are nonnegative functions defined on the set of natural numbers.
• Θ = Asymptotic tight bound = Useful for average case analysis
66
⍬-notation
Example
67
MATHEMATICAL ANALYSIS FOR RECURSIVE ALGORITHMS
Mathematical Analysis of Recursive Algorithms
69
Mathematical Analysis of Recursive Algorithms
EXAMPLE 1:
Compute the factorial function 𝐹(𝑛) = 𝑛! for an arbitrary nonnegative integer n.
Since 𝑛! = 1 •. . . .• (𝑛 − 1) • 𝑛 = (𝑛 − 1)! • 𝑛, 𝑓𝑜𝑟 𝑛 ≥ 1 𝑎𝑛𝑑 0! = 1
by definition, we can compute 𝐹(𝑛) = 𝐹(𝑛 − 1) • 𝑛 with the following recursive
algorithm.
ALGORITHM 𝐹(𝑛)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F(n − 1) * n
70
Mathematical Analysis of Recursive Algorithms
Algorithm analysis
• For simplicity, we consider n itself as an indicator of this algorithm’s input size. i.e. 1.
• The basic operation of the algorithm is multiplication, whose number of executions we
denote M(n). Since the function F(n) is computed according to the formula
F(n) = F(n −1)•n for n > 0.
• The number of multiplications M(n) needed to compute it must satisfy the equality
• M(n − 1) multiplications are spent to compute F(n − 1), and one more multiplication is
needed to multiply the result by n.
71
Mathematical Analysis of Recursive Algorithms
1- Since the calls stop when n = 0, the smallest value of n for which this algorithm
is executed and hence M(n) defined is 0.
2- By inspecting the pseudocode’s exiting line, we can see that when n = 0, the
algorithm performs no multiplications.
72
Mathematical Analysis of Recursive Algorithms
Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications
• 𝑀(𝑛):
• 𝑀(𝑛) = 𝑀(𝑛 − 1) + 1 𝑓𝑜𝑟 𝑛 > 0,
• 𝑀(0) = 0 𝑓𝑜𝑟 𝑛 = 0.
73
Mathematical Analysis of Recursive Algorithms
74
Mathematical Analysis of Recursive Algorithms
75
Activity !!!
• Using method of backward
substitutions Apply similar
analysis for Recursive solution
to the Tower of Hanoi puzzle
76
https://www.codeproject.com/Articles/393158/Towers-of-Hanoi
Analyzing the Time Efficiency of Non-recursive Algorithms
General Plan for Analyzing the Time Efficiency of Non- recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation (in the innermost loop).
3. Check whether the number of times the basic operation is executed depends only on
the size of an input. If it also depends on some additional property, the worst-case,
average-case, and, if necessary, best-case efficiencies have to be investigated
separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation is
executed.
5. Using standard formulas and rules of sum manipulation either find a closed form
formula for the count or at the least, establish its order of growth.
77
Analyzing the Time Efficiency of Non-recursive Algorithms
EXAMPLE 1:
Consider the problem of finding the value of the largest element in a list of n
numbers. Assume that the list is implemented as an array for simplicity
Algorithm analysis
• The measure of an input’s size here is the number of elements in the array, i.e., n.
• There are two operations in the for loop’s body:
o The comparison A[i]> maxval and
o The assignment maxval←A[i].
• The number of comparisons will be the same for all arrays of size n; therefore, there
is no need to distinguish among the worst, average, and best cases here.
79
Analyzing the Time Efficiency of Non-recursive Algorithms
Algorithm analysis
• Let C(n) denotes the number of times this comparison is executed. The algorithm
makes one comparison on each execution of the loop, which is repeated for each
value of the loop’s variable i within the bounds 1 and n − 1, inclusive. Therefore, the
sum for C(n) is calculated as follows:
80
Analyzing the Time Efficiency of Non-recursive Algorithms
EXAMPLE 2:
Consider the element uniqueness problem: check whether all the Elements in a given array
of n elements are distinct.
81
Analyzing the Time Efficiency of Non-recursive Algorithms
Algorithm analysis
• The natural measure of the input’s size here is again n (the number of elements in
the array). Since the innermost loop contains a single operation (the comparison of
two elements), we should consider it as the algorithm’s basic operation.
• The number of element comparisons depends not only on n but also on whether
there are equal elements in the array and, if there are, which array positions they
occupy. We will limit our investigation to the worst case only.
• One comparison is made for each repetition of the innermost loop, i.e., for each
value of the loop variable j between its limits i + 1 and n − 1; this is repeated for each
value of the outer loop, i.e., for each value of the loop variable i between its limits 0
and n − 2.
82
Analyzing the Time Efficiency of Non-recursive Algorithms
Algorithm analysis
83
Empirical Analysis of Algorithm
• Time efficiency and Space efficacy are two major elements for analysis any algorithms
• We introduced the analysis framework and show how can be implemented and
explained Asymptotic notation.
• For some algorithms, the running time can differ considerably for inputs of the same
size, leading to worst-case efficiency, average-case efficiency, and best-case efficiency.
• The established framework for analyzing time efficiency is primarily grounded in the
order of growth of the algorithm’s running time as its input size goes to infinity.
• We showed the mathematical analysis for recursive and non-recursive algorithms with
examples
• Empirical analysis of an algorithm is performed by running a program implementing
the algorithm on a sample of inputs and analyzing the data observed (the basic
operation’s count or physical running time). This often involves generating
pseudorandom numbers.
85
List of Readings
➢ Required Reading :
Chapter 2 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
➢ Recommending Reading :
Chapter 1 from Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, Clifford Stein ( 2022 ). Introduction to Algorithms. MIT Press.
.Fourth edition
86
Thank You
87
ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
College of Computing and Informatics
Module 3
Brute Force and Exhaustive Search
Week Learning Outcomes
➢ Exhaustive Search
Itration 1
Itration 2
Itration 3
Itration 4
Itration 5
Itration 6
Itration 7
Selection Sort
• Algorithm Analysis
• The input size is given by the number of elements n; the basic operation is
the key comparison A[j ] < A[min].
• The number of times it is executed depends only on the array size and is given
by the following sum:
or
• Example
Using Bubble algorithm sort this list 89, 45, 68, 90, 29, 34, 17
and so on
Bubble Sort
• Algorithm Analysis
• The number of key comparisons for the bubble-sort version is the same for all
arrays of size n; it is obtained by a sum that is almost identical to the sum for
selection sort :
• In the worst case of decreasing arrays, it is the same as the number of key
comparisons
Sequential Search and Brute-Force String Matching
Sequential Search
We want to find 𝑖 − 𝑡ℎ𝑒 𝑖𝑛𝑑𝑒𝑥 of the leftmost character of the first matching substring
in the text—such that 𝑡𝑖 = 𝑝 0 , … , 𝑡𝑖 + 𝑗 = 𝑝 𝑗 , … , 𝑡𝑖 + 𝑚 − 1 = 𝑝𝑚 − 1:
text T
pattern P
Brute-Force String Matching
Brute-Force String Matching
Algorithm Analysis
• The algorithm shifts the pattern almost always after a single character comparison.
• The worst case : the algorithm may have to make all m comparisons before
shifting the pattern, which it can happen for each of n − m + 1 tries.
• Worst -case : m(n − m + 1) character comparisons ∈ O(nm) class
• Average-case efficiency should be considerably better than the worst-case
efficiency.
• For searching in random texts, it has been shown to be linear, i.e., 0(n)
Closest-Pair Problem
Closest-Pair Problem
• The problem is for finding the two closest points in a set of n points.
• It is the simplest problems in computational geometry dealing with proximity of
points in the plane or higher-dimensional spaces.
• Points represents such physical objects such as airplanes or post offices as well as
database records, statistical samples, DNA sequences … etc.
• Cluster analysis in statistics is one of the important applications of the closest-pair
problem.
• Hierarchical cluster analysis seeks to organize them in a hierarchy of clusters based
on some similarity metric . Usually similarity the Euclidean distance for numerical
analysis, the Hamming distance for text and other nonnumerical data, metrics
such as
Closest-Pair Problem
• Compute the distance between each pair of distinct points and find a pair with
the smallest distance.
• Considering only the pairs of points (pi, p j ) for which i < j , to avoid computing
distance for same pair twice .
Closest-Pair Problem
Closest-Pair Problem
Algorithm Analysis
• The basic operation of the algorithm is computing the square root which The
number of times will be executed can be computed as follows:
• TSP has been intriguing researchers for the last 150 years.
• The problem asks to find the shortest tour through a given set of n cities that
visits each city exactly once before returning to the city where it started.
• It can be modeled by a weighted graph, with the graph’s vertices representing
the cities and the edge weights specifying the distances.
• Then the problem can be stated as the problem of finding the shortest
Hamiltonian circuit of the graph.
Illustrating a sample of
Hamiltonian circuit
Given n items of known weights w1, w 2 ,..., wn and values v1, v 2 ,..., vn and a knapsack
of capacity W , find the most valuable subset of the items that fit into the knapsack
Example : a transport plane that has to deliver the most valuable set of items to a
remote location without exceeding the plane’s capacity.
Knapsack Problem
• Both TSM and Knapsack problems are the best-known examples for NP-hard
problems – will be discussed in week 11
Knapsack Problem & The exhaustive-search
Knapsack Problem
exhaustive-search
Assignment Problem
Assignment Problem
• There are n people who need to be assigned to execute n jobs, one person per
job. (That is, each person is assigned to exactly one job and each job is assigned
to exactly one person.)
• The cost that would accrue if the ith person is assigned to the j th job is a known
quantity C[i, j ] for each pair i, j = 1, 2,..., n.
• The problem is to find an assignment with the minimum total cost.
Assignment Problem
• For example, for the cost matrix above, (2, 3, 4, 1) indicates the assignment of
Person 1 to Job 2, Person 2 to Job 3, Person 3 to Job 4, and Person 4 to Job 1.
Assignment Problem
a) Graph
b) Traversal’s stack
c) DFS forest with the
tree and back edges
Depth-First Search ( DFS)
• DFS in Graph :
• Starts a graph’s traversal at an arbitrary vertex by marking it as visited
• On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent
to the one it is currently in.
• If there are several such vertices, it can be resolved arbitrarily.
• This process continues until a dead end—a vertex with no adjacent unvisited
vertices— is encountered.
• At a dead end, the algorithm backs up one edge to the vertex it came from and
tries to continue visiting unvisited vertices from there.
• The algorithm eventually halts after backing up to the starting vertex, with the
latter being a dead end. By then, all the vertices in the same connected
component as the starting vertex have been visited. If unvisited vertices still
remain, the depth-first search must be restarted at any one of them.
Depth-First Search ( DFS)
Depth-First Search (DFS)
• It takes just the time proportional to the size of the data structure used for
representing the graph in question.
• For the adjacency matrix representation , the traversal time is in 0(|V |2)
• For the adjacency list representation , it is in 0(|V |+ |E|) where |V | and |E|
are the numbers graph’s vertices and edges, respectively
Breath-First Search ( BFS)
• Examples for applications Breath- First Search
a) Graph
b) Traversal’s stack
c) BFS forest
Breadth-First Search (BFS)
• It proceeds in a concentric manner by visiting first all the vertices that are
adjacent to a starting vertex, then all unvisited vertices two edges apart from it,
and so on, until all the vertices in the same connected component as the starting
vertex are visited.
• If there still remain unvisited vertices, the algorithm has to be restarted at an
arbitrary vertex of another connected component of the graph.
Analysis of BFS:
• Breadth-first search has the same efficiency as depth-first search :
• For the adjacency matrix representation , it is in 0(|V |2) and
• For the adjacency list representation , it is 0(|V |+ |E|).
• In addition , the queue is a FIFO (first-in first-out) structure adds a single
ordering of vertices.
Summary
• The traveling salesman problem, the knapsack problem, and the assignment
problem are typical examples of problems that can be solved, at least
theoretically, by exhaustive-search algorithms.
• Depth-first search (DFS) and breadth-first search (BFS) are two principal graph-
traversal algorithms. By representing a graph in a form of a depth-first or breadth-
first search forest, they help in the investigation of many important properties of
the graph. Both algorithms have the same time efficiency: 0(|V |2) for the
adjacency matrix representation and 0(|V |+ |E|) for the adjacency list
representation.
List of Readings
➢ Required Reading :
Chapter 3 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
Thank You
ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
College of Computing and Informatics
Module 4
Decrease-and-Conquer
Week Learning Outcomes
Describe the insert sort , topological Sorting and analysis their complexity
➢ Insertion Sort
➢ Topological Sorting
➢ Decrease-by-a-Constant-Factor Algorithms
➢ Variable-Size-Decrease Algorithms
Introduction
Sub-Solution
• Once such a relationship is established, Step 3: Combine
it can be exploited either top down or
bottom up. Solution
https://www3.cs.stonybrook.edu/~pramod.ganapathi/doc/algorithms/Algo-DecreaseConquer.pdf
Introduction
https://www.scaler.com/topics/data-structures/topological-sort-algorithm/m/ (1)
Decrease by a constant
1. https://scienceland.info/en/algebra8/euclid-algorithm
Insertion Sort
Insertion Sort
• Lets have a sorted array A[0..n − 1] , a new element x
need to be inserted in the array
• It is required to find an appropriate position for A[n − 1]
among the sorted elements and insert new element x
there.
• By scanning the sorted subarray from right to left until
the first element smaller than or equal to A[n − 1] is
encountered to insert A[n − 1] right after that element.
• The resulting algorithm is called straight insertion sort
or simply insertion sort.
• Insertion sort is clearly based on a recursive idea and
more
• Bottom top implementation is more efficient
https://programmercave0.github.io/blog/2017/08/20/C++-Insertion-Sort-using-STL-%28Sorting%29
Insertion Sort
https://programmercave0.github.io/blog/2017/08/20/C++-Insertion-Sort-using-STL-%28Sorting%29
Insertion Sort
The algorithm analysis:
• The basic operation of the algorithm is the key comparison A[j ] > v .
• The number of comparison depends on input nature.
• In the worst case, A[j ] >v is executed the largest number of times,
i.e., for every j = i − 1, ... , 0.
• Since v = A[i], it happens if and only if A[j ] > A[i] for j = i − 1, ... , 0.
• Thus, for the worst-case input,
we get A[0] > A[1] (for i = 1),
A[1] > A[2] (for i = 2),. . .,
A[n − 2] > A[n − 1] (for i = n − 1)
Insertion Sort
The algorithm analysis:
• The worst-case input is an array of decreasing values
• In the best case, the comparison A[j ] >v is executed only once on every iteration
of the outer loop.
• It happens if and only if A[i − 1] ≤ A[i] for every i = 1,...,n − 1,
i.e., if the input array is already sorted in
non-decreasing order.
Insertion Sort
The algorithm analysis:
• Average-case efficiency is based on investigating the number of element pairs
that are out of order It happens if and only if A[i − 1] ≤ A[i] for every i
= 1,...,n − 1,
• It shows that on randomly ordered arrays, insertion sort makes on average
half as many comparisons as on decreasing arrays,
i.e.,
• This twice-as-fast average-case performance coupled with an excellent
efficiency on almost-sorted arrays
• Insertion sort is better than selection sorting and bubble sort
Topological Sorting
Topological Sorting
What is a directed graph?
• A directed graph, or digraph for short, is a graph with
directions specified for all its edges.
The student can take only one course per term. In which order should the student
take the courses?
Topological Sorting
Motivation Example
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Value 3 14 27 31 39 42 55 70 74 81 85 93 98
r
Iteration 1 l m
Iteration 2 l m r
Iteration 3 l, m r
Binary Search
p value
https://www3.cs.stonybrook.edu/~pramod.ganapathi/doc/algorithms/Algo-DecreaseConquer.pdf
The Lomuto partitioning
https://www3.cs.stonybrook.edu/~pramod.ganapathi/doc/algorithms/Algo-DecreaseConquer.pdf
Quick Select
• To find the kth smallest element in array A[0..n − 1]
• let s be the partition’s split position,
i.e., the index of the array’s element occupied by the pivot after
partitioning.
If s = k − 1, pivot p itself is obviously the kth smallest element
If s > k − 1, the kth smallest element in the entire array can be found as the
kth smallest element in the left part of the partitioned array.
if s < k − 1, it can be found as the (k − s)th smallest element in its right part.
if we do not solve the problem, we reduce its instance to a smaller one,
which can be solved by the same approach, i.e., recursively.
Quick Select
Quick Select
Cbest(n) = n − 1 ∈ 0(n).
Summary
• The topological sorting problem asks to list vertices of a digraph in an order such
that for every edge of the digraph, the vertex it starts at is listed before the vertex it
points to.
• Binary search is a very efficient algorithm for searching in a sorted array. It is a
principal example of a decrease-by-a-constant-factor algorithm.
• the size reduction varies from one iteration of the algorithm to another. Examples
of such variable-size- decrease algorithms include Euclid’s algorithm, the partition-
based algorithm for the selection problem
List of Readings
➢ Required Reading :
Chapter 4 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
Thank You
ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
College of Computing and Informatics
Module 5
Divide-and-Conquer
Week Learning Outcomes
➢ Divide-and-Conquer Explanation
➢ Merge sort & Quick Sort
➢ Binary Tree Traversals and Related Properties
➢ Multiplication of Large Integers and Strassen’s Matrix
Multiplication
➢ The Closest-Pair Problem by Divide-and-Conquer
Divide-and-Conquer Explanation
Introduction
• Divide-and-conquer is the best-known general
algorithm design technique.
• Divide-and-conquer plan is :
1. Divide : a problem is divided into several
subproblems of the same type, ideally
of about equal size.
2. Conquer: the subproblems are solved recursively.
3. Combine : the solutions to the subproblems are
combined to get a solution to the
original problem.
• A divide-and-conquer algorithm is necessarily more
efficient than even a brute-force solution.
• The divide-and-conquer technique can suited for parallel Divide-and-conquer technique
computations used for solving complex problems
Divide-and-conquer
https://www.enjoyalgorithms.com/blog/merge-sort-algorithm
Introduction
Divide-and-conquer Analysis:
• For an instance of size n can be divided into b instances of size n/b, with a of them
needing to be solved. where a and b are constants; a ≥ 1 and b> 1
• Assuming that size n is a power of b
• The recurrence is for the running time :
• f (n) is a function that accounts for the time spent on dividing an instance of size n
into instances of size n/b and combining their solutions
• The order of growth of its solution T (n) depends on the values of the constants a and
b and the order of growth of the function f (n)
Divide-and-conquer Analysis
Master Theorem
T (n) = aT (n/b) + f (n) then
https://www.enjoyalgorithms.com/blog/merge-sort-algorithm
Mergesort Pseudocode
Mergesort Analysis
How efficient is mergesort?
• For simplicity, assume that n is a power of 2, the recurrence relation for the
number of key comparisons C(n) is
• In the worst case, neither of the two arrays becomes empty before the other one
contains just one element
• (e.g., smaller elements may come from the alternating arrays).
• or the worst case, Cmerge(n) = n − 1, and the recurrence
• After a partition is achieved, A[s] will be in its final position in the sorted
array, and we can continue sorting the two subarrays to the left and to the
right of A[s] independently
Quicksort & Hoare Partitioning Pseudocode
Quicksort
https://www.java67.com/2014/07/quicksort-algorithm-in-java-in-place-example.html
Hoare Partitioning – in Quick sort
https://www3.cs.stonybrook.edu/~pramod.ganapathi/doc/algorithms/Algo-DecreaseConquer.pdf
Quick sort Analysis
• The number of key comparisons in the best case satisfies the recurrence
According to the Master Theorem, Cbest(n) ∈ 0(n log2 n); solving it exactly for
https://www.javatpoint.com/binary-tree-java
Binary Tree
• The most important divide-and-conquer algorithms for binary trees are the three
classic traversals: preorder, inorder, and postorder.
• All three traversals visit nodes of a binary tree recursively, i.e., by visiting the
tree’s root and its left and right subtrees. They differ only by the timing of the
root’s visit:
• In the preorder traversal, the root is visited before the left and right subtrees
are visited (in that order).
• In the inorder traversal, the root is visited after visiting its left subtree but
before visiting the right subtree.
• In the postorder traversal, the root is visited after visiting the left and right
subtrees (in that order).
Binary Tree
• Example:
Consider the problem of multiplying two (large) n-digit integers represented by
arrays of their digits such as:
A = 12345678901357986429
B = 87654321284820912836
Multiplication of Large Integers
• The principal insight of the algorithm lies in the discovery that we can
find the product C of two 2 × 2 matrices A and B with just seven
multiplications
Where,
Strassen’s Matrix Multiplication
Algorithm Analysis:
Step 4: For every point 𝑃(𝑥,𝑦)in 𝐶1 , we inspect points in 𝐶2 that may be closer to 𝑃
than d. There can be no more than 6 such points (because 𝑑 ≤ 𝑑2 )
The efficiency of Closest-Pair Problem
Algorithm Analysis:
Summary
• Quicksort works by partitioning its input elements according to their value relative
to some preselected element. Quicksort is noted for its superior efficiency among
n log n algorithms for sorting randomly ordered arrays but also for the quadratic
worst-case efficiency.
• The classic traversals of a binary tree—preorder, inorder, and postorder— and
similar algorithms that require recursive processing of both left and right subtrees
can be considered .
• Strassen’s algorithm needs only seven multiplications to multiply two 2 × 2
matrices. By exploiting the divide-and-conquer technique, this algorithm can
multiply two n × n matrices with about n2.807 multiplications.
• The divide-and-conquer technique can be successfully applied the closest-pair
problem.
List of Readings
➢ Required Reading :
Chapter 5 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
➢ Recommending Reading :
Chapter 4 from Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, Clifford Stein ( 2022 ). Introduction to Algorithms. MIT Press.
.Fourth edition
26/12/2021
College of Computing and Informatics
Module 6
Transform-and-Conquer
Week Learning Outcomes
233
Topic Outline
➢ Transform-and-Conquer Explanation
➢ Presorting
➢ Gaussian Elimination
➢ Balanced Search Trees (AVL trees and its properties )
234
Transform-and-Conquer Explanation
Transform-and-Conquer Explanation
236
Transform-and-Conquer Explanation
237
Instance Simplification - Presorting
Instance Simplification - Presorting
Solve a problem’s instance by transforming it into another simpler/easier instance of the
same problem
Presorting
Many problems involving lists are easier when list is sorted,
• e.g. searching
• computing the median (selection problem)
• checking if all elements are distinct (element uniqueness)
Also:
• Topological sorting helps solving some problems for dags.
• Presorting is used in many geometric algorithms [eg close pr]
239
Instance Simplification - Presorting
How can we fast the sorting process ?
• Theorem: log2 n! n log2 n comparisons are necessary in the worst case to
sort a list of size n by any comparison-based algorithm.
• Note: About n log2 n comparisons are also sufficient to sort array of size n (by
mergesort and also quick sort
240
Searching with Presorting
Presorting-based algorithm:
Stage 1 Sort the array by an efficient sorting algorithm
Stage 2 Apply binary search
241
Element Uniqueness with Presorting
Presorting-based algorithm
Stage 1: sort by efficient sorting algorithm (e.g. mergesort)
Stage 2: scan array to check pairs of adjacent elements
242
Element Uniqueness with Presorting
Presorting-based algorithm
Stage 1: sort by efficient sorting algorithm
(e.g. mergesort)
Stage 2: scan array to check pairs of
adjacent elements
Presorting-based algorithm
244
Element Uniqueness with Presorting
Presorting-based algorithm
245
Activity !!!
• In groups
• Search how hashing is used in
finding uniqness elements in
an array ?
246
Instance simplification – Gaussian Elimination
247
Instance simplification – Gaussian Elimination
Solve the latter by substitutions starting with the last equation and moving up to
the first one
248
Instance simplification – Gaussian Elimination
More Explanation:
The transformation is accomplished by a sequence of elementary operations on
the system’s coefficient matrix (which don’t change the system’s solution):
for i ←1 to n-1 do
replace each of the subsequent rows (i.e., rows i+1, …, n) by
a difference between that row and an appropriate multiple of the i-th row to
make the new coefficient in the i-th column of that row 0
249
Example of Gaussian Elimination
Solve : 2x1 - 4x2 + x3 = 6
3x1 - x2 + x3 = 11
x1 + x2 - x3 = -3
Gaussian elimination
2 -4 1 6 2 -4 1 6
3 -1 1 11 row2 – (3/2)*row1 0 5 -1/2 2
1 1 -1 -3 row3 – (1/2)*row1 0 3 -3/2 -6 row3–(3/5)*row2
2 -4 1 6
0 5 -1/2 2
0 0 -6/5 -36/5
Backward substitution
x3 = (-36/5) / (-6/5) = 6
x2 = (2+(1/2)*6) / 5 = 1
x1 = (6 – 6 + 4*1)/2 = 2
250
Pseudocode and Efficiency of Gaussian Elim.
Stage 1: Reduction to an upper-triangular matrix
for i ← 1 to n-1 do
for j ← i+1 to n do
for k ← i to n+1 do
A[j, k] ← A[j, k] - A[i, k] * A[j, i] / A[i, i] //improve!
253
Taxonomy of Searching Algorithms
• List searching
o sequential search
o binary search
o interpolation search
• Tree searching
o binary search tree
o balanced binary trees:
- AVL trees
- red-black trees
o multiway trees [balanced]:
- 2-3 trees
-2-3-4 trees, B trees
• Hashing
o open hashing (separate chaining)
o closed hashing (open addressing)
254
Binary Search Tree
• Arrange keys in a binary tree with the binary search tree property:
<K >K
Example : 5,3,1,10,12,7,9
Which could/should be the root? Best/Worst?
255
Dictionary Operations on Binary Search Trees
➢ Searching – straightforward
• Insertion – search for key, insert at leaf where search terminated
• Deletion – 3 cases:
• deleting key at a leaf
• deleting key at node with single child
• deleting key at node with two children
➢ Efficiency depends of the tree’s height: log2 n h n-1,
• With height average (random files) be about 3log2 n
• Maximum number of nodes if height k?
• Thus all three operations have
• worst case efficiency: (n)
• average case efficiency: (log n)
256
Balanced Search Trees
257
Balanced trees: AVL trees
Definition An AVL tree is a binary search tree in which, for every node, the
difference between the heights of its left and right subtrees, called the balance
factor, is at most 1 (with the height of an empty tree defined as -1)
Explain?
Tree (a) is an AVL tree; tree (b) is not an AVL tree
258
Rotations
If a key insertion violates the balance requirement at some node, the subtree
rooted at that node is transformed via one of the four rotations.
259
Rotation Types (Single R-rotation)
Single right rotation, or R-rotation:
Imagine rotating the edge connecting the root and its left child in the binary
tree in right side.
Note:
This rotation is performed after a new key is inserted into the left subtree of
the left child of a tree whose root had the balance of +1 before the insertion.
Example of r-rotation
260
Symmetric Single L-rotation
Single lift rotation, or L-rotation:
The mirror image of the single R-rotation.
Note:
It is performed after a new key is inserted into the right subtree of the right
child of a tree whose root had the balance of −1 before the insertion.
261
Rotation Types (Double LR-rotation)
The double left-right rotation (LR- rotation)
A combination of two rotations: we perform the L-rotation of the left subtree of
root r , followed by the R-rotation of the new tree rooted at r
Note: It is performed after a new key is inserted into the right subtree of the
left child of a tree whose root had the balance of +1 before the insertion.
262
Symmetric Double RL-rotation
The double right-left rotation (RL- rotation)
Mirror image for double left-right rotation
Think of it as an exercise?
263
AVL tree construction - an example
264
Analysis of AVL trees
Disadvantages:
• frequent rotations
• complexity
266
Summary
267
List of Readings
➢ Required Reading :
Chapter 6 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
➢ Recommending Reading :
Chapter 6 & Chapter 12 from Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest, Clifford Stein ( 2022 ). Introduction to Algorithms. MIT Press.Fourth edition
268
Thank You
ر
الجامعة السعودية االلكتونية
ر
االلكتونية الجامعة السعودية
26/12/2021
College of Computing and Informatics
Module 7
Transform-and-Conquer
Week Learning Outcomes
273
Topic Outline
274
Heaps and Heapsort
Heaps and Heapsort
Definition
• A heap is a binary tree with keys at its nodes (one key per node) such that:
• It is essentially complete,
• i.e., all its levels are full except possibly the last level, where only some
rightmost keys may be missing
• The key at each node is ≥ all keys in its children (and descendents)
276
Illustration of the heap’s definition
10 10 10
5 7 5 7 5 7
4 2 1 2 1 6 2 1
Note: Heap’s elements are ordered top down (along any path down from its
root), but they are not ordered left to right
277
Some Important Properties of a Heap
• Given n, there exists a unique binary tree (structure) with n nodes that is
essentially complete, with h = log2 n
• A heap can easily be represented as an array (and usually is). Look at the
following example, and then explain why the array representation always
works.
278
Heap’s Array Representation
Example: 9
1 2 3 4 5 6
5 3 9 5 3 1 4 2
1 4 2
Step 1:
Starting with the last (rightmost) parental (ie interior) node, fix the heap rooted at
it, if it doesn’t satisfy the
heap condition: keep exchanging it with its largest child until the heap
condition holds
Step 2:
Repeat Step 1 for the preceding parental node
Bottom up: Adding nodes to heap from bottom to top, pushing elements down,
as needed
Construct a heap for the list 2, 9, 7, 6, 5, 8 . Insert elements into the array.
2 2 2
9 7 > 9 8 9 8
6 5 8 6 5 7 6 5 7
2 9 9
9 8 > 2 8 > 6 8
6 5 7 6 5 7 2 5 7
282
Insert a New Element into a Heap
283
Insert a New Element into a Heap
9 9 10
6 8 > 6 10 > 6 9
2 5 7 10 2 5 7 8 2 5 7 8
Construct a heap for the list 2, 9, 7, 6, 5, 8 [Start with elements in the array.
Process interior nodes from L to R. Add nodes from top to bottom. Add each
node, and push up as needed.
2
29
9/ 2
9/ 2 7
9/ 2 7/ 6
9/ 6 7/ 2; 9/ 6 7/ 2 5; 9/ 6 7/ 2 5, 8; 9/ 6 8/ 2 5, 7
Is this a heap??
285
Heapsort
Stage 1:
Construct a heap for a given list of n keys
Stage 2:
Repeat operation of root removal n-1 times:
Exchange keys in the root and in the last (rightmost) leaf
Decrease heap size by 1
If necessary, repeatedly swap new root (node) with larger child until
the heap condition again holds
286
Example of Sorting by Heapsort
287
Analysis of Heapsort
In-place: yes
Stability: no (e.g., apply heapsort to 1 1)
288
Problem Reduction
Problem Reduction
290
Examples of Solving Problems by Reduction
P = LCM, Q = GCD
Reduce P to Q: Reduce LCM to GCD
LCM(x, y) = (x * y) / GCD(x, y)
LCM(60, 42) = (60*42) / GCD(60, 42) = 2520 / 6 = 420
60=2*2*3*5. 42=2*3*7. GCD=2*3. LCM=2*2*3*5*7.
291
Problem Reduction: Notation
LCM(x, y) = (x * y) / GCD(x, y)
292
Problem Reduction: Best Alg. Performance
Assume reduction steps are faster than Q (eg * and / are Θ(1)).
Assume best alg for GCD is used. Are these stmts true or false?
- The best alg for LCM can be faster than best alg for GCD.
- The best alg for GCD can be faster than best alg for LCM.
- Examples:
Best LCM: O(n) and Best GCD: O(n^2)
Best GCD: O(n) and Best LCM: O(n^2)
294
Summary
• Problem reduction calls for transforming a given problem to another problem that
can be solved by a known algorithm.
• A heap is an essentially complete binary tree with keys (one per node) satisfying
the parental dominance requirement. Though defined as binary trees, heaps are
normally implemented as arrays. Heaps are most important for the efficient
implementation of priority queues; they also underlie heapsort.
• Heapsort is a theoretically important sorting algorithm based on arranging
elements of an array in a heap and then successively removing the largest element
from a remaining heap.
• AVL trees are binary search trees that are always balanced to the extent possible
for a binary tree. The balance is maintained by transformations of four types called
rotations. All basic operations on AVL trees are in O(log n)
295
List of Readings
➢ Required Reading :
Chapter 6 from Anany Levitin (2012) . Introduction to the
Design and Analysis of Algorithms. Pearson, 2012, 3rd edition
296
Thank You