Title: CS 450 Design
1CS 450 Design Analysis of AlgorithmsNotes
12
2Graph Algorithms
- Section VI
- Chapter 23 Minimum Spanning Trees
- Chapter 24 Shortest Path
3Graph Algorithms
b
d
c
a
e
i
h
g
f
Graphs Nodes Edges
4Minimum Spanning Trees
- Spanning Tree
- A tree (i.e., connected, acyclic graph) which
contains all the vertices of the graph - Minimum Spanning Tree (MST)
- Spanning tree with the minimum sum of weights
- Spanning forest
- If a graph is not connected, then there is a
spanning tree for each connected component of the
graph
5Minimum Spanning Trees
- A spanning tree is a subgraph (tree) that
connects all the vertices together. - A minimum spanning tree (MST) is a spanning tree
with weight less than or equal to the weight of
every other spanning tree.
8
7
b
d
c
4
9
2
14
a
e
i
11
4
4
6
8
10
h
g
f
1
2
6Minimum Spanning Trees
8
7
b
d
c
4
9
2
14
a
e
i
11
4
4
6
8
10
h
g
f
1
2
7Applications of MST
- Find the least expensive way to connect a set of
cities, terminals, computers, etc.
8Example
- Problem
- 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 - Everyone stays connected
- i.e., can reach every house from all other
houses - 2. Total repair cost is minimum
9Minimum Spanning Trees
- A connected, undirected graph
- Vertices houses Edges roads
- A weight w(u, v) on each edge (u, v) ? E
- Find T ? E such that
- T connects all vertices
- w(T) S(u,v)?T w(u, v) is minimized
10Properties of Minimum Spanning Trees
- Minimum spanning tree is not unique
- MST has no cycles see why
- We can take out an edge of a cycle, and still
have the vertices connected while reducing the
cost - of edges in a MST is V - 1
11Growing a MST Generic Approach
- Grow a set A of edges (initially empty)
- Incrementally add edges to A such that they would
belong to a MST - Idea add only safe edges
- An edge (u, v) is safe for A if and only if A ?
(u, v) is also a subset of some MST
12Generic MST algorithm
- A ? ?
- while A is not a spanning tree
- do find an edge (u, v) that is safe for A
- A ? A ? (u, v)
- return A
- How do we find safe edges?
13Finding Safe Edges
- Look at edge (h, g)
- Is it safe for A initially?
- Later on
- Let S ? V be any set of vertices that includes h
but not g (so that g is in V - S) - In any MST, there has to be one edge (at least)
that connects S with V - S - Why not choose the edge with minimum weight
(h,g)? Greedy strategy.
14Definitions
- A cut (S, V - S) is a partition of vertices into
disjoint sets S and V - S - An edge crosses the cut (S, V - S) if one
endpoint is in S and the other in V S
S?
?S
V- S ?
? V- S
15Definitions (contd)
- A cut respects a set A of edges ? no edge
- in A crosses the cut
- An edge is a light edge crossing a cut ? its
weight is minimum over all edges crossing the cut - there can be gt 1 light edges crossing
16Minimum Spanning Trees
A crossingedge
b
d
c
a
e
i
h
g
f
A Cut
17Minimum Spanning Trees
Respectededges
b
d
c
a
e
i
h
g
f
A Cut
18Theorem
- Let A be a subset of some MST (call it T) (S, V
- S) be a cut that respects A, and (u, v) be a
light edge crossing (S, V-S). Then (u, v) is safe
for A. - Proof
- Let T be an MST that includes A
- Case1 If T includes (u, v), then
- it would be safe for A done
- Case2 Suppose T does not
- include the edge (u, v)
Edges in A are shaded
19Theorem - Proof
- Let T be a MST that includes A but not (u,v)
- We will construct another MST, T, for G
containing (u,v), thus demonstrating the theorem -
- T now includes A ? (u, v)
20Theorem - Proof
- T contains a unique path p between u and v
- Path p must cross the cut (S, V - S) at least
once let (x, y) be that edge - Lets remove (x, y) ? breaks T into two components
- Adding (u, v) reconnects the components T T -
(x, y) ? (u, v)
21Theorem Proof (cont.)
- T T - (x, y) ? (u, v)
- Need to show T is an MST
- (u, v) is a light edge
- ? w(u, v) w(x, y)
- w(T)
- w(T) - w(x, y) w(u, v) w(T)
- Since T is a spanning tree w(T) w(T ) ? T
must be an MST as well
22Theorem Proof (cont.)
- Need to show that (u, v) is safe for A
- i.e., (u, v) can be a part of an MST
- A ? T since A ? T but (x, y) ? A since A
respects the cut - A ? (u, v) ? T
- Since T is an MST
- ? (u, v) is safe for A
23Minimum Spanning Tree T
A
8
7
b
d
c
4
9
2
14
a
e
u
11
4
6
4
8
10
v
g
f
1
2
- u v are on opposite sides of the cut, there is
at least one edge in T that crosses the cut on
the path from u to v. - Let (x,y) be any such edge.
- (x,y) is not in A since the cut respects A
24Minimum Spanning Tree T
A
8
7
b
d
c
4
9
2
14
a
e
u
11
4
6
4
8
10
v
g
f
1
2
Removing (x,y) breaks the MST into two
parts Adding (u,v) heals it, forming a new MST
T Since (u,v) is a light edge w(T)
w(T) Since T is a MST, w(T) w(T) so T is a
MST
25Prims Algorithm
- Begin with a single vertex - an arbitrary root
VA a - Add a light edge that crosses the cut
- Update the cut and repeat
- Repeat until the tree spans all vertices
- The edges in set A always form a single tree
26Prims Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a single vertex
- Add a light edge that crosses the cut
- Update the cut and repeat
27Prims Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a single vertex
- Add a light edge that crosses the cut
- Update the cut and repeat
28Prims Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a single vertex
- Add a light edge that crosses the cut
- Update the cut and repeat
29How to Find Light Edges Quickly?
- Use a priority queue Q
- Contains vertices not yet
- included in the tree, i.e., (V VA)
- VA a, Q b, c, d, e, f, g, h, i
- We associate a key with each vertex v
- keyv minimum weight of any edge (u, v)
connecting v to VA
w1
w2
a
Keya min(w1,w2)
30How to Find Light Edges Quickly?
- After adding a new node to VA we update the
weights of all the nodes adjacent to it - so after adding a to the tree, kb4 and
kh8 - Key of v is ? if v is not adjacent to any
vertices in VA
31Example
- 0 ? ? ? ? ? ? ? ?
- Q a, b, c, d, e, f, g, h, i
- VA ?
- Extract-MIN(Q) ? a
key b 4 ? b a key h 8 ? h a
4 ? ? ? ? ? 8 ? Q b, c, d, e, f, g, h, i
VA a Extract-MIN(Q) ? b
4
?
?
?
? v is the parent of v
8
7
b
c
d
4
9
?
2
?
a
e
i
11
14
4
6
7
8
10
h
g
f
2
1
?
?
?
8
32Example
key c 8 ? c b key h 8 ? h a -
unchanged 8 ? ? ? ? 8 ? Q c, d, e, f,
g, h, i VA a, b Extract-MIN(Q) ? c
8
?
?
key d 7 ? d c key f 4 ? f c key
i 2 ? i c 7 ? 4 ? 8 2 Q
d, e, f, g, h, i VA a, b,
c Extract-MIN(Q) ? i
7
2
4
33Example
key h 7 ? h i key g 6 ? g i
7 ? 4 6 8 Q d, e, f, g, h VA a, b, c,
i Extract-MIN(Q) ? f
key g 2 ? g f key 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
6
7
10
2
34Example
key h 1 ? h g 7 10 1 Q d, e,
h VA a, b, c, i, f, g Extract-MIN(Q) ? h
1
7 10 Q d, e VA a, b, c, i, f,
g, h Extract-MIN(Q) ? d
35Example
key e 9 ? e f 9 Q e VA
a, b, c, i, f, g, h, d Extract-MIN(Q) ? e Q
? VA a, b, c, i, f, g, h, d, e
9
36PRIM(V, E, w, r)
- Q ? ?
- for each u ? V
- do keyu ? 8
- pu ? NIL
- INSERT(Q, u)
- keyr ? 0
- while Q ? ?
- do u ? EXTRACT-MIN(Q)
- for each v ? Adju
- do if v ? Q and w(u, v) lt keyv
- then pv ? u
- DECREASE-KEY(Q, v,
w(u, v))
Total time O(VlgV ElgV) O(ElgV)
O(V) if Q is implemented as a min-heap
Min-heap operations O(VlgV)
Executed V times
Takes O(lgV)
Executed O(E) times total
O(ElgV)
Takes O(lgV)
37Prims Algorithm
- Prims algorithm is a greedy algorithm
- Greedy algorithms find solutions based on a
sequence of choices which are locally optimal
at each step. - Nevertheless, Prims greedy strategy produces a
globally optimum solution!
38What makes a greedy algorithm?
- Feasible
- Has to satisfy the problems constraints
- Locally Optimal
- The greedy part has to make the best local choice
among all feasible choices available on that step - If this local choice results in a global optimum
then the problem has optimal substructure - Irrevocable
- Once a choice is made it cant be un-done on
subsequent steps of the algorithm - Simple examples
- Playing chess by making best move without
lookahead - Giving fewest number of coins as change
- Simple and appealing, but doesnt always give the
best solution
39Does greedy work?
- The Traveling Salesman Problem
- A salesman wishes to visit k cities. The road
between each pair of cities has a certain cost
(distance). The salesman wants to visit all
cities in the cheapest total cost. - Repetition is not allowed - each city has to be
visited exactly once - The salesman must return home after the last city
a trip is a cycle that goes through each vertex
once - Does greedy work?
- Travelling salesman problem - Wikipedia, the free
encyclopedia
40Traveling Salesman Problem
10
8
7
b
d
c
8
4
2
12
a
e
i
11
4
10
4
6
8
h
g
f
1
2
7
41Traveling Salesman Problem
10
No known polynomial time solution!
8
7
b
d
c
8
4
2
12
a
e
i
11
4
10
4
6
8
h
g
f
1
2
7
Greedy doesnt work counter example
42A different instance of the generic approach
(instance 2)
(instance 1)
- A is a forest containing connected components
- Initially, each component is a single vertex
- Any safe edge merges two components into one
- Each component is a tree
43Kruskals Algorithm
- How is it different from Prims algorithm?
- Prims algorithm grows one tree all the time
- Kruskals algorithm grows
- multiple trees (i.e., a forest)
- Trees are merged together
- using safe edges
- Since an MST has exactly V - 1
- edges, after V - 1 merges,
- we would have only one component
44Kruskals Algorithm
- Start with each vertex being its own component
- Repeatedly merge two components into one by
choosing the light edge that connects them - Which components to consider at each iteration?
- Scan the set of edges in monotonically increasing
order by weight
45Kruskals Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a the lowest cost edge
- Add the lowest cost edge that grows the forest
- Repeat
46Kruskals Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a the lowest cost edge
- Add the lowest cost edge that grows the forest
- Repeat
47Kruskals Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a the lowest cost edge
- Add the lowest cost edge that grows the forest
- Repeat
48Kruskals Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a the lowest cost edge
- Add the lowest cost edge that grows the forest
- Repeat
49Kruskals Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Begin with a the lowest cost edge
- Add the lowest cost edge that grows the forest
- Repeat
50KRUSKAL(V, E, w)
- A ? ?
- for each vertex v ? V
- do MAKE-SET(v)
- sort E into non-decreasing order by w
- for each (u, v) taken from the sorted list
- do if FIND-SET(u) ? FIND-SET(v)
- then A ? A ? (u, v)
- UNION(u, v)
- return A
- Running time O(V ElgE ElgV) O(ElgE)
dependent on the implementation of the
disjoint-set data structure
O(V)
O(ElgE)
O(E)
O(lgV)
51Observations
- An optimal path to a destination node v that
passes through x must contain the optimal path to
x - Additional data
- Dv distance of v from source (inf)
- Pv predecessor of v on the best path
- On to Shortest Path and Dijkstras Algorithm
52Dijkstras Algorithm
8
7
b
d
c
4
9
2
a
e
i
11
14
4
4
6
8
10
h
g
f
1
2
- Want to find the best way to get from a single
source to anywhere else - Also a greedy algorithm
53Dijkstras Algorithm
Make a greedy move and then update if new
shortest paths are found Called Relax
54Shortest Path Problem
- 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
-
- There might be multiple shortest paths from u to
v
55Negative-Weight Edges
- Negative-weight edges may form negative-weight
cycles - If such cycles are reachable from
- the source, then d(s, v) is not properly
- defined!
- Keep going around the cycle, and get w(s, v) -
? for all v on the cycle
56Cycles
- Can shortest paths contain cycles?
- Negative-weight cycles
- Shortest path is not well defined
- 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 same weight
No!
No!
57Shortest-Paths Notation
- For each vertex v ? V
- d(s, v) shortest-path weight
- dv shortest-path weight estimate
- Initially, dv8
- dv?d(s,v) as algorithm progresses
- ?v predecessor of v on a shortest path from s
- If no predecessor, ?v NIL
- ? induces a tree - shortest-path tree
58Dijkstras
- Dijkstra(G, w, s)
- Initialize ? and D / parent and
weight / - S ? nil
- Q ? VG / make the queue /
- while Q ! nil time O(V)
- u extract-min( Q )
- S ? S u / extend the path /
- for each vertex, v, adjacent to u
- if Dv gt Du w(u,v) Relax
- Dv ? Du w(u,v)
- ?v ? u
59Relaxation Step
- Relaxing an edge (u, v) testing whether we can
improve the shortest path to v found so far by
going through u - If dv gt du w(u, v)
- we can improve the shortest path to v
- ? dvduw(u,v)
- ? ?v ? u
After relaxation dv ? du w(u, v)
RELAX(u, v, w)
no change
60Dijkstra (G, w, s)
Qlty,t,x,zgt
Sltsgt
Sltgt Qlts,t,x,z,ygt
t
x
1
?
?
10
9
2
4
3
6
0
s
s
7
5
?
?
2
y
z
61(No Transcript)
62All Pairs Shortest Paths
- Dont have a specific start and end point
63Variants of Shortest Path
- Single-source shortest paths
- G (V, E) ? find a shortest path from a given
source vertex s to each vertex v ? V - Single-destination shortest paths
- Find a shortest path to a given destination
vertex t from each vertex v - Reversing 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 - All-pairs shortest-paths
- Find a shortest path from u to v for every pair
of vertices u and v
64Algorithms
- Bellman-Ford algorithm
- Negative weights are allowed
- Negative cycles reachable from the source are not
allowed. - Dijkstras algorithm
- Negative weights are not allowed
- Operations common in both algorithms
- Initialization
- Relaxation
65Bellman-Ford Algorithm (contd)
- Idea
- Each edge is relaxed V1 times by making V-1
passes over the whole edge set. - To make sure that each edge is relaxed exactly
V 1 times, it puts the edges in an
unordered list and goes over the list V 1
times.
(t, x), (t, y), (t, z), (x, t), (y, x), (y, z),
(z, x), (z, s), (s, t), (s, y)
66BELLMAN-FORD(V, E, w, s)
Pass 1
6
7
E (t, x), (t, y), (t, z), (x, t), (y, x), (y,
z), (z, x), (z, s), (s, t), (s, y)
67(t, x), (t, y), (t, z), (x, t), (y, x), (y, z),
(z, x), (z, s), (s, t), (s, y)
68Floyd-Warshall Algorithm
- Consider a path between va and vfinish
- The path has zero or more intermediary vertices
that are also on the path (vb, vc, etc) - Now consider order vertices of a graph where the
vertices take values v1, v2, etc
69Shortest Paths
- Consider a subset of the vertices
- v1, v2, , vk
- Assume we restrict our shortest paths to use
only these vertices (call this the vk problem) - We can solve this problem recursively
- Either the shortest path doesnt contain vk, and
so is the solution to the vk-1 problem - Or, the shortest path goes through vk and so is
the sum of va vk and vk vfinish
70Alternatively
- d(i,j)k
- FW(W) W is a n by n matrix
- n ? rowsW
- D0 ? W
- for k ? 1 to n
- for i ? 1 to n
- for j ? 1 to n
- dijk min(d(i,j)k-1, d(i,k)k-1 d(k,j)k-1
-
wij If k 0 min(d(i,j)k-1, d(i,k)k-1
d(k,j)k-1 If k gt 1