L59. Graphs: the mathematics of connection
Module 5
2026-10-02 · 14:03 · practice PDF
Practice problems (3)
Problem 1. Given $V = \{1, 2, 3, 4, 5\}$ and $E = \{\{1, 2\}, \{1, 3\}, \{2, 3\}, \{3, 4\}, \{4, 5\}, \{5, 3\}\}$, is this a simple graph? List the degree of each vertex.
Reveal solution
- Check for loops: No edge is of the form $\{u, u\}$.
- Check for multiple edges: Each pair in $E$ is unique.
- Since there are no loops and no multiple edges, it is a simple graph.
- Count edges for each vertex: 1 is in 2 edges, 2 is in 2 edges, 3 is in 4 edges, 4 is in 2 edges, 5 is in 2 edges.
Answer: Yes, it is a simple graph. $\text{deg}(1)=2, \text{deg}(2)=2, \text{deg}(3)=4, \text{deg}(4)=2, \text{deg}(5)=2$.
Problem 2. Consider a directed graph with $V = \{A, B, C\}$ and $E = \{(A, B), (B, C), (C, A), (A, A)\}$. Find the in-degree and out-degree of vertex $A$.
Reveal solution
- In-degree of $A$: Count edges where $A$ is the second element. $(C, A)$ and $(A, A)$ both end at $A$.
- Out-degree of $A$: Count edges where $A$ is the first element. $(A, B)$ and $(A, A)$ both start at $A$.
- The loop $(A, A)$ contributes to both.
Answer: $\text{in-deg}(A) = 2, \text{out-deg}(A) = 2$.
Problem 3. A multigraph has 4 vertices. Vertex 1 has degree 3, Vertex 2 has degree 3, Vertex 3 has degree 2, and Vertex 4 has degree 2. Is it possible for this graph to be a simple graph if Vertex 1 is connected to all other vertices?
Reveal solution
- If Vertex 1 is connected to all other vertices (2, 3, and 4) in a simple graph, it must have exactly one edge to each.
- This would give Vertex 1 a degree of 3, which matches the prompt.
- Now check Vertex 2. It has degree 3. It is already connected to Vertex 1. It needs 2 more edges.
- The only remaining vertices are 3 and 4. If it connects to both, it reaches degree 3.
- Now check Vertex 3. It is connected to 1 and 2. Its degree is now 2. It cannot have any more edges.
- Now check Vertex 4. It is connected to 1 and 2. Its degree is now 2. It cannot have any more edges.
- This configuration satisfies all degree requirements without loops or multiple edges.
Answer: Yes, it is possible.
L60. The handshake lemma — first graph proofs
Module 5
2026-10-03 · 12:11 · practice PDF
Practice problems (3)
Problem 1. Does a graph exist with six vertices having degrees $4, 4, 3, 3, 2, 2$?
Reveal solution
- First, check the number of odd-degree vertices. The degrees are $3$ and $3$, so there are $2$ odd-degree vertices. Since $2$ is even, the parity condition is satisfied.
- Next, calculate the sum of degrees: $4 + 4 + 3 + 3 + 2 + 2 = 18$.
- Apply the Handshake Lemma: $2|E| = 18$, which means $|E| = 9$.
- Since the sum is even and the number of odd vertices is even, such a graph can exist.
Answer: Yes
Problem 2. A graph has $15$ edges. If $5$ vertices have degree $4$, what is the average degree of the remaining vertices if there are $10$ vertices in total?
Reveal solution
- Total sum of degrees is $2 \times |E| = 2 \times 15 = 30$.
- The sum of degrees for the first $5$ vertices is $5 \times 4 = 20$.
- The remaining sum for the other $5$ vertices is $30 - 20 = 10$.
- The average degree of the remaining $5$ vertices is $10 / 5 = 2$.
Answer: 2
Problem 3. Prove that in any group of people, the number of people who have shaken hands an odd number of times is always even.
Reveal solution
- Represent the group as a graph where vertices are people and edges are handshakes.
- The number of handshakes a person has made is the degree of their vertex.
- By the Handshake Lemma, $\sum \text{deg}(v) = 2|E|$, so the total sum of degrees is even.
- The sum of degrees of even-degree vertices is always even.
- Therefore, the sum of degrees of odd-degree vertices must also be even to keep the total sum even.
- A sum of odd numbers is even if and only if there is an even number of terms.
Answer: The number of odd-degree vertices must be even.
L61. Special graphs bestiary
Module 5
2026-10-04 · 12:13 · practice PDF
Practice problems (3)
Problem 1. Calculate the number of edges in the complete bipartite graph $B_{5,7}$.
Reveal solution
- Identify the sizes of the two vertex sets: $m = 5$ and $n = 7$.
- Use the formula for edges in a complete bipartite graph: $|E| = m \times n$.
- Compute $5 \times 7 = 35$.
Answer: 35
Problem 2. A graph $G$ is a hypercube $Q_4$. How many vertices does it have, and what is the degree of each vertex?
Reveal solution
- The number of vertices in $Q_n$ is $2^n$. For $n=4$, $|V| = 2^4 = 16$.
- The degree of every vertex in $Q_n$ is $n$. For $n=4$, $\text{deg}(v) = 4$.
Answer: 16 vertices, degree 4
Problem 3. Determine the number of edges in a complete graph $K_8$ and verify it using the Handshake Lemma.
Reveal solution
- Using the combination formula: $|E| = \binom{8}{2} = \frac{8 \times 7}{2} = 28$.
- Using the Handshake Lemma: each of the 8 vertices has degree $8-1 = 7$.
- Sum of degrees is $8 \times 7 = 56$.
- Number of edges is $56 / 2 = 28$.
Answer: 28
L62. Graph isomorphism — when are two graphs the same?
Module 5
2026-10-05 · 12:22 · practice PDF
Practice problems (3)
Problem 1. Graph $G_1$ has vertices $\{1,2,3,4,5\}$ and edges $\{(1,2), (2,3), (3,4), (4,5), (5,1), (1,3)\}$. Graph $G_2$ has vertices $\{A,B,C,D,E\}$ and edges $\{(A,B), (B,C), (C,D), (D,E), (E,A), (A,C)\}$. Are they isomorphic?
Reveal solution
- Step 1: Count vertices and edges. Both have 5 vertices and 6 edges.
- Step 2: Compute degree sequences. In $G_1$, vertex 1 has degree 3, vertex 3 has degree 3, and others have degree 2. Sequence: $(3,3,2,2,2)$. In $G_2$, vertex A has degree 3, vertex C has degree 3, and others have degree 2. Sequence: $(3,3,2,2,2)$.
- Step 3: Try a mapping. Map $1 \to A, 3 \to C, 2 \to B, 4 \to D, 5 \to E$.
- Step 4: Check edges. $(1,2) \to (A,B)$, $(2,3) \to (B,C)$, $(3,4) \to (C,D)$, $(4,5) \to (D,E)$, $(5,1) \to (E,A)$, $(1,3) \to (A,C)$. All edges match.
Answer: Yes, they are isomorphic.
Problem 2. Two graphs $G_1$ and $G_2$ both have 6 vertices and a degree sequence of $(3,3,3,3,2,2)$. $G_1$ is connected, but $G_2$ consists of two disjoint components. Are they isomorphic?
Reveal solution
- Step 1: Check basic invariants. Both have 6 vertices and the same degree sequence.
- Step 2: Check connectivity. Connectivity is a graph invariant.
- Step 3: Since $G_1$ is connected and $G_2$ is disconnected, they cannot be isomorphic.
Answer: No, they are not isomorphic.
Problem 3. Graph $G_1$ is a cycle $C_6$. Graph $G_2$ is two disjoint cycles $C_3 \cup C_3$. Prove they are not isomorphic using a structural invariant other than connectivity.
Reveal solution
- Step 1: Check degree sequences. Both have all vertices of degree 2.
- Step 2: Look for the smallest cycle (the girth).
- Step 3: In $G_1$, the smallest cycle is of length 6.
- Step 4: In $G_2$, the smallest cycle is of length 3.
- Step 5: Since the length of the shortest cycle is an invariant and they differ, the graphs are not isomorphic.
Answer: Not isomorphic because $G_2$ contains $C_3$ while $G_1$ does not.
L63. Adjacency matrices; counting walks with matrix powers
Module 5
2026-10-06 · 11:41 · practice PDF
Practice problems (3)
Problem 1. Let $G$ be the cycle graph $C_4$ with vertices $1, 2, 3, 4$ connected in a ring. Construct the adjacency matrix $A$ and compute $(A^3)_{11}$.
Reveal solution
- The adjacency matrix $A$ has $A_{12}=A_{21}=A_{23}=A_{32}=A_{34}=A_{43}=A_{41}=A_{14}=1$, and all other entries $0$.
- Compute $A^2$: the diagonal entries are the degrees, so $(A^2)_{ii} = 2$. The off-diagonal entries $(A^2)_{13} = A_{12}A_{23} + A_{14}A_{43} = 1+1 = 2$.
- Compute $(A^3)_{11}$ as the dot product of row 1 of $A^2$ and column 1 of $A$. Row 1 of $A^2$ is $[2, 0, 2, 0]$. Column 1 of $A$ is $[0, 1, 0, 1]^T$.
- The dot product is $2(0) + 0(1) + 2(0) + 0(1) = 0$.
Answer: 0
Problem 2. For a simple graph $G$, prove that the number of walks of length 2 from vertex $v_i$ to $v_j$ (where $i \neq j$) is equal to the number of common neighbors of $v_i$ and $v_j$.
Reveal solution
- A walk of length 2 from $v_i$ to $v_j$ is a sequence $v_i \to v_m \to v_j$.
- This walk exists if and only if $v_m$ is adjacent to both $v_i$ and $v_j$.
- Such a vertex $v_m$ is by definition a common neighbor of $v_i$ and $v_j$.
- The number of such walks is the number of such vertices $v_m$.
Answer: The number of walks is exactly the number of common neighbors.
Problem 3. A graph $G$ has adjacency matrix $A$. If the trace of $A^2$ is 21 and the graph is 3-regular, how many vertices does the graph have?
Reveal solution
- We know that $(A^2)_{ii} = \text{deg}(v_i)$.
- The trace of $A^2$ is the sum of the degrees of all vertices: $\text{tr}(A^2) = \sum \text{deg}(v_i)$.
- For a 3-regular graph with $n$ vertices, every vertex has degree 3, so $\text{tr}(A^2) = 3n$.
- Setting $3n = 21$ gives $n = 7$.
Answer: 7
L64. Connectivity; components via equivalence relations
Module 5
2026-10-07 · 12:01 · practice PDF
Practice problems (3)
Problem 1. Given a graph with $V = \{1, 2, 3, 4, 5\}$ and $E = \{(1,2), (2,3), (4,5)\}$, how many connected components does it have? List the vertices in each component.
Reveal solution
- Start at vertex 1. It is connected to 2, and 2 is connected to 3. This forms the first component: $\{1, 2, 3\}$.
- Look for unvisited vertices. Vertex 4 is unvisited. It is connected to 5. This forms the second component: $\{4, 5\}$.
- All vertices are now visited.
Answer: 2 components: $\{1, 2, 3\}$ and $\{4, 5\}$
Problem 2. A simple graph has 12 vertices and 8 edges. What is the minimum number of connected components this graph must have?
Reveal solution
- Use the formula $|E| \ge n - k$, where $n$ is the number of vertices and $k$ is the number of components.
- Substitute the known values: $8 \ge 12 - k$.
- Rearrange the inequality to solve for $k$: $k \ge 12 - 8$.
- This gives $k \ge 4$.
Answer: 4
Problem 3. Prove that if a graph $G$ is the disjoint union of two connected graphs $G_1$ and $G_2$, then $G$ is disconnected.
Reveal solution
- Let $u$ be a vertex in $G_1$ and $v$ be a vertex in $G_2$.
- By the definition of disjoint union, there are no edges in $G$ that connect a vertex in $G_1$ to a vertex in $G_2$.
- Since any path from $u$ to $v$ would require at least one edge crossing from $G_1$ to $G_2$, no such path exists.
- Because there exists at least one pair of vertices $(u, v)$ with no path between them, $G$ is disconnected by definition.
Answer: The absence of edges between $G_1$ and $G_2$ ensures no path exists between their vertices.
L65. Eulerian paths and circuits — Konigsberg solved
Module 5
2026-10-08 · 12:36 · practice PDF
Practice problems (3)
Problem 1. A connected graph has 5 vertices with degrees 2, 4, 2, 2, and 2. Does it have an Eulerian circuit? Does it have an Eulerian path?
Reveal solution
- Check the degrees of all vertices: 2, 4, 2, 2, 2.
- Observe that every single degree is an even number.
- According to the theorem, if all vertices in a connected graph have even degrees, an Eulerian circuit exists.
- Since every Eulerian circuit is also an Eulerian path, both exist.
Answer: Yes to both.
Problem 2. A connected graph has 6 vertices. Four vertices have degree 3 and two vertices have degree 2. Does it have an Eulerian path?
Reveal solution
- Count the number of odd-degree vertices.
- The vertices with degree 3 are odd. There are 4 such vertices.
- The theorem states an Eulerian path exists if and only if there are 0 or 2 odd-degree vertices.
- Since there are 4 odd-degree vertices, no Eulerian path exists.
Answer: No.
Problem 3. Consider a complete graph $K_5$. Does it contain an Eulerian circuit? Justify your answer.
Reveal solution
- In a complete graph $K_n$, every vertex is connected to every other vertex, so $\deg(v) = n - 1$.
- For $K_5$, $n = 5$, so $\deg(v) = 5 - 1 = 4$ for all vertices.
- Since all vertices have a degree of 4, which is even, and the graph is connected, it satisfies the condition for an Eulerian circuit.
Answer: Yes, because all vertices have degree 4.
L66. Hamiltonian paths — the deceptive twin
Module 5
2026-10-09 · 12:39 · practice PDF
Practice problems (3)
Problem 1. A graph has $n = 7$ vertices. Every vertex has a degree of at least $4$. Does this graph necessarily contain a Hamiltonian cycle? Justify using Dirac's theorem.
Reveal solution
- Identify $n = 7$.
- Calculate the Dirac threshold: $n/2 = 7/2 = 3.5$.
- Check if the minimum degree $\delta(G) \ge 3.5$.
- Since $4 \ge 3.5$, the condition is satisfied.
Answer: Yes, it must contain a Hamiltonian cycle.
Problem 2. Consider a cycle graph $C_5$ (a pentagon). Does it satisfy the conditions of Dirac's theorem? Is it Hamiltonian?
Reveal solution
- For $C_5$, $n = 5$.
- The degree of every vertex is $2$.
- The Dirac threshold is $5/2 = 2.5$.
- Since $2 < 2.5$, Dirac's theorem does not apply.
- However, by definition, a cycle graph $C_n$ is its own Hamiltonian cycle.
Answer: It does not satisfy Dirac's theorem, but it is still Hamiltonian.
Problem 3. A graph has $n = 6$ vertices. One vertex has degree $1$, and the other five have degree $5$. Can this graph have a Hamiltonian cycle? Why or why not?
Reveal solution
- A Hamiltonian cycle must enter and leave every vertex using two distinct edges.
- This means every vertex in a Hamiltonian cycle must have a degree of at least $2$.
- Since one vertex has degree $1$, it is impossible to enter and leave it without reusing an edge or vertex.
- Therefore, no Hamiltonian cycle can exist.
Answer: No, because a vertex of degree $1$ cannot be part of a cycle.
L67. Trees I — five equivalent definitions
Module 5
2026-10-10 · 12:58 · practice PDF
Practice problems (3)
Problem 1. A graph $G$ has $6$ vertices and $5$ edges. If $G$ is connected, is it a tree? Explain using the definitions learned today.
Reveal solution
- Step 1: Identify the number of vertices $n = 6$.
- Step 2: Identify the number of edges $|E| = 5$.
- Step 3: Check the condition $|E| = n - 1$. Here, $5 = 6 - 1$, which is true.
- Step 4: Since the graph is connected and has $n - 1$ edges, it satisfies one of the equivalent definitions of a tree.
Answer: Yes, it is a tree.
Problem 2. Suppose a graph $G$ with $n$ vertices is acyclic and has $n - 1$ edges. Prove that $G$ must be connected and therefore a tree.
Reveal solution
- Step 1: A graph with $n$ vertices and $k$ connected components has at least $n - k$ edges.
- Step 2: For an acyclic graph, the number of edges is exactly $n - k$.
- Step 3: We are given $|E| = n - 1$.
- Step 4: Setting $n - k = n - 1$ gives $k = 1$.
- Step 5: Since there is only one connected component, the graph is connected.
Answer: The graph is connected because $k=1$.
Problem 3. Show that if a graph $G$ is a tree, then adding any edge between two non-adjacent vertices $u$ and $v$ creates exactly one cycle.
Reveal solution
- Step 1: Since $G$ is a tree, there exists a unique path $P$ between $u$ and $v$.
- Step 2: Adding the edge $(u, v)$ creates a cycle consisting of the path $P$ and the new edge $(u, v)$.
- Step 3: If there were a second cycle, there would have to be another path between $u$ and $v$ in the original tree.
- Step 4: But the path in a tree is unique, so only one cycle can be formed.
Answer: The unique path $P$ plus the new edge forms the only possible cycle.
L68. Trees II — Cayley's formula via the Prufer bijection
Module 5
2026-10-11 · 12:12 · practice PDF
Practice problems (3)
Problem 1. Find the Prufer sequence for the path graph on 6 vertices with edges $(1,2), (2,3), (3,4), (4,5), (5,6)$.
Reveal solution
- Smallest leaf is 1, neighbour is 2. Sequence: (2), Tree: {2,3,4,5,6}
- Smallest leaf is 2, neighbour is 3. Sequence: (2,3), Tree: {3,4,5,6}
- Smallest leaf is 3, neighbour is 4. Sequence: (2,3,4), Tree: {4,5,6}
- Smallest leaf is 4, neighbour is 5. Sequence: (2,3,4,5), Tree: {5,6}
- Stop when 2 vertices remain.
Answer: (2, 3, 4, 5)
Problem 2. How many distinct labelled trees can be formed using 6 vertices?
Reveal solution
- Use Cayley's formula $T_n = n^{n-2}$
- Substitute $n = 6$
- Compute $6^{6-2} = 6^4$
- $6^4 = 1296$
Answer: 1296
Problem 3. A labelled tree on 5 vertices has a Prufer sequence $(3, 3, 1)$. What is the degree of vertex 3 in this tree?
Reveal solution
- Use the property that the number of occurrences of vertex $v$ in the sequence is $\deg(v) - 1$.
- Vertex 3 appears 2 times in the sequence $(3, 3, 1)$.
- Set $2 = \deg(3) - 1$.
- Solve for $\deg(3) = 3$.
Answer: 3
L69. Spanning trees; Kruskal's and Prim's algorithms run live
Module 5
2026-10-12 · 11:00 · practice PDF
Practice problems (3)
Problem 1. Given a graph with vertices $V = \{1, 2, 3, 4, 5\}$ and edges $E = \{(1,2,2), (1,3,3), (2,3,1), (2,4,4), (3,4,5), (4,5,1)\}$, find the total weight of the minimum spanning tree.
Reveal solution
- Sort edges: $(2,3,1), (4,5,1), (1,2,2), (1,3,3), (2,4,4), (3,4,5)$.
- Pick $(2,3,1)$. Total weight = 1.
- Pick $(4,5,1)$. Total weight = 2.
- Pick $(1,2,2)$. Total weight = 4.
- Check $(1,3,3)$: adding it creates cycle $(1,2,3)$. Discard.
- Pick $(2,4,4)$. Total weight = 8. All vertices connected.
Answer: 8
Problem 2. In a connected graph where all edge weights are equal to $k$, what is the total weight of any minimum spanning tree with $n$ vertices?
Reveal solution
- Any spanning tree of a graph with $n$ vertices must have exactly $n-1$ edges.
- Since all edges have the same weight $k$, every spanning tree has the same total weight.
- The total weight is the number of edges multiplied by the weight per edge.
Answer: (n-1)k
Problem 3. Consider a graph where all edge weights are distinct. If we add a new edge with a weight smaller than any existing edge in the MST, will the MST change? Explain.
Reveal solution
- Let the new edge be $e$ with weight $w(e) < w(e')$ for all $e' \in MST$.
- Adding $e$ to the MST creates a cycle $C$.
- Since $e$ is the lightest edge in the graph, it must be the lightest edge in $C$.
- Removing any other edge $f$ from $C$ results in a new spanning tree with weight $W(MST) - w(f) + w(e)$.
- Since $w(e) < w(f)$, the total weight decreases, so the MST must change to include $e$.
Answer: Yes, the MST will change to include the new edge.
L70. Shortest paths — Dijkstra's algorithm, watched step by step
Module 5
2026-10-13 · 12:41 · practice PDF
Practice problems (3)
Problem 1. Given a graph with vertices $V = \{1, 2, 3, 4, 5\}$ and edges $E = \{(1,2,10), (1,3,3), (3,2,4), (2,4,2), (3,4,8), (3,5,2), (5,4,4)\}$, find the shortest distance from vertex $1$ to vertex $4$.
Reveal solution
- Start at 1: $d(1)=0$, others $\infty$. Relax neighbors: $d(2)=10, d(3)=3$.
- Visit 3 (min distance 3): Relax neighbors: $d(2)=\min(10, 3+4)=7, d(4)=\min(\infty, 3+8)=11, d(5)=\min(\infty, 3+2)=5$.
- Visit 5 (min distance 5): Relax neighbors: $d(4)=\min(11, 5+4)=9$.
- Visit 2 (min distance 7): Relax neighbors: $d(4)=\min(9, 7+2)=9$.
- Visit 4 (min distance 9): No unvisited neighbors.
Answer: 9
Problem 2. In the same graph as Problem 1, what is the shortest path from vertex $1$ to vertex $4$? List the vertices in order.
Reveal solution
- From the distances in Problem 1, $d(4)=9$.
- Check predecessors: $d(4)$ was reached via vertex 5 ($5+4=9$) or vertex 2 ($7+2=9$).
- If via 2: $d(2)=7$ was reached via vertex 3 ($3+4=7$). $d(3)=3$ was reached via vertex 1.
- Path 1: $1 \to 3 \to 2 \to 4$.
- If via 5: $d(5)=5$ was reached via vertex 3 ($3+2=5$). $d(3)=3$ was reached via vertex 1.
- Path 2: $1 \to 3 \to 5 \to 4$.
Answer: 1 \to 3 \to 2 \to 4 \text{ or } 1 \to 3 \to 5 \to 4
Problem 3. Suppose a graph has a negative edge weight $w(u, v) = -5$. Explain why the greedy choice in Dijkstra's algorithm might fail to find the shortest path to $v$.
Reveal solution
- Dijkstra's algorithm assumes that once a vertex $u$ is visited, its distance $d(u)$ is the absolute minimum.
- If there is a negative edge later in the graph, a path with more edges could potentially have a lower total weight than the current minimum.
- Because Dijkstra's never revisits a 'visited' vertex to update its distance, it will miss any improvements offered by negative edges encountered after the vertex was locked in.
Answer: The greedy assumption that visited vertices have finalized distances is violated by negative weights.
L71. Breadth-first and depth-first search
Module 5
2026-10-14 · 10:58 · practice PDF
Practice problems (3)
Problem 1. Given a graph with vertices $V = \{1, 2, 3, 4, 5\}$ and edges $E = \{(1,2), (1,3), (2,4), (3,4), (3,5)\}$, what is the order of discovery using Breadth-First Search starting from node $1$? Assume neighbors are visited in increasing numerical order.
Reveal solution
- Start at node $1$. Mark $1$ as visited. Queue: $[1]$.
- Pop $1$. Neighbors are $2$ and $3$. Add them to queue. Queue: $[2, 3]$. Visited: $\{1, 2, 3\}$.
- Pop $2$. Neighbor is $4$. Add to queue. Queue: $[3, 4]$. Visited: $\{1, 2, 3, 4\}$.
- Pop $3$. Neighbors are $4$ and $5$. $4$ is visited, so add $5$. Queue: $[4, 5]$. Visited: $\{1, 2, 3, 4, 5\}$.
- Pop $4$. No unvisited neighbors.
- Pop $5$. No unvisited neighbors.
Answer: 1, 2, 3, 4, 5
Problem 2. Using the same graph $V = \{1, 2, 3, 4, 5\}$ and $E = \{(1,2), (1,3), (2,4), (3,4), (3,5)\}$, what is the order of discovery using Depth-First Search starting from node $1$? Assume neighbors are visited in increasing numerical order.
Reveal solution
- Start at node $1$. Mark $1$ as visited.
- Visit neighbor $2$. Mark $2$ as visited.
- From $2$, visit neighbor $4$. Mark $4$ as visited.
- From $4$, visit neighbor $3$. Mark $3$ as visited.
- From $3$, visit neighbor $5$. Mark $5$ as visited.
- No more unvisited neighbors; backtrack to $3$, then $4$, then $2$, then $1$.
Answer: 1, 2, 4, 3, 5
Problem 3. In a graph with $100$ nodes, if you are looking for a target node that is known to be very deep in the graph (far from the start), which algorithm is generally more memory-efficient, and why?
Reveal solution
- BFS must store the entire 'frontier' of the search. In a graph with a high branching factor, the number of nodes in the queue grows exponentially with depth.
- DFS only needs to store the current path from the start node to the current node, plus the untried neighbors of nodes on that path.
- Since the target is deep, BFS would likely store a massive number of nodes at intermediate levels before reaching the target.
- DFS only stores a linear amount of data relative to the maximum depth of the graph.
Answer: DFS, because it only stores the current path (linear space) rather than the entire frontier (exponential space).
L72. Module 5 review + self-test
Module 5
2026-10-15 · 12:28 · practice PDF
Practice problems (3)
Problem 1. Determine if a graph exists with the degree sequence $3, 3, 3, 2, 2, 1$. If so, how many edges does it have?
Reveal solution
- Sum the degrees: $3 + 3 + 3 + 2 + 2 + 1 = 14$.
- Check if the sum is even: $14$ is even, so the handshake lemma is satisfied.
- Calculate edges: $|E| = 14 / 2 = 7$.
Answer: Yes, it exists with 7 edges.
Problem 2. A weighted graph has vertices $A, B, C, D$ and edges $(A,B,2), (B,C,3), (C,D,1), (D,A,4), (A,C,5)$. Find the total weight of the Minimum Spanning Tree using Kruskal's algorithm.
Reveal solution
- Sort edges: $(C,D,1), (A,B,2), (B,C,3), (D,A,4), (A,C,5)$.
- Pick $(C,D,1)$.
- Pick $(A,B,2)$.
- Pick $(B,C,3)$. Now we have 3 edges for 4 vertices, and no cycles.
- Total weight: $1 + 2 + 3 = 6$.
Answer: 6
Problem 3. In an unweighted graph, you start at node $S$. You want to find the shortest path to node $T$. Which algorithm is more efficient for this specific goal: BFS or DFS? Explain why.
Reveal solution
- BFS explores the graph in layers (breadth-first).
- The first time BFS reaches node $T$, it is guaranteed to have taken the path with the minimum number of edges.
- DFS explores as deep as possible and may find a very long path to $T$ before ever finding the shortest one.
- Therefore, BFS is the correct choice for shortest paths in unweighted graphs.
Answer: BFS, because it explores layer by layer and guarantees the shortest path in unweighted graphs.