© 2015 JW Ryder CSCI 203 Data Structures1
© 2015 JW Ryder CSCI 203 Data Structures2
© 2015 JW Ryder CSCI 203 Data Structures3
© 2015 JW Ryder CSCI 203 Data Structures4
© 2015 JW Ryder CSCI 203 Data Structures5
© 2015 JW Ryder CSCI 203 Data Structures6
© 2015 JW Ryder CSCI 203 Data Structures7
© 2015 JW Ryder CSCI 203 Data Structures8 bfs (), dfs () Ways to cover all connected vertices, a way to explore What does O(e) mean? Is O(e) better than O(n)? 0 < O(1) < O(e) <= O(n 2 ) Algorithm is dependent on the number of edges as far as its performance is concerned
© 2015 JW Ryder CSCI 203 Data Structures9 Connected Components void connected (void) { // determine connected components of graph int i; for (i = 0; i < n; i++) if (!visited [i]) { dfs (i); cout << endl; }// end then } // end connected
© 2015 JW Ryder CSCI 203 Data Structures10 Spanning Trees bfs () or dfs () implicitly partitions edges of G into 2 sets –T - Tree edges –N - Non-Tree edges T is the set of edges traversed during execution and N are the rest Can determine (save) edge list by recording edges as algorithm arrives at each T then represents the head of the traversed edge list
© 2015 JW Ryder CSCI 203 Data Structures11 Spanning Tree Definition Any tree that consists solely of edges in G and that includes all the vertices in G May use dfs () or bfs () to create a spanning tree When dfs () used, spanning tree known as dfs spanning tree When bfs () used, what is it called?
© 2015 JW Ryder CSCI 203 Data Structures12 Adding N edges Suppose we add a non-tree edge (v, w) into any spanning tree T Result ==> cycle created consisting of edge (v,w) and all edges on the path from w to v in T Show example with one of the drawn spanning trees
© 2015 JW Ryder CSCI 203 Data Structures13 Minimum Cost Spanning Trees (MST) The cost of a spanning tree of a weighted undirected graph is the sum of the costs (weights) of the edges in the spanning tree. A minimum cost spanning tree is a spanning tree of least cost
© 2015 JW Ryder CSCI 203 Data Structures14 3 Common MST Algorithms Kruskal Prim Sollin All three use a design method called the Greedy Method
© 2015 JW Ryder CSCI 203 Data Structures15 Greedy Method Construct an optimal solution in stages At each stage we make a decision that is the best decision (using some criterion) Cannot change decision later so the decision must result in a feasible solution Can be applied to wide variety of problems
© 2015 JW Ryder CSCI 203 Data Structures16 Selection Criteria Selection of item at each stage is based on either least cost or highest profit criterion Feasible solution is one which works within the constraints specified by problem
© 2015 JW Ryder CSCI 203 Data Structures17 MST - Least Cost Solution must –use only edges within the graph G –use exactly n - 1 edges –may not use edges that would produce a cycle
© 2015 JW Ryder CSCI 203 Data Structures18 Kruskal’s Algorithm Builds a MST by adding edges to T one at a time Selects edges in T in non- decreasing order of cost Edge added to T if it does not form a cycle with edges that are already in T G is connected, has n > 0 vertices, exactly n - 1 edges will be selected to include in T
© 2015 JW Ryder CSCI 203 Data Structures19 Kruskal Continued Draw and do example (only talking no algorithm) Initially, E is set of all edges in G
© 2015 JW Ryder CSCI 203 Data Structures20 Kruskal’s Algorithm T = { }; while (T has < n-1 edges) && (E not Empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) doesn’t create a cycle in T) add (v,w) to T; else discard (v,w); } // end while if (T contains < n-1 edges) printf (“No Spanning Tree\n”);
© 2015 JW Ryder CSCI 203 Data Structures21 Kruskal Particulars Must be able to determine edge with minimum cost edge and delete it –Min-heap; determine and delete next least cost edge in O(lg e); can sort in O(e lg e) To check that new edge (v,w) doesn’t create cycle in T and to add edge to T, use union-find operations. –View each connected component in T as a set containing the vertices in that component –Initially T is empty, each vertex of G is separate set –Before adding edge (v,w), use find operation to determine if v and w are in same set Yes? 2 vertices already connected No? add edge
© 2015 JW Ryder CSCI 203 Data Structures22 Shortest Path Graph represents highway system Edges have weights Length of a path is sum of weights of edges along path Starting vertex is source, ending vertex is destination One way streets are possible; digraph Weights are positive
© 2015 JW Ryder CSCI 203 Data Structures23 Single Source All Destinations Directed Graph, G = (V, E) Weighting function w(e), w(e) > 0 Source vertex is V 0
© 2015 JW Ryder CSCI 203 Data Structures24 Graph in Figure 1 Shortest path from V 0 to all other vertices V 0 to V 1 Greedy algorithm to generate shortest paths S is set of vertices whose shortest paths have been found For w not is S, distance[w] is length of shortest path starting from V 0 going through vertices only in S and ending at w Generate paths in non- decreasing order of lengths
© 2015 JW Ryder CSCI 203 Data Structures25 Observation 1 If the next shortest path is to vertex u, then the path from V 0 to u goes through only those vertices that are in S –Show all intermediate vertices on the shortest path from V 0 to u are already in S –Assume vertex w on path not in S –Path from V 0 to u must go through w and length from V 0 to w must be less than length from V 0 to u –Since paths generated in non- decreasing order we must have generated path to w before –Contradiction! –Ergo cannot be any intermediate vertex that is not in S
© 2015 JW Ryder CSCI 203 Data Structures26 Observation 2 Vertex u is chosen so that it has the minimum distance among all vertices not in S If several vertices with same distance the choice is not important
© 2015 JW Ryder CSCI 203 Data Structures27 Observation 3 Once u is selected and the shortest path has been generated from V 0 to u, u becomes a member of S Adding u to S can change the shortest paths starting at V 0, going through vertices only in S, and ending at vertex w that is not currently in S If distance changes, we have found a shorter path from V 0 to w passing through u –Intermediate vertices are in S, subpath from u to w can be chosen so that there are no intermediate vertices –The length of the shorter path is distance[u] + length( )
© 2015 JW Ryder CSCI 203 Data Structures28 Who made these Observations? Observations and Algorithm credited to Edsger Dijkstra
© 2015 JW Ryder CSCI 203 Data Structures29 Mechanics n vertices numbered from 0 to n-1 Set S maintained as an array with name found[ ] –found[i] = TRUE (1) if vertex i in S else FALSE (0) Graph represented by its cost adjacency matrix –cost [i][j] = weight of edge –If edge not in G set cost [i][j] to some large number Choice of number is arbitrary but can’t overflow sign Must be > any other number in matrix distance [u] + cost [u][w] can’t produce overflow
© 2015 JW Ryder CSCI 203 Data Structures30 Shortest Path Parameters Draw distance [ ], found [ ], cost [ ] [ ], n, v for Figure 1 Draw figures on board, all based on n V0V0 V1V1 V4V4 V5V5 V3V3 V2V Figure 1
© 2015 JW Ryder CSCI 203 Data Structures31 Basic Idea with Words Initialize set S to Empty Initialize distance[ ] to the distances for the source vertex from cost matrix Put v into S; Make sure distance to self is 0 For i = 0 to Number of Vertices in G - 2 do Choose next vertex u to add to S. Will be vertex from the set E-S with the shortest distance Add u to S For w = each vertex in G // 0.. n-1 if w is not already in S if distance from v to u then to w < current shortest to w Make shortest distance from v through u to w for distance [w] endif endfor
© 2015 JW Ryder CSCI 203 Data Structures32 choose ( ) int choose (int distance [ ], int n, short int found [ ] ) { // Find smallest distance not yet checked int i, min, minpos; min = INT_MAX; minpos = -1; for (i = 0; i < n; i++) if (distance [i] < min && !found [i]) { min = distance [i]; minpos = i; } return minpos; } // end choose ( )
© 2015 JW Ryder CSCI 203 Data Structures33 shortestpath ( ) void shortestpath (int v, int cost [ ][MAX_VERTICES], int distance [ ], int n, short int found [ ]) { // distance [i] is shortest path from vertex v to i, found [i] holds a 0 if shortest path for vertex i has not been found else 1, cost is the cost adjacency matrix int i, u, w; for (i = 0; i < n; i++) { // Initialize part found [i] = FALSE; distance [i] = cost [v][i]; } // end for found [v] = TRUE; distance [v] = 0; for (i = 0; i < n-2; i++) { u = choose (distance, n, found); // Pick next vertex to add to S found [u] = TRUE; // Add it to S for (w = 0; w < n; w++) { // Check all vertices adjacent to if (!found [w]) // u but not yet in S if (distance [u] + cost [u][w] < distance [w]) distance [w] = distance [u] + cost [u][w]; } // end for
© 2015 JW Ryder CSCI 203 Data Structures34 Shortest Path Analysis O(n 2 ) n vertices, adjacency matrix First for loop takes O(n) Second for loop executed n-2 times - O(n) –This loop needs O(n) - each execution choose () - O(n) Update distance [ ] - O(n) –Total time needed is O(n 2 ) Each edge in G examined at least once O(e) –Minimum possible with cost adjacency O(n 2 )