lecture 31: finding the best path

Post on 29-Jan-2016

22 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

CSC 213 – Large Scale Programming. Lecture 31: Finding the Best Path. Today’s Goals. Discuss what is meant by weighted graphs Where weights placed within Graph How to use Graph ’s weights to model problems How to solve problems once Graph is set up - PowerPoint PPT Presentation

TRANSCRIPT

LECTURE 31:FINDING THE BEST PATH

CSC 213 – Large Scale Programming

Today’s Goals

Discuss what is meant by weighted graphs Where weights placed within Graph How to use Graph’s weights to model

problems How to solve problems once Graph is set up

Learn about myth & legend of Edsgar Dijkstra Who was he? Why should we care? How is

it related? What was his largest contribution to graph

theory? How does Dijkstra’s algorithm find smallest

path?

Weighted Graphs

Edge’s weight is cost of using edge Distance, cost, travel time, &c. usable as

the weight Weights below are distance in miles

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Cheapest Path Problem

Find path with min. weight between 2 vertices Sum of edge weights is the path weight

Consider the cheapest path from PVD to HNL None of edges is cheapest in this exampleORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Cheapest Path Problem

Subpath on shortest path is shortest path also Otherwise we would use shorter subpath

Tree made by all shortest paths from vertex Consider all shortest paths from PVDORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Dijkstra’s Algorithm

Finds cheapest paths from single vertex Normally, computes cheapest path to all

vertices Stop once vertex computed for single

target vertex Makes several fundamental

assumptions Connected graph needed when targeting

all vertices Only works if edge weights must be

nonnegative

Dijkstra’s Algorithm

Grows cloud of vertices as it goes Cloud starts with source vetex Add vertex to cloud with each step

Tracks distances to each vertex not in cloud For each vertex, considers only cheapest

path Only uses 1 edge from cloud to vertex not

in cloud Each step uses vertex with smallest

distance Adds this vertex to cloud, if not done yet Checks if creates smaller path to any

vertices

Edge Relaxation

Consider e from u to z

When u added to cloud Check adjacent

vertices Assume z not in

cloud Found faster path!

Update via relaxation New minimum

selected:

d(z) = 75z

s

u

d(u) = 50 10

e

( )

( ) ( )min

weight

d z

d u e

Edge Relaxation

Consider e from u to z

When u added to cloud Check adjacent

vertices Assume z not in

cloud Found faster path!

Update via relaxation New minimum

selected:

( )

( ) ( )min

weight

d z

d u e

d(z) = 75z

s

u

d(u) = 50 10

ed(z) = 60

Edge Relaxation

Consider e from u to z

When u added to cloud Check adjacent

vertices Assume z not in

cloud Found faster path!

Update via relaxation New minimum

selected:

( )

( ) ( )min

weight

d z

d u e

d(z) = 75z

s

u

d(u) = 50 10

ed(z) = 60

Edge Relaxation

Consider e from u to z

When u added to cloud Check adjacent

vertices Assume z not in

cloud Found faster path!

Update via relaxation New minimum

selected:

( )

( ) ( )min

weight

d z

d u e

z

s

u

d(u) = 50 10

ed(z) = 60

Dijkstra Example

CB

A

E

D

F

0

428

48

7 1

2 5

2

3 9

Dijkstra Example

CB

A

E

D

F

0

328

5 11

48

7 1

2 5

2

3 9

Dijkstra Example

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Dijkstra Example

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

Dijkstra Example

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

Dijkstra Example

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

Why Dijkstra’s Algorithm Works

Ultimately, Dijkstra was smart Smarter than me, if that is possible

Why Dijkstra’s Algorithm Works

Ultimately, Dijkstra was smart Smarter than me, if that is possible

Why Dijkstra’s Algorithm Works

Ultimately, Dijkstra was smart Smarter than me, if that is possible

Example of a greedy algorithm Takes best choice at each point in time Vertices added in increasing distance Brings vertices closer at each step Stops when vertex cannot move closer

Why No Negative-Weight Edges?

Assume edge has negative weight Greedily chose vertex before finding edge Cloud will include only one endpoint Negative weight changes everything,

however Vertices not added in order

Negative weight cycles? Repeat cycle to optimize CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8

Why No Negative-Weight Edges?

Assume edge has negative weight Greedily chose vertex before finding edge Cloud will include only one endpoint Negative weight changes everything,

however Vertices not added in order

Negative weight cycles? Repeat cycle to optimize

C added when distance was 5,but cheapest distance is 1!

CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8

Not All Is Lost

Slower cheapest paths algorithms can work Bellman-Ford algorithm takes time O(nm) DAG-based algorithms takes time O(n + m) Merlin algorithm takes time O(n + m)

Not All Is Lost

Slower cheapest paths algorithms can work Bellman-Ford algorithm takes time O(nm) DAG-based algorithms takes time O(n + m) Merlin algorithm takes time O(n + m)

For Next Lecture

Weekly assignment available today Due at regular time next week

Programming assignment #2 due today

Midterm #2 in class in one week!

Reading on cheap connections for Monday How can we minimize cost of building

highways? What algorithms can we use to solve this?

top related