5. Graph Notes
5. Graph Notes
prior to moving on to the next depth level. It's often used to find the shortest path
between two nodes in an unweighted graph.
Algorithm:
1. I nitialization:
o Mark all vertices as unvisited.
o Create a queue to store the vertices to be visited.
1. Initialization:
o M ark all vertices as unvisited.
o C reate a stack to store the vertices to be visited.o
Push the starting vertex onto the stack
.
o M ark the starting vertex as visited.
2. Loop:
oWhile the stack is not empty:
Pop a vertex from the top of the stack.
For each unvisited neighbor of the popped vertex:
Mark the neighbor as visited.
Push the neighbor onto the stack.
Kosaraju s Algorithm involves two main phases:
Performing Depth-First Search (DFS) on the Original Graph:We
first do a DFS on the original graph and record the finish times
of nodes (i.e., the time at which the DFS finishes exploring a
node completely).
Performing DFS on the Transposed Graph:We then reverse the
direction of all edges in the graph to create the transposed
graph.
Next, we perform a DFS on the transposed graph, considering
nodes in decreasing order of their finish times recorded in the
first phase.
Each DFS traversal in this phase will give us one SCC.
Prim's algorithm is a greedy algorithm that finds a minimum
spanning tree for a weighted undirected graph.
ALGORITHM:
1. First, sort all the edges from low weight to high.Now, take the edge with the
For every pair (i, j) of the source and destination vertices respectively,
there are two possible cases.
1. k is not an intermediate vertex in shortest path from i to j.
We keep the value of dist[i][j] as it is. k is an intermediate vertex in
shortest path from i to j. We update the value of dist[i][j] as dist[i][k] +
dist[k][j], if dist[i][j] > dist[i][k] + dist[k][j]