Construct a graph which does not contain any pair of adjacent nodes with same value
Last Updated :
18 Aug, 2021
Given an array arr[] consisting of N characters, the task is to generate a graph of N nodes and (N – 1) Edges such that each node i is associated with character arr[i] and no two adjacent nodes have the same value. If it is possible to make such a graph, then print “Possible” with the pairs of edges. Otherwise, print “Not Possible”.
Examples:
Input: N = 5, arr[] = {‘a’, ‘b’, ‘a’, ‘b’, ‘c’}
Output: “Possible”
1 – 2
1 – 4
1 – 5
5 – 3
Explanation:
One possible graph that can be constructed to satisfy the given conditions is as follows:
Input: N = 3, arr[] = {‘z’, ‘z’, ‘z’}
Output: “Not Possible”
Approach: To construct a graph such that no adjacent node has the same value, the idea is to check if at least two unique values exist or not. If found to be true, such a graph can be constructed. Follow the steps below:
- Check for all the values present at each node and if all the node values are the same, it’s not possible to construct the graph.
- If any two values are different, there will always be a way to construct such a graph.
- Now, select any two unique values, connect the occurrence of all the other values to 1st unique value except the value itself.
- Store indices of occurrence of 1st unique value except for its first occurrence and connect all those indices to the second unique value.
- This way there will always be a way to construct such a graph with (N – 1) edges.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printConnections(
vector<pair< int , int > > store,
vector< int > ind, int ind1)
{
for ( auto pr : store) {
cout << pr.first << " "
<< pr.second << "\n" ;
}
if (ind.size() != 0) {
for ( auto x : ind) {
cout << ind1 << " "
<< x + 1 << "\n" ;
}
}
}
void constructGraph( char arr[], int N)
{
vector< int > ind;
vector<pair< int , int > > store;
char x = arr[0];
int count = 0, ind1;
for ( int i = 1; i <= N - 1; ++i) {
if (arr[i] != x) {
ind1 = i + 1;
count++;
store.push_back({ 1, i + 1 });
}
else {
ind.push_back(i);
}
}
if (count == 0) {
cout << "Not Possible" ;
}
else {
cout << "Possible"
<< "\n" ;
printConnections(store, ind, ind1);
}
}
int main()
{
int N = 5;
char arr[] = { 'a' , 'b' , 'a' , 'b' , 'c' };
constructGraph(arr, N);
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 void printConnections(Vector<pair > store,
Vector<Integer> ind,
int ind1)
{
for (pair pr : store)
{
System.out.print(pr.first + " " +
pr.second + "\n" );
}
if (ind.size() != 0 )
{
for ( int x : ind)
{
System.out.print(ind1 + " " +
(x + 1 ) + "\n" );
}
}
}
static void constructGraph( char arr[],
int N)
{
Vector<Integer> ind = new Vector<>();
Vector<pair > store = new Vector<>();
char x = arr[ 0 ];
int count = 0 ;
int ind1=- 1 ;
for ( int i = 1 ; i <= N - 1 ; ++i)
{
if (arr[i] != x)
{
ind1 = i + 1 ;
count++;
store.add( new pair( 1 , i + 1 ));
}
else
{
ind.add(i);
}
}
if (count == 0 )
{
System.out.print( "Not Possible" );
}
else
{
System.out.print( "Possible" +
"\n" );
printConnections(store,
ind, ind1);
}
}
public static void main(String[] args)
{
int N = 5 ;
char arr[] = { 'a' , 'b' ,
'a' , 'b' , 'c' };
constructGraph(arr, N);
}
}
|
Python3
def printConnections(store, ind, ind1):
for pr in store:
print (pr[ 0 ], pr[ 1 ])
if ( len (ind) ! = 0 ):
for x in ind:
print (ind1, x + 1 )
def constructGraph(arr, N):
ind = []
store = []
x = arr[ 0 ]
count, ind1 = 0 , 0
for i in range ( 1 , N):
if (arr[i] ! = x):
ind1 = i + 1
count + = 1
store.append([ 1 , i + 1 ])
else :
ind.append(i)
if count = = 0 :
print ( "Not Possible" )
else :
print ( "Possible" )
printConnections(store, ind, ind1)
if __name__ = = '__main__' :
N = 5
arr = [ 'a' , 'b' , 'a' , 'b' , 'c' ]
constructGraph(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class pair
{
public int first,
second;
public pair( int first,
int second)
{
this .first = first;
this .second = second;
}
}
static void printConnections(List<pair > store,
List< int > ind,
int ind1)
{
foreach (pair pr in store)
{
Console.Write(pr.first + " " +
pr.second + "\n" );
}
if (ind.Count != 0)
{
foreach ( int x in ind)
{
Console.Write(ind1 + " " +
(x + 1) + "\n" );
}
}
}
static void constructGraph( char []arr,
int N)
{
List< int > ind = new List< int >();
List<pair > store = new List<pair>();
char x = arr[0];
int count = 0;
int ind1=-1;
for ( int i = 1; i <= N - 1; ++i)
{
if (arr[i] != x)
{
ind1 = i + 1;
count++;
store.Add( new pair(1, i + 1 ));
}
else
{
ind.Add(i);
}
}
if (count == 0)
{
Console.Write( "Not Possible" );
}
else
{
Console.Write( "Possible" +
"\n" );
printConnections(store,
ind, ind1);
}
}
public static void Main(String[] args)
{
int N = 5;
char []arr = { 'a' , 'b' ,
'a' , 'b' , 'c' };
constructGraph(arr, N);
}
}
|
Javascript
<script>
function printConnections(store, ind, ind1)
{
for (let pr = 0; pr < store.length; pr++)
{
document.write(store[pr][0] + " " +
store[pr][1] + "<br>" );
}
if (ind.length != 0)
{
for (let x = 0; x < ind.length; x++)
{
document.write(ind1 + " " +
(ind[x] + 1) + "<br>" );
}
}
}
function constructGraph(arr, N)
{
let ind = [];
let store = [];
let x = arr[0];
let count = 0;
let ind1 = -1;
for (let i = 1; i <= N - 1; ++i)
{
if (arr[i] != x)
{
ind1 = i + 1;
count++;
store.push([1, i + 1]);
}
else
{
ind.push(i);
}
}
if (count == 0)
{
document.write( "Not Possible" );
}
else
{
document.write( "Possible" + "<br>" );
printConnections(store, ind, ind1);
}
}
let N = 5;
let arr = [ 'a ', ' b ', ' a ', ' b ', ' c' ];
constructGraph(arr, N);
</script>
|
Output:
Possible
1 2
1 4
1 5
5 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...