You are on page 1of 5

Weighted Graphs

a In weighted graph, each edge has


a an associated numerical
value, called the weight of the edge
Edge weights may represent, distances, costs, etc.
D Example:
Ina flight route graph, the weight of an edge represents the
distance in miles between the endpoint airports

849 PVD
SFO
SFO
1843
ORD 1 4 2

1743
1387 (LGA
A)
HNL2555
(AX) 1233 (DFW1120 109
MIA
MIA)
Shortest Paths
a Given a weighted graph and two vertices u and v, we want to
find a path of mimimum total weight between u and v.
Length of a path is the sum of the weights of its edges.
Example:
Shortest path between Providence and Honolulu
a Applications
Internet packet routing
Flight reservations
Driving directions 849 PVD
SFO)
1843

ORD
1743
1387(LGA)

HNL 2555 1233DFW 1120 1099


MIA
MIA

Shortest Path Properties


Property 1:
A subpath of a shortest path is itself a shortest path
Property 2:
is tree of shortest paths from a start vertex to all the other
There a
vertices

Example:
Tree of shortest paths from Providence

849 PVD)
SFO 1843 ORD
387LGA)
1743

CHNL 55LAX)233 DF 1120


1099
MIA
Dijkstra' s Algorithm
u The distance ofa vertex
v from
aWe grow a "cloud" of vertices,
a vertex s is the
length of a shortest path beginning with s and eventually
between s and v covering all the vertices
a We store with each
vertex
Dijkstra's algorithm label D[v]
representing the
va
computes the distances distance of v from s in the
of all the vertices from a
given start vertex s subgraph consisting of the cloud
and its adjacent vertices
Assumptions: a At each step
the graph is
the edges are
connected We add to the cloud the vertex
u Outside the
cloud with the
undirected smallest distance label, D[u]
the edge weights are We update the labels
nonnegative of
vertices adjacent tou the

Edge Relaxation
DV] = the distance of v froms
D i v ] = the shortest distance
of v from s found so far
o Consider an edge e =
(4, z) S 1 0 Da]= 75
such that
u is the vertex most recently'
added to the cloud
z is not in the cloud

a The relaxation of edge e


updates distance dz) as
follows:
1 0 D[z]= 6
Dizlmin{D[z, D[u] + weight(e

b
Dijkstra's Algorithm: Details
Alg. Dijkstra(V, E)
Input: A weighted directed graph G=(V, , V={1, 2,.., ni
Output: The distance from vertex 1 to every other vertex in G

1. X={1} Y-V-{1}; D[1]-0; parent of y


2. for y+2 to n
3. if(yis adjacent to 1) { DIi-lengthH1, ; P[i -1}
4. else D [Y-o;
5. for j - 2 to n
6. Let y eY s.t. D[) is minimum; /l y = argminye r{ D[y]1}
7. X=XU {Y}; /add vertex y tocloud X
8. Y-Y-{Y //delete vertex yfrom Y
9. for each edge (y, w) in E // edge relaxation
10. if
(we Yand D[+lengtHy, w<D[w])
11. D[w-D[A+lengt{Y, wi; p[W] -Y}

Analysis of Dijkstra's Algorithm


a Graph operations
We find all the incident edges once for each vertex
a Label operations
We set/get the distance and locator labels of vertex z O(deg(z) times
Setting/getting a label takes O(1) time
Priority queue operations
Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log m) time
The key of a vertex in the priority queue is modified at most deg(w)
times, where each key change takes O(log n) time
a Dijkstra's algorithm runs in O(1 + m) log n) time provided the
graph is represented by the adjacency list/map structure
Recall that 2, deg(v) = 2m

a The running time can also be expressed as O(m log n) since the
graph is connected (m> n-2).
10

10
Dijkstra's Shortest Path algorithm practice problem (with source = 1)

Node

ode
6
Node

NOGe

25

Node

Node

].dist
initialize 2345|62
Shortest Path from 1 Cost
V=1 20
1025 1 1
|V 6 0 20 30 35 40 10 25 2 12 20 20
V=22
0 20 30 35 40 10 23 3 12793 20+3+2 25
V 7 0 20 25 35 4010 4 12734 20+3+2+3=28
V 3 0 20 25 28 40 10 |5 1 273945 20+3+2+3+4 32
V=4
020 25 28 32 10 6 16 10 10
7 1927 20 3 23
TIlpath
(final value)

T[V).dist Clv][w]VJ.dist+C[v][w]
|T[W].dist
T[w].dist?_
V 6 W:3 = 10+20 30 True
W:4= 10+25 35 True
W:5 = 10+30= 40 True

V-2 W:3 30 20+15 35 False


W:7 25 20+3 23 True
V 7 W:3 30 23+2 25 True
V =3 W:4 35 25+3 28 True

|V=4 W:5=40 28+4=32 True

You might also like