Lecture28

# Lecture28 - Cycle Detection(Undirected static boolean...

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

05/01/09 1 Cycle Detection (Undirected) static boolean hasCycle( Vertex v ) { if ( v.equals(v.getPredecessor()) ) return false; v.setMark( IN_PROGRESS ); Iterator iter = v.getAdjacent(); while ( iter.hasNext() ) { Vertex u = (Vertex) iter.getNext(); u.setPredecessor(v); int mark = u.getMark(); if ( ( mark == IN_PROGRESS ) || ( mark != DONE && hasCycle(u) ) ) { return true; } } v.setMark( DONE ); return false; }

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

View Full Document
05/01/09 2 Cycle Detection Algorithm 3 3 1 1 4 2 1 10 5 5 3 3 1 1 2 6 3 1 1 v s a b h z g y f m d x n e
05/01/09 3 Announcements 5 H3: Due Today (in class or 4pm in my office 5376CS) Final Exam (30%) Saturday, Dec 18th 7:25-9:25pm 1240 CS Exam Review Thursday, Dec 16th 3:45-5:00pm 1240 CS

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

View Full Document
05/01/09 5 This Lecture 5 Cycle Detection Spanning Trees Vertex Coloring Exam Review

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

View Full Document
05/01/09 6 Cycle Detection Algorithm 5 Why do we need to detect cycles? Many graph algorithms (traversals) depend on this ability . How can we detect cycles?
05/01/09 7 Cycle Detection Algorithm 5 Complete a Depth-First Search (assign predessor) If we reach a previously visited node that's "in progress", there 's a cycle. Use three "marks" UNVISITED IN_PROGRESS DONE

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

View Full Document
05/01/09 8 Cycle Detection ( Undirected ) ) static boolean hasCycle( Vertex v ) { if ( v.equals(v.getPredecessor()) ) return false; v.setMark( IN_PROGRESS ); Iterator iter = v.getAdjacent(); while ( iter.hasNext() ) { Vertex u = (Vertex) iter.getNext(); u.setPredecessor(v); int mark = u.getMark(); if ( ( mark == IN_PROGRESS ) || ( mark != DONE && hasCycle(u) ) ) { return true; } } v.setMark( DONE ); return false; }
05/01/09 9 Cycle Detection Algorithm (a) z b x h e y m a s v d n f g 3 3 1 1 4 2 1 10 5 5 3 3 1 1 2 6 3 1 1 IN_PROGRESS DONE

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

View Full Document
05/01/09 10 Cycle Detection Algorithm (a) IN_PROGRESS DONE z b x h e m s v d n f g 3 3 1 1 2 1 1 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 37

Lecture28 - Cycle Detection(Undirected static boolean...

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

View Full Document
Ask a homework question - tutors are online