CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as...
-
Upload
osborn-jennings -
Category
Documents
-
view
217 -
download
0
Transcript of CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as...
![Page 1: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/1.jpg)
CMSC 341
Graphs 2
![Page 2: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/2.jpg)
2
Weighted Shortest Path Problem
Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.
Use Dijkstra’s algorithm
– keep tentative distance for each vertex giving shortest path length using vertices visited so far
– keep vertex before this vertex (to allow printing of path)
– at each step choose the vertex with smallest distance among the unvisited vertices (greedy algorithm)
![Page 3: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/3.jpg)
3
Dijkstra’s Algorithm
Vertex v, w;
Initialize all vertices as unknown with distance = INFINITYstart.dist = 0;
while there are unknown verticesv = smallest unknown distance vertex;v.known = TRUE;for each w adjacent to v
if (!w.known)if (v.dist + cvw < w.dist) {
decrease (w.dist to v.dist + cvw);w.path = v;
}}
![Page 4: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/4.jpg)
4
Dijkstra Example
a b
cd
g
e j i
f h
1
3
4
31
1
2 7
3
4
1
2
5
6
![Page 5: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/5.jpg)
5
Traversal Performance
What is the performance of DF and BF traversal?
Each vertex appears in the stack or queue exactly once. Therefore, the traversals are at least O(|V|). However, at each vertex, we must find the adjacent vertices. Therefore, df- and bf-traversal performance depends on the performance of the getAdjacent operation.
![Page 6: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/6.jpg)
6
getAdjacent
Method 1: Look at every vertex (except u), asking “are you adjacent to u?”List L = new List(<class of vertex>);
for (each vertex v, except u)
if (isAdjacentTo(u,v))
L.doInsert(v);
Assuming O(1) performance on isAdjacentTo, getAdjacent has O(|V|) performance and traversal performance is O(|V2|);
![Page 7: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/7.jpg)
7
getAdjacent (cont)
Method 2: Look only at the edges which impinge on u. Therefore, at each vertex, the number of vertices to be looked at is D(u), the degree of the vertex (use outdegree for directed graph).
This approach is O(D(u)). The traversal performance is
since getAdjacent is done O(|V|) times.However, in a disconnected graph, we must still look at every
vertex, so the performance is O(max(|V|, |E|)) = O(|V| + |E|).
But, what is O(|E|) ??
))((1
vDOV
ii
O ( |E| )
![Page 8: CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.](https://reader035.fdocuments.us/reader035/viewer/2022071709/56649cea5503460f949b5d39/html5/thumbnails/8.jpg)
8
Number of Edges
Theorem: The number of edges in an undirected graph G=(V,E) is O(|V|2)
Proof: Suppose G is fully connected. Let p =|V|. We have the following situation:
vertex connected to
1 2,3,4,5,…, p
2 1,3,4,5,…, p
…
p 1,2,3,4,…,p-1
– There are p(p-1)/2 = O(|V|2) edges.
So O(|E|) = O(|V|2).