Given an undirected graph with V nodes (say numbered from 1 to V) and E edges, the task is to check whether the graph is an Euler Graph or not and if so then convert it into a Directed Euler Circuit.
A Directed Euler Circuit is a directed graph such that if you start traversing the graph from any node and travel through each edge exactly once you will end up on the starting node.
Note: While traversing a Euler circuit every edge is traversed exactly once. A node can be traversed more than once if needed but an edge cannot be traversed more than once.
Example:
Input:
Output:
1 2
2 5
5 1
2 4
4 3
3 2
Explanation:
The Directed Euler Circuit for the given undirected graph will be:
Approach:
- First, we need to make sure the given Undirected Graph is Eulerian or not. If the undirected graph is not Eulerian we cannot convert it to a Directed Eulerian Graph.
- To check it we just need to calculate the degree of every node. If the degree of all nodes is even and not equal to 0 then the graph is Eulerian.
- To check it we just need to calculate the degree of every node. If the degree of all nodes is even and not equal to 0 then the graph is Eulerian.
- We will be using Depth First Search Traversal to assign the directions.
- While traversing we will set the direction of an edge from parent to child. We will maintain a map to make sure an edge is traversed only once.
- While traversing we will set the direction of an edge from parent to child. We will maintain a map to make sure an edge is traversed only once.
Below is the implementation of the above algorithm:
CPP
// C++ program to Convert an// Undirected Graph to a// Directed Euler Circuit#include <bits/stdc++.h>using namespace std;vector<int> g[100];// Array to store degree// of nodes.int deg[100] = { 0 };// Map to keep a track of// visited edgesmap<pair<int, int>, int> m1;// Vector to store the edge// pairsvector<pair<int, int> > v;// Function to add Edgevoid addEdge(int u, int v){ deg[u]++; deg[v]++; g[u].push_back(v); g[v].push_back(u);}// Function to check if graph// is Eulerian or notbool CheckEulerian(int n){ int check = 0; for (int i = 1; i <= n; i++) { // Checking if odd degree // or zero degree nodes // are present if (deg[i] % 2 || deg[i] == 0) { check = 1; break; } } // If any degree is odd or // any vertex has degree 0 if (check) { return false; } return true;}// DFS Function to assign the directionvoid DirectedEuler(int node, vector<int> g[]){ for (auto i = g[node].begin(); i != g[node].end(); i++) { // Checking if edge is already // visited if (m1[make_pair(node, *i)] || m1[make_pair(*i, node)]) continue; m1[make_pair(node, *i)]++; // Storing the edge v.push_back(make_pair(node, *i)); DirectedEuler(*i, g); }}// Function prints the convert// Directed graphvoid ConvertDirectedEuler(int n, int e){ if (!CheckEulerian(n)) { cout << "NOT POSSIBLE" << endl; return; } DirectedEuler(1, g); // Printing directed edges for (auto i = v.begin(); i != v.end(); i++) { cout << (*i).first << " " << (*i).second << endl; }}// Driver codeint main(){ int N = 5; int E = 6; addEdge(1, 2); addEdge(1, 5); addEdge(5, 2); addEdge(2, 4); addEdge(2, 3); addEdge(4, 3); ConvertDirectedEuler(N, E);} |
Python3
# Python program to Convert an# Undirected Graph to a# Directed Euler Circuitfrom typing import Listg = [[] for _ in range(100)]# Array to store degree# of nodes.deg = [0 for _ in range(100)]# Map to keep a track of# visited edgesm1 = dict()# Vector to store the edge# pairsv = []# Function to add Edgedef addEdge(u: int, v: int) -> None: global deg, g deg[u] += 1 deg[v] += 1 g[u].append(v) g[v].append(u)# Function to check if graph# is Eulerian or notdef CheckEulerian(n: int) -> bool: check = 0 for i in range(1, n + 1): # Checking if odd degree # or zero degree nodes # are present if (deg[i] % 2 or deg[i] == 0): check = 1 break # If any degree is odd or # any vertex has degree 0 if (check): return False return True# DFS Function to assign the directiondef DirectedEuler(node: int, g: List[List[int]]) -> None: for i in g[node]: # Checking if edge is already # visited if ((node, i) in m1 or (i, node) in m1): continue if (node, i) not in m1: m1[(node, i)] = 0 m1[(node, i)] += 1 # Storing the edge v.append((node, i)) DirectedEuler(i, g)# Function prints the convert# Directed graphdef ConvertDirectedEuler(n: int, e: int) -> None: if (not CheckEulerian(n)): print("NOT POSSIBLE") return DirectedEuler(1, g) # Printing directed edges for i in v: print("{} {}".format(i[0], i[1]))# Driver codeif __name__ == "__main__": N = 5 E = 6 addEdge(1, 2) addEdge(1, 5) addEdge(5, 2) addEdge(2, 4) addEdge(2, 3) addEdge(4, 3) ConvertDirectedEuler(N, E)# This code is contributed by sanjeev2552 |
1 2 2 5 5 1 2 4 4 3 3 2
Time Complexity: O(( V + E ) * log( E ))
Space Complexity: O(max( V, E ))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.



