0% found this document useful (0 votes)
21 views20 pages

Ch-5 Decrease and Conquer

Chapter 5 discusses the 'Decrease and Conquer' approach, which can be implemented using top-down or bottom-up methods. It covers key algorithms including Insertion Sort, Depth First Search (DFS), Breadth First Search (BFS), and Topological Sorting, detailing their processes and variations. The chapter emphasizes the importance of managing visited nodes and traversal strategies in graph algorithms.

Uploaded by

malirashmi808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Ch-5 Decrease and Conquer

Chapter 5 discusses the 'Decrease and Conquer' approach, which can be implemented using top-down or bottom-up methods. It covers key algorithms including Insertion Sort, Depth First Search (DFS), Breadth First Search (BFS), and Topological Sorting, detailing their processes and variations. The chapter emphasizes the importance of managing visited nodes and traversal strategies in graph algorithms.

Uploaded by

malirashmi808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

1

Chapter – 5
Decrease and Conquer

- By Dr. Shashikumar G. Totad


Chapter – 5
Decrease and Conquer
2

Contents

1. Insertion sort,

2. Depth First search,

3. Breadth First Search,

4. Topological sorting .
3

 This approach can be either implemented as top-down or bottom-up.


 Top-down approach : It always leads to the recursive implementation of the problem.
 Bottom-up approach : It is usually implemented in iterative way, starting with a solution
to the smallest instance of the problem.
 Variations of Decrease and Conquer - There are three major variations of decrease-and-conquer:
1. Decrease by a constant
2. Decrease by a constant factor
3. Variable size decrease
4

 Decrease by a Constant :
o In this variation, the size of an instance is reduced by the same constant on each iteration of
the algorithm.
o Typically, this constant is equal to one , although other constant size reductions do happen
occasionally.
o Example problems :
o Insertion sort
o Graph search algorithms: DFS, BFS
o Topological sorting
Chapter – 5
Decrease and Conquer
5

Contents

1. Insertion sort,

2. Depth First search,

3. Breadth First Search,

4. Topological sorting .
1. Insertion Sort:
6

 Example - Input: a[ ] = {23, 1, 10, 5, 2}


Initial:
• Current element is 23
• The first element in the array is assumed to be sorted.
First Pass:
• The sorted part until 0th index is : [23]
• Compare 1 with 23 (current element with the sorted part).
• Since 1 is smaller, insert 1 before 23 .
•Second Pass: part until 1st index is: [1, 23]
The sorted
• Compare 10 with 1 and 23 (current element with the sorted
part).
Third Pass:
•• Since 10 is greater than 1 and smaller than 23 ,
Compare 5 with 1 , 10 , and 23 (current element with the sorted
Fourth Pass:
insert
part). 10 between 1 and 23 .
•• Compare 2 with
part1,until
5, 10 , andindex
23 (current
[1, element with the sorted
• The
Sincesorted
5 is greater than 12nd is:than
and smaller 10,
10 ,23]
part).
insert 5 between 1 and 10
• Since 2 is greater than 1 and smaller
• The sorted part until 3rd index is : [1, 5, 10, 23]
than 5 insert 2 between 1 and 5 .
• The sorted part until 4th index is: [1, 2, 5, 10, 23]
1. Insertion Sort:
7

Input Output Insertion Sort


for(i=1; i<n; i++)
{
key = a[i];
i=
for(j=i-1; j>=0 && a[j] >
1
j=0 key; j--)
{
i=2
if(key<a[j])
j=1, 0
a[j+1] = a[j];
i=3 }
j=2,1,0 a[j+1] = key;
i=4 }
j=3,2,1,
0
1. Insertion Sort: . . . . . . Comparison of Insertion & Selection sorts
8

Insertion Sort Selection Sort

for(i=1; i<n; i++) for (i = 0; i < n - 1; i++)


{ { min_idx = i;
key = a[i]; for (j = i + 1; j < n; j++)
{
for(j=i-1; j>=0 && a[j] > key; j--)
if (a[j] < a[min_idx])
{
min_idx = j;
if(key<a[j])
}
a[j+1] = a[j];
if (min_idx != i)
}
{ temp=a[min_idx];
a[j+1] = key;
a[min_idx]= a[i];
}
a[i] = a[min_idx]);
}
}
1. Insertion Sort: . . . using while loop
9

for(i=1; i<n; i++)


{
temp = a[i];
j = i-1;
while(temp<a[j] &&
j>=0)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
Chapter – 5
Decrease and Conquer
10

Contents

1. Insertion sort,

2. Depth First search,

3. Breadth First Search,

4. Topological sorting .
2. Depth First Search
11

 Graph Traversal Algorithms


o To traverse a graph is to process every node in the graph exactly once.
o Because there are many paths leading from one node to another, the hardest part about
traversing a graph is making sure that you do not process some node twice.
o At the heart of all graph traversal algorithms - is a list of nodes that we are visited but not yet
processed, and is called the READY list.
o The two most common traversal patterns are breadth-first traversal and depth-first traversal.
o General Traversal Strategy
1. Mark all nodes in the graph as NOT VISITED.
2. Pick a starting node. Mark it as VISITED and place it on the READY list.
3. Pick a node on the READY list. Process it. Remove it from READY.
 Find all its neighbours: those that are NOT VISITED should marked as VISITED and
added to READY.
4. Repeat 3 until READY is empty.
2. Depth First Search
12

 Depth-first search starts visiting vertices of a graph at an arbitrary vertex by marking it as having
been visited.
 On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to the one it is
currently in.
 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.
2. Depth First Search
13

 It is convenient to use a stack to trace the operation of depth-first search.


 In depth-first traversal, READY is a STACK; the most recently reached nodes are processed before earlier
nodes.
 When a vertex is reached for the first time(i.e., when the visit of the vertex starts), the vertex is pushed
onto the STACK/READY list and a vertex is popped off the stack when it becomes a dead end (i.e., the
visit of the vertex ends).
 Depth-first search forest :
o A forest consists of one or more trees.
o Traversal's starting vertex serves as the root of the first tree in a forest.
o Tree edge: is an edge in the forest connecting an unvisited vertex to a visited parent vertex from
which it is being reached.
o Set of all tree edges forms a forest.
o Back edge: is an edge in the forest that connects a vertex to its ancestor that is already visited, other
than the parent, in the depth-first search forest.
o Tree edges are represented with solid lines while the back edges represented with dashed lines.
2. Depth First Search
14

 Example:

Traversal's stack
1st subscript: order in which a vertex was visited/ pushed onto stack;
Graph 2nd subscript: order in which the vertex popped off the stack. DFS forest

ALGORITHM DFS(G) dfs(v)


//Input: Graph G = (V, E) //visits recursively all the unvisited vertices
//Output: Graph G with its vertices marked with consecutive integers // connected to vertex v by a path and numbers
//in the order they've been first encountered by the DFS traversal // them in the order they are encountered
mark each vertex in V with 0 as a mark of being "unvisited" count +---count + 1; mark v with count
count+---0 for each vertex w in V adjacent to v do
for each vertex v in V do if (w is marked with 0) dfs(w)
if (v is marked with 0) dfs(v)
Chapter – 5
Decrease and Conquer
15

Contents

1. Insertion sort,

2. Depth First search,

3. Breadth First Search,

4. Topological sorting .
3. Breadth First Search
18

 Example:

Traversal's Queue
Subscript: Order in which the vertices were visited,
Graph i.e., added to (or removed from) the queue. BFS forest

ALGORITHM BFS(G) bfs(v)


//Input: Graph G = (V, E) count +-count + 1; mark v with count and initialize a queue with v
//Output: Graph G - vertices marked with consecutive while the queue is not empty do
// integers in the order they are visited for each vertex w in v adjacent to the front vertex do
mark each vertex in V with 0 as a mark of being "unvisited" if w is marked with 0
count <- 0 count <- count + 1; mark w with count
for each vertex v in V do add w to the queue
if (v is marked with 0) bfs(v) remove the front vertex from the queue
Chapter – 5
Decrease and Conquer
20

Contents

1. Insertion sort,

2. Depth First search,

3. Breadth First Search,

4. Topological sorting.
4. Topological Sorting
25

 Example:

Graph DFS traversal stack BFS forest


Subscript: indicating the popping off order.
References
26

1. https://www.scaler.com/topics/c-data-structures/
2. https://www.javatpoint.com/primitive-vs-non-primitive-data-structure
3. https://www.javatpoint.com/merge-sort
4. https://www.geeksforgeeks.org/
5. https://www.shiksha.com/online-courses/articles/quicksort-algorithm-with-code/
27

You might also like