Docsity

Dijkstra Algorithm on single source shortest path problem, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Dijkstra Algorithm on single source shortest path problem and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Dijkstra’s Algorithm

Dr. Ying Lu

[email protected]

RAIK 283

Data Structures & Algorithms

RAIK 283

Data 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

Single-Source Shortest-Paths

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

H B C

G E D

F

A

Single-Source Shortest-Paths

H B C

G E D

F

A

Hint: does this problem looks similar?

Can we modify Prim’s algorithm to solve this problem?

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

SFO

0

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

SFO

184

0

946

621

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

SFO

371

328

184

0

946

621

1575

PVD

Dijkstra’s algorithm

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

SFO

371

328

184

0

946

621

1575

3075

BOS

Dijkstra’s algorithm (cont)

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

SFO

371

328

184

0

946

621

1423

3288

2467

MIA

3288

Dijkstra’s algorithm (cont)

ORD

BOS

PVD

JFK

MIA

DFW

LAX

SFO

371

328

184

0

946

621

1423

2658

2467

BWI

DFW

Dijkstra’s algorithm (cont)

LAX

SFO

371

328

184

0

946

621

1423

2658

2467

ORD

BOS

PVD

JFK

BWI

MIA

DFW

LAX

In-Class Exercises

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.

fill up the gas tank in City 1

for 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