You are given an edge list 'Edges' denoting a directed graph of 'N' nodes, the nodes are numbered from 0 to N - 1. Your task is to find out if the given graph is an eulerian graph or not.
A graph is an eulerian graph if it contains an Euler circuit. Euler circuit is the path in the graph that visits every edge exactly once and starts and finished at the same node.
For example:You are given the ‘Edges’ = [[0,1], [1, 2], [1, 4], [2, 3], [3, 0], [3, 1], [4, 3]], describing the following graph:

Here you can have the path, [0 -> 1 -> 2 -> 3 -> 1 -> 4 -> 3 -> 0], which is using every edge of the graph. Hence the graph is Eulerian and the answer is True.
The first line of input contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, representing the number of nodes and edges in the given graph, respectively.
The following M lines of each test case contains two space-separated integers, Edges[i][0] and Edges[i][1] representing an directed edge in the graph from ‘Edges[i][0]’ to ‘Edges[i][1]’
Output Format:
For each test case, print ‘True’ if the given graph is eulerian, else print 'False'.
Print a separate line for each test case.
1 ≤ T ≤10
1 ≤ N, M ≤ 10^3
1 ≤ Edges[i][0], Edges[i][1] < N
Time Limit: 1 sec
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
2
5 7
0 1
1 2
1 4
2 3
3 0
3 1
4 3
2 1
0 1
True
False
For the first test case, ‘edges’ = [[0,1], [1, 2], [1, 4], [2, 3], [3, 0], [3, 1], [4, 3]], describing the following graph:

Here you can have the path, [0 -> 1 -> 2 -> 3 -> 1 -> 4 -> 3 -> 0], which is using every edge of the graph. Hence the graph is Eulerian and the answer is True.
For the second test case, ‘edged’ = [[0, 1]], describing the following graph:

Here you can see the path [0 -> 1] uses all the edges but does not start and end at the same location. Hence there is no Euler circuit in the graph and the answer is False.
2
3 3
0 1
1 2
2 0
4 4
1 2
2 3
3 0
0 1
True
True
Find the conditions for each node for the graph to be eulerian
In this approach, we can see that for the graph to be Eulerian,
If the above two conditions are met, the graph will contain an eulerian circuit and thus will be an eulerian graph.
We will use the Kosaraju algorithm to find if the graph has a single strongly connected component and then calculate the indegree and outdegree of each node.
We create a kosarajuAlorithm(adjList, n) function to get the strongly connected components of the graph, where ‘adjList’ is the adjacency list of the graph and n is the number of nodes.
Kosaraju algorithm needs a few more utility functions of the graph:
Algorithm:
O( N + M ), where N and M are the numbers of nodes and edges in the graph.
The complexity of making the adjacency list O(N + M) and calculating the in-degrees of all nodes is O(N + M). The Kosaraju Algorithm takes O(N + M) time.
Hence the total time complexity is O(N + M).
O( N + M ), where N and M are the numbers of nodes and edges in the graph.
We maintain an adjacency list that will have O(N + M) items at most. The recursion depth of both DFS functions(topological sort and get connected components) is O(N).
Hence the overall space complexity is O(N + M).