Which method is commonly used to detect cycles in a directed graph?
Think about how to track nodes currently being explored to find back edges.
Depth-first search (DFS) with a recursion stack helps detect back edges, which indicate cycles in directed graphs.
What is a common approach to detect cycles in an undirected graph?
Consider how revisiting a node that is not the immediate parent indicates a cycle.
In undirected graphs, DFS can detect cycles by checking if a visited node is encountered again and it is not the parent node.
Given an undirected graph, which statement about using Union-Find (Disjoint Set Union) to detect cycles is true?
Think about what it means if two nodes are already connected before adding an edge.
If two vertices are already in the same set, connecting them again forms a cycle. Union-Find efficiently tracks connected components.
Which of the following correctly contrasts cycle detection in directed and undirected graphs?
Consider how direction affects the way cycles are detected.
Directed graphs need recursion stack tracking to detect back edges; undirected graphs detect cycles by checking visited nodes excluding the parent.
Consider the following undirected graph edges: (1-2), (2-3), (3-4), (4-2). After running a cycle detection algorithm using DFS, what is the number of cycles detected?
Edges: [(1,2), (2,3), (3,4), (4,2)]
Trace the DFS and identify if multiple cycles or a single cycle exists.
The edges form a single cycle involving nodes 2, 3, and 4. Node 1 connects without forming a cycle.