You are given a connected undirected unweighted graph of ‘N’ nodes and ‘M’ edges, such that there is only one undirected edge between any two nodes and no self-loop. Your task is to find the length of the shortest path that visits all ‘N’ nodes at least once.
Note :
1) Here length of the path refers to the number of edges in that path.
2) You can start and stop at any node.
3) You can visit any node 0 or more times.
4) You can use any given edge 0 or more times.
Input Format :
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, where ‘N’ denotes the total number of nodes and ‘M’ denotes the total number of edges in the graph.
The next ‘M’ lines contain two space-separated integers, ‘U’ and ‘V’, where ‘U’ and ‘V’ represent the nodes that share a common undirected edge between them.
Output Format :
For each test case, print the length of the shortest path that visits all ‘N’ nodes in the given graph.
The output of each test case will be printed in a separate line.
Constraints :
1 <= T <= 5
1 <= N <= 10
N - 1 <= M <= N * (N - 1) / 2
0 <= U, V <= N - 1
Where ‘T’ is the number of test cases, ‘N’ denotes the total number of nodes and ‘M’ denotes the total number of edges in the graph, and ‘U’ and ‘V’ denotes the nodes that share a common undirected edge between them.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
2
3
3
0 1
0 2
1 2
4
3
0 1
1 2
2 3
2
3
Test Case 1 :
The given graph is shown below.

One possible shortest path that starts from node 0 ends at node 2 and visits all nodes is [0 - 1 - 2]
The number of edges in this path is 2.
So the length of the shortest path that visits all nodes in the given graph will be 2.
Test Case 2 :
The given graph is shown below.
One possible shortest path that starts from node 0 ends at node 3 and visits all nodes is [0 - 1 - 2 - 3]
The number of edges in this path is 3.
So the length of the shortest path that visits all nodes in the given graph will be 3.
1
5
8
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
4
Test Case 1 :
The given graph is shown below.
One possible shortest path that starts from node 3 ends at node 4 and visits all nodes is shown below. The edges marked in red-colored are the edges that are traversed in the path.

The number of edges in the path [3 - 1 - 2 - 0 - 4] is 4.
So the length of the shortest path that visits all nodes in the given graph will be 4.
Do a depth-first search on the given graph and try all possible paths that visit all the ‘N’ nodes.
The idea here is to do a depth-first search from all ‘N’ nodes and try all possible paths that visit all the nodes in the given graph. Out of all possible paths that start from any node ‘i’ and end at any node ‘j’, where ‘i’ and ‘j’ are from 0 to ‘N’ - 1 inclusive, we will take the path that has the shortest length, i.e., the path with the minimum number of edges and visits all the ‘N’ nodes in the given graph.
This function is used to traverse the given graph and find the length of the shortest path that visits all the ‘N’ nodes.
This function will take four parameters :
int depthFirstSearch(node, visited, inProcess, adjList)
O(N ^ N + M), where ‘N’ is the number of nodes and ‘M’ is the number of edges in the given graph.
Here, building the adjacency list will take O(M) time since we have ‘M’ edges to insert in the adjacency list.
In the worst case, we can have 2 * N - 1 nodes in the shortest path, this case occurs when the given graph is a star graph, and for every node, we have ‘N’ choices. Therefore there will be approximately N ^ N different possible paths.
Hence, the overall time complexity will be O(M) + O(N ^ N), which is O(N ^ N + M).
O(M + N + (N * 2 ^ N)), where ‘N’ is the number of nodes and ‘M’ is the number of edges in the given graph.
We are using an adjacency list that takes O(M) space to insert ‘M’ edges into the list. Moreover, we need O(N) extra space in the form of recursive calls for depth-first search when the given graph is a path graph. Also, O(N * (2 ^ N)) space is taken by the ‘inProcess’ array.
Hence, the overall space complexity will be O(M) + O(N) + O(N * (2 ^ N)), which is O(M + N + N * (2 ^ N)).