Graph Applications Topological Sort Shortest Path Problems Spanning Trees Data Structures 1 Graph Applications
Application: Topological Sort Given a set of jobs, courses, etc. with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. J4 J8 J1 J2 J7 J9 J3 J5 Model the problem as a DAG - relationships are directed, acyclic since cycles will cause conflicting set of prereqs. Topological sort can be performed using a DFS traversal (reverse order is generated), or by employing a queue. Order Generated Using DFS: J8, J4, J9, J7, J2, J5, J3, J1 Order Generated Using Queue:J1, J2, J3, J4, J5, J7, J8, J9 Data Structures 2 Graph Applications
Topological Sort(Using a Queue) void t o p s o r t ( Graph G) { Queue<Integer > Q = new AQueue<Integer >(G. n ( ) ) ; i n t [ ] Count = new i n t [G. n ( ) ] ; i n t v ; for ( v =0; v<g. n ( ) ; v++) Count [ v ] = 0; / / I n i t i a l i z e for ( v =0; v<g. n ( ) ; v++) / / Process every edge for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) Count [w] + + ; / / Add to v2 s prereq count for ( v =0; v<g. n ( ) ; v++) / / I n i t i a l i z e Queue i f ( Count [ v ] == 0) / / V has no p r e r e q u i s i t e s Q. enqueue ( v ) ; while (Q. l ength ( ) > 0) { / / Process the v e r t i c e s v = Q. dequeue ( ). i n t V a l u e ( ) ; p r i n t o u t ( v ) ; / / P r e V i s i t f o r Vertex V for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) { Count [w] ; / / One less p r e r e q u i s i t e i f ( Count [w] == 0) / / This vertex i s now f r e e Q. enqueue (w ) ; Data Structures 3 Graph Applications
Topological Sort(Using Recursion) void t o p s o r t ( Graph G) { / / Topo s o r t : r e c u r s i v e for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e Mark array G. setmark ( i, UNVISITED ) ; for ( i n t i =0; i <G. n ( ) ; i ++) / / Process a l l v e r t i c e s i f (G. getmark ( i ) == UNVISITED ) tophelp (G, i ) ; / / C a l l helper f u n c t i o n void tophelp ( Graph G, i n t v ) { / / Topsort helper G. setmark ( v, VISITED ) ; for ( Edge w = G. f i r s t ( v ) ; G. isedge (w ) ; w = G. next (w) ) i f (G. getmark (G. v2 (w) ) == UNVISITED ) tophelp (G, G. v2 (w ) ) ; p r i n t o u t ( v ) ; / / P o s t V i s i t f o r Vertex v Data Structures 4 Graph Applications
Shortest Paths Problems 10 B 20 5 D A 2 11 3 C 15 E Numerous applications where weighted graphs are used for optimization; shortest path problems are an instance. Example: road maps (cities and distances) Problems are non-trivial since the shortest path can be via a long path of edges, rather than the straight edge from source to destination. Data Structures 5 Graph Applications
Single-Source Shortest Paths Given a vertex s in graph G, find the shortest path from s to every other vertex in G B 20 10 5 D A 2 11 3 C 15 E Closely related problem is to find the shortest (minimum cost) path between two nodes of a graph. This problem is no more complicated than the algorithm to find all of the shortest (reachable) paths from a single source. Application-Networking: Find least expensive routes to broadcast messages to other computers in the network. Unweighted graphs - can use BFS; for weighted graphs, this will Data Structures 6 Graph Applications not work.
Dijkstra s Shortest Paths Algorithm Goal: Given start vertex s, find the shortest path from s to all other vertices Visit vertices in some order, compute shortest paths for all vertices seen(visited) so far, then add shortest path to the next vertex x. Problem: Shortest path to a vertex already processed might go through x. Solution: Process vertices in order of distance from s. Data Structures 7 Graph Applications
Dijkstra s Algorithm: Example 10 B 20 5 D A 2 11 3 C 15 E A B C D E Initial 0 Process A 0 10 3 20 Process C 0 5 3 20 18 Process B 0 5 3 10 18 Process D 0 5 3 10 18 Process E 0 5 3 10 18 Data Structures 8 Graph Applications
Dijkstra s Algorithm: Implementation / / Compute s h o r t e s t path distances from s, s t o r e i n D s t a t i c void D i j k s t r a ( Graph G, i n t s, i n t [ ] D) { for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e D[ i ] = I n t e g e r.max VALUE; D[ s ] = 0; for ( i n t i =0; i <G. n ( ) ; i ++) { / / Process v e r t i c e s i n t v = minvertex (G, D ) ; / / Get next c l o s e s t v ertex G. setmark ( v, VISITED ) ; i f (D[ v ] == I n t e g e r.max VALUE) return ; for (w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next (w) ) i f (D[w] > (D[ v ] + G. weight ( v, w ) ) ) D[w] = D[ v ] + G. weight ( v, w ) ; s t a t i c i n t minvertex ( Graph G, i n t [ ] D) { i n t v = 0; / / I n i t i a l i z e v to any u n v i s i t e d vertex ; for ( i n t i =0; i <G. n ( ) ; i ++) i f (G. getmark ( i ) == UNVISITED ) { v = i ; break ; for ( i n t i =0; i <G. n ( ) ; i ++) / / Find s m a l l e s t value i f ( (G. getmark ( i ) == UNVISITED ) && (D[ i ] < D[ v ] ) ) v = i ; return v ; Data Structures 9 Graph Applications
Dijkstra s Algorithm: Analysis minvertex() costs V, and this is done ( V + E ) times (Dijkstra()), resulting in a complexity Θ( V 2 + E ) minvertex() can be improved using a min-heap, ordered by D values. Removing a minimum value, can be done in Θ(log V ) time (need to reheap after root element is removed). Modifications to D values, can delete and reinsert the new values - can be done in Θ(log V ), as well. Using a min-heap, the complexity becomes Θ( V + E )log E. Data Structures 10 Graph Applications
Minimum-Cost Spanning Trees A Minimum-Cost-Spanning Tree (MST) of Graph G is the graph containing the vertices of G and subset of its weighted edges such that the edges have minimum cost (sum of weights of all edges) vertices are connected. Application Soldering the shortest set of edges to connect a set of terminals Connecting cities for a telephone network to minimize cable length. Data Structures 11 Graph Applications
Minimum Spanning Tree: Prim s Algorithm B 5 D A 10 2 20 11 3 C 15 E Pick any vertex N to be the start vertex. Pick the least cost outgoing edge from N; if this edge is connected vertex M, add M and edge NM to the MST. Pick the least outgoing edge from the MST (N or M) and the edge vertex is not visited, add to MST, and so on. Data Structures 12 Graph Applications
Minimum Spanning Tree: Prim s Algorithm / Compute a minimal cost spanning t r e e / s t a t i c void Prim ( Graph G, i n t s, i n t [ ] D, i n t [ ] V) { for ( i n t i =0; i <G. n ( ) ; i ++) / / I n i t i a l i z e D[ i ] = I n t e g e r.max VALUE; D[ s ] = 0; for ( i n t i =0; i <G. n ( ) ; i ++) { / / Process the v e r t i c e s i n t v = minvertex (G, D ) ; G. setmark ( v, VISITED ) ; i f ( v!= s ) AddEdgetoMST (V [ v ], v ) ; i f (D[ v ] == I n t e g e r.max VALUE) return ; / / Unreachable for ( i n t w = G. f i r s t ( v ) ; w < G. n ( ) ; w = G. next ( v, w) ) i f (D[w] > G. weight ( v, w) ) { D[w] = G. weight ( v, w ) ; V [w] = v ; Data Structures 13 Graph Applications
Minimum Spanning Tree: Prim s Algorithm: Example Can use a priority queue to pick the minimum weight edge at eacn iteration. Data Structures 14 Graph Applications