# a1 - CSE 60111 Complexity and Algorithms Spring Semester...

This preview shows pages 1–3. Sign up to view the full content.

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 ) .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Algorithm 2 SQUARE MATRIX( G ) 1: GG [ v ] [ w ] 0 , 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. (20 points) Solution: Ideas/Observations: Let’s first observe that there exist four types of edges in a graph: tree, forward, back, and cross edges. If a directed graph only contain tree edges then it must be singly connected. On the other hand, if the graph contains a forward edge, then it is not singly connected, as there exists at least two vertices u and v V such that there is more than one
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern