


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.
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.
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.
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.
You do not need to print anything, it has already been taken care of. Just implement the given function.
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)
The idea here is to do a breadth-first search from all the ‘N’ nodes and return the length of the path(number of edges in that path) that visits all the ‘N’ nodes first, since the given graph is unweighted, doing a breadth-first search from any node ‘i’ to any node ‘j’, where ‘i’ and ‘j’ are from 0 to ‘N’ - 1 inclusive, will give the shortest path between node ‘i’ and ‘j’(refer to this).
The idea here is to use iterative dynamic programming. We will calculate answers from bottom to top. Each state of the dp can be represented as the subset of nodes visited till now plus the current node. We will create a 2 - Dimensional array ‘dp[2 ^ N][N]’ where dp[visited][currNode] will store the length of the shortest path that visits the ‘visited’ subset of nodes and starts from ‘currNode’.