SlideShare a Scribd company logo
1 of 38
DECREASE AND CONQUER
CONTENTS
 Introduction
 Topological Sorting
 Insertion sort
 Graph searching algorithms
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 2
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
TOPOLOGICAL SORTING
Example
C
1
C
2
C
3
C
4
C
5
C
2
C
3
C
4
C
5
Delete C1
C
3
C
4
C
5
Delete C2
C
4
C
5
Delete C3
C
5
Delete C4
Delete C5
Topological Order
C1, C2, C3, C4, C5
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 19
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
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
INSERTION SORT
 The working of Insertion Sort algorithm is demonstrated
below.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 22
89 45 68 90 29 34 17
89 45 68 90 29 34 17
89 45 68 90 29 34 17
Sorted Part Unsorted
Part
89 45 68 90 29 34 17
45 89 68 90 29 34 17
45 89 68 90 29 34 17
45 68 89 90 29 34 17
45 68 89 90 29 34 17
45 68 89 90 29 34 17
45 68 89 90 29 34 17
29 45 68 89 90 34 17
29 45 68 89 90 34 17
29 34 45 68 89 90 17
29 34 45 68 89 90 17
17 29 34 45 68 89 90
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
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,
𝑪𝒘𝒐𝒓𝒔𝒕 𝒏 = 𝜽(𝒏𝟐)
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
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
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
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
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
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
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
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
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
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
GRAPH SEARCHING ALGORITHMS
BFS_Algorithm
Exercise
Trace the BFS algorithm on the following graph.
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 35
a b c
d e f
g
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
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 𝜃( 𝑉 + 𝐸 ) 𝜃( 𝑉 + 𝐸 )
END OF
MODULE 2
9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 38

More Related Content

Similar to Module 2_Decrease and Conquer_2021 Scheme.pptx

Module 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxModule 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxRITIKKUMAR168218
 
The Fundamental theorem of calculus
The Fundamental theorem of calculus The Fundamental theorem of calculus
The Fundamental theorem of calculus AhsanIrshad8
 
A Lexi Search Approach to Generalized Travelling Salesman Problem
A Lexi Search Approach to Generalized Travelling Salesman ProblemA Lexi Search Approach to Generalized Travelling Salesman Problem
A Lexi Search Approach to Generalized Travelling Salesman ProblemBRNSSPublicationHubI
 
Module2 stiffness- rajesh sir
Module2 stiffness- rajesh sirModule2 stiffness- rajesh sir
Module2 stiffness- rajesh sirSHAMJITH KM
 
Module2 stiffness- rajesh sir
Module2 stiffness- rajesh sirModule2 stiffness- rajesh sir
Module2 stiffness- rajesh sirSHAMJITH KM
 
0.7 Radical Expressions
0.7 Radical Expressions0.7 Radical Expressions
0.7 Radical Expressionssmiller5
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingzukun
 
Enhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetEnhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetAlaaZ
 
Webinar on Graph Neural Networks
Webinar on Graph Neural NetworksWebinar on Graph Neural Networks
Webinar on Graph Neural NetworksLucaCrociani1
 
Global threshold and region based active contour model for accurate image seg...
Global threshold and region based active contour model for accurate image seg...Global threshold and region based active contour model for accurate image seg...
Global threshold and region based active contour model for accurate image seg...sipij
 
Essay On Linear Function
Essay On Linear FunctionEssay On Linear Function
Essay On Linear FunctionAngie Lee
 
Modules Linear Algebra Drills
Modules Linear Algebra DrillsModules Linear Algebra Drills
Modules Linear Algebra DrillsDaniel Bragais
 
A Comparative Analysis Of Assignment Problem
A Comparative Analysis Of Assignment ProblemA Comparative Analysis Of Assignment Problem
A Comparative Analysis Of Assignment ProblemJim Webb
 
Ch5_Constraint Satisfaction Problems_Lecture 1.pdf
Ch5_Constraint Satisfaction Problems_Lecture 1.pdfCh5_Constraint Satisfaction Problems_Lecture 1.pdf
Ch5_Constraint Satisfaction Problems_Lecture 1.pdfYanKyaw6
 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxssuser01e301
 

Similar to Module 2_Decrease and Conquer_2021 Scheme.pptx (20)

Module 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptxModule 3_Greedy Technique_2021 Scheme.pptx
Module 3_Greedy Technique_2021 Scheme.pptx
 
Determinants
DeterminantsDeterminants
Determinants
 
The Fundamental theorem of calculus
The Fundamental theorem of calculus The Fundamental theorem of calculus
The Fundamental theorem of calculus
 
MyStataLab Assignment Help
MyStataLab Assignment HelpMyStataLab Assignment Help
MyStataLab Assignment Help
 
A Lexi Search Approach to Generalized Travelling Salesman Problem
A Lexi Search Approach to Generalized Travelling Salesman ProblemA Lexi Search Approach to Generalized Travelling Salesman Problem
A Lexi Search Approach to Generalized Travelling Salesman Problem
 
Module2 stiffness- rajesh sir
Module2 stiffness- rajesh sirModule2 stiffness- rajesh sir
Module2 stiffness- rajesh sir
 
Module2 stiffness- rajesh sir
Module2 stiffness- rajesh sirModule2 stiffness- rajesh sir
Module2 stiffness- rajesh sir
 
0.7 Radical Expressions
0.7 Radical Expressions0.7 Radical Expressions
0.7 Radical Expressions
 
Skiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracingSkiena algorithm 2007 lecture15 backtracing
Skiena algorithm 2007 lecture15 backtracing
 
Enhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial DatasetEnhance The K Means Algorithm On Spatial Dataset
Enhance The K Means Algorithm On Spatial Dataset
 
1406
14061406
1406
 
Webinar on Graph Neural Networks
Webinar on Graph Neural NetworksWebinar on Graph Neural Networks
Webinar on Graph Neural Networks
 
K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
Global threshold and region based active contour model for accurate image seg...
Global threshold and region based active contour model for accurate image seg...Global threshold and region based active contour model for accurate image seg...
Global threshold and region based active contour model for accurate image seg...
 
Cluster Analysis
Cluster Analysis Cluster Analysis
Cluster Analysis
 
Essay On Linear Function
Essay On Linear FunctionEssay On Linear Function
Essay On Linear Function
 
Modules Linear Algebra Drills
Modules Linear Algebra DrillsModules Linear Algebra Drills
Modules Linear Algebra Drills
 
A Comparative Analysis Of Assignment Problem
A Comparative Analysis Of Assignment ProblemA Comparative Analysis Of Assignment Problem
A Comparative Analysis Of Assignment Problem
 
Ch5_Constraint Satisfaction Problems_Lecture 1.pdf
Ch5_Constraint Satisfaction Problems_Lecture 1.pdfCh5_Constraint Satisfaction Problems_Lecture 1.pdf
Ch5_Constraint Satisfaction Problems_Lecture 1.pdf
 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptx
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 

Module 2_Decrease and Conquer_2021 Scheme.pptx

  • 2. CONTENTS  Introduction  Topological Sorting  Insertion sort  Graph searching algorithms 9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 2
  • 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
  • 19. TOPOLOGICAL SORTING Example C 1 C 2 C 3 C 4 C 5 C 2 C 3 C 4 C 5 Delete C1 C 3 C 4 C 5 Delete C2 C 4 C 5 Delete C3 C 5 Delete C4 Delete C5 Topological Order C1, C2, C3, C4, C5 9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 19
  • 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
  • 22. INSERTION SORT  The working of Insertion Sort algorithm is demonstrated below. 9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 22 89 45 68 90 29 34 17 89 45 68 90 29 34 17 89 45 68 90 29 34 17 Sorted Part Unsorted Part 89 45 68 90 29 34 17 45 89 68 90 29 34 17 45 89 68 90 29 34 17 45 68 89 90 29 34 17 45 68 89 90 29 34 17 45 68 89 90 29 34 17 45 68 89 90 29 34 17 29 45 68 89 90 34 17 29 45 68 89 90 34 17 29 34 45 68 89 90 17 29 34 45 68 89 90 17 17 29 34 45 68 89 90
  • 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
  • 35. GRAPH SEARCHING ALGORITHMS BFS_Algorithm Exercise Trace the BFS algorithm on the following graph. 9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 35 a b c d e f g
  • 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 𝜃( 𝑉 + 𝐸 ) 𝜃( 𝑉 + 𝐸 )
  • 38. END OF MODULE 2 9/17/2023 Dr. K. Balakrishnan, Dept. of CSE, SaIT, 38

Editor's Notes

  1. 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.
  2. 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.