Graphs
Chapter: 3.5 Graphs Level: 2 — Recall (definitions, standard textbook problems, short derivations) Time Limit: 30 minutes Total Marks: 40
Q1. Define the following graph terms in one line each: (a) simple graph, (b) multigraph, (c) weighted graph, (d) directed graph. (4 marks)
Q2. State the space complexity of (a) an adjacency matrix and (b) an adjacency list for a graph with vertices and edges. For a graph with and , which representation uses less space? Justify briefly. (4 marks)
Q3. State the time complexity of BFS and DFS on a graph stored as an adjacency list. Explain in one sentence why BFS finds shortest paths in an unweighted graph. (4 marks)
Q4. Consider the undirected graph with edges: . Starting BFS from (visiting neighbours in alphabetical order), give the BFS order of vertices and the shortest-path distance (in edges) from to . (4 marks)
Q5. State whether each statement is True or False, with a one-line reason: (a) Dijkstra's algorithm works correctly with negative edge weights. (b) Bellman-Ford can detect negative-weight cycles. (c) A topological sort exists for any directed graph. (d) Kruskal's algorithm uses a Disjoint Set Union structure. (4 marks)
Q6. For the weighted directed graph run Dijkstra from source . Edges (u→v, weight): , , , , . Give the final shortest distances from to , , and . (5 marks)
Q7. State the time complexities of: (a) Dijkstra with a binary heap, (b) Bellman-Ford, (c) Floyd-Warshall, (d) Kruskal's algorithm (with sorting). (4 marks)
Q8. (a) State the max-flow min-cut theorem in one sentence. (b) State the time complexity of the Edmonds-Karp algorithm. (3 marks)
Q9. Define a bipartite graph. State the standard test used to check bipartiteness and give its time complexity. (4 marks)
Q10. For Union-Find with path compression and union by rank, state the amortized time per operation. Name the two optimizations and state in one line what each does. (4 marks)
End of paper.
Answer keyMark scheme & solutions
Q1. (4 marks) — 1 mark each
- (a) Simple graph: no self-loops and no multiple (parallel) edges between the same pair of vertices.
- (b) Multigraph: allows multiple parallel edges between the same pair of vertices (and possibly self-loops).
- (c) Weighted graph: each edge carries a numeric weight/cost.
- (d) Directed graph: edges have direction (ordered pairs); edge .
Q2. (4 marks)
- (a) Adjacency matrix: . (1)
- (b) Adjacency list: . (1)
- For : matrix entries; list . (1) List uses far less space because the graph is sparse (). (1)
Q3. (4 marks)
- BFS: . (1) DFS: . (1) (each vertex and edge examined once)
- BFS explores vertices in non-decreasing order of distance (level by level), so the first time a vertex is reached is via a minimum number of edges. (2)
Q4. (4 marks)
- BFS order from : . (2) (A → neighbours B,C → their unvisited neighbour D → then E)
- Distance : path (or ) = 3 edges. (2)
Q5. (4 marks) — 1 mark each
- (a) False — Dijkstra's greedy choice fails with negative edges.
- (b) True — a further (V-th) relaxation succeeding indicates a negative cycle.
- (c) False — only for DAGs (acyclic); a cycle prevents topological ordering.
- (d) True — Kruskal uses Union-Find to detect whether adding an edge forms a cycle.
Q6. (5 marks)
- (direct ). (1)
- (via ). (2)
- (via ). (2)
Final: .
Q7. (4 marks) — 1 mark each
- (a) Dijkstra (binary heap): .
- (b) Bellman-Ford: .
- (c) Floyd-Warshall: .
- (d) Kruskal: (dominated by sorting; ).
Q8. (3 marks)
- (a) The maximum flow from source to sink equals the capacity of the minimum - cut. (2)
- (b) Edmonds-Karp: . (1)
Q9. (4 marks)
- Bipartite graph: vertices can be split into two disjoint sets such that every edge connects a vertex in to one in (no edge within a set). (2)
- Test: 2-coloring via BFS/DFS — attempt to color adjacent vertices with alternating colors; bipartite iff no conflict. (1) Complexity . (1)
Q10. (4 marks)
- Amortized time per operation: (inverse Ackermann, effectively constant). (1)
- Path compression: flattens the tree by pointing nodes directly to the root during
find. (1.5) - Union by rank: attaches the smaller/shorter tree under the taller one to keep trees shallow. (1.5)
[
{"claim": "Adjacency list count 7000 < matrix 10^6 for V=1000,E=3000", "code": "V=1000; E=3000; matrix=V*V; lst=V+2*E; result = (lst==7000) and (matrix==1000000) and (lst<matrix)"},
{"claim": "Dijkstra Q6 distances S->A=2,B=3,C=6", "code": "dA=2; dB=min(5,dA+1); dC=min(dA+7,dB+3); result = (dA==2) and (dB==3) and (dC==6)"},
{"claim": "BFS shortest distance A to E is 3 edges", "code": "import collections; adj={'A':['B','C'],'B':['A','D'],'C':['A','D'],'D':['B','C','E'],'E':['D']}; dist={'A':0}; q=collections.deque(['A'])\nwhile q:\n u=q.popleft()\n for w in adj[u]:\n if w not in dist:\n dist[w]=dist[u]+1; q.append(w)\nresult = dist['E']==3"},
{"claim": "Kruskal complexity E log E equals E log V asymptotically for E<=V^2", "code": "import sympy; V=sympy.Symbol('V',positive=True); result = sympy.simplify(sympy.log(V*V)-2*sympy.log(V))==0"}
]