3. INTRODUCTION
We understand Decrease and Conquer in comparison with
Divide and Conquer.
For a given problem, Divide and Conquer divides the problem
into sub problems, finds the solution for the sub problems and
then combines the solutions of the sub problems to get the
solution for the main problem.
The Decrease and Conquer technique is similar to Divide
and Conquer, except, instead of partitioning a problem into
multiple sub problems of smaller size, we use some technique
to reduce our problem into a single problem that is smaller
than the original.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 3
4. INTRODUCTION
There are 3 variations of Decrease and Conquer:
Decrease by a constant
Decrease by a constant factor
Variable size decrease
Decrease by a constant
In this variation, the size of the problem is reduced by the
same constant in each iteration.
This constant is usually 1.
Let us take an example of finding 𝒂𝒏, to understand this case.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 4
5. INTRODUCTION
If 𝒂𝒏
is the problem, we attempt to reduce this problem size
by 1.
On doing this, we get 𝒂𝒏−𝟏.
Let us now build a relationship between the actual problem
and its smaller instance. We have,
𝒂𝒏 = 𝒂𝒏−𝟏 * a
We represent this using the following recurrence,
𝑓 𝑛 =
𝑓 𝑛 − 1 ∗ 𝑎, 𝑛 > 1
𝑎 𝑛 = 1
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 5
6. INTRODUCTION
This variation is diagrammatically represented as follows,
Problem of size n
Sub problem of
size n-1
Solution to the Sub
problem
Solution to the
Original problem
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 6
7. INTRODUCTION
Decrease by a constant factor
In this variation, the size of the problem is reduced by the same
constant factor in each iteration.
This constant is usually 2.
Let us again take the example of finding 𝒂𝒏, to understand this
case.
If the problem is 𝒂𝒏, then after reducing this problem by 2, we
have 𝒂𝒏/𝟐.
This relationship is represented as,
𝒂𝒏
= 𝒂𝒏/𝟐 𝟐
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 7
8. INTRODUCTION
The recurrence for this operation is given by,
𝒂𝒏/𝟐 𝟐
𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛 𝑎𝑛𝑑 + 𝑣𝑒
𝒂( 𝒏−𝟏 )/𝟐 𝟐
∗ 𝒂 𝑖𝑓 𝑛 𝑖𝑠 𝑜𝑑𝑑 𝑎𝑛𝑑 + 𝑣𝑒
𝒂 𝑖𝑓 𝑛 = 1
𝒂𝒏
=
Another classic example for this variation is the Binary Search
algorithm.
This is a debatable example. Some authors consider it to
follow Divide and Conquer, while the others have a different
view.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 8
9. INTRODUCTION
This variation is diagrammatically represented as follows,
Problem of size n
Sub problem of
size n/2
Solution to the Sub
problem
Solution to the
Original problem
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 9
10. INTRODUCTION
Variable size Decrease
As the name clearly says, the size of the problem reduces by
different sizes at each iteration.
Because of this variation, we cannot define a fixed recurrence
for such type of problems.
A classic example for this variation is the Euclid’s algorithm to
find GCD of two numbers, i.e. gcd(m,n)
if( n == 0)
return m;
else
return gcd(n, m mod n);
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 10
11. TOPOLOGICAL SORTING
Based on Directed graphs or Digraphs.
“A Directed Graph is a graph in which directions are
specified for each edge”
Example
Consider a set of five required courses {C1, C2, C3, C4,C5} a
part-time student has to take in some degree program. The
courses can be taken in any order as long as the following
course prerequisites are met:
C1 and C2 have no prerequisites, C3 requires C1 and
C2
C4 requires C3, C5 requires C3 and C4.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 11
12. TOPOLOGICAL SORTING
We now represent the problem as a directed graph
C
1
C
2
C
3
C
4
C
5
Vertices represent courses and the edges represent the
prerequisites.
We now define the topological sorting problem based on this
digraph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 12
13. TOPOLOGICAL SORTING
“For a given digraph, the problem of Topological Sorting
is to list all the vertices of the graph in such a way that, for every
edge in the graph, the vertex where the edge starts is listed
before the vertex where the edge ends.”
An important condition to perform topological sorting is that the
given graph must be a Direct Acyclic Graph(DAG).
Topological Sorting can be performed in 2 ways:
DFS
Source-Removal approach
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 13
14. TOPOLOGICAL SORTING
DFS method
There are 2 simple steps:
Step 1: Find the DFS for the given graph.
Step 2: Reverse this DFS order. The result is the Topological
order for the given DAG.
We now see the procedure to obtain DFS for a given graph. It
is a 2 step process:
Step 1: When a vertex is visited, push it onto the stack.
Step 2: Pop a vertex, when it becomes a dead end.
The sequence of popped vertices is the DFS for the graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 14
15. TOPOLOGICAL SORTING
We now find the DFS sequence for the below graph
h
i
a
d
c f
e
b
g
j
𝑎1
𝑐2
𝑑3
𝑑3,1
POP
Sequence
d
𝑎1
𝑐2
𝑓4
𝑏5
𝑒6
𝑒6,2
e
𝑏5,3
b
𝑓4,4 f
𝑐2,5
c
𝑎1,6 𝑔7
ℎ8
𝑖9
𝑗10
𝑗10,7
j
𝑖9,8
i
ℎ8,9
h
a
𝑔7,10
g
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 15
16. TOPOLOGICAL SORTING
The DFS forest for the given graph is as follows,
h
i
a
d
c f
e
b
g
j
a
c
d f
b
e
g
h
i
j
Tree Edge
Back Edge
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 16
17. TOPOLOGICAL SORTING
We now apply this method for our example and get the
topological order.
C
1
C
2
C
3
C
4
C
5
POP
Sequence
𝐶11
𝐶32
𝐶43
𝐶54
𝐶54,1
𝐶5
𝐶43,2
𝐶4
𝐶32,3
𝐶3
𝐶11,4
𝐶1
𝐶25
𝐶25,5
𝐶2
Topological Order
𝐶2 𝐶1 𝐶3 𝐶4 𝐶5
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 17
18. TOPOLOGICAL SORTING
Source Removal Method
In this method, we start by selecting a vertex in the graph that
does not have any incoming edges.
This vertex is then deleted from the graph with all its outgoing
edges.
This process is repeated until all vertices are deleted from the
graph.
The sequence in which the vertices are deleted gives us the
topological order for that graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 18
20. TOPOLOGICAL SORTING
Exercise
Find the topological ordering for the following graphs using both
DFS and Source Removal methods.
c
a
f
d
b
g
e
a b c d
g
f
e
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 20
21. INSERTION SORT
Consider an array A[0,……,n-1].
Like Selection Sort, Insertion Sort also partitions the given
array into 2 parts,
Sorted Part
Unsorted Part
In each iteration of Insertion Sort,
“An element A[i] is inserted in its appropriate place
among the first i elements of the array that have been already
sorted.”
However, unlike Selection Sort, the position of elements in the
sorted part is not fixed in Insertion Sort.
The positions of the elements in the sorted part keeps changing
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 21
23. INSERTION SORT
Insertion Sort Algorithm
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 23
0 1 2 3 4
85 43 24 36 64
i
0 1 2 3 4
85 43 24 36 64
v = A[i] = A[1] =
43
j = i - 1 = 0
j i
0 1 2 3 4
85 43 24 36 64
while j ≥ 0 and A[j] >v
while j ≥ 0 True and 85 >43 True
A[j + 1] ← A[j]
j ← j − 1
A[1] ← 85
j ← -1
i
0 1 2 3 4
85 85 24 36 64
A[j + 1] ← v
A[0] ← 43
i
0 1 2 3 4
43 85 24 36 64
j i
0 1 2 3 4
43 85 24 36 64
v = A[i] = A[2] =
24
j = i - 1 = 1
while j ≥ 0 True and 85 >24 True
A[2] ← 85
j ← 0
j i
0 1 2 3 4
43 85 85 36 64
j = 0
while j ≥ 0 True and 43 >24 True
A[1] ← 43
j ← -1
i
0 1 2 3 4
43 43 85 36 64
j = -1
A[0] ← 24
i
0 1 2 3 4
24 43 85 36 64
24. INSERTION SORT
Analysis
Input size for the problem is n.
Basic operation is comparison.
The basic operation depends only on the input size n. Therefore the
algorithm has the same Best, Worst and Average case efficiencies.
We now define the summation expression for the given algorithm as
follows,
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 24
𝑪𝒘𝒐𝒓𝒔𝒕 𝒏 =
𝒊=𝟏
𝒏−𝟏
𝒋=𝟎
𝒊−𝟏
𝟏
On solving the above summation, the time complexity of Insertion
Sort is obtained as,
𝑪𝒘𝒐𝒓𝒔𝒕 𝒏 = 𝜽(𝒏𝟐)
25. GRAPH SEARCHING ALGORITHMS
The term Graph Search or Graph Traversal refers to a class of
algorithms that systematically explore the vertices and edges of a
graph.
There are 2 popular Graph Searching Algorithms:
Depth First Search (DFS)
Breadth First Search (BFS)
Depth First Search (DFS)
Depth-first search 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 vertex that has been currently visited.
If there are several adjacent vertices for a vertex, the tie is broken by
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 25
26. GRAPH SEARCHING ALGORITHMS
This process continues until a dead end, i.e., 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, after 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 for the corresponding component.
The operation of DFS is traced using a STACK.
A vertex is pushed on the STACK when it is reached and popped
when it becomes a dead end.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 26
27. GRAPH SEARCHING ALGORITHMS
DFS Tree and DFS Forest
A DFS Tree is a tree that is constructed based on the DFS traversal
performed on a graph.
The starting vertex of the DFS traversal acts as the ROOT node for
the DFS tree.
A DFS Tree is made up of two types of edges:
Tree Edge
Back Edge
A Tree Edge is an edge that connects a vertex to its child in the DFS
traversal.
A Back Edge is an edge that connects a vertex to its ancestors. A
Back Edge exists between a node and its ancestor only if the node is
connected to the ancestor in the original graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 27
28. GRAPH SEARCHING ALGORITHMS
Example
Traverse the following Graphs using DFS. Construct the corresponding DFS
forest.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 28
f b c g
e
a
d
a
b
d
f
c
g
e
29. GRAPH SEARCHING ALGORITHMS
Example
Traverse the following Graphs using DFS. Construct the corresponding DFS
forest.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 29
1
4 2
3
10 9
5
8
7
6
1
2
3
4
9
10
5
6
7
8
30. GRAPH SEARCHING ALGORITHMS
DFS_Algorithm
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 30
A C
D
B
E
A B C D E
V
=
Count =
0
A B C D E
0 0 0 0 0
v = A
dfs(A
)
Count =
1
A B C D E
1 0 0 0 0
Adjacent to v = B, C
w = B
dfs(B
)
v = B
Count =
2
A B C D E
1 2 0 0 0
Adjacent to v = A, C, D
w = A
w = C
dfs(C
)
v = C
Count =
3
A B C D E
1 2 3 0 0
Adjacent to v = A, B, D, E
w = A
w = B
w = D
dfs(D
)
v = D
Count =
4
A B C D E
1 2 3 4 0
Adjacent to v = B, C, E
w = E dfs(E
)
v = E
Count =
5
A B C D E
1 2 3 4 5
Adjacent to v = C, D
w = C
31. GRAPH SEARCHING ALGORITHMS
Breadth First Search (BFS)
Like DFS, Breadth First Search is also a Graph Traversal
algorithm.
In BFS, the vertices of a graph are visited Level by Level.
BFS starts from a root node and visits all the vertices that are
adjacent(one edge away) from the root.
Then, all vertices in the next level(2 edges away from the root) are
visited.
This continues until all the vertices in the same connected
component as the root vertex are visited.
If there still remain any unvisited vertices, the algorithm has to be
restarted at an arbitrary vertex of another connected component of
the graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 31
32. GRAPH SEARCHING ALGORITHMS
BFS is implemented using a QUEUE.
For a given graph, its root node is marked as visited and
inserted into the queue.
Hereafter, for each iteration, BFS identifies all the unvisited
vertices that are adjacent to the FRONT vertex in the queue.
All such vertices are marked as visited and inserted into the
queue.
After the above step, the FRONT vertex is deleted from the
queue.
This process repeats until all vertices are visited in the given
graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 32
33. GRAPH SEARCHING ALGORITHMS
BFS Tree and BFS Forest
A BFS Tree is a tree that is constructed based on the BFS traversal
performed on a graph.
The starting vertex of the BFS traversal acts as the ROOT node for
the BFS tree.
A BFS Tree is made up of two types of edges:
Tree Edge
Cross Edge
A Tree Edge is an edge that connects a vertex to its child in the BFS
traversal.
A Cross Edge is an edge that connects a vertex to its ancestors. A
Back Edge exists between a node and its ancestor only if the node is
connected to the ancestor in the original graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 33
34. GRAPH SEARCHING ALGORITHMS
Exampl
e
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 34
h
i
a
d
c f
e
b
g
j
f,r
Queue
𝒂𝟏
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔
f,r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗
f r
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗 𝒊𝟏𝟎
f
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗 𝒊𝟏𝟎
f
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗 𝒊𝟏𝟎
f
𝒂𝟏 𝒄𝟐 𝒅𝟑 𝒆𝟒 𝒇𝟓 𝒃𝟔 𝒈𝟕 𝒉𝟖 𝒋𝟗 𝒊𝟏𝟎
a
c d e
f b
g
h j
i
BFS Forest
36. GRAPH SEARCHING ALGORITHMS
Practice Examples
Use BFS to traverse the following graphs. Also construct the BFS
forest.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 36
f b c g
e
a
d
1
4 2
3
10 9
5
8
7
6
37. GRAPH SEARCHING ALGORITHMS
DFS vs
BFS
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 37
DFS BFS
Data structure Stack Queue
Number of vertex orderings two orderings one ordering
Edge types (undirected graphs) tree and back edges tree and cross edges
Efficiency for adjacency matrix 𝜃( 𝑉2 ) 𝜃( 𝑉2 )
Efficiency for adjacency lists 𝜃( 𝑉 + 𝐸 ) 𝜃( 𝑉 + 𝐸 )
In the above example, every entry in the stack has 2 subscripts.
The first subscript indicates the order in which the nodes were pushed onto the stack.
The second subscript indicates the order in which the nodes were popped out of the stack.
NOTE:
In the above example, the subscript for each node represents the sequence in which they are visited.
Tree edges are represented by solid lines in the above example.
Cross edges are represented by dotted lines in the above example.