Construct a Graph which does not Contain any Pair of Adjacent Nodes with Same Value


The constructed graph may be a non-repetitive hub course of action where no two adjoining hubs share the same esteem. Each hub speaks to a special value, and interfacing edges connect hubs without rehashing values. This chart embodies a design where differing qualities and uniqueness are prioritized, guaranteeing that neighboring hubs are continuously diverse from each other. By following this run of the show, the chart cultivates a locked-in and outwardly unmistakable representation, which can be pertinent in different areas like organizational planning, information visualization, or asset allotment. Its structure dodges dull groupings and advances a dynamic and shifted association between hubs, contributing to a more instructive and engaging graphical representation.

Methods Used

  • Recursive Construction

Recursive Construction

In this technique, the chart is constructed utilizing a recursive capability. The ongoing hub, its worth, and the rundown of values are passed as boundaries to the capability. The capability endeavors to add an edge with an alternate worth to any of the current hubs at each step. On the off chance that it does, it adds the edge and recursively requires the ensuing worth. On the off chance that it can't interface with a reasonable hub, it returns to the main hub and endeavors for an alternate worth.

Algorithm

  • Begin with a clear diagram and a rundown of values you need to give every hub in the chart.

  • Develop a recursive capability named "constructGraph" that has three boundaries: the ongoing hub, its worth, and a rundown of the leftover qualities.

  • Held inside the "constructGraph" capability:

  • a-Remember the ongoing hub and its related incentive for the chart.

  • b-Emphasize through the diagram's ongoing hubs to see whether any of them have values that vary from the one at the ongoing hub.

  • a-Add an edge between the ongoing hub and the current hub in the event that we find a hub with an alternate worth.

  • Involving the ongoing hub as the beginning hub, call the "constructGraph" capability over and over for each worth that is as yet present in the rundown.

Example

#include <iostream>
#include <vector>
using namespace std;

const int N = 5; // Number of nodes in the graph

class Graph {
public:
   vector<int> graph[N];

   void constructGraph(int currentNode, int currentValue, vector<int>& 
remainingValues) {
      graph[currentNode].push_back(currentValue);
      for (int i = 0; i < N; ++i) {
         if (i != currentNode) {
            for (int j = 0; j < remainingValues.size(); ++j) {
               int nextValue = remainingValues[j];
               remainingValues.erase(remainingValues.begin() + j);
               constructGraph(i, nextValue, remainingValues);
               remainingValues.insert(remainingValues.begin() + j, nextValue);
            }
         }
      }
   }
};

int main() {
   Graph g;
   vector<int> values = {1, 2, 3};
   g.constructGraph(0, 0, values);

   for (int i = 0; i < N; ++i) {
      cout << "Node " << i << ": ";
      for (int neighbor : g.graph[i]) {
         cout << neighbor << " ";
      }
      cout << endl;
   }

   return 0;
}

Output

Node 0: 0 2 3 3 2 3 2 3 2 1 3 3 1 3 1 3 1 1 2 2 1 2 1 2 1 2 3 3 2 3 2 3 2 1 3 
3 1 3 1 3 1 1 2 2 1 2 1 2 1 2 3 3 2 3 2 3 2 1 3 3 1 3 1 3 1 1 2 2 1 2 1 2 1 2 
3 3 2 3 2 3 2 1 3 3 1 3 1 3 1 1 2 2 1 2 1 2 1 
Node 1: 1 3 2 3 2 3 2 3 2 2 3 1 3 1 3 1 3 1 3 2 1 2 1 2 1 2 1 3 2 2 3 3 2 3 2 
3 1 1 3 3 1 3 1 2 1 1 2 2 1 2 1 3 2 2 3 3 2 3 2 3 1 1 3 3 1 3 1 2 1 1 2 2 1 2 
1 3 2 2 3 3 2 3 2 3 1 1 3 3 1 3 1 2 1 1 2 2 1 2 1 
Node 2: 3 2 2 3 3 2 3 2 3 1 1 3 3 1 3 1 2 1 1 2 2 1 2 1 1 3 2 3 2 3 2 3 2 2 3 
1 3 1 3 1 3 1 3 2 1 2 1 2 1 2 1 3 2 3 2 2 3 3 2 3 1 3 1 1 3 3 1 2 1 2 1 1 2 2 
1 3 2 3 2 2 3 3 2 3 1 3 1 1 3 3 1 2 1 2 1 1 2 2 1 
Node 3: 3 2 3 2 2 3 3 2 3 1 3 1 1 3 3 1 2 1 2 1 1 2 2 1 3 2 3 2 2 3 3 2 3 1 3 
1 1 3 3 1 2 1 2 1 1 2 2 1 1 3 2 3 2 3 2 3 2 2 3 1 3 1 3 1 3 1 3 2 1 2 1 2 1 2 
1 3 2 3 2 3 2 2 3 3 1 3 1 3 1 1 3 2 1 2 1 2 1 1 2 
Node 4: 3 2 3 2 3 2 2 3 3 1 3 1 3 1 1 3 2 1 2 1 2 1 1 2 3 2 3 2 3 2 2 3 3 1 3 
1 3 1 1 3 2 1 2 1 2 1 1 2 3 2 3 2 3 2 2 3 3 1 3 1 3 1 1 3 2 1 2 1 2 1 1 2 1 3 
2 3 2 3 2 3 2 2 3 1 3 1 3 1 3 1 3 2 1 2 1 2 1 2 1 

Conclusion

A graph that doesn't have any adjoining hubs with the same value can be made utilizing any of the three strategies. The chosen methodology is determined by the specific prerequisites, the open information structures, and the complexity of the coming-about chart. Each methodology offers accommodating tips for making such charts and can be extended to address more complicated circumstances.

Updated on: 04-Aug-2023

29 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements