Data Structures and Algorithms · Unit 9
Graphs and Graph Algorithms
Exam-focused notes for Graphs and Graph Algorithms (Data Structures and Algorithms, BIT201): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
Graphs and Graph Algorithms in short
A graph models things and the links between them, such as cities and roads, or people and friendships. Graph algorithms find routes, check connections and build cheap networks.
What this unit covers
- Graph definition and types
- Graph representation using adjacency matrix
- Graph traversal definition
- Breadth first search (BFS) algorithm
- Depth first search (DFS) algorithm
- BFS and DFS tracing with examples
- Shortest path problem definition
- Dijkstra's algorithm for shortest path
- Spanning tree definition
- Minimum spanning tree (MST) definition
- Prim's algorithm for MST
- Round Robin algorithm for MST
BFS and DFS tracing with examples
2082 · 5 marks
Traverse the following graph using BFS and DFS. [5]
Show solved answerHide answer
BFS and DFS Graph Traversal
Given
The question asks to traverse a graph using BFS and DFS, but the actual graph (the figure/diagram) is not included in the text provided. No adjacency list, adjacency matrix, edge set, or vertex set is given.
Missing data: The specific graph (vertices and edges) required to perform the traversal is not available. Without it, the exact traversal orders cannot be determined, since traversal output depends entirely on the graph structure and the chosen starting vertex.
Because the graph is unreadable/absent, I will present the methodology and demonstrate it on a clearly labelled representative example, while flagging that the numeric traversal order is example-dependent and not the "official" answer to the missing figure.
Solution
Example Graph
1
/ \
2 3
/ \ \
4 5 6
Adjacency list:
- $1 \to 2, 3$
- $2 \to 4, 5$
- $3 \to 6$
- $4 \to$ (none)
- $5 \to$ (none)
- $6 \to$ (none)
Breadth First Search (BFS)
Idea: Use a Queue (FIFO); visit level by level.
| Step | Queue (front→back) | Visited | Action |
|---|---|---|---|
| 1 | [1] | {1} | Start at 1 |
| 2 | [2, 3] | {1,2,3} | Dequeue 1, enqueue 2,3 |
| 3 | [3, 4, 5] | {1,2,3,4,5} | Dequeue 2, enqueue 4,5 |
| 4 | [4, 5, 6] | {1,2,3,4,5,6} | Dequeue 3, enqueue 6 |
| 5 | [5, 6] | - | Dequeue 4 (no neighbors) |
| 6 | [6] | - | Dequeue 5 (no neighbors) |
| 7 | [] | - | Dequeue 6 (no neighbors) |
BFS Order: $$\boxed{1 \to 2 \to 3 \to 4 \to 5 \to 6}$$
Depth First Search (DFS)
Idea: Use a Stack (LIFO) or recursion; go deep before backtracking.
| Step | Action | Visited |
|---|---|---|
| 1 | Visit 1 | {1} |
| 2 | Visit 2 (first neighbor of 1) | {1,2} |
| 3 | Visit 4 (first neighbor of 2) | {1,2,4} |
| 4 | Backtrack to 2 (4 dead-end) | {1,2,4} |
| 5 | Visit 5 | {1,2,4,5} |
| 6 | Backtrack to 1 | {1,2,4,5} |
| 7 | Visit 3 | {1,2,4,5,3} |
| 8 | Visit 6 | {1,2,4,5,3,6} |
DFS Order: $$\boxed{1 \to 2 \to 4 \to 5 \to 3 \to 6}$$
Comparison
| Feature | BFS | DFS |
|---|---|---|
| Data structure | Queue (FIFO) | Stack / Recursion |
| Traversal manner | Level by level | Deepest branch first |
| Order (this graph) | $1,2,3,4,5,6$ | $1,2,4,5,3,6$ |
| Typical use | Shortest path (unweighted) | Topological sort, cycle detection |
Note to examiner/student: The traversal orders above are valid only for the assumed example graph. If the real exam figure differs, apply the same procedure (queue for BFS, stack/recursion for DFS) to that graph's adjacency list to obtain the correct order.
Spanning tree definition
2080 · 5 marks
Define spanning tree. Explain minimum spanning tree with example. [5]
Show solved answerHide answer
Spanning Tree and Minimum Spanning Tree
Spanning Tree
A spanning tree of a connected, undirected graph G = (V, E) is a subgraph that:
- Includes all vertices of the graph
- Is a tree (connected and acyclic)
- Has exactly |V| - 1 edges
A graph with n vertices will have a spanning tree with exactly n - 1 edges.
Minimum Spanning Tree (MST)
A Minimum Spanning Tree is a spanning tree of a weighted, connected, undirected graph in which the total sum of edge weights is minimum among all possible spanning trees.
Key Properties:
- Contains all V vertices and exactly V - 1 edges
- No cycles are present
- The total edge weight is minimized
Example
Consider the following weighted graph with 4 vertices:
2
A -------- B
| \ |
6| \ 3 | 4
| \ |
D -------- C
5
Edges and weights:
| Edge | Weight |
|---|---|
| A-B | 2 |
| A-C | 3 |
| B-C | 4 |
| A-D | 6 |
| D-C | 5 |
Finding MST (using Kruskal's approach - pick minimum weight edges without forming cycle):
| Step | Edge Selected | Weight | Cycle? |
|---|---|---|---|
| 1 | A - B | 2 | No |
| 2 | A - C | 3 | No |
| 3 | B - C | 4 | Yes (skip) |
| 4 | D - C | 5 | No |
MST Edges: A-B, A-C, D-C
2
A -------- B
\
3 \
\
C -------- D
5
Total MST Weight = 2 + 3 + 5 = 10
This is the minimum possible weight to connect all 4 vertices, using exactly 4 - 1 = 3 edges.
Common Algorithms to find MST:
- Kruskal's Algorithm - sorts edges by weight and adds them greedily
- Prim's Algorithm - grows the MST one vertex at a time from a starting vertex
Graph representation using adjacency matrix
2079 · 5 marks
Explain adjacency matrix representation of graphs with example. [5]
Dijkstra's algorithm for shortest path
2078 · 10 marks
What is shortest path algorithm? Use Dijkstra's algorithm to find shortest path between the vertices of a and z in the graph given below.[10]
Graph traversal definition
2078 · 5 marks
What is graph traversal? Explain breadth first search. [5]
Prim's algorithm for MST
0 · 5 marks
Trace Prim's Algorithm to find minimum spanning tree for the following graph. [5]
Make Unit 9 stick
Practice BIT201 with flashcards & quizzes