Depth-First Search
- 25Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What are the two main graph search algorithms discussed?
Breadth-first search (BFS) and Depth-first search (DFS).
Or swipe the card
25 cards
What are the two main graph search algorithms discussed?
Breadth-first search (BFS) and Depth-first search (DFS).
How is a graph typically specified as input for graph algorithms in this context?
As an adjacency list (or adjacency lists plural).
What is the general goal of graph search algorithms like BFS and DFS?
To explore the graph by visiting all vertices, visiting each vertex only once.
What is a key advantage of Breadth-First Search (BFS)?
It explores things layer by layer, providing shortest paths from a particular source vertex.
What can BFS detect regarding reachability from a source vertex s?
BFS can tell you which vertices are unreachable from s.
How is Depth-First Search (DFS) conceptually similar to solving a maze?
It recursively explores the graph, backtracking as necessary, much like leaving 'breadcrumbs' in a maze to know where to go next or when to turn around.
How can DFS be implemented very easily on a computer?
Using recursion.
What is the primary mechanism to prevent repeating vertices in DFS?
Marking a vertex as 'seen' (e.g., by setting its parent pointer) when it is first visited, and skipping it if already seen.
Describe the core logic of the
DFS_visit(s, adj, parent)function.It marks s as seen (sets its parent), then for each neighbor v of s, if v has not been visited, it sets v's parent to s and recursively calls
DFS_visit(v, adj, parent).How does the top-level
DFS(V, adj)algorithm ensure all vertices in a graph are visited, even if it's disconnected?It iterates through all vertices s in V. If s has not been visited yet, it calls
DFS_visit(s, adj, parent)to explore everything reachable from s.What is the running time complexity of Depth-First Search (DFS) for a graph with V vertices and E edges?
Θ(V+E) (linear time).
Why is DFS not suitable for finding shortest paths?
DFS explores deeply into one branch before exploring others, often finding long paths to reachable vertices before shorter ones. For example, going from 'a' to 'd' might take a path of length 3 (a-b-e-d) instead of a direct edge (a-d).
What are 'tree edges' in the context of DFS edge classification?
Edges that lead to a newly discovered (unvisited) vertex during the DFS traversal. They form a directed forest (or tree if the graph is connected).
What are 'forward edges' in DFS edge classification for a directed graph?
Edges (u,v) where v is a descendant of u in the DFS tree, but (u,v) is not a tree edge.
What are 'backward edges' in DFS edge classification for a directed graph?
Edges (u,v) where v is an ancestor of u in the DFS tree.
What are 'cross edges' in DFS edge classification for a directed graph?
Edges (u,v) where u and v are not ancestor-descendant related in the DFS tree.
How can you detect backward edges during DFS execution?
By marking nodes as 'in process' (on the recursion stack) when
DFS_visitbegins and 'finished' when it ends. If an edge (u,v) points to a node v that is currently 'in process', it's a backward edge.In an undirected graph, which types of DFS edges can exist?
Only tree edges and backward edges can exist.
How does DFS detect cycles in a graph?
A graph G has a cycle if and only if a Depth-First Search of G reveals a backward edge.
If DFS finds a backward edge, how can a cycle be easily identified?
Follow the tree edges from the node where the backward edge originates back to its ancestor, which is the destination of the backward edge.
What is a Directed Acyclic Graph (DAG)?
A directed graph that contains no cycles.
What is the goal of topological sort?
To order the vertices of a Directed Acyclic Graph (DAG) such that for every directed edge (u,v), u comes before v in the ordering.
How is Depth-First Search used to perform a topological sort?
Run DFS on the graph and then output the vertices in the reverse of their finishing times.
Why does reversing the DFS finishing times produce a valid topological sort?
Because in a DAG (which has no back edges), for any edge (u,v), v must always finish before u.
What property of DAGs is essential for the topological sort algorithm based on DFS to work?
The absence of back edges in the graph.

Ask anything
We've read this whole study set, so questions about it get the best answers.