Minimum Spanning Trees
description
Transcript of Minimum Spanning Trees
![Page 1: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/1.jpg)
CS 477/677 - Lecture 20 1
Minimum Spanning TreesProblem• A town has a set of houses
and a set of roads• A road connects 2 and only
2 houses• A road connecting houses u and v has a repair cost
w(u, v)Goal: Repair enough (and no more) roads such
that:1. Everyone stays connected: can reach every house
from all other houses, and2. Total repair cost is minimum
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 2: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/2.jpg)
CS 477/677 - Lecture 20 2
Minimum Spanning Trees
• A connected, undirected graph:
– Vertices = houses, Edges = roads
• A weight w(u, v) on each edge (u, v) E
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
Find T E such that:
1. T connects all vertices
2. w(T) = Σ(u,v)T w(u, v) is
minimized
![Page 3: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/3.jpg)
CS 477/677 - Lecture 20 3
Minimum Spanning Trees
• T forms a tree = spanning tree • A spanning tree whose weight is minimum over
all spanning trees is called a minimum spanning tree, or MST.
a
b c d
e
g g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 4: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/4.jpg)
CS 477/677 - Lecture 20 4
Properties of Minimum Spanning Trees
• Minimum spanning trees are not unique– Can replace (b, c) with (a, h) to obtain a different
spanning tree with the same cost
• MST have no cycles– We can take out an edge of
a cycle, and still have the
vertices connected while reducing the cost
• # of edges in a MST:– |V| - 1
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 5: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/5.jpg)
CS 477/677 - Lecture 20 5
Growing a MST
Minimum-spanning-tree problem: find a MST for a connected, undirected graph, with a weight function associated with its edges
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
A generic solution:• Build a set A of edges (initially
empty)
• Incrementally add edges to A such that they would belong to a MST
• An edge (u, v) is safe for A if and only if A {(u, v)} is also a subset of some MST
We will add only safe edges
![Page 6: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/6.jpg)
CS 477/677 - Lecture 20 6
Generic MST algorithm
1. A ←
2. while A is not a spanning tree
3. do find an edge (u, v) that is safe for A
4. A ← A {(u, v)}
5. return A
• How do we find safe edges?
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 7: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/7.jpg)
CS 477/677 - Lecture 20 7
We would addedge (c, f)
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
The Algorithm of Kruskal
• Start with each vertex being its own component
• Repeatedly merge two components into one by choosing the light edge that connects them
• Scan the set of edges in monotonically increasing order by weight
• Uses a disjoint-set data structure to determine whether an edge connects vertices in different components
![Page 8: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/8.jpg)
CS 477/677 - Lecture 20 8
Operations on Disjoint Data Sets
• MAKE-SET(u) – creates a new set whose only member is u
• FIND-SET(u) – returns a representative element from the set that contains u– May be any of the elements of the set that has a
particular property
– E.g.: Su = {r, s, t, u}, the property is that the element
be the first one alphabetically
FIND-SET(u) = r FIND-SET(s) = r– FIND-SET has to return the same value for a given set
![Page 9: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/9.jpg)
CS 477/677 - Lecture 20 9
Operations on Disjoint Data Sets
• UNION(u, v) – unites the dynamic sets that
contain u and v, say Su and Sv
– E.g.: Su = {r, s, t, u}, Sv = {v, x, y}
UNION (u, v) = {r, s, t, u, v, x, y}
![Page 10: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/10.jpg)
CS 477/677 - Lecture 20 10
KRUSKAL(V, E, w)1. A ← 2. for each vertex v V3. do MAKE-SET(v)4. sort E into non-decreasing order by weight w5. for each (u, v) taken from the sorted list6. do if FIND-SET(u) FIND-SET(v)7. then A ← A {(u, v)} 8. UNION(u, v)9. return ARunning time: O(E lgV) – dependent on the implementation
of the disjoint-set data structure
![Page 11: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/11.jpg)
CS 477/677 - Lecture 20 11
Example
1. Add (h, g)2. Add (c, i)3. Add (g, f)4. Add (a, b)5. Add (c, f)6. Ignore (i, g)7. Add (c, d)8. Ignore (i, h)9. Add (a, h)10. Ignore (b, c)11. Add (d, e)12. Ignore (e, f)13. Ignore (b, h)14. Ignore (d, f)
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
1: (h, g)2: (c, i), (g, f)4: (a, b), (c, f)6: (i, g)7: (c, d), (i, h)
8: (a, h), (b, c) 9: (d, e)10: (e, f)11: (b, h)14: (d, f)
{g, h}, {a}, {b}, {c}, {d}, {e}, {f}, {i}
{g, h}, {c, i}, {a}, {b}, {d}, {e}, {f}
{g, h, f}, {c, i}, {a}, {b}, {d}, {e}
{g, h, f}, {c, i}, {a, b}, {d}, {e}
{g, h, f, c, i}, {a, b}, {d}, {e}
{g, h, f, c, i}, {a, b}, {d}, {e}
{g, h, f, c, i, d}, {a, b}, {e}
{g, h, f, c, i, d}, {a, b}, {e}
{g, h, f, c, i, d, a, b}, {e}
{g, h, f, c, i, d, a, b}, {e}
{g, h, f, c, i, d, a, b, e}
{g, h, f, c, i, d, a, b, e}
{g, h, f, c, i, d, a, b, e}
{g, h, f, c, i, d, a, b, e}{a}, {b}, {c}, {d}, {e}, {f}, {g}, {h}, {i}
![Page 12: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/12.jpg)
CS 477/677 - Lecture 20 12
The algorithm of Prim
• The edges in set A always form a single tree
• Starts from an arbitrary “root”: VA = {a}
• At each step:
– Find a light edge crossing cut (VA, V - VA)
– Add this edge to A
– Repeat until the tree spans all vertices
• Greedy strategy– At each step the edge added contributes the minimum amount
possible to the weight of the tree
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 13: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/13.jpg)
CS 477/677 - Lecture 20 13
How to Find Light Edges Quickly?Use a priority queue Q:
• Contains all vertices not yet
included in the tree (V – VA)– V = {a}, Q = {b, c, d, e, f, g, h, i}
• With each vertex we associate a key:
key[v] = minimum weight of any edge (u, v) connecting v to a vertex in the tree
– Key of v is if v is not adjacent to any vertices in VA
– After adding a new node to VA we update the weights of all the nodes
adjacent to it
– We added node a key[b] = 4, key[h] = 8
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
![Page 14: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/14.jpg)
CS 477/677 - Lecture 20 14
PRIM(V, E, w, r)1. Q ← 2. for each u V
3. do key[u] ← ∞
4. π[u] ← NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0)
7. while Q
8. do u ← EXTRACT-MIN(Q)
9. for each v Adj[u]10. do if v Q and w(u, v) < key[v]11. then π[v] ← u12. DECREASE-KEY(Q, v, w(u, v))
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
0
0 Q = {a, b, c, d, e, f, g, h, i} VA =
Extract-MIN(Q) a
![Page 15: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/15.jpg)
CS 477/677 - Lecture 20 15
Example 0
Q = {a, b, c, d, e, f, g, h, i} VA =
Extract-MIN(Q) a
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [b] = 4 [b] = akey [h] = 8 [h] = a
4 8 Q = {b, c, d, e, f, g, h, i} VA = {a}
Extract-MIN(Q) b
4
8
![Page 16: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/16.jpg)
CS 477/677 - Lecture 20 16
4
8
8
Example
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [c] = 8 [c] = bkey [h] = 8 [h] = a - unchanged
8 8 Q = {c, d, e, f, g, h, i} VA = {a, b}
Extract-MIN(Q) c
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [d] = 7 [d] = ckey [f] = 4 [f] = ckey [i] = 2 [i] = c
7 4 8 2 Q = {d, e, f, g, h, i} VA = {a, b, c}
Extract-MIN(Q) i
4
8
8
7
4
2
![Page 17: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/17.jpg)
CS 477/677 - Lecture 20 17
Example
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [h] = 7 [h] = ikey [g] = 6 [g] = i
7 4 8 Q = {d, e, f, g, h} VA = {a, b, c, i}
Extract-MIN(Q) f
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [g] = 2 [g] = fkey [d] = 7 [d] = c unchanged
key [e] = 10 [e] = f 7 10 2 8
Q = {d, e, g, h} VA = {a, b, c, i, f}
Extract-MIN(Q) g
4 7
8 4
8
2
7 64 7
7 6 4
8
2
2
10
![Page 18: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/18.jpg)
CS 477/677 - Lecture 20 18
Example
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [h] = 1 [h] = g 7 10 1 Q = {d, e, h} VA = {a, b, c, i, f, g}
Extract-MIN(Q) h
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
7 10 Q = {d, e} VA = {a, b, c, i, f, g, h}
Extract-MIN(Q) d
4 7
10
7 2 4
8
2
14 7
10
1 2 4
8
2
![Page 19: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/19.jpg)
CS 477/677 - Lecture 20 19
Example
a
b c d
e
h g f
i
4
8 7
8
11
1 2
7
2
4 14
9
106
key [e] = 9 [e] = f 9 Q = {e} VA = {a, b, c, i, f, g, h, d}
Extract-MIN(Q) eQ = VA = {a, b, c, i, f, g, h, d, e}
4 7
10
1 2 4
8
2 9
![Page 20: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/20.jpg)
CS 477/677 - Lecture 20 20
PRIM(V, E, w, r)1. Q ← 2. for each u V
3. do key[u] ← ∞
4. π[u] ← NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0) ► key[r] ← 07. while Q
8. do u ← EXTRACT-MIN(Q)
9. for each v Adj[u]10. do if v Q and w(u, v) < key[v]11. then π[v] ← u12. DECREASE-KEY(Q, v, w(u, v))
O(V) if Q is implemented as a min-heap
Executed |V| times
Takes O(lgV)
Min-heap operations:O(VlgV)
Executed O(E) times
Constant
Takes O(lgV)
O(ElgV)
Total time: O(VlgV + ElgV) = O(ElgV)
![Page 21: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/21.jpg)
CS 477/677 - Lecture 20 21
Shortest Path Problems
• How can we find the shortest route between two points on a map?
• Model the problem as a graph problem:– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)
![Page 22: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/22.jpg)
CS 477/677 - Lecture 20 22
Shortest Path Problems
• Input:– Directed graph G = (V, E)
– Weight function w : E → R
• Weight of path p = v0, v1, . . . , vk
• Shortest-path weight from u to v:
δ(u, v) = min w(p) : u v if there exists a path from u to v
∞ otherwise
• Shortest path u to v is any path p such that w(p) = δ(u, v)
k
iii vvwpw
11 ),()(
p
0
3 9
5 11
3
6
57
6
s
t x
y z
22 1
4
3
![Page 23: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/23.jpg)
CS 477/677 - Lecture 20 23
Variants of Shortest Paths
• Single-source shortest path– G = (V, E) find a shortest path from a given source vertex s to
each vertex v V• Single-destination shortest path
– Find a shortest path to a given destination vertex t from each vertex v
– Reverse the direction of each edge single-source
• Single-pair shortest path– Find a shortest path from u to v for given vertices u and v– Solve the single-source problem
• All-pairs shortest-paths– Find a shortest path from u to v for every pair of vertices u and v
![Page 24: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/24.jpg)
CS 477/677 - Lecture 20 24
Optimal Substructure of Shortest Paths
Given:– A weighted, directed graph G = (V, E)
– A weight function w: E R,
– A shortest path p = v1, v2, . . . , vk from v1 to vk
– A subpath of p: pij = vi, vi+1, . . . , vj, with 1 i j k
Then: pij is a shortest path from vi to vj
Proof: p = v1 vi vj vk
w(p) = w(p1i) + w(pij) + w(pjk) Assume pij’ from vi to vj with w(pij’) < w(pij) w(p’) = w(p1i) + w(pij’) + w(pjk) < w(p) contradiction!
p1i pij pjk
v1
vi
vj
vk
p1i
pij
pij’
pjk
![Page 25: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/25.jpg)
CS 477/677 - Lecture 20 25
Negative-Weight Edges
• s a: only one path
(s, a) = w(s, a) = 3
• s b: only one path
(s, b) = w(s, a) + w(a, b) = -1
• s c: infinitely many paths
s, c, s, c, d, c, s, c, d, c, d, c
cycle has positive weight (6 - 3 = 3)
s, c is shortest path with weight (s, b) = w(s, c) = 5
0
3 -1
- -
3
-4
2
8
-6
s
a b
e f
-5 11-3y
3
56
4
7
c d g
What if we have negative-
weight edges?
![Page 26: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/26.jpg)
CS 477/677 - Lecture 20 26
Negative-Weight Edges• s e: infinitely many paths:
s, e, s, e, f, e, s, e, f, e, f, e– cycle e, f, e has negative
weight: 3 + (- 6) = -3
– can find paths from s to e with arbitrarily large negative weights
(s, e) = - no shortest path exists between s and e
– Similarly: (s, f) = - , (s, g) = -
0
3 -1
- -
3
-4
2
8
-6
s
a b
e f
-5 11-3y
3
56
4
7
c d g
j
h i2
3-8
(s, h) = (s, i) = (s, j) =
h, i, j notreachablefrom s
![Page 27: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/27.jpg)
CS 477/677 - Lecture 20 27
Negative-Weight Edges
• Negative-weight edges may form
negative-weight cycles
• If such cycles are reachable from
the source: (s, v) is not properly
defined
– Keep going around the cycle, and get
w(s, v) = - for all v on the cycle
0
3
-4
2
8
-6
s
a b
e f
-3y3
56
4
7
c d g
![Page 28: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/28.jpg)
CS 477/677 - Lecture 20 28
Cycles
• Can shortest paths contain cycles?
• Negative-weight cycles
• Positive-weight cycles:– By removing the cycle we can get a shorter path
• Zero-weight cycles– No reason to use them
– Can remove them to obtain a path with similar weight
• We will assume that when we are finding shortest paths, the paths will have no cycles
No!No!
![Page 29: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/29.jpg)
CS 477/677 - Lecture 20 29
Shortest-Path Representation
For each vertex v V:• d[v] = δ(s, v): a shortest-path estimate
– Initially, d[v]=∞– Reduces as algorithms progress
[v] = predecessor of v on a shortest path from s– If no predecessor, [v] = NIL induces a tree—shortest-path tree
• Shortest paths & shortest path trees are not unique
0
3 9
5 11
3
6
57
6
s
t x
y z
22 1
4
3
![Page 30: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/30.jpg)
CS 477/677 - Lecture 20 30
Initialization
Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v V
2. do d[v] ←
3. [v] ← NIL
4. d[s] ← 0
• All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE
![Page 31: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/31.jpg)
CS 477/677 - Lecture 20 31
Relaxation• Relaxing an edge (u, v) = testing whether we
can improve the shortest path to v found so far by going through u
If d[v] > d[u] + w(u, v) we can improve the shortest path to v update d[v] and [v]
5 92
u v
5 72
u v
RELAX(u, v, w)
5 62
u v
5 62
u v
RELAX(u, v, w)
After relaxation:d[v] d[u] + w(u, v)
s s
![Page 32: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/32.jpg)
CS 477/677 - Lecture 20 32
RELAX(u, v, w)
1. if d[v] > d[u] + w(u, v)2. then d[v] ← d[u] + w(u, v)3. [v] ← u
• All the single-source shortest-paths algorithms – start by calling INIT-SINGLE-SOURCE– then relax edges
• The algorithms differ in the order and how many times they relax each edge
![Page 33: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/33.jpg)
CS 477/677 - Lecture 20 33
Bellman-Ford Algorithm
• Single-source shortest paths problem– Computes d[v] and [v] for all v V
• Allows negative edge weights• Returns:
– TRUE if no negative-weight cycles are reachable from the source s
– FALSE otherwise no solution exists• Idea:
– Traverse all the edges |V – 1| times, every time performing a relaxation step of each edge
![Page 34: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/34.jpg)
CS 477/677 - Lecture 20 34
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)2. for i ← 1 to |V| - 13. do for each edge (u, v) E4. do RELAX(u, v, w)5. for each edge (u, v) E6. do if d[v] > d[u] + w(u, v)7. then return FALSE8. return TRUE
0
6
5
7
7
9
s
t x
y z
8-3
2-4
-2
0
6
5
7
7
9
s
t x
y z
8-3
2-4
-2
E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
6
7
![Page 35: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/35.jpg)
CS 477/677 - Lecture 20 35
Example
0
6
7
6
5
7
7
9
s
t x
y z
8-3
2-4
-2
(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
0
6
7
6
5
7
7
9
s
t x
y z
8-3
2-4
-211
2
4
0
6
7
6
5
7
7
9
s
t x
y z
8-3
2-4
-211
2
42
0
6
7
6
5
7
7
9
s
t x
y z
8-3
2-4
-211
2
42
-2
Pass 1 Pass 2
Pass 3 Pass 4
![Page 36: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/36.jpg)
CS 477/677 - Lecture 20 36
Detecting Negative Cycles
• for each edge (u, v) E• do if d[v] > d[u] + w(u, v)• then return FALSE• return TRUE
0
c
s b2
3-8
0
c
s b2
3-8
2
5
-3 -3 2
5
c
s b2
3-8
-1
2
-6Look at edge (s, b):
d[b] = -1d[s] + w(s, b) = -4
d[b] > d[s] + w(s, b)
![Page 37: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/37.jpg)
CS 477/677 - Lecture 20 37
BELLMAN-FORD(V, E, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)2. for i ← 1 to |V| - 13. do for each edge (u, v) E4. do RELAX(u, v, w)5. for each edge (u, v) E6. do if d[v] > d[u] + w(u, v)7. then return FALSE8. return TRUE
Running time: O(VE)
(V)O(V)
O(E)
O(E)
![Page 38: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/38.jpg)
CS 477/677 - Lecture 20 38
Shortest Path Properties
• Triangle inequalityFor all (u, v) E, we have:
δ(s, v) ≤ δ(s, u) + w(u, v)
• If u is on the shortest path to v we have the equality sign
5 72
u v
5 62
u v
s
s
![Page 39: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/39.jpg)
CS 477/677 - Lecture 20 39
Shortest Path Properties• Upper-bound property
We always have d[v] ≥ δ(s, v) for all v.
Once d[v] = δ(s, v), it never changes.– The estimate never goes up – relaxation only lowers the
estimate
0
6
7
6
5
7
7
9
s
v x
y z
8-3
2-4
-211
2
4
0
6
7
6
5
7
7
9
s
v x
y z
8-3
2-4
-211
2
42Relax (x, v)
![Page 40: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/40.jpg)
CS 477/677 - Lecture 20 40
Shortest Path Properties
• No-path propertyIf there is no path from s to v then d[v] = ∞ always.– δ(s, h) = ∞ and d[h] ≥ δ(s, h) d[h] = ∞
0
3 -1
- -
3
-4
2
8
-6
s
a b
e f
-5 11-3y
3
56
4
7
c d g
j
h i2
3-8
(s, h) = (s, i) = (s, j) =
h, i, j notreachablefrom s
![Page 41: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/41.jpg)
CS 477/677 - Lecture 20 41
Shortest Path Properties
• Convergence propertyIf s u → v is a shortest path, and if d[u] = δ(s, u) at any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times afterward.
0
6
7
5
2
4
s
u v1185
4
• If d[v] > δ(s, v) after relaxation:d[v] = d[u] + w(u, v)
d[v] = 5 + 2 = 7• Otherwise, the value remains unchanged, because it must have been the shortest path value
![Page 42: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/42.jpg)
CS 477/677 - Lecture 20 42
Shortest Path Properties
• Path relaxation propertyLet p = v0, v1, . . . , vk be a shortest path from s
= v0 to vk. If we relax, in order, (v0, v1), (v1, v2), . . . ,
(vk-1, vk), even intermixed with other relaxations,
then d[vk ] = δ(s, vk).
0
6
5
2
4s
v1 v2
113
5 7
11
14d[v1] = δ(s, v1)
d[v2] = δ(s, v2)
d[v3] = δ(s, v3)
d[v4] = δ(s, v4)v3
v4
![Page 43: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/43.jpg)
CS 477/677 - Lecture 20 43
Single-Source Shortest Paths in DAGs
• Given a weighted DAG: G = (V, E) – solve the shortest path problem
• Idea:– Topologically sort the vertices of the graph
– Relax the edges according to the order given by the topological sort
• for each vertex, we relax each edge that starts from that vertex
• Are shortest-paths well defined in a DAG?– Yes, (negative-weight) cycles cannot exist
6 5
2
4
y
u v11
4
x
![Page 44: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/44.jpg)
CS 477/677 - Lecture 20 44
Dijkstra’s Algorithm
• Single-source shortest path problem:– No negative-weight edges: w(u, v) > 0 (u, v) E
• Maintains two sets of vertices:– S = vertices whose final shortest-path weights have
already been determined
– Q = vertices in V – S: min-priority queue• Keys in Q are estimates of shortest-path weights (d[v])
• Repeatedly select a vertex u V – S, with the minimum shortest-path estimate d[v]
![Page 45: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/45.jpg)
CS 477/677 - Lecture 20 45
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
2. S ←
3. Q ← V[G]
4. while Q
5. do u ← EXTRACT-MIN(Q)
6. S ← S {u}
7. for each vertex v Adj[u]8. do RELAX(u, v, w)
0
10
1
5
2
s
t x
y z
2 3
9
7
4 6
0
10
1
5
2
s
t x
y z
2 3
9
7
4 6
10
5
![Page 46: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/46.jpg)
CS 477/677 - Lecture 20 46
Example
0
10
5
10
1
5
2
s
t x
y z
2 3
9
7
4 6
8 14
7
0
8 14
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
13
0
8 13
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
9
0
8 9
5 7
10
1
5
2
s
t x
y z
2 3
9
7
4 6
![Page 47: Minimum Spanning Trees](https://reader035.fdocuments.in/reader035/viewer/2022081515/56814073550346895dabf3c1/html5/thumbnails/47.jpg)
CS 477/677 - Lecture 20 47
Dijkstra (G, w, s)1. INITIALIZE-SINGLE-SOURCE(V, s)
2. S ←
3. Q ← V[G]
4. while Q
5. do u ← EXTRACT-MIN(Q)
6. S ← S {u}
7. for each vertex v Adj[u]8. do RELAX(u, v, w)
Running time: O(VlgV + ElgV) = O(ElgV)
(V)
O(V) build min-heap
Executed O(V) times
O(lgV)
O(E) times; O(lgV)