Graph Algorithms 2 CS 202 Fundamental Structures of































- Slides: 31
Graph Algorithms - 2 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 1
Shortest Path Algorithms n Input is weighted graph: q n Associated with edge (vi, vj), there is a cost ci, j to traverse the edge. The cost of a path v 1 v 2…v. N is: Weighted Path Length n Unweighted path length is simply the number of edges on the path, namely N-1. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 2
Single-Source Shortest Path Problem n Given as input a weighted graph, G = (V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other 2 vertex in G. v 1 v 2 4 1 2 v 3 v 6 2 v 4 8 5 10 3 4 1 v 5 6 v 7 Shortest (Weighted) Path from V 1 to V 6 is: v 1, v 4, v 7, v 6. Cost=6 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 3
Single-Source Shortest Path Problem n We will examine different algorithms q q 1) Unweighted shortest path algorithm 2) Weighted shortest path algorithm n CS 202, Spring 2003 Assuming no negative edges. Fundamental Structures of Computer Science II Bilkent University 4
Unweighted Shortest Paths v v 1 v 4 v 3 v 6 n n n 2 v 5 v 7 Assume all edges are unweighted. We are interested in all shortest paths to all nodes from a given node, s. Example: assume v 3 is the node from where we will find all the shortest paths. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 5
Unweighted Shortest Paths v v 1 v 4 v 3 v 6 n n n 2 v 5 v 7 Assume all edges are unweighted. We are interested in all shortest path to all nodes from a given node, s. Example: assume v 3 is the node from where we will find all the shortest paths. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 6
Algorithm Sketch n 1. Start with initial node s. q n 2. Find all nodes adjacent to s. For all these nodes: q n Mark the distance of s to s as 0. Mark their distances to s as distances of previous nodes + 1. 3. Repeat step 2 for all adjacent nodes. q Stop when you exhausted all the nodes (vertices). CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 7
Example We want to find out all the shortest paths to all nodes from node v 3 v 1 v 2 S v 4 v 3 v 5 0 v 6 CS 202, Spring 2003 v 7 Fundamental Structures of Computer Science II Bilkent University 8
Vertex Adjacent Vertices v 3 v 1, v 6 1 v 2 S v 4 v 3 v 5 0 v 6 v 7 1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 9
Vertex Adjacent Vertices v 1 v 2, v 4 2 v 1 v 2 1 S v 3 v 4 0 2 1 v 6 CS 202, Spring 2003 v 5 v 7 Fundamental Structures of Computer Science II Bilkent University 10
Vertex Adjacent Vertices v 2 v 4, v 5 2 v 1 v 2 1 S v 3 v 4 0 2 1 v 6 CS 202, Spring 2003 v 5 3 v 7 Fundamental Structures of Computer Science II Bilkent University 11
Vertex Adjacent Vertices v 4 v 6, v 7 2 v 1 v 2 1 S v 3 v 4 0 2 1 v 6 v 5 3 v 7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 12
Vertex Adjacent Vertices v 5 v 4, v 7 2 v 1 v 2 1 S v 3 v 4 0 2 1 v 6 v 5 3 v 7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 13
Vertex Adjacent Vertices v 6 none v 7 v 6 2 v 1 v 2 1 S v 3 v 4 0 2 1 v 6 v 5 3 v 7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 14
Algorithm – Initial Configuration vertex known Distance to S Previous node v 1 F ∞ 0 v 2 F ∞ 0 v 3 F ∞ 0 v 4 F ∞ 0 V 5 F ∞ 0 V 6 F ∞ 0 V 7 F ∞ 0 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 15
void Graph: : unweighted_shortest_paths(vertex s) { Queue q(NUM_VERTICES); Vertex v, w; } q. enqueue(s); s. dist = 0; while (!q. is. Empty()) { 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); } } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 16
Final Configuration vertex known Distance to S Previous node v 1 T 1 v 3 v 2 T 2 v 1 v 3 T 0 0 v 4 T 2 v 1 V 5 T 3 v 2 V 6 T 1 v 3 V 7 T 3 v 4 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 17
Weighted Shortest Paths n Dijkstra’s Algorithm q q n 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. q Then the adjacent nodes of v (which are denoted as w) are updated with new distance information. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 18
Example: Weighted Directed Graph G is shown! 2 v 1 4 1 2 v 3 v 6 10 3 2 v 4 8 5 v 2 4 1 v 5 6 v 7 We are interested in all shortest paths to all nodes from node v 1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 19
Initial Configuration vertex known Distance to S Previous node v 1 F 0 0 v 2 F ∞ 0 v 3 F ∞ 0 v 4 F ∞ 0 V 5 F ∞ 0 V 6 F ∞ 0 V 7 F ∞ 0 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 20
0 ∞ 2 v 1 ∞ 4 1 2 v 3 ∞ v 6 ∞ CS 202, Spring 2003 10 3 2 v 4 8 5 v 2 4 1 v 5 ∞ 6 v 7 ∞ Fundamental Structures of Computer Science II Bilkent University 21
0 2 2 v 1 ∞ 4 1 2 v 3 1 v 6 ∞ CS 202, Spring 2003 10 3 2 v 4 8 5 v 2 4 1 v 5 ∞ 6 v 7 ∞ Fundamental Structures of Computer Science II Bilkent University 22
0 2 2 v 1 3 4 1 2 v 3 1 v 6 9 CS 202, Spring 2003 10 3 2 v 4 8 5 v 2 4 1 v 5 3 6 v 7 5 Fundamental Structures of Computer Science II Bilkent University 23
0 2 2 v 1 3 4 1 2 v 3 1 v 6 9 CS 202, Spring 2003 10 3 2 v 4 8 5 v 2 4 1 v 5 3 6 v 7 5 Fundamental Structures of Computer Science II Bilkent University 24
0 2 2 v 1 3 4 1 2 v 3 1 v 6 9 CS 202, Spring 2003 10 3 2 v 4 8 5 v 2 4 1 v 5 3 6 v 7 5 Fundamental Structures of Computer Science II Bilkent University 25
0 2 2 v 1 3 4 1 2 v 3 1 8 CS 202, Spring 2003 v 6 9 10 3 2 v 4 8 5 v 2 4 1 v 5 3 6 v 7 5 Fundamental Structures of Computer Science II Bilkent University 26
0 2 2 v 1 3 4 1 2 v 3 1 8 CS 202, Spring 2003 6 2 v 4 4 1 v 6 10 3 8 5 v 2 v 5 3 6 v 7 5 Fundamental Structures of Computer Science II Bilkent University 27
0 2 2 v 1 3 4 1 2 v 3 1 v 2 4 1 v 6 2 v 4 8 5 10 3 6 v 5 3 6 v 7 5 Finished! CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 28
Final Configuration vertex known Distance to S Previous node v 1 T 0 0 v 2 T 2 v 1 v 3 T 3 v 4 T 1 v 1 V 5 T 3 v 4 V 6 T 6 v 7 V 7 T 5 v 4 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 29
Implementation of Algorithm struct Vertex { 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: : create. Table( vector<Vertex> &t) { read. Graph(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(); } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 30
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; } } CS 202, Spring 2003 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 31