# day25 - COP 3503 Computer Science II CLASS NOTES DAY#25...

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

COP 3503 – Computer Science II CLASS NOTES - DAY #25 Cycle Detection Algorithms Many algorithms rely on detecting cycles in graphs. Many cycle detection algorithms are “brute force” algorithms and are quite inefficient. However, there are several algorithms which are quite efficient. One such algorithm is based upon a depth-first traversal of the graph. For undirected graphs, a single line needs to be added to the algorithm we presented earlier for depth-first traversal. This algorithm as well as the “modified” version are shown below: Original Algorithm: DFS(v) num (v) = i++; for all vertices u adjacent to v if num (u) is 0 attach edge (uv) to edges; DFS(u); depthFirstSearch( ) for all vertices v num (v) = 0; edges = null; i = 1; while there is a vertex v such that num (v) is 0 DFS(v); output edges; Modified Algorithm: cycleDetection (v) num (v) = i++; for all vertices u adjacent to v if num(u) is 0 attach edge (uv) to edges; cycleDetection(u); else cycle detected ; For digraphs, the situation is a bit more complicated, since there may be edges between different spanning subtrees, called side edges . An edge (a back edge) Day 25 - 1 More Graph Problems

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

View Full Document
indicates a cycle if it joins two vertices already included in the same spanning subtree. To consider only this case, a number higher than any number generated in subsequent searches is assigned to a vertex being currently visited after all its descendants have also been visited. In this way, if a vertex is about to be joined by an edge with a vertex having a lower number, then a cycle has been detected. The algorithm for cycle detection using this technique in a digraph is shown below: digraphCycleDetection (v) num (v) = i++; for all vertices u adjacent to v if num (u) is 0 attach edge (uv) to edges; digraphCycleDetection(u); else if num (u) is not cycle is detected ; num (v) = ; Kruskal’s Algorithm to Generate a Minimum Spanning Tree We have already seen Prim’s algorithm for generating a minimum spanning tree. Prim’s technique, although we presented it in tabular form, basically creates a single tree and expands the tree from the root as edges are considered. Kruskal’s algorithm takes a different approach in which a set of trees (a forest) is condensed to a single tree. In Kruskal’s method, all edges are ordered by weight, and then each edge in this ordered sequence is checked to see whether it can be considered as part of the tree which is under construction. The edge is added to the tree only if no cycle arises after its inclusion. Kruskal’s algorithm is quite simple and is shown below: KruskalAlgorithm ( weighted connected undirected graph) tree = null ; edges = sequence of all edges of graph sorted by weight ; for (i = 1; i E and tree < V -1; i++) if e i from edges does not form a cycle with edges in tree add e i to tree; The complexity of this algorithm is determined by the complexity of the sorting algorithm which is applied, which for an efficient sorting algorithm is O( E log 2 Day 25 - 2
E ). It also depends on the complexity of the algorithm used for the cycle detection.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

day25 - COP 3503 Computer Science II CLASS NOTES DAY#25...

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

View Full Document
Ask a homework question - tutors are online