1 / 8

CMSC 341

CMSC 341. Graphs 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

taylor
Download Presentation

CMSC 341

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 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)

  3. Dijkstra’s Algorithm • Vertex v, w; • Initialize all vertices as unknown with distance = INFINITY • start.dist = 0; • while there are unknown vertices • v = 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; • } • }

  4. Dijkstra Example 3 a b g 1 3 1 2 7 5 3 c f h d 1 6 4 4 2 e j i 1

  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.

  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|);

  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|) ?? V å O ( |E| ) = O ( D ( v )) i = i 1

  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).

More Related