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

5. Graph Notes

The document outlines various graph algorithms including BFS, DFS, Kosaraju's Algorithm, Prim's Algorithm, Kruskal's Algorithm, the transitive closure, Floyd-Warshall Algorithm, and Dijkstra's Algorithm. Each algorithm is described with its initialization steps, looping structure, and specific applications or purposes. The focus is on methods for traversing graphs, finding shortest paths, and constructing minimum spanning trees.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

5. Graph Notes

The document outlines various graph algorithms including BFS, DFS, Kosaraju's Algorithm, Prim's Algorithm, Kruskal's Algorithm, the transitive closure, Floyd-Warshall Algorithm, and Dijkstra's Algorithm. Each algorithm is described with its initialization steps, looping structure, and specific applications or purposes. The focus is on methods for traversing graphs, finding shortest paths, and constructing minimum spanning trees.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

BFS is a graph traversal algorithm that explores all neighbor nodes at the present depth

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.

o Enqueue the starting vertex.


o Mark the starting vertex as visited.
2. L oop:
o While the queue is not empty:
oDequeue a vertex from the front of the queue.
oFor each unvisited neighbor of the dequeued
vertex:
o Mark the neighbor as visited.
oEnqueue the neighbor.
DFS is a graph traversal algorithm that explores as deep as
possible along a branch before backtracking. It's often used
for topological sorting and cycle detection.

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.

Step 1: Determine an arbitrary vertex as the starting vertex of the


MST.
Step 2: Follow steps 3 to 5 till there are vertices that are not
included in the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe
vertices.
Step 4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any
cycle.
Step 6: Return the MST and exit
Kruskal's Algorithm is used to find the minimum spanning tree for a connected
weighted graph.

ALGORITHM:

1. First, sort all the edges from low weight to high.Now, take the edge with the

lowestweight and add it to the spanning tree.


2. If the edge to be added creates a cycle, then reject the edge.
3. Continue to add the edges until we reach all vertices, and a minimum spanning
tree is created.
The applications of Kruskal's algorithm are :
-Kruskal's algorithm can be used to layout electrical wiring among cities.
It can be used to lay down LAN connections.
The transitive closure of a graph is a graph that shows if there is a path
between any two vertices in the original graph. It's a data structure
that helps answer questions about reachability in a graph.
Floyd Warshall Algorithm Algorithm:
Initialize the solution matrix same as the input graph matrix as a first step.
Then update the solution matrix by considering all vertices as an
intermediate vertex.
The idea is to pick all vertices one by one and updates all shortest paths
which include the picked vertex as an intermediate vertex in the shortest
path.
When we pick vertex number k as an intermediate vertex, we already have
considered vertices {0, 1, 2, .. k-1} as intermediate vertices.

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]

Pseudo-Code of Floyd Warshall Algorithm :


For k = 0 to n 1
For i = 0 to n 1
For j = 0 to n 1
Distance[i, j] = min(Distance[i, j], Distance[i, k] + Distance[k, j])

where i = source Node, j = Destination Node, k = Intermediate Node


Algorithm for Dijkstra s Algorithm:
Mark the source node with a current distance of 0 and the rest with infinity.
Set the non-visited node with the smallest current distance as the current node.
For each neighbor, N of the current node adds the current distance of the adjacent node
with the weight of the edge connecting 0->1.
If it is smaller than the current distance of Node, set it as the new current distance of N.
Mark the current node 1 as visited.
Go to step 2 if there are any nodes are unvisited.

You might also like