Beruflich Dokumente
Kultur Dokumente
Weighted Graphs
22c:21 Computer Science II Data Structures
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:
In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports
SFO
ORD LGA
PVD
Shortest Paths
HNL LAX DFW
MIA
2
Example:
Shortest path between Providence and Honolulu
Property 2:
There is a tree of shortest paths from a start vertex to all the other vertices
Applications
Internet packet routing Flight reservations Driving directions
Example:
Tree of shortest paths from Providence
SFO HNL
ORD LGA
ORD LGA
PVD
LAX
DFW
LAX
DFW
MIA
3
MIA
4
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 computes the distances of all the vertices from a given start vertex s Assumptions:
the graph is connected the edges are undirected the edge weights are nonnegative
Edge Relaxation
Consider an edge e = (u,z) such that
u is the vertex most recently added to the cloud z is not in the cloud s d(u) = 50 u e
We grow a cloud of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v 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 of the vertices adjacent to u
d(z) = 75
z
d(z) = 60
z
2010/11/17
Example
0 8 2 B 2 8 E 7 3 C 2 9 F 1 D 5 4 B 2 8 5 E 7 3 A 4 8 2 C 2 9 F 1 8 D 5 2 3 7 B 5 E 7 3 A 0 4 8 2 2 C 9 A 0
Example (cont.)
4 1 8 F 3 D 5 0 8 4 2 C 3 E 9 F 1 8 3 D 2 5
7
0 8 2 8 B 2 5 E 7 3 2 C 9 1 11 F 3 D 5 B 2 5 7 7 A 4 8 2 A
0 B
A 2 7 5 E 3 2 9 1 8 F
4 3
D 5
Dijkstras Algorithm
Graph operations A priority queue stores the vertices outside the cloud
Key: distance Element: vertex Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if (v == s) setDistance(v, 0) else setDistance(v, ) Q.insert(getDistance(v), v) while Q.isEmpty() u Q.removeMin() for all e G.incidentEdges(u)dge e z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r) Q.update(r, z)
9
Analysis
Method incidentEdges is called once for each vertex
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
Locator-based methods
insert(k, v) inserts a vertex with key into Q update(k, v) changes the key of an item
Dijkstras algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure
Recall that v deg(v) = 2m
The running time can also be expressed as O(m log n) since the graph is connected and n = O(m). 10
2
9
3 0 s
2 6 9
s
14
18
18 14
6
30
6
30 15 5 6 20 16 11
5
11
19 15 5
19
6 20 16
44
t distance label
44
2010/11/17
2 0 s
9 18 14 24
3 0 s
2 6 9
X 9 2
24
18 14
6
30
X 14 6
30
5
11
5
11
19 15 5 6
19
15 5 20
6 20 16
16
44
7 X 15
44
X 9 2
24
X 14 6
30
X 14 6
30
5
11
5
11
19 15 5 6
19
15 5 20
6 20 16
16
7 X 15
44
7
16
44
X 15
X 9 2 delmin
18 14 24
X 33 3
X 14 6
30
X 14 6
30
5
11
5
11
19 15 5 6
19
15 5 20
6 20 16
16
7
17
44
7 X 15
44
X 15
2010/11/17
X X 33 3
X X 33 3
X 14 6
30
X 14 6
30
44 X 5
11
19 15 5 6
44 X 5
11
19
15 5 20
6 20 16
16
7 X 15
44
7 X 15 delmin
44
delmin 32
X 9 2
24
X X 33 3
X 14 6
30
X 14 6
30
44 35 X X
11
19 15 5 6
44 35 X X
11
19
15 5 20
6 20 16
16
7 X 15
44
t 59 X
7 X 15
44
t 59 X
X X 33 3
X X 33 3
X 14 6
30
X 14 6
30
44 35 34 XX X
11
19 15 5 6
44 35 34 XX X
11
19
15 5 20
6 20
16
delmin
44
16
7 X 15
44
t 51 59 X X
7 X 15
t 51 59 X X
2010/11/17
X X 33 3
X X 33 3
X 14 6
30
X 14 6
30
44 35 34 XX X
11
45 X 4
19 15 5 6
44 35 34 XX X
11
45 X 4 delmin
6
19
15 5 20
16
20
16
7 X 15
44
t 50 51 59 X X X
7 X 15
44
t 50 51 59 X X X
X X 33 3
X X 33 3
X 14 6
30
X 14 6
30
44 35 34 XX X
11
45 X 4
19 15 5 6
44 35 34 XX X
11
45 X 4
19
15 5 20
6 20 16
16
7 X 15
44
t 50 51 59 X X X
7 X 15
44
t delmin 50 51 59 X X X
X X 33 3
X X 33 3
X 14 6
30
X 14 6
30
44 35 34 XX X
11
45 X 4
19 15 5 6
44 35 34 XX X
11
45 X 4
19
15 5 20
6 20 16
16
7 X 15
44
t 50 51 59 X X X
7 X 15
44
t 50 51 59 X X X
2010/11/17
Extension
Using the template method pattern, we can extend Dijkstras algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third label: parent edge in the shortest path tree In the edge relaxation step, we update the parent label
Algorithm DijkstraShortestPathsTree(G, s) for all v G.vertices() setParent(v, ) for all e G.incidentEdges(u) { relax edge e } z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r) setParent(z,e) Q.replaceKey(getLocator(z),r)
31
32
Bellman-Ford Algorithm
Works even with negativeweight edges Must assume directed edges (for otherwise we would have negative-weight cycles) Iteration i finds all shortest paths that use i edges. Running time: O(nm). Can be extended to detect a negative-weight cycle if it exists How?
Algorithm BellmanFord(G, s) for all v G.vertices() if v = s setDistance(v, 0) else setDistance(v, ) for i 1 to n-1 do for each e G.edges() { relax edge e } u G.origin(e) z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r)
34
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.
Bellman-Ford Example
Nodes are labeled with their d(v) values
8 -2 -2 7 3 9 5 -2 1 8 7 3 0 4 8 -2 -2 9 5 1 4 0 4
DAG-based Algorithm
Works even with negative-weight edges Uses topological order Doesnt use any fancy data structures Is much faster than Dijkstras algorithm Running time: O(n+m).
Algorithm DagDistances(G, s) for all v G.vertices() if v = s setDistance(v, 0) else setDistance(v, ) Perform a topological sort of the vertices for u 1 to n do // in topological order for each e G.outEdges(u) // relax edge e z G.opposite(u,e) r getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r)
36
8 -2 5 8 1 -2 6 7 3
0 1 9 9
4 -1
8 -2 4 5 5 -2 7 3 1
0 1 9 4 9
-2
-2
-1 5
35
2010/11/17
DAG Example
Nodes are labeled with their d(v) values
1 1
8 -2
3
0 7
2
4 1 9 5
5 4 3
8 -5 8 7 3
2
-5
3
6 1
8 -2
3
0 7 1
6 2
4 1 9 7
5 4
8 -2 4 5 -1
3
0 7 0 1 3
6 2
4 1 9 7 4
5 37 4
8 -5
-2
5 -5
-2
-1 5
Find the distance between every pair of vertices in a weighted directed graph G. We can make n calls to Dijkstras algorithm (if no negative edges), which takes O(nmlog n) time. Likewise, n calls to Bellman-Ford would take O(n2m) time. We can achieve O(n3) time using dynamic programming (similar to the Floyd-Warshall algorithm).
Algorithm AllPair(G) {assumes vertices 1,,n} for all vertex pairs (i,j) if i = j D0[i,i] 0 else if (i,j) is an edge in G D0[i,j] weight of edge (i,j) else D0[i,j] + for k 1 to n do for i 1 to n do for j 1 to n do Dk[i,j] min{Dk-1[i,j], Dk-1[i,k]+Dk-1[k,j]} return Dn
i