You are given an undirected graph 'EDGE_LIST' of ‘N’ nodes and ‘M’ edges. Your task is to return any Euler path of the graph. If no Euler path exists then you need to return -1.
Note :An Euler path is a path in a graph such that every edge must be visited exactly once. You can visit the same vertex multiple 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 an edge from ‘U’ to ‘V’.
Output Format:
For each test case, the output will be 1 if you have returned the correct Euler path, else it will be 0.
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
1 <= N <= 100
1 <= M <= min(100, N*(N-1)/2)
0 <= U, V <= N - 1
Time Limit: 1 sec
2
3 2
0 1
0 2
4 5
0 1
0 2
0 3
1 2
2 3
1
1
Test Case 1 :
The given graph is shown below.

Euler path = 1 -> 0 -> 2
Also, 2 -> 0 -> 1 is also a valid Euler path.
If we return any one of the above two paths then we will get output as 1.
Test Case 2 :
The given graph is shown below.

Euler path = 2 -> 0 -> 1 -> 2 -> 3 -> 0
If we return the above path then we will get output as 1.
Note : Here we have visited node 0 and 2 multiple times but we have visited each edge exactly once.
2
5 4
0 4
0 1
1 3
1 2
5 4
0 1
1 2
2 3
3 4
1
1
Test Case 1 :
The given graph is shown below.

There is no Euler path possible for the above graph. So, we need to return -1.
Test Case 2 :
The given graph is shown below.

Euler path = 0 -> 1 -> 2 -> 3 -> 4
Try to find the Euler path starting at 'X' for each vertex 'X'.
The idea here is to use Fleury's algorithm. If the graph contains zero or two nodes having an odd degree then the graph has an Euler Path else not. The idea of Fluery’s algorithm is to first start with an odd degree vertex then at each step visit the edge which is a non – bridge egde.
An edge is said to be a non – bridge if after removal of the edge the graph will still remain connected. We are choosing non – bridge edges here because if we choose the edge which is bridges then we can’t come back to our current node.
Below is the detailed algorithm:
Description of Euler path function.
This function is used to store the Euler path in ‘ANSWER’.
This function will take 3 arguments
void eulerPath(‘NODE’, ‘ADJ’, ‘ANSWER’) :
Description of isNonBridge function.
This function is used to check whether the given edge is a non - bridge or not.
This function will take 3 arguments:
bool isNonBridge (‘U’, ‘V’, ‘ADJ’) :
Description of dfs function:
This function is used to count all reachable nodes from a given node.
This function will take 4 arguments
void dfs(‘U’, ‘COUNT’, ‘ADJ’ , ‘VIS’) :
O(N * M), where ‘N’ is the total number of nodes and ‘M’ is the total number of edges in the graph.
Where building a graph takes O(M) time as we need to add ‘M’ edges to the graph. Also, the fluery’s algorithm takes O(N * M) as our Euler path function takes O(M) time as we need to check O(M) edges in the worst case. Also, to check the validity of each edge, we are using a function isNonBridge that is taking O(N) time by calling the dfs function as we need to visit O(N) nodes in the worst case.
Hence, the overall time complexity will be O(M) + O(N * M) = O(N * M).
O(N + M), where ‘N’ is the total number of nodes and ‘M’ is the total number of edges in the graph.
We are using an adjacency list that takes O(M) space as we need to insert ‘M’ edges to the list. Our Euler path function takes O(M) stack space in recursive calls as we can have O(M) edges in the recursive stack. Our dfs functions take O(N) stack space in recursive calls as we can have O(N) nodes in the recursive stack. We are using a visited array that takes O(N) space.
Hence, the overall space complexity will be O(M) + O(M) + O(N) + O(N) = O(N + M).