This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 4 Solutions 2 4.1. The shortest path tree is shown in Figure 1. Iteration Node 1 2 3 4 5 6 7 A B 1 1 1 1 1 1 1 C 3 3 3 3 3 3 D 4 4 4 4 4 E 4 4 4 4 4 4 4 F 8 7 7 7 7 6 6 G 7 5 5 5 5 5 H 8 8 6 6 Figure 1: Shortestpath tree for 4.1. 4.2. The shortest path tree is shown in Figure 2. Iteration Node 1 2 3 4 5 6 S A 7 7 7 7 7 7 B 11 11 11 11 11 C 6 5 5 5 5 5 D 8 7 7 7 7 E 6 6 6 6 6 6 F 5 4 4 4 4 4 G 9 8 8 8 H 9 7 7 7 7 I 8 7 7 4.3. Suppose the input graph G is given as an adjacency matrix. Notice that G contains a square if and only if there are two vertices u and v that share more than one neighbor. For any u , v we can check this in time O (  V  ) by comparing the row of u and the row of v in the adjacency matrix of G . Because we need to repeat this process O (  V  2 ) to iterate over all u and v , this algorithm has running time O (  V  3 ). We can do better by noticing that, when comparing the rows of the adjacency matrix a u and a v , we are actually checking if a u a v is greater than 1, i.e. if [ A ( G ) 2 ] uv > 1. It suffices then to 3 Figure 2: Shortestpath tree for 4.2. compute A ( G ), which we can do in time O (  V  2 . 71 using our matrix multiplication algorithm and check all nondiagonal entries to see if we find one larger than 1. 4.4. The graph in Figure 3 is a counterexample: vertices are labelled with their level in the DFS tree, back edges are dashed. The shortest cycle consists of vertices 1 4 5, but the cycle found by the algorithm is 1 2 3 4. In general, the strategy will fail if the shortest cycle contains more than one back edge. 2 1 3 4 5 Figure 3: Counterexample for 4.4. 4.5 We perform a BFS on the graph starting from u , and create a variable num paths ( x ) for the number of paths from u to x , for all vertices x . If x 1 ,x 2 ,...x k are vertices at depth l in the BFS tree and x is a vertex at depth l + 1 such that ( x 1 ,x ) ,..., ( x k ,x ) E then we want to set num paths ( x ) = num paths ( x 1 )+ ... + num paths ( x k ). The easiest way to do this is to start with num p aths ( x ) = 0 for all vertices x negationslash = u and num paths ( u ) = 1. We then update num paths ( y ) = num paths ( y ) + num paths ( x ), for each edge ( x,y ) that goes down one level in the tree. Since, we only modify BFS to do one extra operation per edge, this takes linear time. The pseudocode is as follows function count paths ( G,u,v ) for all x V : dist ( x ) = num paths ( x ) = 0 dist ( u ) = num paths ( u ) = 1 Q = [ u ] while Q is not empty: x = eject( Q ) for all edges ( x,y ) E if dist( y ) = dist( x ) + 1: 4 num paths( y ) = num paths( y ) + num paths( x ) if dist( y ) = : inject( Q , y ) dist( y ) = dist( x ) + 1 num paths( y ) = num paths( x ) 4.6. This is true as long as every node u V is reachable from s , the node from which Dijkstras algorithm...
View Full
Document
 Spring '10
 CHOW

Click to edit the document details