310 likes | 325 Views
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.
E N D
Graph Algorithms - 2 CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department 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 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 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
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
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
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
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
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
1 v1 v2 S v4 v5 v3 v3 0 v6 v7 1 Fundamental Structures of Computer Science II Bilkent University
2 v1 v1 v2 1 S v3 v4 v5 v3 2 0 1 v6 v7 Fundamental Structures of Computer Science II Bilkent University
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
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
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
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
Algorithm – Initial Configuration Fundamental Structures of Computer Science II Bilkent University
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
Final Configuration 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. Fundamental Structures of Computer Science II Bilkent University
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
Initial Configuration Fundamental Structures of Computer Science II Bilkent University
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
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
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
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
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
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
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
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
Final Configuration Fundamental Structures of Computer Science II Bilkent University
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
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