第07章加权图WeightedGraphs

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

8
8 B
7
0 A
2 2
C
53 9 2E
4
1
3 D
8
7 B
7
0 A
2 2
C
11 F5
53 9 2E
Weighted Graphs
4
1
3 D
8 F5
8
Example (cont.)
8
7 B
7
5 2E
0 A
2 2
C
39
4
1
3 D
8 F5
8
7 B
7
5 2E
0 A
2 2
C
39
4
1
3 D
8 F5
Weighted Graphs

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)
9
Dijkstra’s Algorithm
A priority queue stores the vertices outside the cloud
Key: distance
Element: vertex
Locator-based methods
insert(k,e) returns a locator
Priority queue operations
Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time
Edge Relaxation
Consider an edge e = (u,z)
such that
u is the vertex most recently
added to the cloud
s
z is not in the cloud
The relaxation of edge e updates distance d(z) as follows:
Recall that Sv deg(v) = 2m
The running time can also be expressed as O(m log n) since the graph is connected
Weighted Graphs
11
Extension
Using the template method pattern, we can extend Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all other vertices
Weighted Graphs
10源自文库
Analysis
Graph operations
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
replaceKey(l,k) changes the key of an item
We store two labels with each vertex:
Distance (d(v) label)
locator in priority queue
Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if v = s setDistance(v, 0) else setDistance(v, ) l Q.insert(getDistance(v), v) setLocator(v,l) while Q.isEmpty() u Q.removeMin() 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) Q.replaceKey(getLocator(z),r)
Weighted Graphs
12
Why Dijkstra’s Algorithm Works
d(z) min{d(z),d(u) + weight(e)}
s
d(u) = 50
u
e
d(z) = 75
z
d(u) = 50
u
e
d(z) = 60
z
Weighted Graphs
7
Example
8
8 B
7
0 A
2 2
C
4
1
4 D
3 2E
9 F5
8
8 B
7
5 2E
0 A
2 2
C
39
4
1
3 D
8 F5
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)
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
Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure
相关文档
最新文档