0% found this document useful (0 votes)
16 views

DSA Java Graph Part 2 Sort Algo Part 1 (M 26)

Uploaded by

Sameeksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DSA Java Graph Part 2 Sort Algo Part 1 (M 26)

Uploaded by

Sameeksh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Kosaraju Algorithm

Kosaraju's Algorithm
Kosaraju's Algorithm is based on the depth-first search algorithm implemented
twice.
Three steps are involved.
1.Perform a depth first search on the whole graph.

Let us start from vertex-0, visit all of its child vertices, and mark the visited
vertices as done. If a vertex leads to an already visited vertex, then push this
vertex to the stack.

For example: Starting from vertex-0, go to vertex-1, vertex-2, and then to vertex-
3. Vertex-3 leads to already visited vertex-0, so push the source vertex (ie.
vertex-3) into the stack.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

Go to the previous vertex (vertex-2) and visit its child vertices i.e. vertex-4,
vertex-5, vertex-6 and vertex-7 sequentially. Since there is nowhere to go from
vertex-7, push it into the stack.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

Go to the previous vertex (vertex-6) and visit its child vertices. But, all of its child
vertices are visited, so push it into the stack.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

Similarly, a final stack is created.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

2. Reverse the original graph

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

3. Perform depth-first search on the reversed graph.

Start from the top vertex of the stack. Traverse through all of its child vertices.
Once the already visited vertex is reached, one strongly connected component is
formed.

For example: Pop vertex-0 from the stack. Starting from vertex-0, traverse through
its child vertices (vertex-0, vertex-1, vertex-2, vertex-3 in sequence) and mark
them as visited. The child of vertex-3 is already visited, so these visited vertices
form one strongly connected component.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Kosaraju Algorithm

Kosaraju's Algorithm Complexity


Kosaraju's algorithm runs in linear time i.e. O(V+E).

Strongly Connected Components Applications

• Vehicle routing applications

• Maps

• Model-checking in formal verification

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
• Searching & Sorting Algorithms

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Bubble Sort Algorithm
Algo.

The bubble sort algorithm compares two adjacent elements and swaps them if
they are not in the intended order.

For example,
Suppose we have an unsorted array with elements: 45, 1, 4.

Here, the bubble sort algorithm compares the first element 45 and second
element 1.

Since 45 is greater than 1, they are swapped (1, 45, 4).

Similarly, the new second element is compared with the next element. This
process continues until all the elements are sorted.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

Working of Bubble Sort Algorithm


Here, we are trying to sort the elements
in ascending order.
1. First Iteration (Compare and Swap)
1.Starting from the first index, compare
the first and the second elements.
2.If the first element is greater than the
second element, they are swapped.
3.Now, compare the second and the
third elements. Swap them if they are
not in order.
4.The above process goes on until the
last element.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

2. Remaining Iteration
The same process goes on for the
remaining iterations.
After each iteration, the largest
element among the unsorted elements
is placed at the end.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

In each iteration, the comparison takes


place up to the last unsorted element.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

The array is sorted when all the unsorted


elements are placed at their correct
positions.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Selection Sort Algorithm
Algo.

• Selection sort is an algorithm that selects the smallest element from an

unsorted list in each iteration and places that element at the beginning of the

unsorted list.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

How Selection Sort Works?


1. Set the first element as minimum

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

2. minimum with the second element. If


the second element is smaller than
minimum, assign the second element as
minimum.

Compare minimum with the third


element. Again, if the third element is
smaller, then assign minimum to the
third element otherwise do nothing. The
process goes on until the last element.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

After each iteration, minimum is placed in the front of the unsorted list

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

For each iteration, indexing


starts from the first unsorted
element. Step 1 to 3 are
repeated until all the elements
are placed at their correct
positions.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Sorting and Searching
Algo.

This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video

You might also like