Home  >  Article  >  Backend Development  >  Construct a graph that does not contain any pairs of adjacent nodes with the same value

Construct a graph that does not contain any pairs of adjacent nodes with the same value

王林
王林forward
2023-09-14 14:33:03974browse

Construct a graph that does not contain any pairs of adjacent nodes with the same value

The chart constructed may be a non-repeating center course of action where no two adjacent centers share the same value. Each center represents a unique value, and connecting edges connect centers without duplicating values. The diagram reflects a design that prioritizes diversity and uniqueness, ensuring that adjacent centers are always distinct from each other. By following this rule, diagrams cultivate a focused and visually unique representation that can be relevant in areas as diverse as organizational planning, information visualization, or resource allocation. Its structure avoids boring clusters and promotes dynamic and diverse connections between centers, contributing to a more educational and engaging graphical representation.

usage instructions

  • Recursive construction

Recursive construction

In this technique, the chart is constructed using recursive functions. The running center, its value, and the list of values ​​are passed as boundaries for the function. This function works on adding an edge with an alternative value to any current hub at each step. If it does, it adds advantage and requires subsequent values ​​recursively. If it cannot connect to a suitable hub, it will return to the primary hub and try other values.

algorithm

  • Start by drawing a clear graph and listing the values ​​required for each center in the graph.

  • Develop a recursive function called "constructGraph" that has three bounds: the current center, its value, and a list of remaining masses.

  • Saved in "constructGraph" function:

  • a- Remember the ongoing centers and incentives associated with the chart

  • b - Emphasize going through the continuous center of the chart to see if any values ​​in it are different from the values ​​in the continuous center.

  • a - If we find a hub with an alternative value, add an edge between the current hub and the current hub.

  • With the continuing center as the starting center, for each value still present in the list, call the "constructGraph" function repeatedly

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 

in conclusion

Any of these three strategies can be utilized to make a chart that does not have any adjacent centers with the same value. The chosen method depends on the specific prerequisites, the open information structure and the complexity of the upcoming diagram. Each method provides practical tips for making such diagrams and can be extended to address more complex situations.

The above is the detailed content of Construct a graph that does not contain any pairs of adjacent nodes with the same value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete