7002 Ds Graph Traversal
7002 Ds Graph Traversal
2. Breadth-First Search (BFS): Breadth-First Search explores the graph level by level,
starting from the given source node. It uses a queue to track all the vertices that need to be
explored next. BFS is ideal for finding the shortest path on unweighted graphs and is used in
algorithms that need to explore all vertices at the present depth before moving on to vertices at
the next depth level.
3. Bidirectional Search: Bidirectional search is used primarily for searching a path from a
start vertex to a target vertex in unweighted graphs. It runs two simultaneous breadth-first
searches—one from the start vertex and the other from the target vertex, meeting in the
middle.This can drastically reduce the search space and is efficient for finding the shortest path.
4. Dijkstra’s Algorithm: While technically a shortest path algorithm, Dijkstra’s uses a BFS
approach to traverse the graph. It is used for finding the shortest paths from a source node to all
other nodes in a graph with non-negative edge weights. The algorithm uses a priority queue to
greedily select the next vertex with the smallest distance.
Conceptual
BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.
Difference
BFS is more suitable for searching vertices closer to DFS is more suitable when there are solutions away
Suitable for
the given source. from source.
5. Dijkstra-Based Variants
Approach: Though not strictly designed for MST, Dijkstra's algorithm can be
adapted to find spanning trees by modifying its relaxation criteria to focus on edges
connecting to unvisited vertices.
Best For: Specific graph configurations or as part of hybrid algorithms.
Kruskal Approach:
Select the minimum weight edge that does not form a cycle
Kruskal's Algorithm:
sort the edges of G in increasing order by length
keep a subgraph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S
Prim Approach:
Choose an arbitrary start node v
At any point in time, we have connected component N containing v
and other nodes V-N
Choose the minimum weight edge from N to V-N
Prim's Algorithm:
let T be a single vertex x
while (T has fewer than n vertices)
{
find the smallest edge connecting T to G-T
add it to T
}
89 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Steps in Prim's Algorithm
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
8 12
13 9
2
11 20 40 14
7
50 6
10
1 3
Prim’s and Kruskal’s algorithms are both efficient for finding Minimum
Spanning Trees, with Prim’s being more suited for dense graphs due to
its vertex-focused approach and Kruskal’s excelling in sparse graphs
with its edge-centric strategy.
The choice between them depends on the graph's density and
representation, making both valuable tools in different scenarios.