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. Examples:
Input:
Output: 1 2 1 3 1 4
Input:
Output: -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:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
vector< int > gr[N];
int colour[N];
vector<pair< int , int > > edges;
bool bip;
void add_edge( int x, int y)
{
gr[x].push_back(y);
gr[y].push_back(x);
edges.push_back(make_pair(x, y));
}
void dfs( int x, int col)
{
colour[x] = col;
for ( auto i : gr[x]) {
if (colour[i] == -1)
dfs(i, col ^ 1);
else if (colour[i] == col)
bip = false ;
}
}
void Directed_Graph( int n, int m)
{
memset (colour, -1, sizeof colour);
bip = true ;
dfs(1, 1);
if (!bip) {
cout << -1;
return ;
}
for ( int i = 0; i < m; i++) {
if (colour[edges[i].first] == 0)
swap(edges[i].first, edges[i].second);
cout << edges[i].first << " "
<< edges[i].second << endl;
}
}
int main()
{
int n = 4, m = 3;
add_edge(1, 2);
add_edge(1, 3);
add_edge(1, 4);
Directed_Graph(n, m);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int N = 100005 ;
static Vector<Integer> []gr = new Vector[N];
static int []colour = new int [N];
static Vector<pair> edges = new Vector<>();
static boolean bip;
static void add_edge( int x, int y)
{
gr[x].add(y);
gr[y].add(x);
edges.add( new pair(x, y));
}
static void dfs( int x, int col)
{
colour[x] = col;
for (Integer i : gr[x])
{
if (colour[i] == - 1 )
dfs(i, col ^ 1 );
else if (colour[i] == col)
bip = false ;
}
}
static void Directed_Graph( int n, int m)
{
for ( int i = 0 ; i < N; i++)
colour[i] = - 1 ;
bip = true ;
dfs( 1 , 1 );
if (!bip)
{
System.out.print(- 1 );
return ;
}
for ( int i = 0 ; i < m; i++)
{
if (colour[edges.get(i).first] == 0 )
{
Collections.swap(edges, edges.get(i).first,
edges.get(i).second);
}
System.out.println(edges.get(i).first + " " +
edges.get(i).second);
}
}
public static void main(String[] args)
{
int n = 4 , m = 3 ;
for ( int i = 0 ; i < N; i++)
gr[i] = new Vector<>();
add_edge( 1 , 2 );
add_edge( 1 , 3 );
add_edge( 1 , 4 );
Directed_Graph(n, m);
}
}
|
Python3
N = 100005
gr = [[] for i in range (N)]
colour = [ - 1 ] * N
edges = []
bip = True
def add_edge(x, y):
gr[x].append(y)
gr[y].append(x)
edges.append((x, y))
def dfs(x, col):
colour[x] = col
global bip
for i in gr[x]:
if colour[i] = = - 1 :
dfs(i, col ^ 1 )
elif colour[i] = = col:
bip = False
def Directed_Graph(n, m):
dfs( 1 , 1 )
if not bip:
print ( - 1 )
return
for i in range ( 0 , m):
if colour[edges[i][ 0 ]] = = 0 :
edges[i][ 0 ], edges[i][ 1 ] = edges[i][ 1 ], edges[i][ 0 ]
print (edges[i][ 0 ], edges[i][ 1 ])
if __name__ = = "__main__" :
n, m = 4 , 3
add_edge( 1 , 2 )
add_edge( 1 , 3 )
add_edge( 1 , 4 )
Directed_Graph(n, m)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static int N = 100005;
static List< int > []gr = new List< int >[N];
static int []colour = new int [N];
static List<pair> edges = new List<pair>();
static Boolean bip;
static void add_edge( int x, int y)
{
gr[x].Add(y);
gr[y].Add(x);
edges.Add( new pair(x, y));
}
static void dfs( int x, int col)
{
colour[x] = col;
foreach ( int i in gr[x])
{
if (colour[i] == -1)
dfs(i, col ^ 1);
else if (colour[i] == col)
bip = false ;
}
}
static void Directed_Graph( int n, int m)
{
for ( int i = 0; i < N; i++)
colour[i] = -1;
bip = true ;
dfs(1, 1);
if (!bip)
{
Console.Write(-1);
return ;
}
for ( int i = 0; i < m; i++)
{
if (colour[edges[i].first] == 0)
{
var v = edges[i].first;
edges[i].first = edges[i].second;
edges[i].second = v;
}
Console.WriteLine(edges[i].first + " " +
edges[i].second);
}
}
public static void Main(String[] args)
{
int n = 4, m = 3;
for ( int i = 0; i < N; i++)
gr[i] = new List< int >();
add_edge(1, 2);
add_edge(1, 3);
add_edge(1, 4);
Directed_Graph(n, m);
}
}
|
Javascript
<script>
let N = 100005
let gr = new Array(N);
for (let i = 0; i < N; i++){
gr[i] = new Array()
}
let colour = new Array(N).fill(-1)
let edges = []
let bip = true
function add_edge(x, y){
gr[x].push(y)
gr[y].push(x)
edges.push([x, y])
}
function dfs(x, col){
colour[x] = col
for (let i of gr[x]){
if (colour[i] == -1)
dfs(i, col ^ 1)
else if (colour[i] == col)
bip = false
}
}
function Directed_Graph(n, m){
dfs(1, 1)
if (bip == 0){
document.write(-1, "</br>" )
return
}
for (let i=0;i<m;i++){
if (colour[edges[i][0]] == 0){
let temp = edges[i][0]
edges[i][0] = edges[i][1]
edges[i][1] = temp
}
document.write(edges[i][0], edges[i][1], "</br>" )
}
}
let n =4, m = 3
add_edge(1, 2)
add_edge(1, 3)
add_edge(1, 4)
Directed_Graph(n, m)
</script>
|
Time Complexity: O(V + E)
The time complexity of the above approach is O(V + E) where V is the number of vertices and E is the number of edges in the given graph. Since we are traversing the graph using DFS, the time complexity is proportional to the number of vertices plus the number of edges in the graph.
Space Complexity: O(V + E)
The space complexity of the above approach is also O(V + E) as we are using an adjacency list to store the graph and also a vector to store the edges.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!