shortest paths (review)

44
Shortest Paths (Review)

Upload: feng

Post on 05-Jan-2016

32 views

Category:

Documents


1 download

DESCRIPTION

Shortest Paths (Review). Single Source Shortest Path. Dijkstra’s Algorithm Label Path length to each vertex as  (or 0 for the start vertex) Loop while there is a vertex Remove up the vertex with minimum path length Check and if required update the path length of its adjacent neighbors - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Shortest Paths (Review)

Shortest Paths(Review)

Page 2: Shortest Paths (Review)

Update rule: Let ‘a’ be the vertex removed and ‘b’ be its adjacent vertex Let ‘e’ be the edge connecting a to b. if (a.pathLength + e.weight < b.pathLength)

b.pathLengtha.PathLength + e.weightb.parent a

Single Source Shortest Path• Dijkstra’s Algorithm

– Label Path length to each vertex as (or 0 for the start vertex)

– Loop while there is a vertex• Remove up the vertex with minimum path length

• Check and if required update the path length of its adjacent neighbors

– end loop

C D

AB

6

7

2

56 E6 1

8

C D

AB

6

7

2

56 E6 1

8

2

5

6

10

Page 3: Shortest Paths (Review)

Single Source Shortest Path• DAG based Algorithm

May have negative weight– Label Path length to each vertex as (or 0 for the start vertex)– Compute Topological Ordering

– loop for i 1 to n-1• Check and if required update the path length of adjacent neighbors of vi

– end loop

Path Length from ATo i=1 i=2 i=3

B

C

D

E

i=4

C D

AB

-2

8

9

2

56

E-5 3

Page 4: Shortest Paths (Review)

All-Pairs Shortest Paths

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

Uses only vertices numbered 1,…,k(compute weight of this edge)

otherwise

edge an is ),( if),(

if00, jijiji vvvvedgeweight

ji

D

1,

1,

1,, ,min k

jkkki

kji

kji DDDD

Page 5: Shortest Paths (Review)

All-Pairs Shortest PathsDynamic Programming approach

• Initialize a nn table with 0 or or edge length. • Check and Update the table bottom up in nested loops

for k, for i,

for j

1,

1,

1,, ,min k

jkkki

kji

kji dddd

ij

C D

AB

-2

8

9

2

56

E-5 3

d v1 v2 v3 v4 v5

v1

v2

v3

v4

v5

Page 6: Shortest Paths (Review)

Minimum Spanning Trees

JFK

BOS

MCO

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 7: Shortest Paths (Review)

Minimum Spanning TreeSpanning subgraph

– Subgraph of a graph G containing all the vertices of G

Spanning tree– Spanning subgraph that is

itself a (free) tree

Minimum spanning tree (MST)– Spanning tree of a weighted

graph with minimum total edge weight

• Applications– Communications networks

– Transportation networks

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 8: Shortest Paths (Review)

Cycle PropertyCycle Property:

– Let T be a minimum spanning tree of a weighted graph G

– Let e be an edge of G that is not in T

– Let C let be the cycle formed by e with T

– For every edge f of C, weight(f) weight(e)

Proof:– By contradiction– If weight(f) weight(e) we

can get a spanning tree of smaller weight by replacing e with f

84

2 36

7

7

9

8e

C

f

84

2 36

7

7

9

8

C

e

f

Replacing f with e yieldsa better spanning tree

Page 9: Shortest Paths (Review)

U V

Partition PropertyPartition Property:

– Consider a partition of the vertices of G into subsets U and V

– Let e be an edge of minimum weight across the partition

– There is a minimum spanning tree of G containing edge e

Proof:– Let T be an MST of G– If T does not contain e, consider the

cycle C formed by e with T and let f be an edge of C across the partition

– By the cycle property,weight(f)

weight(e) – Thus, weight(f) weight(e)– We obtain another MST by replacing

f with e

74

2 85

7

3

9

8 e

f

74

2 85

7

3

9

8 e

f

Replacing f with e yieldsanother MST

U V

Page 10: Shortest Paths (Review)

Minimum Spanning Tree• Prim’s Algorithm (Prim-Jarnik Algorithm)

– Label cost of each vertex as (or 0 for the start vertex)

– Loop while there is a vertex• Remove a vertex that will extend the tree with minimum

additional cost

• Check and if required update the path length of its adjacent neighbors (Update rule different from Dijkstra’s algorithm)

– end loop

C D

AB

6

7

2

6 E6 1

8

Update rule: Let ‘a’ be the vertex removed and ‘b’ be its adjacent vertex Let ‘e’ be the edge connecting a to b. if (e.weight < b.cost)

b.coste.weightb.parent a

2

Page 11: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

0

2

Page 12: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

7,A

2,A

2

Page 13: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

7,A

2,A

2

Page 14: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

6,B

2,A8,B

2

Page 15: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

6,B

2,A8,B

2

Page 16: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

1,D

2,A6,D

2

Page 17: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

1,D

2,A6,D

2

Page 18: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

1,D

2,A6,D

2

Page 19: Shortest Paths (Review)

MST: Prim-Jarnik’s Algorithm

C D

AB

6

7

2

6 E6 1

8

2,A0

1,D

2,A6,D

2

Page 20: Shortest Paths (Review)

Prim-Jarnik AlgorithmAlgorithm MST (G)

Q new priority queueLet s be any vertexfor all v G.vertices()

if v = s v.cost 0

else v.cost v. parent null

Q.enQueue(v.cost, v)while Q.isEmpty()

v Q .removeMin() v.pathKnown true

for all e G.incidentEdges(v) w opposite(v,e)if w.pathKnown

if weight(e) < w.cost w.cost weight(e) w. parent v

update key of w in Q

O(n)

O(n log n)

O((n+m) log n)

Page 21: Shortest Paths (Review)

Example

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

8

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5 4

7

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5

7

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5

7

Page 22: Shortest Paths (Review)

Example (contd.)

BD

C

A

F

E

74

28

5

7

3

9

8

03

2

5 4

7

BD

C

A

F

E

74

28

5

7

3

9

8

03

2

5 4

7

Page 23: Shortest Paths (Review)

Minimum Spanning Tree• Kruskal’s Algorithm

– Create a forest of n trees– Loop while (there is > 1 tree in the forest)

• Remove an edge with minimum weight• Accept the edge only if it connects 2 trees from the forest

in to one.

– end loop

C D

AB

6

7

2

26 E6 1

8

E D C B A

Page 24: Shortest Paths (Review)

MST: Kruskal’s Algorithm

C D

AB

6

7

2

26 E6 1

8

E D C B A

Page 25: Shortest Paths (Review)

MST: Kruskal’s Algorithm

C D

AB

6

7

2

26 E6 1

8

E D C B A

Page 26: Shortest Paths (Review)

MST: Kruskal’s Algorithm

C D

AB

6

7

2

26 E6 1

8

E D C B A

Page 27: Shortest Paths (Review)

MST: Kruskal’s Algorithm

C D

AB

6

7

2

26 E6 1

8

E D C B A

Page 28: Shortest Paths (Review)

Kruskal’s AlgorithmAlgorithm KruskalMST(G)

let Q be a priority queue.Insert all edges into Q using their

weights as the keyCreate a forest of n trees

where each vertex is a tree numberOfTrees n

while numberOfTrees > 1do edge e Q.removeMin()

Let u, v be the endpoints of eif Tree(v) Tree(u) then

Combine Tree(v) and Tree(u) using edge e

decrement numberOfTrees return T

O(m log m)

O(m log m)

Page 29: Shortest Paths (Review)

Kruskal Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 30: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 31: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 32: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 33: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 34: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 35: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 36: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 37: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 38: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 39: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 40: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 41: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 42: Shortest Paths (Review)

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 43: Shortest Paths (Review)

Minimum Spanning Tree• Baruvka’s Algorithm

– Create a forest of n trees– Loop while (there is > 1 tree in the forest)

• For each tree Ti in the forest

– Find the smallest edge e = (u,v), in the edge list with u in Ti and v in Tj Ti

– connects 2 trees from the forest in to one.

– end loop

C D

AB

6

7

2

56 E6 1

8

E D C B A

Page 44: Shortest Paths (Review)

Baruvka’s Algorithm• Like Kruskal’s Algorithm, Baruvka’s algorithm grows many

“clouds” at once.

• Each iteration of the while-loop halves the number of connected compontents in T.– The running time is O(m log n).

Algorithm BaruvkaMST(G)T V {just the vertices of G}

while T has fewer than n-1 edges dofor each connected component C in T do

Let edge e be the smallest-weight edge from C to another component in T.if e is not already in T then

Add edge e to Treturn T