cs 2133: algorithms

43
CS 2133: Algorithms Minimum Spanning Tree Shortest Paths

Upload: risa

Post on 08-Jan-2016

36 views

Category:

Documents


4 download

DESCRIPTION

CS 2133: Algorithms. Minimum Spanning Tree Shortest Paths. Review: Getting Dressed. Underwear. Socks. Watch. Pants. Shoes. Shirt. Belt. Tie. Jacket. Socks. Underwear. Pants. Shoes. Watch. Shirt. Belt. Tie. Jacket. Review: Topological Sort Algorithm. Topological-Sort() { - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 2133: Algorithms

CS 2133: Algorithms

Minimum Spanning Tree

Shortest Paths

Page 2: CS 2133: Algorithms

Review: Getting Dressed

Underwear Socks

ShoesPants

Belt

Shirt

Watch

Tie

Jacket

Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

Page 3: CS 2133: Algorithms

Review: Topological Sort Algorithm

Topological-Sort()

{

Run DFS

When a vertex is finished, output it

Vertices are output in reverse topological order

}

Time: O(V+E)

Page 4: CS 2133: Algorithms

Review: Minimum Spanning Tree

Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 4

5

2

9

15

8

Page 5: CS 2133: Algorithms

Review: Minimum Spanning Tree

Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight

1410

3

6 4

5

2

9

15

8

Page 6: CS 2133: Algorithms

Review: Minimum Spanning Tree

MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees

If T is MST of G, and A T is a subtree of T, and (u,v) is the min-weight edge connecting A to V-A, then (u,v) T

Page 7: CS 2133: Algorithms

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);

1410

3

6 45

2

9

15

8

Run on example graph

Page 8: CS 2133: Algorithms

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);

1410

3

6 45

2

9

15

8

Run on example graph

Page 9: CS 2133: Algorithms

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);

0

1410

3

6 45

2

9

15

8

Pick a start vertex r

r

Page 10: CS 2133: Algorithms

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);

0

1410

3

6 45

2

9

15

8

Red vertices have been removed from Q

u

Page 11: CS 2133: Algorithms

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);

0

3

1410

3

6 45

2

9

15

8

Red arrows indicate parent pointers

u

Page 12: CS 2133: Algorithms

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);

14

0

3

1410

3

6 45

2

9

15

8

u

Page 13: CS 2133: Algorithms

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);

14

0

3

1410

3

6 45

2

9

15

8u

Page 14: CS 2133: Algorithms

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);

14

0 8

3

1410

3

6 45

2

9

15

8u

Page 15: CS 2133: Algorithms

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);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 16: CS 2133: Algorithms

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);

10

0 8

3

1410

3

6 45

2

9

15

8u

Page 17: CS 2133: Algorithms

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);

10 2

0 8

3

1410

3

6 45

2

9

15

8u

Page 18: CS 2133: Algorithms

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);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8u

Page 19: CS 2133: Algorithms

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);

10 2

0 8 15

3

1410

3

6 45

2

9

15

8

u

Page 20: CS 2133: Algorithms

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);

10 2 9

0 8 15

3

1410

3

6 45

2

9

15

8

u

Page 21: CS 2133: Algorithms

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);

10 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 22: CS 2133: Algorithms

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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 23: CS 2133: Algorithms

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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 24: CS 2133: Algorithms

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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 25: CS 2133: Algorithms

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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 26: CS 2133: Algorithms

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);

5 2 9

0 8 15

3

4

1410

3

6 45

2

9

15

8

u

Page 27: CS 2133: Algorithms

Review: 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);

What is the hidden cost in this code?

Page 28: CS 2133: Algorithms

Review: 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;

DecreaseKey(v, w(u,v));

Page 29: CS 2133: Algorithms

Review: 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;

DecreaseKey(v, w(u,v));

How often is ExtractMin() called?How often is DecreaseKey() called?

Page 30: CS 2133: Algorithms

Review: 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);// log V for each v Adj[u] if (v Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); // decrease

// key=O(log V)

What will be the running time?A: Depends on queue binary heap: O(E lg V)

Page 31: CS 2133: Algorithms

Single-Source Shortest Path

Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v “Shortest-path” = minimum weight Weight of path is sum of edges E.g., a road map: what is the shortest path from

Chapel Hill to Charlottesville?

Page 32: CS 2133: Algorithms

Shortest Path Properties

Again, we have optimal substructure: the shortest path consists of shortest subpaths:

Proof: suppose some subpath is not a shortest path There must then exist a shorter subpath Could substitute the shorter subpath for a shorter path But then overall path is not shortest path. Contradiction

Page 33: CS 2133: Algorithms

Shortest Path Properties

Define (u,v) to be the weight of the shortest path from u to v

Shortest paths satisfy the triangle inequality: (u,v) (u,x) + (x,v)

“Proof”: x

u v

This path is no longer than any other path

Page 34: CS 2133: Algorithms

Shortest Path Properties

In graphs with negative weight cycles, some shortest paths will not exist (Why?):

< 0

Page 35: CS 2133: Algorithms

Relaxation

A key technique in shortest path algorithms is relaxation Idea: for all v, maintain upper bound d[v] on (s,v)Relax(u,v,w) {

if (d[v] > d[u]+w) then d[v]=d[u]+w;

}

952

752

Relax

652

652

Relax

Page 36: CS 2133: Algorithms

Dijkstra’s Algorithm

Here we assume no negative edge weights Similar to breadth-first search

Grow a tree gradually, advancing from vertices taken from a queue

Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]

Page 37: CS 2133: Algorithms

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}; for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation

StepNote: thisis really a call to Q->DecreaseKey()

B

C

DA

10

4 3

2

15

Ex: run the algorithm

Page 38: CS 2133: Algorithms

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}; for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);

How many times is ExtractMin() called?

How many times is DecreaseKey() called?

What will be the total running time?

Page 39: CS 2133: Algorithms

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}; for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);

How many times is ExtractMin() called?

How many times is DecraseKey() called?

A: O(E lg V) using binary heap for QCan acheive O(V lg V + E) with Fibonacci heaps

Page 40: CS 2133: Algorithms

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}; for each v u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v);

Correctness: we must show that when u is removed from Q, it has already converged

Page 41: CS 2133: Algorithms

Correctness Of Dijkstra's Algorithm

Note that d[v] (s,v) v Let u be first vertex picked s.t. shorter path than d[u] d[u] > (s,u) Let y be first vertex V-S on actual shortest path from su d[y] = (s,y)

Because d[x] is set correctly for y's predecessor x S on the shortest path, and When we put x into S, we relaxed (x,y), giving d[y] the correct value

s

xy

up2

p2

Page 42: CS 2133: Algorithms

Correctness Of Dijkstra's Algorithm

Note that d[v] (s,v) v Let u be first vertex picked s.t. shorter path than d[u] d[u] > (s,u) Let y be first vertex V-S on actual shortest path from su d[y] = (s,y) d[u] > (s,u)

= (s,y) + (y,u) (Why?)= d[y] + (y,u) d[y] But if d[u] > d[y], wouldn't have chosen u. Contradiction.

s

xy

up2

p2

Page 43: CS 2133: Algorithms

The End