Lecture28

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

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

View Full Document Right Arrow Icon
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; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
05/01/09 4 Last Lecture 5 Graphs Implementation Adjacency List Adjacency Matrix
Background image of page 4
05/01/09 5 This Lecture 5 Cycle Detection Spanning Trees Vertex Coloring Exam Review
Background image of page 5

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

View Full DocumentRight Arrow Icon
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?
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online