CS203 Week 11
CS203 Week 11
The Breadth-First Search (BFS) relies on its ability to visit every reachable node in a graph exactly
once, systematically exploring outward from the starting node. This ensures that no reachable node
is missed and that the shortest path to each node is discovered.
Proof :-
Assume the converse: let’s suppose there exists a vertex v for which dist(s, v) ̸= d[v]. Let v be the
vertex with the smallest dist(s, v), where v ̸= s (as d[s] = dist(s, s) = 0).
By Lemma 2, d[v] ≥ dist(s, v), implying d[v] ≥ dist(s, v) + 1. As dist(s, v) represents the shortest
distance from s to v, there must be a neighboring vertex u of v such that dist(s, u) + 1 = dist(s, v).
This implies dist(s, u) < dist(s, v), and by our choice of v, we have dist(s, u) = d[u]. According to
Corollary 2, we then have d[v] ≤ d[u] + 1.
Therefore, d[v] ≤ dist(s, u) + 1, leading to d[v] ≤ dist(s, v). But this contradicts our initial as-
sumption that dist(s, v) ̸= d[v].
1
2 Depth-First Search (DFS) :-
Depth-First Search (DFS) is a fundamental algorithm for traversing or searching tree and graph
data structures. It works by exploring one branch as far as possible before backtracking and
exploring other branches.
Properties :-
2. Non-optimality :- DFS does not necessarily visit the nodes in the shortest order, mak-
ing it suboptimal for finding shortest paths.
3. Space complexity :- DFS uses a stack data structure to keep track of the explored path,
leading to a space complexity of O(n).
4. Time complexity :- DFS has a time complexity of O(m + n), where m is the number
of edges and n is the number of nodes.
Applications :-
• Finding connected components in a graph
• Cycle detection
• Topological sorting
• Solving mazes
• Garbage collection
Variations :-
• Iterative DFS:- Utilizes an explicit stack to implement the recursive calls, making it more
memory efficient.
• Pre-order, In-order, and Post-order DFS :- Represent different traversal orders based on the
order of visiting nodes and their children.
2
Algorithm :- Depth-First Search (DFS)
Procedure : DFS(G)
1. for each vertex u in G.V do
2. u.color ← WHITE
3. u.π ← NIL
4. end for
5. time ← 0
6. for each vertex u in G.V do
7. if u.color = WHITE then
8. DFS-VISIT(G, u)
9. end if
10. end for
11. end Procedure
Procedure : DFS-VISIT(G, u)
3
3 Topological Sorting :-
Topological sorting is a fundamental algorithm used to arrange the vertices of a Directed Acyclic
Graph (DAG) in a linear order. This ordering ensures that for every directed edge (u, v), vertex u
always appears before vertex v in the sequence.
Key Properties :-
• Useful for various applications like task scheduling, makefile generation, and dependency
analysis
Algorithm :- TOPOLOGICAL-SORT
1. Procedure TopologicalSort(G)
2. Perform Depth-First Search (DFS) on graph G to compute finish times v.f for each vertex
v.
4. Sort vertices in decreasing order of v.f and iterate through each vertex v
5. Insert v at the front of linked list L. (Insert each finished vertex at the front of the linked
list)
6. end for
8. end procedure
4
Properties :-
• Prim’s Algorithm: This greedy algorithm starts with an arbitrary vertex and iteratively
adds the cheapest edge that connects the existing tree to a new vertex until all vertices are
included.
• Kruskal’s Algorithm: This greedy algorithm sorts all edges by weight and then adds them
to the MST in ascending order, ensuring inclusion only if it doesn’t create a cycle.
Applications :-
• Network design :- Optimizing the layout of communication networks, like wired or wireless
networks, by minimizing the total cable length.
• Clustering :- Grouping similar data points together by finding the minimum cost connections
between them.
• Image segmentation :- Identifying and separating different objects in an image by minimizing
the total cost of cutting edges between pixels.
• Circuit design :- Optimizing the layout of circuits by minimizing the total wire length.
Algorithm :- GENERIC-MST(G, w)
1. Procedure GENERIC-MST(G, w)
2. Let A be an empty set initially, representing the MST.
3. While A does not form a spanning tree.
4. Find an edge (u, v) that is safe to add to A. (This depends on the specific algorithm used)
5. Add the edge (u, v) to A.
6. Return A as the Minimum Spanning Tree.
7. end procedure