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 atleast two unique values exists 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++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function that prints the edges of

// the generated graph

**void** printConnections(

vector<pair<``**int**``, **int**``> > store,

vector<``**int**``> ind, **int** ind1)

{

// First print connections

// stored in store[]

**for** (``**auto** pr : store) {

cout << pr.first << " "

<< pr.second << "\n"``;

}

// Check if there is more than one

// occurrence of 1st unique element

**if** (ind.size() != 0) {

// Print all other occurrence

// of 1st unique element with

// second unique element

**for** (``**auto** x : ind) {

cout << ind1 << " "

<< x + 1 << "\n"``;

}

}

}

// Function to construct the graph such

// that the every adjacent nodes have

// different value

**void** constructGraph(``**char** arr[], **int** N)

{

vector<``**int**``> ind;

// Stores pair of edges formed

vector<pair<``**int**``, **int**``> > store;

// Stores first unique occurrence

**char** x = arr[0];

**int** count = 0, ind1;

**for** (``**int** i = 1; i <= N - 1; ++i) {

// Check for the second

// unique occurrence

**if** (arr[i] != x) {

// Store indices of 2nd

// unique occurrence

ind1 = i + 1;

// To check if arr has only

// 1 unique element or not

count++;

// Store the connections of all

// unique elements with Node 1

store.push_back({ 1, i + 1 });

}

// If value at node (i + 1) is

// same as value at Node 1 then

// store its indices

**else** {

ind.push_back(i);

}

}

// If count is zero then it's not

// possible to construct the graph

**if** (count == 0) {

cout << "Not Possible"``;

}

// If more than 1 unique

// element is present

**else** {

cout << "Possible"

<< "\n"``;

// Print the edges

printConnections(store, ind, ind1);

}

}

// Driver Code

**int** main()

{

**int** N = 5;

// Given array having node values

**char** arr[] = { 'a'``, 'b'``, 'a'``, 'b'``, 'c' };

// Function Call

constructGraph(arr, N);

**return** 0;

}

Output:

Possible
1 2
1 4
1 5
5 3

Time Complexity:_ O(N)_

Auxiliary Space:_ O(N)_

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.

#arrays #graph #greedy #searching #frequency-counting

Construct a graph which does not contain any pair of adjacent nodes with same value
1.35 GEEK