This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSE 60111: Complexity and Algorithms Spring Semester 2007 Assignment 6 Note: Unless otherwise specified explicitly, the graphs use the adjacency-list representation. 1. Exercise 22.1-5, page 530. The square of a directed graph G = ( V,E ) is the graph G 2 = ( V,E 2 ) such that ( u,w ) E 2 if and only if for some v V , both ( u,v ) E and ( v,w ) E . That is, G 2 contains an edge between u and w whenever G contains a path with exactly two edges between u and w . Describe efficient algorithms for computing G 2 from G for both the adjacency-list and adjacency-matrix representations of G . Analyze the running times of your algorithms (15 points) Solution: Ideas/Observations: If our graph is represented by an adjacency list, then we will consider each vertex v one by one. We will look into v s adjacency list and, for each connecting vertex u , look into u s adjacency list and add an edge ( v,w ) , where w Adj ( u ) . If our graph is represented by an adjacency matrix, then we will proceed in a similar manner. We will consider each vertex v and check if there exists edges ( v,u ) and ( u,w ) . If so, we add an edge ( v,w ) to G 2 . Pseudocode: The pseudocode for finding the square of G using an adjacency-list representation is shown in Algorithm 1 and that using an adjacency-matrix representation can be found in Algorithm 2. Algorithm 1 SQUARE LIST( G ) 1: GG ( v ) , for all v V 2: for each vertex v V do 3: for each vertex u Adj ( v ) do 4: for each vertex w Adj ( u ) do 5: add w to GG ( v ) 6: end for 7: end for 8: end for 9: return GG Time Complexity: For the adjacency-list case, we look through all the outgoing edges from the current vertex under consideration. This step is performed | V | times. So, in the worst case, our algorithm takes O ( | V | | E | ) time. For the adjacency-matrix case, each vertex will require going though the entire matrix once, which will take O ( V 2 ) time. Since we have to perform this operation for all vertices, the total running time of our algorithm is O ( V 3 ) . Algorithm 2 SQUARE MATRIX( G ) 1: GG [ v ] [ w ] , for all v,w V 2: for each vertex v V do 3: for each vertex u V do 4: for each vertex w V do 5: if G [ v ] [ u ] = 1 and G [ u ] [ w ] = 1 then 6: GG [ v ] [ w ] 1 7: end if 8: end for 9: end for 10: end for 11: return GG Correctness: Both algorithms basically take a brute-force approach in that they look through all possibilities for each vertex. Hence, the G 2 graph is guaranteed to contain all the correct edges. 2. Exercise 22.3-12, page 549. A directed graph G = ( V,E ) is singly connected if u ! v implies that there is at most one simple path from u to v for all vertices u,v V . Give an efficient algorithm to determine whether or not a directed graph is singly connected....
View Full Document
This note was uploaded on 02/10/2009 for the course RLG 100 taught by Professor Pearly during the Spring '08 term at University of Toronto- Toronto.
- Spring '08