Graph Algorithms PPT

advertisement
Single-Source Shortest Path
&
Minimum Spanning Trees
Dijkstra’s, Prim’s, and Kruskal’s
Algorithms
Single-Source Shortest Path
Defined
• Given
– G = (V, E)
– Source vertex s  V
• Compute
– {SP} = set of shortest paths from s to all
vertices in V
Path Weight Defined
Weight of path p = {v1, v2, … vk}
Shortest-path weight from u to v
Determining Path Lengths
a
1
3
s
b
5
0
2
4
c
Determining Path Lengths
a
3
1
3
s
b
5
0
5
2
4
c
2
Determining Path Lengths
a
3
1
3
s
b
5
0
5
2
4
c
2
d[b] <= d[c] + w(c,b)
5 <= 2 + 4
Determining Path Lengths
a
d[b] > d[a] + w(a,b)
5 > 3 + 1
3
1
3
s
b
5
0
5
2
4
c
2
Determining Path Lengths
a
d[b] > d[a] + w(a,b)
5 > 3 + 1
3
1
3
s
b
5
0
4
2
4
c
2
Path Lengths - FIN
a
All vertices are in S
3
1
3
s
b
5
0
4
2
4
c
2
Cycles in Paths
a
b
-4
3
-1
3
s
4
6
c
d
5
0
5
2
e
-
-3
11
3
-6
g
8
-
f
-
7
Properties of Shortest Paths
• Cannot contain a negative-weight cycle
– No need to contain 0-weight cycle
– No need to contain positive-weight cycle
– Thus contains no cycles
• Will contain at most V-1 edges
– Assumes no cycles (as stated above)
• Maintain predecessor information
– E & V
Shortest Paths Not Unique
t
x
6
3
9
3
s
2
1
4
2
7
0
3
5
5
y
t
z
x
6
3
11
6
t
9
3
s
9
3
2
0
x
6
3
1
4
2
7
s
2
0
1
4
3
5
5
y
2
7
3
11
5
5
6
z
y
11
6
z
Shortest Path Distance Is Unique
x
s
u
z
y
[z] must be either x or y, but not both
This is true even if d[x] + w(x,z) = d[y] + w(y,z)
establishing that if s is source, d[z] is well-defined and unique
v
Setting up the Solution
• For each vertex v in V[G]
– Predecessor of v ([v]) = NIL
– Distance to v from source (d[v]) = 
• RELAX: as we process the graph, we’ll
– “Improve” [v] & d[v] by “relaxing”
if (d[v] > d[u] + w(u,v))
d[v] = d[u] + w(u,v)
[v] = u
Init & Relax
Initialize(G, s)
for each v in V[G]
d[v] = INFINITY
[v] = NULL
d[s] = 0
Relax(u, v, w)
if (d[v] > d[u] + w(u,v))
d[v] = d[u] + w(u,v)
[v] = u
Dijkstra’s Algorithm (1959)
Assume G = (V, E), source s, & weight function w
Also assume all edges are non-negative
Dijkstra(G, w, s)
Initialize(G, s)
S = {}
Q = V[G]
while Q != {}
u = Extract_Min(Q)
S.Add(u)
for each vertex v in u.Adjacency()
Relax(u, v, w)
Example: Dijkstra’s
t
s
1

10
2
x

3
9
0
4
6
7
5

y

2
z
Example: Dijkstra’s
t
1

10
10
s
x
2
3
9
0
4
6
7
5

5
y
2
z
Example: Dijkstra’s
t
1
8
10
s
x
2
14
3
9
0
4
6
7
5
5
y
7
2
z
Example: Dijkstra’s
t
1
8
10
s
x
2
13
3
9
0
4
6
7
5
5
y
7
2
z
Example: Dijkstra’s
t
1
8
10
s
x
2
9
3
9
0
4
6
7
5
5
y
7
2
z
Dijkstra’s - FIN
t
1
8
10
s
x
2
9
3
9
0
4
6
7
5
5
y
7
2
z
Runtime of Dijkstra’s
• O(V lgV + E lgV)
• O(E lgV)
– If G is connected
• O(V lgV + E)
– If we use a Fibonacci heap
Better
if V << E
(i.e. sparsely
connected)
MST Defined
Minimum Spanning Tree
• Connect all nodes at minimum cost.
• Can start anywhere?
– Why?
• One solution.
– i.e. There is just one minimum
• Two algorithms
– Prim’s
– Kruskal’s
A Graph
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
start
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
5
4
2
5
6
6
5
44
The Same Graph
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
9
4
5
12
8
6
7
2
5
8
6
8
5
7
9
12
11
6
5
4
2
5
4
2
5
6
6
5
44
Download