1 dijkstra’s algorithm dr. ying lu [email protected] raik 283 data structures & algorithms

20
1 Dijkstra’s Algorithm Dr. Ying Lu [email protected] RAIK 283 Data Structures & Algorithms

Upload: rodger-blake

Post on 14-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

1

Dijkstra’s Algorithm

Dr. Ying [email protected]

RAIK 283Data Structures & Algorithms

Page 2: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

2

RAIK 283Data Structures & Algorithms

Giving credit where credit is due:» Most of slides for this lecture are based on slides

created by Dr. John Vergara at Ateneo De Manila University

» I have modified them and added new slides

Page 3: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

3

Single-Source Shortest-Paths Problem Problem: given a connected graph G with non-negative weights

on the edges and a source vertex s in G, determine the shortest paths from s to all other vertices in G.

Useful in many applications (e.g., road map applications)

1410

3

6 4

5

2

9

15

8

Page 4: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

4

Single-Source Shortest-Paths

For instance, the following edges form the shortest paths from node A

H B C

G E D

F

A

1410

3

6 4

5

2

9

15

8

Page 5: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

5

Single-Source Shortest-Paths

H B C

G E D

F

A

1410

3

6 4

5

2

9

15

8

• Hint: does this problem looks similar?• Can we modify Prim’s algorithm to solve this problem?

Page 6: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

6

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 s, 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

» Here, priority is defined differently from that of the Prim’s algorithm» Like Prim’s algorithm, it needs to refine data structure so that the update of

key-values in priority queue is allowed» Like Prim’s algorithm, time complexity is O( (n + m) log n )

Page 7: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

7

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 8: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

8

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 9: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

9

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 10: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

10

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 11: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

11

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 12: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

12

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 13: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

13

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 14: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

14

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 15: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

15

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 16: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

16

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

Page 17: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

17

In-Class Exercises

Page 18: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

18

In-Class Exercises

Design a linear-time, (i.e., ( n + m)) algorithm for solving the single-source shortest-paths problem for dags (directed acyclic graphs) represented by their adjacency lists.

Hint: topologically sort the dag’s vertices first.

Page 19: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

19

In-Class Exercise

Suppose a person is making a travel plan driving from city 1 to city n, n > 1, following a route that will go through cities 2 through n –1 in between. The person knows the mileages between adjacent cities, and knows how many miles a full tank of gasoline can travel. Based on this information, the problem is to minimize the number of stops for filling up the gas tank, assuming there is exactly one gas station in each of the cities. Design a greedy algorithm to solve this problem and analyze its time complexity.

Page 20: 1 Dijkstra’s Algorithm Dr. Ying Lu ylu@cse.unl.edu RAIK 283 Data Structures & Algorithms

fill up the gas tank in City 1for c = 2 to n –1 do// decide whether to stop in City c for gas while approaching(2.1) if there is still enough gas to go to City (c+1) then don’t stop in City c (2.2) else

fill up the gas tank in City c

It can be proved that this greedy algorithm minimizes the number of gas stops. The time complexity is O(n) because the loop in Step (2) runs O(n) iterations in which each iteration uses O(1) time (in Steps (2.1) and (2.2)).

Proofs Techniques for Greedy Algorithms