



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




(a) Akin to tree case: we pick a node, traverse to all of its children, then do their children ... (b) 1 bfs( graph g, vertex s ) 2 { // Initialization begins 3 foreach vertex u in set { g.vertices() - { s } } 4 { 5 color[u] = white; 6 distance[u] = infinity; 7 predecessor[u] = NULL; 8 } 9 color[s] = grey; 10 distance[s] = 0; 11 predecessor[s] = NULL; 12 q = { s }; // Initialization ends 13 while (! q.empty() ) // While we have not visited all vertices 14 { 15 u = q.head(); // Take the first one not yet visited 16 foreach ( v adjacent to u ) // Look at its neighbors 17 { 18 if ( color[v] == white ) // If they are not yet visited 19 { 20 color[v] = grey; // Change their color to grey 21 distance[v] = distance[u] + 1; // Update their distance from s 22 predecessor[v] = u; // Tell them their predecessor in BFS-tree 23 q.enqueue( v ); // Schedule traversal from them 24 } // if 25 } // foreach 26 /* We might want to do something with vertex u here; we have scheduled 27 traversal to its neighbors and are about to move on to them ... */ 28 q.dequeue(); // Dequeue the completed vertex 29 color[u] = black; // We’re done -- color it black 30 } // while 31 }
(c) Initialization requires |V |; main loop requires |V |; inner loop requires average of |E|/|V |, counting outer loop’s statements, requires total of about |V | + |E|; summing this with initialization section, we get 2|V | + |E| or O(|V | + |E|). (d) In process, a BFS-tree (or predecessor tree) is formed; sometimes useful in other algorithms. (e) Note: does not discover components not connected to given BFS root.
(a) Akin to tree case: we pick a node, traverse its first child, (and then traverse to its first child) – as far as we can go.
(b) 1 dfs( graph g ) /* Sometimes: dfs( graph g, vertex s ) / 2 { // Initialization begin 3 / Sometimes: for each vertex u in set { g.vertices() - { s } } / 4 for each vertex u in set { g.vertices() } 5 { 6 color[u] = white; 7 predecessor[u] = NULL; 8 } 9 time = 0; // Initialization end 10 / Sometimes just dfs-visit( s ) */ 11 for each vertex u in set { g.vertices() } // For every vertex, try to dfs 12 if ( color[u] == white ) 13 dfs-visit( u ); 14 } 15 dfs-visit( vertex v ) 16 { 17 color[u] = grey; // The vertex has been visited: color it 18 discovery[u] = ++time; // Timestamp the discovery 19 foreach ( v adjacent to u ) // Look at its neighbors 20 { 21 if ( color[v] == white ) // If they have not been visited 22 { 23 predecessor[v] = u; // Set their predecessor 24 dfs-visit( v ); // Visit them 25 } 26 } 27 color[u] = black; // Finished with all of u’s neighbors 28 finish[u] = ++time; // Set u’s finish time 29 }
(c) Running time: initialization requires about 2|V | operations, dfs()’s main loop executes once for every vertex giving us |V | operations; for each iteration, it calls dfs-visit; dfs-visit executes about an average of |E|/|V | operations; including the check in dfs()’s main loop and initialization, we come to 2|V | + |V | + |E| or O(|V | + |E|) operations. (d) In the process, a DFS-forest is created; sometimes useful in other algorithms.
(a) A strongly connected component of a graph is a subgraph in which all vertices are reachable from each other. (b) In the DFS of any such component, all component vertices of it must be in the same tree. (c) One vertex, by necessity, will have to serve as a DFS ancestor of all vertices; call this vertex p. By definition p will have the maximum finish time of any vertex in the component set. (d) So, upon performing a DFS, we know that we can find at least one such p – the vertex with the highest valued finish time. (e) But how to find all vertices capable of reaching it? (We know which are reachable from it – they are in our predecessor subtree.) (f) If we reverse the directed edges (if we form GT^ ), we can find just this set of edges by performing another DFS. (g) Once we find the vertices capable of reaching the current p vertex, we can eliminate them from consideration (they are their own strongly-connected component) and report them as a SCC.
(d) Running time: without investigating the set class, it’s difficult to see, however initialization requires O(|V |), sorting the edges requires O(|E| log |E|), there are O(|E|) operations on all sets each of which is known to require O(log |E|) time. Summing these, gets us (|V | + 2|E| log |E|) or O(|E| log |E|).
(a) Prim’s algorithm grows an MST by forming larger and larger minimum spanning subtrees. It initializes a minimum spanning subtree of the graph’s MST by arbitarily choosing a root vertex. For this subtree of the MST, it examines its adjacent vertices (on the other side of the cut) to find the light edge. When found, it adds the vertex “holding” the light edge and grows its vertex set by one. It repeats this step until a tree of size |V | − 1 is formed. (b) 1 prim( graph g, weight w(), vertex r ) 2 { 3 priority_queue q = { g.vertices() }; // Initialization begin 4 for each vertex v ( q.elements() ) 5 { 6 q.increase_key( v, infinity ); 7 key[v] = infinity; 8 } 9 q.decrease_key( r, 0 ); key[r] = 0; 10 predecessor[r] = NULL; // Initialization end 11 while (! q.empty() ) // While there are still vertices 12 { 13 vertex u = q.delete_min(); // Take the minimum 14 for each vertex v ( adjacent to u ) // We can reach its neighbors, so 15 if ( v is in q && w(u,v) < key[v] ) // If their weight is made 16 { // less by this new path, update, 17 predecessor[v] = u; // adjust their predecessor and 18 q.decrease_key( v, w(u,v) ); key[v] = w(u,v); // percolate them. 19 } 20 } 21 }
(c) Running time: initialization requires |V | + |V | log |V | + log |V | = O(|V | log |V |); the main loop requires |V | + |E| log |V | = O(|E| log |V |) for a total of O(|V | log |V | + |E| log |V |); if |E| >> |V |, |E| dominates |V | and we reach O(|E| log |V |). Faster bound of O(|V | log |V |) is known possible using Fibonacci heaps.
(a) Given as input a weighted graph, G = (V, E) and a distinguished (source/root) vertex, s, find the shortest weighted path from s to every other vertex in G.
(a) Recall the BFS algorithm – each “level” in the graph away from the source vertex receives one unit’s greater distance than the previous. (The distance[] array in the pseudo-code above stores this value.) (b) If all edges have unit cost, there can be no shorter path to any given vertex than that which BFS finds first (if there were, we would have found/marked it in a previous iteration). (c) Thus BFS itself provides a shortest path algorithm for non-edge weighted graphs in O(|V | + |E|).
(a) Similar to Prim’s algorithm: it builds a subgraph of the given graph starting from the given vertex, by forming a set of vertices connected by edges of least weight. (b) 1 edge-evaluate( vertex u, vertex v, weight w(), priority_queue q ) 2 { // Given two vertices and a weight function 3 // If v’s current dist. > dist. of u + w(u,v) 4 if ( distance[v] > distance[u] + w(u,v) ) 5 { 6 distance[v] = distance[u] + w(u,v); // Use the path w/edge (u,v) instead 7 if ( v is in q ) // Test can be done w/ array: O(1) 8 q.decrease_key( v, distance[v] ); // If v is in q, percolate up 9 predecessor[v] = u; // Change the predecessor 10 } 11 } 12 dijkstra( graph g, weight w(), vertex s ) 13 { 14 foreach vertex v ( g.vertices() ) // Initialization begin 15 { 16 distance[v] = infinity; 17 predecessor[v] = NULL; 18 } 19 distance[s] = 0; 20 set s = NULL; // Initialization end 21 priority_queue q = g.vertices(); // Keyed on distance[] metric 22 while (! q.empty() ) // While there are still vertices 23 { 24 u = q.delete_min(); // Grab the lowest 25 s = s union { u }; // Add it to our set 26 foreach vertex v ( adjacent to u ) // For all adjacent to new vertex 27 edge-evaluate( u, v, w, q ) // Reevaluate their distance from s 28 } 29 }
(c) Running time: initialization requires O(|V |); building the priority queue requires O(|V | log |V |) operations; the main loop executes O(|V |) times, the inner loop seeks each edge adjacent to the vertex set by the outer loop and therefore runs a total of O(|E|) times (or average of O(|E|/|V |) for each iteration); edge-evaluate() runs O(log |V |) time. Therefore adding these together, we arrive at a total of O(|V | + |V | log |V | + |V |(|E|/|V |(log |V |))) = O(|E| log |V |). Improvement to O(|E| + |V | log |V |) is possible using more complex data structures (Fibonacci heaps). (d) What about negative edge weights? What happens? (e) What about negative weight cycles?
(a) A network in graph-theoretic terms is quite different in model than the networks we use daily to exchange data. A (graph-theoretic) network is defined as a directed graph with two specially marked vertices – a “source” s and a “sink” t. A source should have in-degree zero; a sink should have outdegree zero. (b) A flow network adds a capacity function, similar to the weighting function of weighted graphs, that maps from edges to edge capacities and the restriction that at any vertex v that is not s or t, the total incoming flow must be equal to the total outgoing flow.