📌  相关文章
📜  构造一个不包含任何具有相同值的相邻节点对的图

📅  最后修改于: 2021-09-06 11:13:29             🧑  作者: Mango

给定一个由N 个字符组成的数组arr[] ,任务是生成N 个节点和(N – 1) 条边的图,使得每个节点i与字符arr[i]相关联,并且没有两个相邻节点具有相同的值.如果可以制作这样的图,则用边对打印“可能” 。否则,打印“不可能”

例子:

方法:要构造一个图,使得相邻节点都没有相同的值,其想法是检查是否至少存在两个唯一值。如果发现为真,则可以构建这样的图。请按照以下步骤操作:

  • 检查每个节点上存在的所有值,如果所有节点值都相同,则无法构建图形。
  • 如果任何两个值不同,总会有一种方法来构造这样的图。
  • 现在,选择任意两个唯一值,将所有其他值的出现连接到除值本身之外的第一个唯一值。
  • 存储第一个唯一值的出现索引,除了第一次出现,并将所有这些索引连接到第二个唯一值。
  • 这样,总有一种方法可以构造这样一个具有(N – 1) 条边的图。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function that prints the edges of
// the generated graph
void printConnections(
    vector > store,
    vector 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 ind;
 
    // Stores pair of edges formed
    vector > 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;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
     
static class pair
{
  int first, second;
  public pair(int first,
              int second) 
  {
    this.first = first;
    this.second = second;
  }   
}
 
// Function that prints the edges of
// the generated graph
static void printConnections(Vector store,
                             Vector ind,
                             int ind1)
{
  // First print connections
  // stored in store[]
  for (pair pr : store)
  {
    System.out.print(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 (int x : ind)
    {
      System.out.print(ind1 + " " +
                      (x + 1) + "\n");
    }
  }
}
 
// Function to conthe graph such
// that the every adjacent nodes have
// different value
static void constructGraph(char arr[],
                           int N)
{
  Vector ind = new Vector<>();
 
  // Stores pair of edges formed
  Vector store = new Vector<>();
 
  // Stores first unique occurrence
  char x = arr[0];
 
  int count = 0;
  int ind1=-1;
 
  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.add(new pair(1, i + 1 ));
    }
 
    // If value at node (i + 1) is
    // same as value at Node 1 then
    // store its indices
    else
    {
      ind.add(i);
    }
  }
 
  // If count is zero then it's not
  // possible to conthe graph
  if (count == 0)
  {
    System.out.print("Not Possible");
  }
 
  // If more than 1 unique
  // element is present
  else
  {
    System.out.print("Possible" +
                     "\n");
 
    // Print the edges
    printConnections(store,
                     ind, ind1);
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 5;
 
  // Given array having
  // node values
  char arr[] = {'a', 'b',
                'a', 'b', 'c'};
 
  // Function Call
  constructGraph(arr, N);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function that prints the edges of
# the generated graph
def printConnections(store, ind, ind1):
     
    # First print connections
    # stored in store[]
    for pr in store:
        print(pr[0], pr[1])
 
    # Check if there is more than one
    # occurrence of 1st unique element
    if (len(ind) != 0):
         
        # Print all other occurrence
        # of 1st unique element with
        # second unique element
        for x in ind:
            print(ind1, x + 1)
             
# Function to construct the graph such
# that the every adjacent nodes have
# different value
def constructGraph(arr, N):
     
    ind = []
 
    # Stores pair of edges formed
    store = []
 
    # Stores first unique occurrence
    x = arr[0]
  
    count, ind1 = 0, 0
 
    for i in range(1, N):
         
        # 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 += 1
 
            # Store the connections of all
            # unique elements with Node 1
            store.append([1, i + 1])
 
            # If value at node (i + 1) is
            # same as value at Node 1 then
            # store its indices
        else:
            ind.append(i)
 
    # If count is zero then it's not
    # possible to construct the graph
    if count == 0:
        print("Not Possible")
    else:
         
        # If more than 1 unique
        # element is present
        print("Possible")
 
        # Print the edges
        printConnections(store, ind, ind1)
             
# Driver Code
if __name__ == '__main__':
     
    N = 5
 
    # Given array having node values
    arr = [ 'a', 'b', 'a', 'b', 'c' ]
 
    # Function Call
    constructGraph(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
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;
  }   
}
 
// Function that prints the edges of
// the generated graph
static void printConnections(List store,
                             List ind,
                             int ind1)
{
  // First print connections
  // stored in store[]
  foreach (pair pr in store)
  {
    Console.Write(pr.first + " " +
                  pr.second + "\n");
  }
 
  // Check if there is more than one
  // occurrence of 1st unique element
  if (ind.Count != 0)
  {
    // Print all other occurrence
    // of 1st unique element with
    // second unique element
    foreach (int x in ind)
    {
      Console.Write(ind1 + " " +
                   (x + 1) + "\n");
    }
  }
}
 
// Function to conthe graph such
// that the every adjacent nodes have
// different value
static void constructGraph(char []arr,
                           int N)
{
  List ind = new List();
 
  // Stores pair of edges formed
  List store = new List();
 
  // Stores first unique occurrence
  char x = arr[0];
 
  int count = 0;
  int ind1=-1;
 
  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.Add(new pair(1, i + 1 ));
    }
 
    // If value at node (i + 1) is
    // same as value at Node 1 then
    // store its indices
    else
    {
      ind.Add(i);
    }
  }
 
  // If count is zero then it's not
  // possible to conthe graph
  if (count == 0)
  {
    Console.Write("Not Possible");
  }
 
  // If more than 1 unique
  // element is present
  else
  {
    Console.Write("Possible" +
                     "\n");
 
    // Print the edges
    printConnections(store,
                     ind, ind1);
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 5;
 
  // Given array having
  // node values
  char []arr = {'a', 'b',
                'a', 'b', 'c'};
 
  // Function Call
  constructGraph(arr, N);
}
}
 
// This code is contributed by gauravrajput1


输出:
Possible
1 2
1 4
1 5
5 3








时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live