Loading...

PPT – Shortest Paths PowerPoint presentation | free to download - id: 685262-OWY0N

The Adobe Flash plugin is needed to view this content

Shortest Paths

Weighted Graphs

- In a weighted graph, each edge has an associated

numerical value, called the weight of the edge - Edge weights may represent, distances, costs,

etc. - Example
- A vertex represents an airport and stores the

three-letter airport code - An edge represents a flight route between two

airports and stores the mileage of the route

Shortest Paths

- Weight of the path P
- sum of the weights of all the edges on the path

P - Shortest path
- path with smallest weight
- The problem
- Given a weighted graph and two vertices u and v,

we want to find a path of minimum total weight

(shortest path) between u and v.

Shortest Path Properties

- Property 1
- A sub-path of a shortest path is itself a

shortest path - Property 2
- There is a tree of shortest paths from a start

vertex to all the other vertices - Example
- Tree of shortest paths from Providence

The Distance of a Shortest Path

Case 1 The graph may have negative edges but no

negative cycles. The shortest distance from s to

t can be computed.

d(s,t)6

Case 2 The graph contains negative weight

cycles, and a path from s to t includes an edge

on a negative weight cycle. The shortest path

distance is -?.

1

8

d(s,t)- ?

1

s

t

A

B

-3

Shortest Path Algorithms

- Dijkstras algorithm does NOT allow negative

edges. - Uses a greedy heuristic.
- undirected/directed graph
- Bellman-Fords and Floyds algorithms work

correctly for any graph and can detect negative

cycles. - Must be directed graph if negative edges are

included.

Shortest Path Algorithms

- Dijkstras algorithm does NOT allow negative

edges. - Uses a greedy heuristic.
- undirected/directed graph
- Bellman-Fords and Floyds algorithms work

correctly for any graph and can detect negative

cycles. - Must be directed graph if negative edges are

included. - We will study Dijkstras algorithm only. The

others are beyond the scope of this course

Dijkstras Algorithm

- The distance of a vertex v from a vertex s is the

length of a shortest path between s and v - Dijkstras algorithm (pronounced dyke-stra)

computes the distances of all the vertices from a

given start vertex s - Assumptions
- the graph is connected
- the edge weights are nonnegative

Dijkstras Algorithm

- We grow a cloud of vertices, beginning with s

and eventually covering all the vertices - We store with each vertex u a label d(u)

representing the distance of u from s in the

subgraph consisting of the cloud and its adjacent

vertices - At each step
- We add to the cloud the vertex u outside the

cloud with the smallest distance label, d(u) - We update the labels d(.) of the vertices

adjacent to u

s

s

0

0

d(u)

A

A

4

4

8

8

2

2

2

2

4

1

3

8

7

8

7

1

C

B

D

C

B

D

9

5

3

11

3

9

?

?

2

5

E

F

2

5

E

F

Dijkstras Algorithm - Edge Relaxation

d(u) 50

- Consider an edge e (u,z) such that
- u is the vertex most recently added to the cloud
- z is not in the cloud
- The relaxation of edge e updates distance d(z) as

follows - d(z) mind(z),d(u) weight(e)

d(z) 75

10

e

u

z

s

d(u) 50

d(z) 60

10

e

u

z

s

Example

Example

Dijkstras Algorithm

- A priority queue stores the vertices outside the

cloud (Key distance, Element vertex)

Algorithm ShortestPath(G, v) Input A weighted

graph G with nonnegative edge weights, and a

start vertex v of G Output a label Du for

each u of G , so that Du is the length of a

shortest path from v to u in G Initialize

Dv 0 , and Du ? for every vertex u ?

v Let a priority queue Q contain all the

vertices of G using the D label as a key while Q

is not empty do pull a new vertex u into the

cloud u Q.removeMin() for each vertex z

adjacent to u such that z is in Q do perform

the relaxation on edge (u,z) if Du

w((u,z)) lt Dz then Dz Du w((u,z))

change to Dz the key of vertex z in Q

return the label Du of each vertex u

Analysis of Dijkstras Algorithm

- Inserting all the vertices in Q with their

initial key value can be done in O(n log n) - At each iteration of the while loop,
- we spend O(log n) time to remove vertex u from Q

and - O(degree (v) log n) time to perform the

relaxation procedure on each edge //note that it

takes log n to update Dz in the heap - The overall running time for the entire while

loop is O(m log n) - remember Sv degree(v) 2m
- Dijkstras algorithm runs in O((n m) log n)

time provided the graph is represented by the

adjacency list structure - // the use of adjacency list allows us to step

through the vertices adjacent to u during the

relaxation step in time proportional to their

number

Why It Doesnt Work for Negative-Weight Edges

- Dijkstras algorithm is based on the idea that it

adds vertices by increasing distance. - If a node with a negative incident edge were to

be added late to the cloud, it could mess up

distances for vertices already in the cloud.

0

A

4

8

6

4

5

7

7

1

C

B

D

0

-8

5

9

2

5

E

F

Cs true distance is 1, but it is already in the

cloud with d(C)5!