Assignment 03 - DFS cycle detection (Solution)

Assignment 03 - DFS cycle detection (Solution) - The Hong...

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

View Full Document Right Arrow Icon
COMP 271: Design and Analysis of Algorithms Fall 2007 Solution to Assignment 3 Question 1: Design an algorithm to determine whether an undirected graph G = ( E, V ) contains a cycle. The running time of your algorithm should be O ( V ) ( and hence should not depend on | E | ). Prove the correctness of your algorithm. Solution: Idea of the algorithm (4 points) We claim that G has a cycle if and only if any DFS of G produces a back edge. If there is a back edge ( u, v ), then clearly there is a cycle containing ( u, v ) in the graph. And if there is no back edge, then by the theorem that an edge in an undirected graph is either a tree edge or a back edge, there are only tree edges, implying that the graph is a forest, and hence is acyclic. We exploit DFS algorithm to judge the existence of back edges. The algo- rithm visit vertices on G in the order of depth ±rst. When visiting a vertex u , the algorithm check, for each adjacent vertex v of
Background image of page 1

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

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

This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.

Page1 / 2

Assignment 03 - DFS cycle detection (Solution) - The Hong...

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

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