1 / 31

Graph Algorithms - 2

Graph Algorithms - 2. CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department. Shortest Path Algorithms. Input is weighted graph: Associated with edge (v i , v j ), there is a cost c i,j to traverse the edge.

dunnam
Télécharger la présentation

Graph Algorithms - 2

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. Graph Algorithms - 2 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department Fundamental Structures of Computer Science II Bilkent University

  2. Shortest Path Algorithms • Input is weighted graph: • Associated with edge (vi, vj), there is a cost ci,j to traverse the edge. • The cost of a path v1v2…vN is: • Unweighted path length is simply the number of edges on the path, namely N-1. Weighted Path Length Fundamental Structures of Computer Science II Bilkent University

  3. 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. 2 v1 v2 10 4 1 3 2 2 v4 v5 v3 8 4 6 5 1 v6 v7 Shortest (Weighted) Path from V1 to V6 is: v1, v4, v7, v6. Cost=6 Fundamental Structures of Computer Science II Bilkent University

  4. Single-Source Shortest Path Problem • We will examine different algorithms • 1) Unweighted shortest path algorithm • 2) Weighted shortest path algorithm • Assuming no negative edges. Fundamental Structures of Computer Science II Bilkent University

  5. Unweighted Shortest Paths v1 v2 v4 v5 v3 v6 v7 • Assume all edges are unweighted. • We are interested in all shortest paths to all nodes from a given node, s. • Example: assume v3 is the node from where we will find all the shortest paths. Fundamental Structures of Computer Science II Bilkent University

  6. Unweighted Shortest Paths v1 v2 v4 v5 v3 v6 v7 • Assume all edges are unweighted. • We are interested in all shortest path to all nodes from a given node, s. • Example: assume v3 is the node from where we will find all the shortest paths. Fundamental Structures of Computer Science II Bilkent University

  7. Algorithm Sketch • 1. Start with initial node s. • Mark the distance of s to s as 0. • 2. Find all nodes adjacent to s. For all these nodes: • Mark their distances to s as distances of previous nodes + 1. • 3. Repeat step 2 for all adjacent nodes. • Stop when you exhausted all the nodes (vertices). Fundamental Structures of Computer Science II Bilkent University

  8. Example We want to find out all the shortest paths to all nodes from node v3 v1 v2 S v4 v5 v3 0 v6 v7 Fundamental Structures of Computer Science II Bilkent University

  9. 1 v1 v2 S v4 v5 v3 v3 0 v6 v7 1 Fundamental Structures of Computer Science II Bilkent University

  10. 2 v1 v1 v2 1 S v3 v4 v5 v3 2 0 1 v6 v7 Fundamental Structures of Computer Science II Bilkent University

  11. 2 v1 v1 v2 v2 1 S v3 v4 v5 v3 3 2 0 1 v6 v7 Fundamental Structures of Computer Science II Bilkent University

  12. 2 v1 v1 v2 v2 1 S v3 v4 v5 v3 v4 3 2 0 1 v6 v7 3 Fundamental Structures of Computer Science II Bilkent University

  13. 2 v1 v1 v2 v2 1 S v3 v4 v5 v3 v4 v5 3 2 0 1 v6 v7 3 Fundamental Structures of Computer Science II Bilkent University

  14. 2 v1 v1 v2 v2 1 S v3 v4 v5 v3 v4 v5 3 2 0 1 v6 v6 v7 v7 3 Fundamental Structures of Computer Science II Bilkent University

  15. Algorithm – Initial Configuration Fundamental Structures of Computer Science II Bilkent University

  16. void Graph::unweighted_shortest_paths(vertex s) { Queue q(NUM_VERTICES); Vertex v,w; q.enqueue(s); s.dist = 0; while (!q.isEmpty()) { v= q.dequeue(); v.known = true; // not needed anymore for each w adjascent to v if (w.dist == INFINITY) { w.dist = v.dist + 1; w.path = v; q.enqueue(w); } } } Fundamental Structures of Computer Science II Bilkent University

  17. Final Configuration Fundamental Structures of Computer Science II Bilkent University

  18. Weighted Shortest Paths • Dijkstra’s Algorithm • Example of a greedy algorithm • Do the best thing in each step. • At each state, the algorithm chooses a vertex v, which has the smallest distance to s (dv) among all the unknown vertices. • Then the adjacent nodes of v (which are denoted as w) are updated with new distance information. Fundamental Structures of Computer Science II Bilkent University

  19. Example: Weighted Directed Graph G is shown! 2 v1 v2 10 4 1 3 2 2 v4 v5 v3 8 4 6 5 1 v6 v7 We are interested in all shortest paths to all nodes from node v1 Fundamental Structures of Computer Science II Bilkent University

  20. Initial Configuration Fundamental Structures of Computer Science II Bilkent University

  21. 0 ∞ 2 v1 v2 10 4 1 3 ∞ 2 2 v4 v5 v3 ∞ ∞ 8 4 6 5 1 v6 v7 ∞ ∞ Fundamental Structures of Computer Science II Bilkent University

  22. 0 2 2 v1 v1 v2 10 4 1 3 ∞ 2 2 v4 v5 v3 ∞ 1 8 4 6 5 1 v6 v7 ∞ ∞ Fundamental Structures of Computer Science II Bilkent University

  23. 0 2 2 v1 v1 v2 10 4 1 3 3 2 2 v4 v5 v3 v4 3 1 8 4 6 5 1 v6 v7 9 5 Fundamental Structures of Computer Science II Bilkent University

  24. 0 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v4 v5 v3 v4 3 1 8 4 6 5 1 v6 v7 9 5 Fundamental Structures of Computer Science II Bilkent University

  25. 0 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v4 v5 v3 v4 v5 3 1 8 4 6 5 1 v6 v7 9 5 Fundamental Structures of Computer Science II Bilkent University

  26. 0 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v4 v5 v3 v3 v4 v5 3 1 8 4 6 5 1 v6 v7 8 9 5 Fundamental Structures of Computer Science II Bilkent University

  27. 0 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v4 v5 v3 v3 v4 v5 3 1 8 4 6 5 1 v7 v6 v7 8 6 5 Fundamental Structures of Computer Science II Bilkent University

  28. 0 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v4 v5 v3 v3 v4 v5 3 1 8 4 6 5 1 v6 v7 v6 v7 6 5 Finished! Fundamental Structures of Computer Science II Bilkent University

  29. Final Configuration Fundamental Structures of Computer Science II Bilkent University

  30. Implementation of Algorithm structVertex { List adj; // adjacency list boolean known; // if we are finished processing the node int dist; // distance to source. Vertex path; // the previous node towards the source } void Graph::createTable( vector<Vertex> &t) { readGraph(t); for (int i=0; i<t.size(); i++) { t[i].known = FALSE; t[i].dist = INFINITY; t[i].path = NOT_A_VERTEX; //NULL } NUM_VERTICES = t.size(); } Fundamental Structures of Computer Science II Bilkent University

  31. void Graph::dijkstra( vector<Vertex> &s) { Vertex v, w; s.dist = 0; for ( ; ; ) { v = an unknown vertex whose distance to s is minimum; if (v == NOT_A_VERTEX) break; // we are finished v.known = TRUE; for each w adjacent to v if (w.known == FALSE) if (v.dist + cost_v_w < w.dist) { // update w w.dist = v.dist + cost_v_w; w.path = v; } } } Fundamental Structures of Computer Science II Bilkent University

More Related