Graph Theory Introduction
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What distinguishes an undirected graph from a directed graph?
What distinguishes an undirected graph from a directed graph?
44 questions
- 1
What distinguishes an undirected graph from a directed graph?
- A
Directed graphs only exist for bipartite structures, while undirected graphs are for general structures.
- B
Undirected graphs always have weighted edges, while directed graphs have unweighted edges.
- C
Undirected graphs cannot contain cycles, while directed graphs can.
- D
Edges in an undirected graph have no orientation, while edges in a directed graph have a direction.
Correct answer
DEdges in an undirected graph have no orientation, while edges in a directed graph have a direction.
Explanation
In an undirected graph, an edge from u to v is identical to an edge from v to u. In a directed graph, edges have an orientation, so an edge from u to v does not imply an edge from v to u.
- A
- 2
What is a tree in graph theory?
- A
An undirected graph with no cycles and exactly n-1 edges connecting n nodes.
- B
A graph where every pair of nodes is connected by a unique edge.
- C
A directed graph where every node has exactly one outgoing edge.
- D
A graph with exactly one cycle and all nodes connected.
Correct answer
AAn undirected graph with no cycles and exactly n-1 edges connecting n nodes.
Explanation
A tree is an undirected graph with no cycles. One equivalent definition is that it has n nodes and n-1 edges. The leftmost graph shown in the video is a tree because it has no cycles.
- A
- 3
Which of the following statements about bipartite graphs is true?
- A
Every complete graph is bipartite.
- B
A bipartite graph must have an even number of vertices.
- C
A bipartite graph always contains an odd-length cycle.
- D
A bipartite graph's vertices can be split into two independent groups such that every edge connects between the groups.
Correct answer
DA bipartite graph's vertices can be split into two independent groups such that every edge connects between the groups.
Explanation
A bipartite graph is one where vertices can be divided into two sets U and V, and every edge connects a vertex in U to a vertex in V. This property is equivalent to being two-colorable and having no odd-length cycles.
- A
- 4
What does a complete graph K_n denote?
- A
A graph with n vertices that forms a single cycle.
- B
A graph with n vertices where there is a unique edge between every pair of vertices.
- C
A graph where every vertex is connected to at least one other vertex.
- D
A graph with n vertices where every vertex has degree n.
Correct answer
BA graph with n vertices where there is a unique edge between every pair of vertices.
Explanation
A complete graph K_n has exactly one edge between every pair of distinct vertices. It is the densest possible simple graph on n vertices.
- A
- 5
What is the main advantage of using an adjacency matrix to represent a graph?
- A
It allows constant-time edge weight lookups and is space-efficient for dense graphs.
- B
It provides constant-time insertion of new vertices.
- C
It requires O(V) space for sparse graphs and allows fast iteration over all edges.
- D
It is always more memory-efficient than an adjacency list.
Correct answer
AIt allows constant-time edge weight lookups and is space-efficient for dense graphs.
Explanation
An adjacency matrix provides O(1) edge weight lookups and is space-efficient for dense graphs because it stores a VxV matrix. However, it requires O(V^2) space which can be infeasible for large sparse graphs.
- A
- 6
Which graph representation is best suited for sparse graphs when you need to iterate over all edges efficiently?
- A
Adjacency list
- B
Incidence matrix
- C
Edge list
- D
Adjacency matrix
Correct answer
AAdjacency list
Explanation
An adjacency list only stores edges that exist, making it space-efficient for sparse graphs. Iterating over all edges takes O(V+E) time, which is efficient for sparse graphs.
- A
- 7
What is a major disadvantage of using an edge list to represent a graph?
- A
Edge lookup can be slow because you may need to scan the entire list.
- B
It requires O(V^2) space even for sparse graphs.
- C
It cannot represent directed graphs.
- D
It does not allow storing edge weights.
Correct answer
AEdge lookup can be slow because you may need to scan the entire list.
Explanation
An edge list is a simple list of all edges, which makes edge lookup slow (potentially O(E)) because you cannot directly access a specific edge without searching.
- A
- 8
What is the time complexity of a depth-first search (DFS) on a graph with V vertices and E edges?
- A
O(E log V)
- B
O(V + E)
- C
O(V * E)
- D
O(V^2)
Correct answer
BO(V + E)
Explanation
DFS visits each vertex once and explores each edge once, resulting in O(V + E) time complexity when using an adjacency list.
- A
- 9
In a depth-first search, what happens when the algorithm cannot go any further from the current node?
- A
It starts a new DFS from the source node.
- B
It marks the current node as visited and stops.
- C
It backtracks to the previous node and continues exploration.
- D
It terminates the entire search immediately.
Correct answer
CIt backtracks to the previous node and continues exploration.
Explanation
When DFS cannot proceed further (all neighbors visited or no outgoing edges), it backtracks to the previous node and continues exploring any remaining unvisited neighbors.
- A
- 10
How does a breadth-first search (BFS) explore a graph?
- A
It randomly selects edges to traverse without any structure.
- B
It processes nodes in decreasing order of their distance from the start node.
- C
It explores nodes in layers, visiting all neighbors of a node before moving to the next level.
- D
It plunges depth-first into the graph, backtracking only when stuck.
Correct answer
CIt explores nodes in layers, visiting all neighbors of a node before moving to the next level.
Explanation
BFS explores nodes layer by layer: it visits all nodes at the current distance from the start before moving to nodes at the next distance. This is achieved using a queue.
- A
- 11
What data structure is essential for a breadth-first search?
- A
A queue
- B
A stack
- C
A hash table
- D
A priority queue
Correct answer
AA queue
Explanation
BFS uses a queue to keep track of nodes to visit next, ensuring that nodes are processed in the order they are discovered (FIFO).
- A
- 12
How can you find the number of connected components in an undirected graph using DFS?
- A
Use the union-find data structure but not DFS.
- B
Run DFS from every unvisited node, incrementing a counter each time a new DFS starts.
- C
Run BFS once from any node and count the number of visited nodes.
- D
Count the number of nodes with odd degree.
Correct answer
BRun DFS from every unvisited node, incrementing a counter each time a new DFS starts.
Explanation
To count connected components, you iterate over all nodes, and whenever you encounter an unvisited node, you start a DFS (or BFS) from it and mark all reachable nodes as visited; each such DFS corresponds to one connected component.
- A
- 13
In a grid where you can move up, down, left, and right, how can you easily access neighboring cells from a given cell (r, c)?
- A
Iterate over all cells in the grid and check if they are adjacent.
- B
Use direction vectors such as [-1, 0], [1, 0], [0, -1], [0, 1] and add them to (r, c).
- C
Precompute a list of neighbors for every cell.
- D
Use an adjacency matrix to store neighbor relationships.
Correct answer
BUse direction vectors such as [-1, 0], [1, 0], [0, -1], [0, 1] and add them to (r, c).
Explanation
Using direction vectors is an efficient technique: define the four possible moves as (dr, dc) pairs, then for each, compute the new cell (r+dr, c+dc) and validate bounds and obstacles.
- A
- 14
What is a topological ordering of a directed acyclic graph?
- A
An ordering of nodes based on decreasing edge weights.
- B
An ordering of nodes where every node appears exactly once and edges can go in any direction.
- C
An ordering where the node with the highest out-degree comes first.
- D
An ordering of nodes where for every directed edge u -> v, u appears before v.
Correct answer
DAn ordering of nodes where for every directed edge u -> v, u appears before v.
Explanation
A topological ordering arranges the nodes in a linear sequence such that all directed edges point from earlier nodes to later nodes. This is only possible for DAGs.
- A
- 15
Which graph cannot have a topological ordering?
- A
A graph with multiple components
- B
A graph with self-loops
- C
A graph with a directed cycle
- D
An undirected graph
Correct answer
CA graph with a directed cycle
Explanation
A directed cycle creates a circular dependency where no node can come before another, making it impossible to produce a topological ordering.
- A
- 16
How does the DFS-based topological sort algorithm work?
- A
Find a node with no incoming edges repeatedly and remove it.
- B
Do a DFS, and on the callback, add the current node to the topological ordering in reverse order.
- C
Use a priority queue to always pick the node with the smallest indegree.
- D
Do a BFS and add nodes to the ordering in the order they are visited.
Correct answer
BDo a DFS, and on the callback, add the current node to the topological ordering in reverse order.
Explanation
The standard DFS topological sort performs a DFS from each unvisited node, and when backtracking (callback), it prepends the current node to the ordering. This yields a reverse post-order which is a valid topological order.
- A
- 17
What is the time complexity to find the shortest path from a single source on a DAG?
- A
O(V^2) using Bellman-Ford
- B
O(V + E) using topological sort and edge relaxation
- C
O(E log V) using Floyd-Warshall
- D
O(V log V) using Dijkstra's algorithm
Correct answer
BO(V + E) using topological sort and edge relaxation
Explanation
On a DAG, we can compute a topological ordering in O(V+E) and then process nodes in that order, relaxing each edge. This gives O(V+E) linear time for single-source shortest paths, even with negative edges.
- A
- 18
How can you find the longest path in a DAG?
- A
Multiply all edge weights by -1 and use BFS.
- B
Use Dijkstra's algorithm with negative weights.
- C
Use Floyd-Warshall on the DAG.
- D
Negate all edge weights, find the shortest path, then negate the result.
Correct answer
DNegate all edge weights, find the shortest path, then negate the result.
Explanation
The longest path problem on a DAG can be solved by multiplying all edge weights by -1, then running the shortest path algorithm for DAGs, and negating the resulting distances.
- A
- 19
What constraint is required for Dijkstra's algorithm to work correctly?
- A
The graph must be a DAG.
- B
There must be no cycles in the graph.
- C
All edge weights must be non-negative.
- D
The graph must be undirected.
Correct answer
CAll edge weights must be non-negative.
Explanation
Dijkstra's algorithm requires non-negative edge weights because it greedily processes nodes based on the assumption that once a node is visited, its distance cannot be improved by a later negative edge.
- A
- 20
What is the main difference between the lazy and eager implementations of Dijkstra's algorithm?
- A
The lazy version only works on undirected graphs.
- B
The eager version has worse time complexity.
- C
The lazy version uses a regular priority queue and may insert duplicate keys; the eager version uses an indexed priority queue and supports decrease-key.
- D
The lazy version uses a stack; the eager version uses a queue.
Correct answer
CThe lazy version uses a regular priority queue and may insert duplicate keys; the eager version uses an indexed priority queue and supports decrease-key.
Explanation
In the lazy implementation, duplicate key-value pairs are inserted into the priority queue, and stale entries are ignored when popped. The eager implementation uses an indexed priority queue to update a node's value directly, avoiding duplicates.
- A
- 21
Which algorithm is suitable for finding shortest paths in a graph with negative edge weights and can detect negative cycles?
- A
Dijkstra's algorithm
- B
Topological sort
- C
Breadth-first search
- D
Bellman-Ford algorithm
Correct answer
DBellman-Ford algorithm
Explanation
Bellman-Ford can handle negative edge weights and detect negative cycles by running an extra iteration after V-1 relaxations. Dijkstra fails with negative weights, and BFS only works for unweighted graphs.
- A
- 22
What is the time complexity of the Bellman-Ford algorithm?
- A
O(V * E)
- B
O(V^2)
- C
O(V + E)
- D
O(E log V)
Correct answer
AO(V * E)
Explanation
Bellman-Ford relaxes all E edges V-1 times, resulting in O(V*E) time complexity.
- A
- 23
How does the Floyd-Warshall algorithm compute all-pairs shortest paths?
- A
It uses a divide-and-conquer approach.
- B
It runs Dijkstra from every vertex.
- C
It uses dynamic programming, considering each node as an intermediate vertex.
- D
It finds the shortest path tree for each node.
Correct answer
CIt uses dynamic programming, considering each node as an intermediate vertex.
Explanation
Floyd-Warshall uses DP with a 3D table (or optimized 2D) that iterates over all possible intermediate nodes k, updating the shortest path from i to j through nodes 0..k.
- A
- 24
What is one method to detect negative cycles using Floyd-Warshall?
- A
Only use Bellman-Ford; Floyd-Warshall cannot detect negative cycles.
- B
Check if any diagonal entry becomes negative.
- C
Run the algorithm a second time and if any distance can be improved, mark it as negative infinity.
- D
Count the number of iterations; if more than V, there is a negative cycle.
Correct answer
CRun the algorithm a second time and if any distance can be improved, mark it as negative infinity.
Explanation
After computing all-pairs shortest paths, you run the algorithm a second time. If any distance decreases, that indicates the presence of a negative cycle, and you mark that path as negative infinity.
- A
- 25
What is a minimum spanning tree (MST) of a graph?
- A
A forest that covers all vertices with minimal number of edges.
- B
A tree that includes all vertices and has the maximum possible total weight.
- C
A spanning tree with exactly V-1 edges but not necessarily minimal cost.
- D
A subset of edges that connects all vertices together without cycles and with minimum total weight.
Correct answer
DA subset of edges that connects all vertices together without cycles and with minimum total weight.
Explanation
An MST is a tree (no cycles) that spans all vertices and has the smallest possible total edge weight. There can be multiple MSTs with the same cost.
- A
- 26
In Prim's algorithm, what data structure is used to efficiently select the next edge with minimum weight?
- A
A priority queue (min-heap)
- B
A stack
- C
A queue
- D
An array
Correct answer
AA priority queue (min-heap)
Explanation
Prim's algorithm maintains a priority queue of edges (or node-edge pairs) sorted by weight to always pick the cheapest edge that connects the visited set to an unvisited node.
- A
- 27
What is the time complexity of the lazy version of Prim's algorithm?
- A
O(V^2)
- B
O(E log E)
- C
O(V log V)
- D
O(E log V)
Correct answer
BO(E log E)
Explanation
In the lazy version, each edge is inserted into the priority queue, and each poll operation is O(log E). With E insertions and E deletions, the complexity is O(E log E).
- A
- 28
How does the eager version of Prim's algorithm improve over the lazy version?
- A
It uses a binary heap without decrease-key, which is faster.
- B
It uses a queue instead of a priority queue.
- C
It uses an indexed priority queue to avoid duplicate entries and supports decrease-key operations, reducing complexity to O(E log V).
- D
It only processes edges once, independent of the graph's density.
Correct answer
CIt uses an indexed priority queue to avoid duplicate entries and supports decrease-key operations, reducing complexity to O(E log V).
Explanation
The eager version maintains an indexed priority queue of size V, where each vertex has exactly one entry. It updates the best edge to a vertex using decrease-key, avoiding duplicates and reducing the overall complexity to O(E log V).
- A
- 29
What is a flow network?
- A
A bipartite graph used for matching.
- B
An undirected graph where edges have weights representing flow.
- C
A graph where all edges have the same capacity.
- D
A directed graph where each edge has a capacity, with a source and sink node.
Correct answer
DA directed graph where each edge has a capacity, with a source and sink node.
Explanation
A flow network is a directed graph with a source node s and a sink node t. Each edge has a non-negative capacity, and the goal is to push as much flow from s to t without exceeding capacities.
- A
- 30
What is the Ford-Fulkerson method for finding maximum flow?
- A
Use a depth-first search to find all paths and sum their flows.
- B
Repeatedly find an augmenting path from source to sink in the residual graph and augment flow until no more paths exist.
- C
Use a breadth-first search to find the shortest path and send all flow through it.
- D
Combine all edges into one path and calculate total capacity.
Correct answer
BRepeatedly find an augmenting path from source to sink in the residual graph and augment flow until no more paths exist.
Explanation
Ford-Fulkerson method iteratively finds augmenting paths (paths with available capacity) in the residual graph and increases flow along them. This continues until no augmenting path exists, at which point flow is maximal.
- A
- 31
What is the key difference between Ford-Fulkerson with DFS and Edmonds-Karp algorithm?
- A
Ford-Fulkerson with DFS guarantees polynomial runtime.
- B
Edmonds-Karp uses BFS to find the shortest augmenting path, while Ford-Fulkerson with DFS uses an arbitrary path.
- C
Edmonds-Karp can handle negative edges.
- D
Edmonds-Karp uses a depth-first search, while Ford-Fulkerson uses breadth-first search.
Correct answer
BEdmonds-Karp uses BFS to find the shortest augmenting path, while Ford-Fulkerson with DFS uses an arbitrary path.
Explanation
Edmonds-Karp is a specific implementation of Ford-Fulkerson that always uses BFS to find the augmenting path with the fewest number of edges. This ensures a strongly polynomial time complexity O(VE^2), independent of capacity values.
- A
- 32
What is the worst-case time complexity of the Ford-Fulkerson method when implemented with DFS?
- A
O(V * E)
- B
O(E log V)
- C
O(V^2)
- D
O(E * |f|) where |f| is the maximum flow value
Correct answer
DO(E * |f|) where |f| is the maximum flow value
Explanation
If the DFS finds augmenting paths that only push 1 unit of flow each time, the number of iterations equals the maximum flow value |f|. Each DFS takes O(E) time, so total is O(E * |f|).
- A
- 33
What is the idea behind capacity scaling in max flow algorithms?
- A
Scale down all capacities by a factor to make the graph smaller.
- B
Prioritize edges with larger remaining capacity first to reduce the number of augmentations.
- C
Use a scaling factor to convert the graph to a unit capacity graph.
- D
Start with the smallest capacity and gradually increase.
Correct answer
BPrioritize edges with larger remaining capacity first to reduce the number of augmentations.
Explanation
Capacity scaling adds a parameter Δ that determines the minimum remaining capacity an edge must have to be considered in an augmenting path. By initially only allowing large capacities, the algorithm pushes large amounts of flow quickly, reducing iterations.
- A
- 34
What is a level graph in Dinic's algorithm?
- A
A graph where each node is assigned a level based on its degree.
- B
The residual graph with all edges.
- C
A subgraph containing only edges that go from level L to L+1 in a BFS from the source.
- D
A graph that shows the maximum flow so far.
Correct answer
CA subgraph containing only edges that go from level L to L+1 in a BFS from the source.
Explanation
Dinic's algorithm builds a level graph by performing a BFS from the source, assigning distances (levels). Only edges that go from a node at level L to a node at level L+1 with remaining capacity are included.
- A
- 35
What is a blocking flow in Dinic's algorithm?
- A
A flow that blocks all edges from the source.
- B
The maximum flow achievable in one iteration of level graph construction.
- C
A flow that causes the algorithm to terminate early.
- D
A flow situation where no more augmenting paths exist in the current level graph.
Correct answer
DA flow situation where no more augmenting paths exist in the current level graph.
Explanation
A blocking flow is a flow in the level graph such that no additional augmenting path from source to sink exists in that level graph. Once found, Dinic rebuilds the level graph and finds another blocking flow.
- A
- 36
What is the time complexity of Dinic's algorithm on a bipartite graph?
- A
O(V + E)
- B
O(E log V)
- C
O(V^2 * E)
- D
O(E * sqrt(V))
Correct answer
DO(E * sqrt(V))
Explanation
In bipartite graphs, Dinic's algorithm runs in O(E * sqrt(V)) time, which is very efficient for large bipartite matching problems.
- A
- 37
How can an unweighted bipartite matching problem be solved using max flow?
- A
Create a source connected to all nodes with capacity 1 and find max flow.
- B
Use a greedy algorithm to assign matches.
- C
Run BFS from left nodes and stop when sink is reached.
- D
Create a source connected to left nodes with capacity 1, left nodes to right nodes (if match possible) with capacity 1, and right nodes to sink with capacity 1, then find max flow.
Correct answer
DCreate a source connected to left nodes with capacity 1, left nodes to right nodes (if match possible) with capacity 1, and right nodes to sink with capacity 1, then find max flow.
Explanation
The standard reduction: add a source node with capacity 1 to each left vertex, edges between left and right with capacity 1, and each right vertex to sink with capacity 1. The max flow equals the size of the maximum matching.
- A
- 38
What condition must hold for an undirected graph to have an Eulerian circuit?
- A
Exactly two vertices have odd degree.
- B
Every vertex must have an even degree.
- C
The graph must be connected and have no vertices of odd degree.
- D
All vertices must have the same degree.
Correct answer
BEvery vertex must have an even degree.
Explanation
An undirected graph has an Eulerian circuit if and only if it is connected (except for isolated vertices) and every vertex has even degree.
- A
- 39
What is a bridge (cut edge) in a graph?
- A
An edge that connects two different connected components.
- B
An edge with the largest weight in the graph.
- C
An edge whose removal increases the number of connected components.
- D
An edge that is part of a cycle.
Correct answer
CAn edge whose removal increases the number of connected components.
Explanation
A bridge is an edge that, if removed, disconnects the graph (or increases the number of connected components). Bridges represent vulnerabilities in a network.
- A
- 40
In the DFS-based algorithm to find bridges, what condition indicates that an edge (u, v) (where u is parent of v) is a bridge?
- A
The ID of u is greater than the low-link value of v.
- B
The low-link value of u is equal to the ID of v.
- C
The low-link value of v is greater than the ID of u.
- D
The ID of v is greater than the low-link value of u.
Correct answer
CThe low-link value of v is greater than the ID of u.
Explanation
The condition is that the ID (discovery time) of u is strictly less than the low-link value of v. This means there is no back edge from the subtree rooted at v to u or any ancestor of u.
- A
- 41
What is a strongly connected component (SCC) in a directed graph?
- A
A maximal set of vertices where every vertex can reach every other vertex in the set.
- B
A set of vertices with no incoming edges from outside.
- C
A set of vertices that form a cycle.
- D
A component that is connected when ignoring direction.
Correct answer
AA maximal set of vertices where every vertex can reach every other vertex in the set.
Explanation
An SCC is a subgraph where for every pair of vertices u and v, there is a directed path from u to v and from v to u. It is maximal: no additional vertex can be added without breaking this property.
- A
- 42
In Tarjan's SCC algorithm, what is the low-link value?
- A
The highest node ID reachable from the node.
- B
The depth of the node in the DFS tree.
- C
The number of outgoing edges from the node.
- D
The smallest node ID reachable from that node, including itself, considering only nodes on the stack.
Correct answer
DThe smallest node ID reachable from that node, including itself, considering only nodes on the stack.
Explanation
The low-link value of a node is the smallest discovery time of any node reachable from that node, including itself, but only considering nodes that are currently on the stack (to avoid mixing SCCs).
- A
- 43
What is the Traveling Salesman Problem (TSP)?
- A
Find a cycle that visits each edge exactly once.
- B
Find the minimum spanning tree of the cities.
- C
Find the longest path that visits all cities without revisiting any.
- D
Find the shortest possible route that visits each city exactly once and returns to the origin city.
Correct answer
DFind the shortest possible route that visits each city exactly once and returns to the origin city.
Explanation
TSP asks: given a list of cities and distances between each pair, what is the shortest possible round trip that visits each city exactly once and returns to the starting city? It is NP-hard.
- A
- 44
What is the time complexity of the dynamic programming solution for TSP (Held-Karp algorithm)?
- A
O(n^2 * 2^n)
- B
O(2^n)
- C
O(n!)
- D
O(n^3)
Correct answer
AO(n^2 * 2^n)
Explanation
The Held-Karp algorithm uses DP over subsets, requiring O(n^2 * 2^n) time and O(n * 2^n) space. This is much better than O(n!) brute force.
- A

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