CS2040S Tutorial 8
Name: Ravichandran Gokul
Matriculation No.: A0309353E
Q1a) Since the parent pointer is reflective of the very first time that a
node is visited, even if the graph is cyclic, a node will be marked as visited
and not be visited again. Hence, the parent edges will form a tree. That is
my understanding of this question.
1b) DFS on graphs require the additional check of whether a node has
been visited or not whilst DFS on trees does not require this check. This is
because trees are acyclic. The implementation of DFS on trees is
essentially just in order traversal of trees which takes O(V) time. Iterative
DFS on graphs requires us to use a stack and an adjacency list whilst
recursive DFS on graphs is pretty similar in nature and the whole thing
take us O(V + E) time
For BFS, I am only well versed with the iterative version which requires a
queue. However, for graphs once again you need to perform the
additional visited check. The respective times are similar, O(V) and O(V +
E) for trees and graphs respectively.
Graph BFS/DFS works for trees. If you don’t start with root node, you are
essentially re-rooting the tree.
Q2) To check if the graph is connected, just use BFS/DFS from one node
and visit as many nodes as possible. If number of nodes visited, is equal
to total number of nodes, the graph is connected.
For number of connected components, just count the number of times you
perform BFS/DFS.
3) I think this is a cycle detection problem. My idea is having a Boolean
visited array and then do DFS. Keep count of the number of nodes visited.
If the number of nodes visited hits n, there is a cycle. If we run into a
visited node, there is a cycle. We can therefore prove whether the graph is
a tree or not if all edges have been visited and the node count is less than
n and if we didn’t run into visited nodes. Still O(n) because you break.
4a) For the symptomatic case, you can make symptomatic person A as
the starting node. Check if there exists a path to symptomatic person B by
performing BFS. The people are represented as nodes, and we can have a
field to indicate whether person is infected or not. Ensure BFS only goes
through symptomatic persons.
For scenario two,
b) We could use a bipartite graph, one side with location nodes, another
with patient nodes. Check if the degree of the location nodes is more than
one. Multiply each location by number of days if you want
c) Store jobs as nodes, with start time and end time stored within these
nodes. Augment overlap function within nodes to check if there is overlap.
Create a graph such that an edge between two nodes exists if there is no
overlap, graph to be represented by adjacency list. Find strongly
connected components in the graph using DFS.
d) Store students as nodes, answers as fields within the node. Augment
similar function within nodes to check if answers are similar. Create a
graph such that an edge between two nodes exists if there is similarity,
graph to be represented by adjacency list. Find cliques in the graph.
Nodes part of the clique represent cheaters.
e) Find the set of edges that don’t share endpoints. Matching problem.
5a) Modelling the graph works something like each node had a letter and
a number that represents that node. Form an edge, if the number is
distinct for the letters. Leaf node represents the possible permutations for
the letters and numbers.
b) Use DFS. You can terminate as soon as you have found a valid
permutation.
c) Search finishes once you have found a valid permutation.
6) Connect two nodes if they appear in the same card. If there is a cycle
i.e., not bipartite, it is not consistent. If there is one connected
component, check if the partition of vertices in the bipartite graph is
equal. If equal, it is not sufficient. If there’s more than one connected
component, check if the difference between good and bad in any two
connected components is equal. If so, it is sufficient.