Given an undirected graph with N vertices and M edges and no self loops or multiple edges. The task is to convert the given undirected graph into a directed graph such that there is no path of length greater than 1. If it is possible to make such a graph then print two space-separated integers u and v in M lines where u, v denotes source and destination vertices respectively. If not possible then print -1.
For the given graph it is not possible to get a directed graph
such that there is no path of length greater than 1
Approach: Let suppose the graph contains a cycle of odd length. It means that some two consecutive edges of this cycle will be oriented in the same way and will form a path of length two. Then the answer is -1.
And if the graph contains no cycles of odd length. Then it is bipartite. Let’s color it and see what we got. We got some vertices in the left part, some vertices in the right part and all edges connecting vertices from different parts. Let’s orient all edges such that they will go from the left part to the right part.
Below is the implementation of the above approach:
1 2 1 3 1 4
- Convert undirected connected graph to strongly connected directed graph
- Conversion of an Undirected Graph to a Directed Euler Circuit
- Eulerian Path in undirected graph
- Find if there is a path between two vertices in a directed graph
- Find if there is a path between two vertices in a directed graph | Set 2
- Longest Path in a Directed Acyclic Graph
- Path with minimum XOR sum of edges in a directed graph
- Shortest Path in Directed Acyclic Graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Find if there is a path between two vertices in an undirected graph
- Eulerian path and circuit for undirected graph
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path in a directed graph by Dijkstra’s algorithm
- Convert Directed Graph into a Tree
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Longest path in a directed Acyclic graph | Dynamic Programming
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Cycles of length n in an undirected and connected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.