Professional Documents
Culture Documents
oduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
1 Introduction
Representation of graphs
Consists of an array Adj of |V| lists, one for each vertex in V. We assume that the vertices are numbered 1, 2,..., |V| in
some arbitrary manner.
For each u ∈ V, the adjacency list Adj[u] contains all the |V| × |V| matrix A = (aij ) such that
vertices v such that there is an edge (u, v) ∈ E. (
1 if (i,j) ∈ E,
If edges have weights, put the weights in the lists. aij =
0, otherwise.
Weight: w : E→R
Space: Θ(V2 ).
Space: Θ(V + E).
Time: to list all vertices adjacent to u: Θ(V).
Time: to list all vertices adjacent to u: Θ(degree(u)). Time: to determine if (u, v) ∈ E: Θ(1).
Time: to determine if (u, v) ∈ E: O(degree(u)).
Can store weights instead of bits for weighted graph.
5 6
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Breadth-first search
Figure: 5.1 Representations of an undirected graph (with 5 vertices and 7 It systematically explores the edges of G to ”discover” every
edges).
vertex that is reachable from s.
Algorithm 1 BFS(G, s)
1: for each vertex u ∈ G.V - {s} do
To keep track of progress, breadth-first search colors each vertex 2: u.color = WHITE
3: u.d = ∞
white, gray, or black. 4: u.π = NIL
5: end for
6: s.color = GRAY
All vertices start out white and may later become gray and 7: s.d = 0
then black. 8: s.π = NIL
9: Q = ∅
10: ENQUEUE(Q, s)
If (u, v) ∈ E and vertex u is black, then vertex v is either 11: while Q 6= ∅ do
gray or black. 12: u = DEQUEUE(Q)
13: for each v ∈ G.Adj[u] do
14: if v.color == WHITE then
Gray vertices may have some adjacent white vertices; they 15: v.color = GRAY
represent the frontier between discovered and undiscovered 16: v.d = u.d + 1
17: v.π = u
vertices. 18: ENQUEUE(Q, v)
19: end if
20: end for
21: u.color = BLACK
22: end while
9 10
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Analysis
After initialization
Thus, the total running time of the BFS procedure is O(V + E).
Depth-first search
Once all of v’s edges have been explored, the search The predecessor subgraph of a depth-first search forms a
”backtracks”. depth-first forest comprising several depth-first trees. The
edges in Eπ are tree edges.
This process continues until we have discovered all the
vertices.
13 14
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
15 16
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
17 18
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The loops on lines 1-4 and lines 6-10 of DFS take time Θ(V).
Figure: 5.4 The progress of the depth-first-search algorithm DFS on a directed graph.
19 20
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
We can define four edge types in terms of the depth-first forest Gπ Sample problem:
produced by a depth-first search on G:
A town has a set of houses and a set of roads.
1 Tree edges are edges in the depth-first forest Gπ . A road connects 2 and only 2 houses.
A road connecting houses u and v has a repair cost of w(u,
2 Back edges are those edges (u, v) connecting a vertex u to v).
an ancestor v in a depth-first tree.
21 22
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
23 24
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
25 26
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Use the loop invariant to show that this generic algorithm works.
Algorithm 4 GENERIC-MST(G, w) Initialization: The empty set trivially satisfies the loop
invariant.
1: A = ∅
2: while A does not form a spanning tree do
Maintenance: Since we add only safe edges, A remains a
3: find an edge (u, v) that is safe for A
subset of some MST.
4: A = A ∪ {(u, v)}
5: end while
Termination: All edges added to A are in an MST, so when we
6: return A
stop, A is a spanning tree that is also an MST.
27 28
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
A cut respects A if and only if no edge in A crosses the cut. Figure: 5.6 Black vertices are in the set S, and white vertices are in V -
S. The edge (d, c) is the unique light edge crossing the cut. A subset A
An edge is a light edge crossing a cut if and only if its weight of the edges is shaded; note that the cut (S, V - S) respects A, since
is minimum over all edges crossing the cut. For a given cut, no edge of A crosses the cut.
there can be > 1 light edge crossing it.
29 30
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
Kruskal’s algorithm
Algorithm 5 MST-KRUSKAL(G, w)
1: A = ∅
G = (V, E) is a connected, undirected, weighted graph
2: for each vertex u ∈ G.V do
w : E → R.
3: MAKE-SET(v)
Starts with each vertex being its own component. 4: sort the edges of G.E into nondecreasing order by weight w
5: end for
Repeatedly merges two components into one by choosing the 6: for each edges (u, v) ∈ G.E, taken in nondecreasing order by
light edge that connects them (i.e., the light edge crossing the weight do
cut between them). 7: if FIND-SET(u) 6= FIND-SET(v) then
8: A = A ∪ {(u, v)}
Scans the set of edges in monotonically increasing order by
9: UNION(u, v)
weight (greedy).
10: end if
Uses a disjoint-set data structure to determine whether an 11: end for
edge connects vertices in different components. 12: return A
31 32
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
33 34
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
Analysis
Initialize A: O(1)
Sort E: O(E lg E)
Second for loop: O(E) FIND-SETs and UNIONs
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
Prim’s algorithm
How to find the light edge quickly?
Starts from an arbitrary ”root” r. Key of v is minimum weight of any edge (u, v), where u ∈
VA .
At each step, find a light edge crossing cut (VA , V − VA ), where
Then the vertex returned by EXTRACT-MIN is v such that there
VA = vertices that A is incident on. Add this edge to A.
exists u ∈ VA and (u, v) is light edge crossing (VA , V − VA ).
37 38
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
Algorithm 6 MST-PRIM(G, w, r)
The edges of A will form a rooted tree with root r: 1: for each vertex u ∈ G.V do
2: u.key = ∞
r is given as an input to the algorithm, but it can be any 3: u.π = NIL
vertex. 4: end for
5: r.key = 0
Each vertex knows its parent in the tree by the attribute v.π
= parent of v. 6: Q = G.V
7: while Q 6= ∅ do
v.π = NIL if v = r or v has no parent. 8: u = EXTRACT-MIN(Q)
9: for each v ∈ G.adj[u] do
As the algorithm progresses, A = {(v, v.π) : v ∈ V -
10: if v ∈ Q and w(u, v) < v.key then
{r} - Q}.
11: v.π = u
At termination, VA = V ⇒ Q = ∅, so MST is A = {(v, v.π) : 12: v.key = w(u, v)
v ∈ V - {r}}. 13: end if
14: end for
15: end while
39 40
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
The algorithms of Kruskal and Prim The algorithms of Kruskal and Prim
41 42
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
Variants
45 46
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
Negative-weight edges:
47 48
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
Cycles
49 50
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
Shortest-paths tree rooted at s is a directed subgraph G’ = (V’, We initialize the shortest-path estimates and predecessors by
E’), where V’ ⊆ V and E’ ⊆ E, such that the following Θ(V)-time procedure:
51 52
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Introduction Introduction
55 56
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
In line 2-6 |V| - 1 passes over the edges ⇒ each take Θ(E)
time
57
Figure: 5.11 The execution of the Bellman-Ford algorithm. 58
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Dijkstra’s algorithm
59 60
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Algorithm 10 DIJKSTRA(G, w, s)
1: INITIALIZE-SINGLE-SOURCE(G, s)
Line 1 initializes the d and π values in the usual way.
2: S = ∅
Line 2 initializes the set S to the empty set.
3: Q = G.V
4: while Q 6= ∅ do Line 3 initializes the min-priority queue Q.
5: u = EXTRACT-MIN(Q)
6: S = S ∪ {u} Line 5 extracts a vertex u from Q = V - S and line 6 adds it
7: for each vertex v ∈ G.adj[u] do to set S, thereby maintaining the loop invariant.
8: RELAX(u, v, w)
9: end for Then, lines 7-8 relax each edge (u, v).
10: end while
61 62
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Analysis
The running time of Dijkstra’s algorithm depends on how we
implement the min-priority queue.
How fast is Dijkstra’s algorithm?
If binary min-heap is used:
It maintains the min-priority queue Q by calling three priority-queue
operations. Each EXTRACT-MIN operation then takes time O(lg V).
The time to build the binary min-heap is O(V).
1 INSERT (implicit in line 3), Each DECREASE-KEY operation takes time O(lg V).
The algorithm calls both INSERT and EXTRACT-MIN once per Dijkstra’s algorithm resembles both breadth-first search and Prim’s
vertex and it calls DECREASE-KEY at most |E| times overall. algorithm for computing minimum spanning trees.
65 66
Outline Introduction Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths
Dijkstra’s algorithm
End of Chapter 5
Questions?
67