hw2-solutions - CMPS102 Homework#2 Solutions TA Adam M...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPS102: Homework #2 Solutions TA: Adam M. Smith ( amsmith@soe.ucsc.edu ) Due Date: April 14, 2010 1 Kleinberg & Tardos, Ch. 3, #3 Algorithm The original algorithm referenced in the problem is sufficient for finding an ordering if the graph is a DAG. However, on a general graph, the step where a node with no incoming edges is found may fail. In this situation, we need an additional procedure to find one cycle that breaks the original algorithm. Below is complete solution, with lines 9–20 representing the interesting part. Algorithm 1 Compute a topological ordering or a cycle of G 1. if G contains a node v with no incoming edges then 2. Invoking this algorithm recursively on G- { v } . 3. if an ordering O was returned then 4. return v :: O 5. else { a cycle C was returned } 6. return C 7. end if 8. else { all nodes had an incoming edge } 9. Mark all nodes as unvisited. 10. Let v be any node of G 11. Let P be an empty sequence of nodes (to be filled with a path) 12. loop 13. if v was visited then 14. return the sub-path in P from the first occurrence of v to the end 15. else 16. Mark v as visited. 17. Append v to P . 18. v ← any node connected to v by an incoming edge. 19. end if 20. end loop 21. end if 1 Running Time With suitable data structures (an adjacency list representation), the original topological ordering algorithm given in the book runs in O ( n + m ) time where n and m are the number of nodes and edges respectively. It takes O ( n + m ) time to produce an inverted graph (a new adjacency list representation with edge directions reversed) suitable for efficient traversal of incoming edges. The mod- ification to the original algorithm includes a sub-algorithm which is invoked at most once, visiting each remaining node at most once, doing a constant amount of work at each. Thus, the overall running time of the modified algorithm is still O ( n + m ). Correctness In class, we already proved that there exists a topological ordering if and only if the graph is a DAG. If a graph is a DAG, then the procedure inherited from the original algorithm clearly ensures that a topological ordering is produced. What remains to be shown is the correctness of the algorithm on a graph with cycles. Claim. If G has any cycles, the modified algorithm will return one. Proof. The algorithm must, at some point, fail to find a node with no incoming edges (because each node in a cycle in a has an incoming edge from another node in the cycle). At this point, the algorithm will choose any node v . The algorithm begins chasing incoming edges looking for a cycle. At each step, either a node is marked as visited, or a cycle is found. Because there are only n nodes and there is at least one incoming edge for every node (there is always a valid move), this process must eventually terminate in finding a cycle. That is, in the worst case, after all n nodes have been marked visited, the final hop always leads to a visited node, completing a cycle....
View Full Document

This note was uploaded on 11/08/2010 for the course CS 455 taught by Professor Cheng during the Fall '10 term at SIU Carbondale.

Page1 / 8

hw2-solutions - CMPS102 Homework#2 Solutions TA Adam M...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online