13
Shortest Paths and Dijkstra’s Algorithm CS 105

Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

Embed Size (px)

Citation preview

Page 1: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

Shortest Paths andDijkstra’s Algorithm

CS 105

Page 2: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 2

Single-source shortest paths

Given a weighted graph G and a source vertex v in G, determine the shortest paths from v to all other vertices in G Path length: sum of all edges in path

Useful in road map applications (e.g., google maps or map quest) for example

Page 3: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 3

Dijkstra’s Algorithm Solves the single-source shortest paths problem Involves keeping a table of current shortest path

lengths from source vertex (initialize to infinity for all vertices except v, which has length 0)

Repeatedly select the vertex u with shortest path length, and update other lengths by considering the path that passes through that vertex

Stop when all vertices have been selected Could use a priority queue to facilitate selection of

shortest lengths Need to refine data structure so that the update of key-

values in priority queue is allowed Time complexity: O( (n + m) log n ) or O( n2 log

n ),O( n2 ) if computation of minimum is simplified

Page 4: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 4

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

0

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

Page 5: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 5

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

184

0

946

621

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

Page 6: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 6

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

184

0

946

621

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

JFK

Page 7: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 7

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

371

328

184

0

946

621

1575

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

PVD

Page 8: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 8

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

371

328

184

0

946

621

1575

3075

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704BOS

Page 9: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 9

3075

1575

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

371

328

184

0

946

621

1423

2467

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

ORD

Page 10: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 10

Dijkstra’s algorithm (cont)

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX

SFO

371

328

184

0

946

621

1423

3288

2467

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

MIA

Page 11: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 11

3288

Dijkstra’s algorithm (cont)

ORD

BOS

PVD

JFK

MIA

DFWLAX

SFO

371

328

184

0

946

621

1423

2658

2467

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

BWI

DFW

Page 12: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 12

Dijkstra’s algorithm (cont)

LAX

SFO

371

328

184

0

946

621

1423

2658

2467

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

ORD

BOS

PVD

JFK

BWI

MIA

DFW

SFO

Page 13: Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine

SSSPSlide 13

Dijkstra’s algorithm (cont)

LAX

SFO

371

328

184

0

946

621

1423

2658

2467

849

867

187144

1841258

1090

946

740621

1391

802

1121

2342

1235

1464

337

1846

2704

ORD

BOS

PVD

JFK

BWI

MIA

DFWLAX