# Minimum Spanning Trees part 2

Document Sample

```					Shortest Path
Applications

In circuit design, one application is circuit
design: the time it takes for a change in
input to affect an output depends on the
shortest path

http://www.hp.com/
Applications

The Internet is a collection of interconnected
computer networks
Information is passed through packets
Packets are passed from the source, through
routers, to their destination
Routers are connected to either:
   individual computers, or
   other routers
These may be represented as graphs
Features of Dijkstra’s Algorithm

• A greedy algorithm
• “Visits” every vertex only once, when it becomes
the vertex with minimal distance amongst those
still in the priority queue
• Distances may be revised multiple times:
current values represent „best guess‟ based on
our observations so far
• Once a vertex is visited we are guaranteed to
have found the shortest path to that vertex….
why?

• Prove D(u) represent the shortest path to u (visited node)
• Assume u is the first vertex visited such that D(u) is not a
shortest path (thus the true shortest path to u must pass
through some unvisited vertex)
• Let x represent the first unvisited vertex on the true
shortest path to u        visited
unvisited
x
s
u

• D(x) must represent a shortest path to x, and D(x) 
Dshortest(u).
• However, Dijkstra‟s always visits the vertex with the
smallest distance next, so we can‟t possibly visit u before
we visit x
5
Shortest Path Problem

Shortest path network.
Directed graph G = (V, E).
Source s, destination t.
Length e = length of edge e.

Shortest path problem: find shortest directed path from s to t.

cost of path = sum of edge costs in path

2                       23                            3
9
s
18                                        Cost of path s-2-3-5-t
14                                                     6
2                               = 9 + 23 + 2 + 16
6
30                              4            19        = 48.
11
15                          5
5
6
20                     16

7                                                         t
44
Dijkstra's Algorithm

Dijkstra's algorithm.
 Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
 Initialize S = { s }, d(s) = 0.
 Repeatedly choose unexplored node v which minimizes
 (v )          min              d (u )   e ,
e  (u , v ) : u  S

add v to S, and set d(v) = (v).                                    shortest path to some u in explored
part, followed by a single edge (u, v)

e          v
d(u)
u
S
s
Dijkstra's Algorithm

Dijkstra's algorithm.
 Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
 Initialize S = { s }, d(s) = 0.
 Repeatedly choose unexplored node v which minimizes
 (v )          min              d (u )   e ,
e  (u , v ) : u  S

add v to S, and set d(v) = (v).                                    shortest path to some u in explored
part, followed by a single edge (u, v)

e          v
d(u)
u
S
s
Dijkstra's Algorithm: Proof of Correctness

Invariant. For each node u  S, d(u) is the length of the shortest s-u path.
Pf. (by induction on |S|)
Base case: |S| = 1 is trivial.
Inductive hypothesis: Assume true for |S| = k  1.
 Let v be next node added to S, and let u-v be the chosen edge.
 The shortest s-u path plus (u, v) is an s-v path of length (v).
 Consider any s-v path P. We'll see that it's no shorter than (v).
 Let x-y be the first edge in P that leaves S,
P
and let P' be the subpath to x.
 P is already too long as soon as it leaves S.            P'      x     y

s

S   u
v
 (P)   (P') +  (x,y)  d(x) +  (x, y)  (y)  (v)

nonnegative     inductive        defn of (y)   Dijkstra chose v
Dijkstra’s algorithm

d[s]  0
for each v  V – {s}
do d[v]  
S
QV           ⊳ Q is a priority queue maintaining V – S
while Q  
do u  EXTRACT-MIN(Q)
S  S  {u}
do if d[v] > d[u] + w(u, v)
then d[v]  d[u] + w(u, v)
Dijkstra’s algorithm

d[s]  0
for each v  V – {s}
do d[v]  
S
QV           ⊳ Q is a priority queue maintaining V – S
while Q  
do u  EXTRACT-MIN(Q)
S  S  {u}
do if d[v] > d[u] + w(u, v)        relaxation
then d[v]  d[u] + w(u, v)      step
Implicit DECREASE-KEY
Example of Dijkstra’s algorithm

Graph with                                        2
B           D
nonnegative                    10
edge weights:
8
A            1 4           7 9

3
C        2   E
Example of Dijkstra’s algorithm

Initialize:                                            
2
B           D
10
8
0 A             1 4           7 9

3
Q: A B C D E                               C       2   E
0                                           

S: {}
Example of Dijkstra’s algorithm

“A”  EXTRACT-MIN(Q):                             
2
B            D
10
8
0 A             1 4           7 9

3
Q: A B C D E                         C        2   E
0                                        

S: { A }
Example of Dijkstra’s algorithm

Relax all edges leaving A:              10           
2
B            D
10
8
0 A             1 4           7 9

3
Q: A B C D E                           C        2   E
0                              3           
10   3       

S: { A }
Example of Dijkstra’s algorithm

10           
“C”  EXTRACT-MIN(Q):                          2
B            D
10
8
0 A             1 4           7 9

3
Q: A B C D E                         C        2   E
0                              3           
10   3       

S: { A, C }
Example of Dijkstra’s algorithm

Relax all edges leaving C:               7           11
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                           C        2   E
0                              3           5
10   3        
7       11    5
S: { A, C }
Example of Dijkstra’s algorithm

“E”  EXTRACT-MIN(Q):                   7           11
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                          C        2   E
0                              3           5
10   3        
7       11    5
S: { A, C, E }
Example of Dijkstra’s algorithm

Relax all edges leaving E:               7           11
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                           C        2   E
0                              3           5
10   3        
7       11    5
7       11            S: { A, C, E }
Example of Dijkstra’s algorithm

“B”  EXTRACT-MIN(Q):                   7           11
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                          C        2   E
0                              3           5
10   3        
7       11    5
7       11            S: { A, C, E, B }
Example of Dijkstra’s algorithm

Relax all edges leaving B:               7           9
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                           C        2   E
0                              3           5
10   3        
7       11    5
7       11            S: { A, C, E, B }
9
Example of Dijkstra’s algorithm

“D”  EXTRACT-MIN(Q):                   7           9
2
B           D
10
8
0 A            1 4           7 9

3
Q: A B C D E                          C        2   E
0                              3           5
10   3        
7       11    5
7       11            S: { A, C, E, B, D }
9
Analysis of Dijkstra

while Q  
do u  EXTRACT-MIN(Q)
S  S  {u}
do if d[v] > d[u] + w(u, v)
then d[v]  d[u] + w(u, v)
Analysis of Dijkstra

while Q  
do u  EXTRACT-MIN(Q)
|V |         S  S  {u}
times          for each v  Adj[u]
do if d[v] > d[u] + w(u, v)
then d[v]  d[u] + w(u, v)
Analysis of Dijkstra

while Q  
do u  EXTRACT-MIN(Q)
|V |                S  S  {u}
times                 for each v  Adj[u]
degree(u)       do if d[v] > d[u] + w(u, v)
times                then d[v]  d[u] + w(u, v)
Analysis of Dijkstra

while Q  
do u  EXTRACT-MIN(Q)
|V |                S  S  {u}
times                 for each v  Adj[u]
degree(u)       do if d[v] > d[u] + w(u, v)
times                then d[v]  d[u] + w(u, v)

Handshaking Lemma  Q(E) implicit DECREASE-KEY’s.
Analysis of Dijkstra

while Q  
do u  EXTRACT-MIN(Q)
|V |                S  S  {u}
times                 for each v  Adj[u]
degree(u)       do if d[v] > d[u] + w(u, v)
times                then d[v]  d[u] + w(u, v)

Handshaking Lemma  Q(E) implicit DECREASE-KEY’s.
Time = Q(V·TEXTRACT-MIN + E·TDECREASE-KEY)
Note: Same formula as in the analysis of Prim’s
minimum spanning tree algorithm.
Analysis of Dijkstra (continued)

Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
Q    TEXTRACT-MIN TDECREASE-KEY             Total
Analysis of Dijkstra (continued)

Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
Q     TEXTRACT-MIN TDECREASE-KEY             Total

array      O(V)                 O(1)           O(V2)
Analysis of Dijkstra (continued)

Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
Q     TEXTRACT-MIN TDECREASE-KEY              Total

array      O(V)                 O(1)            O(V2)
binary
heap      O(lg V)              O(lg V)         O(E lg V)
Analysis of Dijkstra (continued)

Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY
Q      TEXTRACT-MIN TDECREASE-KEY              Total

array      O(V)                 O(1)            O(V2)
binary
heap      O(lg V)              O(lg V)         O(E lg V)
Fibonacci O(lg V)                O(1)    O(E + V lg V)
heap    amortized            amortized worst case
Dijkstra's Algorithm: Implementation

For each unexplored node, explicitly maintain  (v)                    min            d(u)    e   .
e  (u,v) : u  S

   Next node to explore = node with minimum (v).
When exploring v, for each incident edge e = (v, w), update



 (w)  min {  (w),  (v) e }.

Efficient implementation. Maintain a priority queue of unexplored
 nodes, prioritized by (v).

Priority Queue
PQ Operation      Dijkstra    Array    Binary heap
Insert           n            n       log n
ExtractMin          n            n       log n
ChangeKey          m            1       log n
IsEmpty           n            1         1
Total                         n2     m log n
Dijkstra's Algorithm: Implementation

For each unexplored node, explicitly maintain  (v)                          min          d(u)    e   .
e  (u,v) : u  S

   Next node to explore = node with minimum (v).
When exploring v, for each incident edge e = (v, w), update



 (w)  min {  (w),  (v) e }.

Efficient implementation. Maintain a priority queue of unexplored
 nodes, prioritized by (v).

Priority Queue
PQ Operation      Dijkstra    Array      Binary heap     d-way Heap          Fib heap †
Insert           n            n          log n         d log d n                 1
ExtractMin          n            n          log n         d log d n                log n
ChangeKey          m            1          log n           log d n                 1
IsEmpty           n            1            1               1                     1
Total                         n2        m log n       m log m/n n        m + n log n

† Individual ops are amortized bounds

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 2 posted: 10/27/2011 language: English pages: 33
Description: A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees; for instance the complete graph on four vertices
How are you planning on using Docstoc?