2026-02-26
Implement graph traversal algorithms (BFS, DFS) in C++.
Determine reachability between nodes in a graph.
Perform topological sorting on directed acyclic graphs (DAGs).
Identify connected components using BFS/DFS in C++.
This lab consists of three main activities:
A real-world application of reachability queries is determining if you can travel from one station to another in a transportation network. For example, in the Singapore (MRT) system, you might want to know if there’s a path from Dhoby Ghaut to Bencoolen.

In this graph representation: Vertices represent MRT stations. Edges represent train lines connecting stations. A reachability query answers: “Can I get from station \(s\) to station \(t\)”?
You are given an unweighted undirected graph \(G = (V, E)\) in form of edges. Your task is to decide whether there is a path from a source vertex \(s\) to a target vertex \(t\).

bool validPath(int n, vector<vector<int>> &edges, int source, int destination);reachable using BFS or DFS so that it runs in \(O(|V| + |E|)\).Submission (Gradescope, 20 marks). Upload your completed valid_path.cpp on Gradescope.
true immediately when the destination is found.false if all reachable vertices have been explored (queue/stack is exhausted or recursion completes) without finding the destination.Topological sorting is useful in many real-world applications:
A Directed Acyclic Graph (DAG) is a directed graph with no cycles. This property is essential for topological sorting - if a graph contains cycles, no valid topological ordering exists.
Example of a Directed Acyclic Graph (DAG): 
One of many valid DFS trees on the DAG, assuming shirt is the source vertex: 
Topological sort (or topological ordering) is a linear ordering of the vertices of a directed acyclic graph (DAG) such that for every directed edge \((u, v)\), vertex \(u\) comes before vertex \(v\) in the ordering.
Topological sort can be performed using DFS. The key insight is to process nodes in reverse order of finishing times during DFS.
The resulted topological sorting: 
The valid topological order might not be unique because DFS can produce different orderings depending on the starting node and the order of neighbor selection. However, any topological order produced will always remain valid (a node will appear before its dependent nodes).
TOPOLOGICAL-SORT(G):
1. Initialize an empty list L for the topological order
2. For each vertex v in G:
3. If v is not visited:
4. DFS-VISIT(G, v, L)
5. Return L (reversed)
DFS-VISIT(G, v, L):
1. Mark v as visited
2. For each neighbor u of v:
3. If u is not visited:
4. DFS-VISIT(G, u, L)
5. Add v to the front of L (or push to stack)
Model “getting dressed in the morning” as a DAG.
Underwear.Submission (xSITe Dropbox, 10 marks). Upload your DFS spanning tree with visiting and finishing times, and the corresponding topological order.
A connected component of an undirected graph is a maximal set of vertices such that there is a path between every pair of vertices in the set. In other words, all vertices in a connected component are reachable from each other.

Connected Components Example: The graph above has 2 connected components.
Connected components have numerous real-world applications:
You are given an undirected graph with \(n\) vertices and a list of edges. Your task is to count the number of connected components in the graph.
connected_component.cpp contains a stub for function int countComponents(int n, vector<vector<int>> &edges);countComponents to return the number of connected components in the graph.Submission (Gradescope, 30 marks). Upload your completed connected_component.cpp file on Gradescope.
visited array and current_component_id.current_component_id.For directed graphs, we use the concept of Strongly Connected Components (SCC). A strongly connected component is a maximal set of vertices such that for every pair of vertices \(u\) and \(v\) in the set, there is a directed path from \(u\) to \(v\) and from \(v\) to \(u\).

Strongly Connected Component Example: A single SCC in a directed graph.

Multiple SCCs Example: A directed graph with multiple strongly connected components.
By the end of this lab you should be able to:
Week 08: Elementary Graphs (Lab)