Interview Questions: Undirected Graphs
Question 1
Nonrecursive depth-first search. Implement depth-first search in an undirected graph without using recursion.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question ExplanationHint: use an explicit stack.
Question 2
Diameter and center of a tree. Given a connected graph with no cycles
- Diameter: design a linear-time algorithm to find the longest simple path in the graph.
- Center: design a linear-time algorithm to find a vertex such that its maximum distance from any other vertex is minimized.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question ExplanationHint (diameter): to compute the diameter, pick a vertex s; run BFS from s; then run BFS again from the vertex that is furthest from s.
Hint (center): consider vertices on the longest path.
Hint (center): consider vertices on the longest path.
Question 3
Eulierian cycle. An Eulierian cycle in a graph is a cycle (not necessarily simple) that uses every edge in the graph exactly one.
- Show that a graph has an Eulerian cycle if and only if it is both connected and every vertex has even degree.
- Design a linear-time algorithm to determine whether a graph has an Eulerian cycle, and if so, find one.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question ExplanationHint: use depth-first search and piece together the cycles you discover.
Interview Questions: Directed Graphs
Question 1
Shortest directed cycle. Given a digraph G, design an efficient algorithm to find a directed cycle with the minimum number of edges (or report that the graph is acyclic). The running time of your algorithm should be at most proportional to V(E+V) and use space proportional to E+V, where V is the number of vertices and E is the number of edges.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint: run BFS from each vertex.
Question 2
Hamiltonian path in a DAG. Given a directed acyclic graph, design a linear-time algorithm to determine whether it has a Hamiltonian path (a simple path that visits every vertex), and if so, find one.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint: topological sort.
Question 3
Reachable vertex.
- DAG: Design a linear-time algorithm to determine whether a DAG has a vertex that is reachable from every other vertex, and if so, find one.
- Digraph: Design a linear-time algorithm to determine whether a digraph has a vertex that is reachable from every other vertex, and if so, find one.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint (DAG): compute the outdegree of each vertex.
Hint (digraph): compute the strong components and look at the kernel DAG (the digraph that results when you contract each strong component to a single vertex).
Interview Questions: Minimum Spanning Trees
Question 1
Bottleneck minimum spanning tree. Given a connected edge-weighted graph, design an efficient algorithm to find a minimum bottleneck spanning tree. The bottleneck capacity of a spanning tree is the weights of its largest edge. A minimum bottleneck spanning tree is a spanning tree of minimum bottleneck capacity.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint: prove that an MST is a minimum bottleneck spanning tree.
Extra challenge: Compute a minimum bottleneck spanning tree in linear time in the worst case. Assume that you can compute the median of n keys in linear time in the worst case.
Question 2
Is an edge in a MST. Given an edge-weighted graph G and an edge e, design a linear-time algorithm to determine whether e appears in some MST of G.
Note: Since your algorithm must take linear time in the worst case, you cannot afford to compute the MST itself.
Note: Since your algorithm must take linear time in the worst case, you cannot afford to compute the MST itself.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint: consider the subgraph G′ of G containing only those edges whose weight is strictly less than that of e.
Question 3
Minimum-weight feedback edge set. A feedback edge set of a graph is a subset of edges that contains at least one edge from every cycle in the graph. If the edges of a feedback edge set are removed, the resulting graph is acyclic. Given an edge-weighted graph, design an efficient algorithm to find a feedback edge set of minimum weight. Assume the edge weights are positive.
Your Answer | Score | Explanation | |
---|---|---|---|
Total | 0.00 / 0.00 |
Question Explanation
Hint: complement of an MST.