20.Graphs
20.Graphs
r s t u
v w x y
Breadth-First Search: Example
r s t u
0
v w x y
Q: s
Breadth-First Search: Example
r s t u
1 0
1
v w x y
Q: w r
Breadth-First Search: Example
r s t u
1 0 2
1 2
v w x y
Q: r t x
Breadth-First Search: Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Breadth-First Search: Properties
● BFS calculates the shortest-path distance to
the source node
■ Shortest-path distance (s,v) = minimum number
of edges from s to v, or if v not reachable from s
● BFS builds breadth-first tree, in which paths to
root represent shortest paths in G
Depth-First Search
● Depth-first search is another strategy for
exploring a graph
■ Explore “deeper” in the graph whenever possible
■ Edges are explored out of the most recently
discovered vertex v that still has unexplored edges
■ When all of v’s edges have been explored,
backtrack to the vertex from which v was
discovered
Depth-First Search
Depth-First Search: The Code
DFS Example
DFS Example
DFS Example
DFS Example
Minimum Spanning Trees
Spanning Tree
● A spanning tree of a graph, G, is a set of |V|-1
edges that connect all vertices of the graph.
Thus a spanning tree for G is a subgraph of G,
T = (V’, E’) with the following properties:
■ V’ = V
■ T is connected
■ T is acyclic.
Problem: Laying Telephone Wire
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
6 4
5 9
14 2
10
15
3 8
Minimum Spanning Tree
● Problem: given a connected, undirected,
weighted graph, find a spanning tree using
edges that minimize the total weight
6 4
5 9
14 2
10
15
3 8
Minimum Spanning Tree
● Which edges form the minimum spanning tree
(MST) of the below graph?
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Minimum Spanning Tree
● Answer:
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Generic MST
A= ф
While A does not form a spanning tree
do find an edge (u, v) that is safe for A
A = A U {(u,v)}
Return A
Kruskal’s Algorithm
Kruskal()
{
T = ;
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if adding (u,v) does not form a
cycle)
T = T U {{u,v}};
}
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1?
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2? 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5?
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8? 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9?
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13? 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14? 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17?
8 25
5
21 13 1
Kruskal’s Algorithm
2 19?
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21? 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25?
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Kruskal’s Algorithm
2 19
9
14 17
8 25
5
21 13 1
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
Q = V[G]; 5 9
for each u Q
key[u] = ; 14
10 2
key[r] = 0;
15
p[r] = NULL;
while (Q not empty) 3 8
u = ExtractMin(Q);
for each v Adj[u] Run on example graph
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
Q = V[G]; 5 9
for each u Q
key[u] = ; 14
10 2
key[r] = 0; 15
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Run on example graph
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
6 4
5 9
14 2
10
15
r 0
3 8
14 2
10
15
u 0
3 8
14 2
10
15
u 0
3 3 8
14 2
10
15
u 0
3 3 8
Prim’s Algorithm
6 4
5 9
14
14 2
10
15
0
3 3 8
u
Prim’s Algorithm
6 4
5 9
14
14 2
10
15
0 8
3 3 8
u
Prim’s Algorithm
6 4
5 9
10
14 2
10
15
0 8
3 3 8
u
Prim’s Algorithm
6 4
5 9
10
14 2
10
15
0 8
3 3 8
u
Prim’s Algorithm
6 4
5 9
10 2
14 2
10
15
0 8
3 3 8
u
Prim’s Algorithm
6 4
5 9
10 2
14 2
10
15
0 8 15
3 3 8
u
Prim’s Algorithm
6 4
u
5 9
10 2
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
6 4
u
5 9
10 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
6 4 4
u
5 9
10 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
6 4 4
u
5 9
5 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
u
6 4 4
5 9
5 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
u 6 4 4
5 9
5 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
u
6 4 4
5 9
5 2 9
14 2
10
15
0 8 15
3 3 8
Prim’s Algorithm
6 4 4
5 9
5 2 9
14 2 u
10
15
0 8 15
3 3 8
Simplified Algorithm
Dijkstra’s Algorithm
Shortest Paths Problems
1
v t
5 4 9
8 13
u 3 x 4 y
2 10
1 1 2
w z
6
Relax Relax
2 2
5 7 5 6
Dijkstra’s Algorithm
Dijkstra(G)
for each v V
d[v] = ;
d[s] = 0; S = ; Q = V;
while (Q )
u = ExtractMin(Q);
S = S U {u};
for each v Adj[u]
if (d[v] > d[u]+w(u,v))
Relaxation
d[v] = d[u]+w(u,v); Step
Example
u v
1
10
9
2 3
s 0 4 6
5 7
2
x y
Example
u v
1
10
10
9
2 3
s 0 4 6
5 7
5
2
x y
Example
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example
B
10 2
source A 4 3 D
5 1
C
Ex: run the algorithm
All Pairs Shortest Path
Floyd-Warshall Algorithm
Intermediate Vertices
Without loss of generality, we will assume that
V={1,2,…,n}, i.e., that the vertices of the graph
are numbered from 1 to n.
104
Intermediate Vertices
Consider a shortest path p from i to j such that
the intermediate vertices are from the set {1,
…,k}.
● If the vertex k is not an intermediate vertex on
p, then dij(k) = dij(k-1)
● If the vertex k is an intermediate vertex on p,
then dij(k) = dik(k-1) + dkj(k-1)
Interestingly, in either case, the subpaths contain merely nodes
from {1,…,k-1}.
Shortest Path
Therefore, we can conclude that
107
Floyd-Warshall Algorithm
109
Example
5 1 2 3
D(0)
1 3
1 0 8 5
8
3 2 2 3 0 ∞
2 3 ∞ 2 0
Final Result