Fundamental Structures of Computer Science II 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
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 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
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 v3 v4 2 v5 8 4 6 5 1 v6 v7 Shortest (Weighted) Path from V1 to V6 is: v1, v4, v7, v6. Cost=6 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Single-Source Shortest Path Problem We will examine different algorithms 1) Unweighted shortest path algorithm 2) Weighted shortest path algorithm Assuming no negative edges. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Unweighted Shortest Paths v1 v2 v3 v4 v5 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. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Unweighted Shortest Paths v1 v2 v3 v4 v5 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. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 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). CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Example We want to find out all the shortest paths to all nodes from node v3 v1 v2 S v3 v4 v5 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v3 v1, v6 1 v1 v2 S v3 v3 v4 v5 v6 v7 1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v1 v2, v4 2 v1 v1 v2 1 S v3 v3 v4 v5 2 1 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v2 v4, v5 2 v1 v1 v2 v2 1 S v3 v3 v4 v5 3 2 1 v6 v7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v4 v6, v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 3 2 1 v6 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v5 v4, v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 v5 3 2 1 v6 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Vertex Adjacent Vertices v6 none v7 2 v1 v1 v2 v2 1 S v3 v3 v4 v4 v5 v5 3 2 1 v6 v6 v7 v7 3 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Algorithm – Initial Configuration vertex known Distance to S Previous node v1 F ∞ v2 v3 v4 V5 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 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); } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Final Configuration vertex known Distance to S Previous node v1 T 1 v3 v2 2 v4 V5 3 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
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. CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Example: Weighted Directed Graph G is shown! 2 v1 v2 10 4 1 3 2 v3 v4 2 v5 8 4 6 5 1 v6 v7 We are interested in all shortest paths to all nodes from node v1 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Initial Configuration vertex known Distance to S Previous node v1 F v2 ∞ v3 v4 V5 V6 V7 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II ∞ 2 v1 v2 10 4 1 3 ∞ 2 2 v3 v4 v5 ∞ ∞ 8 4 6 5 1 v6 v7 ∞ ∞ CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 10 4 1 3 ∞ 2 2 v3 v4 v5 ∞ 1 8 4 6 5 1 v6 v7 ∞ ∞ CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 8 9 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v7 v7 8 6 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 2 2 v1 v1 v2 v2 10 4 1 3 3 2 2 v3 v3 v4 v4 v5 v5 3 1 8 4 6 5 1 v6 v6 v7 v7 6 5 Finished! CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II Final Configuration vertex known Distance to S Previous node v1 T v2 2 v3 3 v4 1 V5 V6 6 v7 V7 5 CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
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::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(); CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University
Fundamental Structures of Computer Science II 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; } CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University