Week 07 Tutorial Notes
Graph Traversal, Graph Problems
Group Question
- Given the following adjacency matrix representation of an undirected graph on the left - draw in the edges for the graph on the right.
Graph
0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 0 1 0 1 0 2 0 1 0 0 1 0 3 0 0 0 0 0 1 4 0 1 1 0 0 0 5 0 0 0 1 0 0
- Given the following undirected graph:
- Draw the adjacency list representation of the graph.
- Perform a Depth First Search starting from node 0, selecting the smallest neighbours first.
- Write down the order the nodes are visited.
- Highlight the edges traversed.
- Write down the predecessors array.
- Using the same graph as Question 2, perform a Breadth First Search starting from node 6.
- Write down the order the nodes are visited.
- Highlight the edges traversed.
- Write down the predecessors array.
- You are given the following connected components array of the Graph from Question 1.
Index 0 1 2 3 4 5 Value 0 1 1 2 1 2
Write pseudocode to find which connected component contains \(k\) nodes.
int containsKNodes(int k, int *connectedComponents, int size, int numCC);
-1 if no components match. Otherwise return the component number that matches \(k\) nodes. If there are multiple components containing \(k\) nodes return any of them.size is the length of the connected components array.
numCC is the total number of connected components present in the connected components array.
containsKNode(3, connectedComponents, 6, 3) returns 1 containsKNode(2, connectedComponents, 6, 3) returns 2 containsKNode(1, connectedComponents, 6, 3) returns 0 containsKNode(0, connectedComponents, 6, 3) returns -1
Group Work Answers:
Click each section to reveal.
-
-
- Order Nodes Visited: 0 2 1 3 6 4 5
-
Predecessor Array
Index 0 1 2 3 4 5 6 Value 0 2 0 1 0 4 1 -
- Order Nodes Visited: 6 1 2 3 0 4 5
-
Predecessor Array
Index 0 1 2 3 4 5 6 Value 2 6 6 1 0 0 6 -
int containsKNodes(int k, int *connectedComponents, int size, int numCC) {
for (int i = 0; i < numCC; i++) {
int counter = 0;
for (int index = 0; index < size; index++) {
if (connectedComponents[index] == i) counter++;
}
if (counter == k) return i;
}
return -1;
}