Graph Algorithms: Text Book: Introduction To Algorithms Byclrs
Graph Algorithms: Text Book: Introduction To Algorithms Byclrs
STRONGLY-CONNECTED-COMPONENTS(G)
1 call DFS(G) to compute finishing times u.f for each
vertex u
2 compute GT
3 call DFS(GT), but in the main loop of DFS, consider the
vertices in order of decreasing u.f (as computed in line
1)
4 output the vertices of each tree in the depth-first forest
formed in line 3 as a separate strongly connected
component
Strongly connected components- Example
Strongly connected components- Example
Component Graph
Minimal Spanning tree
Minimal Spanning tree
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm
• Running time O(E lg V)
Prim’s Algorithm
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Prim’s Algorithm - Example
Generic MST Algorithm
Light Edge
• A cut (S, V – S) of an undirected graph G=(V,E) is
a partition of V .
• An edge (u,v) є E crosses the cut (S, V-S) if one of
its endpoints is in S and the other is in V - S.
• A cut respects a set A of edges if no edge in A
crosses the cut.
• An edge is a light edge crossing a cut if its weight
is the minimum of any edge crossing the cut.
Safe Edge - Theorem
Let G=(V,E) be a connected, undirected graph
with a real-valued weight function w defined
on E. Let A be a subset of E that is included in
some minimum spanning tree for G, let (S, V-
S) be any cut of G that respects A, and let
(u,v) be a light edge crossing (S, V-S). Then,
edge (u,v) is safe for A.
Cut - Example
Cut – Another View
Shortest Path
• The weight w(p) of path p = <v0, v1, . . . , vk> is
the sum of the weights of its constituent
edges:
Shortest Path Problems
• single-source shortest-paths problem
• Single-destination shortest-paths
problem
• Single-pair shortest-path problem
• All-pairs shortest-paths problem
Shortest Path Problems
• Property
– Subpaths of shortest paths are shortest paths
• Graphs with negative edges
• Graphs with cycles
– Can a shortest path contain a cycle?
Are shortest paths unique?
Are shortest paths unique?
Are shortest paths unique?
Shortest Path Algorithms