Problem of the day
You are given a reference/address of a node in a connected undirected graph containing N nodes and M edges. You are supposed to return a clone of the given graph which is nothing but a deep copy. Each node in the graph contains an integer “data” and an array/list of its neighbours.
The structure of the graphNode class is as follows:
class graphNode
{
public:
int data;
vector<graphNode*> neighbours;
}
Note :
1. Nodes are numbered from 1 to N.
2. Your solution will run on multiple test cases. If you are using global variables make sure to clear them.
The first line of input contains an integer 'T' representing the number of the test case. Then the test cases are as follows.
The first line of each test case contains a single integer ‘N’ representing the number of nodes in the graph.
The second line of each test case contains a single integer ‘M’ representing the number of edges.
The next ‘M’ lines in each test case contain two integers ‘U’ and ‘V’ separated by a single space denoting an undirected edge between nodes U and V.
Output Format :
For each test case, print a single line containing "true" if the graph is cloned correctly otherwise it will print "false".
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 100000
1 <= M <= min(N(N-1)/2,100000)
1 <= E[i][0], E[i][1] <= N
Where ‘N’ is the number of nodes in the given graph, ‘M’ denotes the number of edges and ‘E’ denotes the edge matrix.
Time Limit: 1 sec.
2
5
6
1 2
4 1
2 4
3 4
5 2
1 3
3
2
1 2
1 3
true
true
In the first test case, the returned graph contains 5 nodes and 6 edges which are:
1 2
4 1
2 4
3 4
5 2
1 3
Since it is similar to the given graph with different address nodes then the solution is correct.
In the second test case, the returned graph contains 3 nodes and 2 edges which are:
1 2
1 3
Since it is similar to the given graph with different address nodes then the solution is correct.
2
5
4
1 2
2 3
3 4
4 5
2
1
1 2
true
true
In the first test case, the returned graph contains 5 nodes and 4 edges which are:
1 2
2 3
3 4
4 5
Since it is similar to the given graph with different address nodes then the solution is correct.
In the second test case, the returned graph contains 2 nodes and 1 edge which is:
1 2
Since it is similar to the given graph with different address nodes then the solution is correct.
Think of BFS graph traversal algorithm.
To clone a graph, we will need to traverse it. The approach here is to use BFS graph traversal. To clone a graph, you need to have a copy of each node and you need to avoid copying the same node multiple times. So we need a mapping from an original node to its copy.
The steps are as follows :
O(N + M), where ‘N’ is the number of nodes and ‘M’ is the number of edges.
In order to clone a graph, we visit each node and traverse each edge exactly once. The Breadth-First Search traversal takes O(N+M) time. Push and pop operations in a queue take constant time. Thus, the overall time complexity will be O(N+M).
O(N), where ‘N’ is the number of nodes.
The queue in the BFS approach takes the space equivalent to the width of the graph and in the worst-case scenario, it can go up to O(N). Thus the overall space complexity will be O(N).