📌  相关文章
📜  检查最长连接的组件是否在无向图中形成回文

📅  最后修改于: 2021-05-17 17:34:38             🧑  作者: Mango

给定具有V个顶点和E个边的无向图,任务是检查图的最大连接部分是否在无向图中形成回文。

例子:

方法:想法是使用深度优先搜索遍历来跟踪无向图中的连接组件。在每次遍历时,如果连接的组件的当前长度大于连接的组件的全局长度的长度,则更新最长的连接的组件。最后,检查最长的连接组件形成回文。

下面是上述方法的实现:

C++
// C++ implementation to check if
// longest connected component is
// palindrome in undirected graph
 
#include 
 
using namespace std;
 
// Function to traverse the undirected
// graph using the Depth first traversal
void depthFirst(int v, vector graph[],
                vector& visited,
                vector& storeChain)
{
    // Marking the visited
    // vertex as true
    visited[v] = true;
 
    // Store the connected chain
    storeChain.push_back(v);
 
    for (auto i : graph[v]) {
        if (visited[i] == false) {
             
            // Recursive call to
            // the DFS algorithm
            depthFirst(i, graph,
               visited, storeChain);
        }
    }
}
 
// Function to check that the connected
// component forms a palindrome
void checkPalin(int arr[], int n)
{
    // Container to store the frequency
    // of each occurring element
    map frequency;
  
    // Container to visit elements
    unordered_set element;
  
    for(int i = 0; i < n; i ++)
    {
        // If element has not been visited already
        // the element is inserted with freq = 1
        // frequency of occurrence, if visited
        // already, then frequency is updated
        if(!(element.find(arr[i]) != element.end()))
        {
            frequency.insert({arr[i], 1});
        }
        else
        {
            frequency[arr[i]] ++;
        }
        element.insert(arr[i]);
    }
  
    // Variable to store final result
    int result = 1;
  
    // For even array size, all the elements
    // are checked for even occurrences, if
    // odd array size, then it is checked if
    // a single element with odd frequency
    // is present or not
    if(n % 2 == 0)
    {
        for(auto i: frequency)
        {
            if(i.second % 2 != 0)
            {
                result = 0;
                break;
            }
        }
    }
    else
    {
        int countFreq = 0;
        for(auto i: frequency)
        {
            if(i.second % 2 != 0)
            {
                countFreq ++;
            }
        }
        if(countFreq != 1)
            result = 0;
    }
  
    // Printing the final result
    if(result)
        cout << "Longest connected component is Palindrome";
    else
        cout << "Longest connected component not a Palindrome";
}
 
// Function to check that longest connected
// component forms a palindrome
void longestConnectionPalin(
    vector graph[], int vertices,
                   vector values)
{
    // Initializing boolean array
    // to mark visited vertices
    vector visited(10001, false);
 
    // maxChain stores the
    // maximum chain size
    int maxChain = 0;
 
    // Container to store longest chain
    vector maxStoreChain;
 
    // Following loop invokes DFS algorithm
    for (int i = 1; i <= vertices; i++) {
        if (visited[i] == false) {
             
            // Variable to hold
            // temporary length
            int sizeChain;
 
            // Container to store each chain
            vector storeChain;
 
            // DFS algorithm
            depthFirst(i, graph, visited, storeChain);
 
            // Variable to hold each chain size
            sizeChain = storeChain.size();
 
            if (sizeChain > maxChain) {
                maxChain = sizeChain;
                maxStoreChain = storeChain;
            }
        }
    }
 
    // Container to store values
    // of vertices of longest chain
    int longChainValues[maxChain+1];
  
    // Storing the values of longest chain
    for(int i = 0; i < maxChain; i ++)
    {
        int temp = values[maxStoreChain[i]-1];
        longChainValues[i] = temp;
    }
  
    // Function call to check for Palindrome
    checkPalin(longChainValues, maxChain);
}
 
// Driver function to test above function
int main()
{
    // Initializing graph in the form of adjacency list
    vector graph[1001];
 
    // Defining the number of edges and vertices
    int E, V;
    E = 4;
    V = 7;
 
    // Assigning the values for each
    // vertex of the undirected graph
    vector values;
    values.push_back(10);
    values.push_back(25);
    values.push_back(5);
    values.push_back(15);
    values.push_back(5);
    values.push_back(20);
    values.push_back(0);
 
    // Constructing the undirected graph
    graph[1].push_back(2);
    graph[2].push_back(1);
    graph[3].push_back(4);
    graph[4].push_back(3);
    graph[3].push_back(5);
    graph[5].push_back(3);
    graph[6].push_back(7);
    graph[7].push_back(6);
 
    longestConnectionPalin(graph, V, values);
    return 0;
}


Java
// Java implementation to check if
// longest connected component is
// palindrome in undirected graph
import java.util.*;
class GFG{
     
// Initializing boolean array
// to mark visited vertices
static boolean [] visited =
       new boolean[10001];
     
// Container to store longest chain
static VectorstoreChain =
                      new Vector<>();
 
// Function to traverse
// the undirected graph
// using the Depth first
// traversal
static void depthFirst(int v,
                       Vector graph[])
{
  // Marking the visited
  // vertex as true
  visited[v] = true;
 
  // Store the connected chain
  storeChain.add(v);
 
  for (int i : graph[v])
  {
    if (visited[i] == false)
    {
      // Recursive call to
      // the DFS algorithm
      depthFirst(i, graph);
    }
  }
}
 
// Function to check that
// the connected component
// forms a palindrome
static void checkPalin(int arr[],
                       int n)
{
  // Container to store the
  // frequency of each occurring
  // element
  HashMap frequency =
                   new HashMap<>();
 
  // Container to visit elements
  HashSet element =
                   new HashSet<>();
 
  for(int i = 0; i < n; i ++)
  {
    // If element has not been
    // visited already the element
    // is inserted with freq = 1
    // frequency of occurrence,
    // if visited already, then
    // frequency is updated
    if((element.contains(arr[i])))
    {
      frequency.put(arr[i],
      frequency.get(arr[i]) + 1);
    }
    else
    {
      frequency.put(arr[i], 1);
    }
     
    element.add(arr[i]);
  }
 
  // Variable to store
  // final result
  int result = 1;
 
  // For even array size, all
  // the elements are checked
  // for even occurrences, if
  // odd array size, then it
  // is checked if a single
  // element with odd frequency
  // is present or not
  if(n % 2 == 0)
  {
    for (Map.Entry i : frequency.entrySet())
    {
      if(i.getValue() % 2 != 0)
      {
        result = 0;
        break;
      }
    }
  }
  else
  {
    int countFreq = 0;
    for (Map.Entry i : frequency.entrySet())
    {
      if(i.getValue() % 2 != 0)
      {
        countFreq ++;
      }
    }
    if(countFreq != 1)
      result = 0;
  }
 
  // Printing the
  // final result
  if(result != 0)
    System.out.print("Longest connected " +
                     "component is Palindrome");
  else
    System.out.print("Longest connected " +
                     "component not a Palindrome");
}
 
// Function to check that longest
// connected component forms a palindrome
static void longestConnectionPalin(Vector graph[],
                                   int vertices,
                                   Vector values)
{
  // maxChain stores the ;
  // maximum chain size
  int maxChain = 0;
 
  // Container to store each chain
  Vector maxStoreChain =
                  new Vector<>(); 
  // Following loop invokes
  // DFS algorithm
  for (int i = 1; i <= vertices; i++)
  {
    if (visited[i] == false)
    {
      // Variable to hold
      // temporary length
      int sizeChain;
 
      // DFS algorithm
      depthFirst(i, graph);
 
      // Variable to hold each chain size
      sizeChain = storeChain.size();
 
      if (sizeChain > maxChain)
      {
        maxChain = sizeChain;
        maxStoreChain = storeChain;
      }
    }
  }
 
  // Container to store values
  // of vertices of longest chain
  int []longChainValues =
        new int[maxChain+1];
 
  // Storing the values of
  // longest chain
  for(int i = 0; i < maxChain; i ++)
  {
    int temp = values.get(maxStoreChain.get(i) - 1);
    longChainValues[i] = temp;
  }
 
  // Function call to check
  // for Palindrome
  checkPalin(longChainValues,
             maxChain);
}
 
// Driver code
public static void main(String[] args)
{
  // Initializing graph in the
  // form of adjacency list
  Vector graph[] =
                  new Vector[1001];
   
  for (int i = 0; i < graph.length; i++)
    graph[i] = new Vector();
   
  // Defining the number of
  // edges and vertices
  int E, V;
  E = 4;
  V = 7;
 
  // Assigning the values
  // for each vertex of
  // the undirected graph
  Vector values =
         new Vector<>();
  values.add(10);
  values.add(25);
  values.add(5);
  values.add(15);
  values.add(5);
  values.add(20);
  values.add(0);
 
  // Constructing the
  // undirected graph
  graph[1].add(2);
  graph[2].add(1);
  graph[3].add(4);
  graph[4].add(3);
  graph[3].add(5);
  graph[5].add(3);
  graph[6].add(7);
  graph[7].add(6);
 
  longestConnectionPalin(graph, V, values);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation to check if
# longest connected component is
# palindrome in undirected graph
 
# Function to traverse the undirected
# graph using the Depth first traversal
def depthFirst(v):
     
    global graph, visited, storeChain
 
    # Marking the visited
    # vertex as true
    visited[v] = True
 
    # Store the connected chain
    storeChain.append(v)
 
    for i in graph[v]:
        if (visited[i] == False):
 
            # Recursive call to
            # the DFS algorithm
            depthFirst(i)
 
# Function to check that the connected
# component forms a palindrome
def checkPalin(arr, n):
     
    # Container to store the frequency
    # of each occurring element
    frequency = {}
 
    # Container to visit elements
    element = {}
 
    for i in range(n):
         
        # If element has not been visited already
        # the element is inserted with freq = 1
        # frequency of occurrence, if visited
        # already, then frequency is updated
        if (arr[i] not in element):
            frequency[arr[i]] = 1
        else:
            frequency[arr[i]] += 1
             
        element[arr[i]] = 1
 
    # Variable to store final result
    result = 1
 
    # For even array size, all the elements
    # are checked for even occurrences, if
    # odd array size, then it is checked if
    # a single element with odd frequency
    # is present or not
    if (n % 2 == 0):
        for i in frequency:
            if (frequency[i] % 2 != 0):
                result = 0
                break
    else:
        countFreq = 0
         
        for i in frequency:
            if frequency[i] % 2 != 0:
                countFreq += 1
                 
        if (countFreq != 1):
            result = 0
 
    # Printing the final result
    if(not result):
        print("Longest connected "
              "component is Palindrome")
    else:
        print("Longest connected "
              "component not a Palindrome")
 
# Function to check that longest connected
# component forms a palindrome
def longestConnectionPalin(vertices):
     
    global visited, graph, storeChain, values
 
    # maxChain stores the
    # maximum chain size
    maxChain = 0
 
    # Container to store longest chain
    maxStoreChain = []
 
    # Following loop invokes DFS algorithm
    for i in range(1, vertices + 1):
        if (visited[i] == False):
 
            # Variable to hold
            # temporary length
            sizeChain = 0
 
            # DFS algorithm
            depthFirst(i)
 
            # Variable to hold each chain size
            sizeChain = len(storeChain)
 
            if (sizeChain > maxChain):
                maxChain = sizeChain
                maxStoreChain = storeChain
 
    # Storing the values of longest chain
    for i in range(maxChain):
        temp = values[maxStoreChain[i] - 1]
        longChainValues[i] = temp
 
    # Function call to check for Palindrome
    checkPalin(longChainValues, maxChain)
     
# Driver Code
if __name__ == '__main__':
     
    # Initializing graph in the form
    # of adjacency list
    graph  = [[] for i in range(10001)]
    visited = [False for i in range(10001)]
    storeChain = []
    longChainValues = [0 for i in range(10001)]
 
    # Defining the number of edges and vertices
    E = 4
    V = 7
 
    # Assigning the values for each
    # vertex of the undirected graph
    values = []
    values.append(10)
    values.append(25)
    values.append(5)
    values.append(15)
    values.append(5)
    values.append(20)
    values.append(0)
 
    # Constructing the undirected graph
    graph[1].append(2)
    graph[2].append(1)
    graph[3].append(4)
    graph[4].append(3)
    graph[3].append(5)
    graph[5].append(3)
    graph[6].append(7)
    graph[7].append(6)
 
    longestConnectionPalin(V)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to check if
// longest connected component is
// palindrome in undirected graph
using System;
using System.Collections.Generic;
 
class GFG{
     
// Initializing bool array
// to mark visited vertices
static bool [] visited = new bool[10001];
     
// Container to store longest chain
static ListstoreChain = new List();
 
// Function to traverse
// the undirected graph
// using the Depth first
// traversal
static void depthFirst(int v,
                       List []graph)
{
   
  // Marking the visited
  // vertex as true
  visited[v] = true;
 
  // Store the connected chain
  storeChain.Add(v);
 
  foreach (int i in graph[v])
  {
    if (visited[i] == false)
    {
       
      // Recursive call to
      // the DFS algorithm
      depthFirst(i, graph);
    }
  }
}
 
// Function to check that
// the connected component
// forms a palindrome
static void checkPalin(int []arr,
                       int n)
{
   
  // Container to store the
  // frequency of each occurring
  // element
  Dictionary frequency = new Dictionary();
   
  // Container to visit elements
  HashSet element = new HashSet();
 
  for(int i = 0; i < n; i ++)
  {
     
    // If element has not been
    // visited already the element
    // is inserted with freq = 1
    // frequency of occurrence,
    // if visited already, then
    // frequency is updated
    if ((element.Contains(arr[i])))
    {
      frequency[arr[i]]++;
    }
    else
    {
      frequency.Add(arr[i], 1);
    }
    element.Add(arr[i]);
  }
   
  // Variable to store
  // readonly result
  int result = 1;
   
  // For even array size, all
  // the elements are checked
  // for even occurrences, if
  // odd array size, then it
  // is checked if a single
  // element with odd frequency
  // is present or not
  if (n % 2 == 0)
  {
    foreach(KeyValuePair i in frequency)
    {
      if (i.Value % 2 != 0)
      {
        result = 0;
        break;
      }
    }
  }
  else
  {
    int countFreq = 0;
    foreach(KeyValuePair i in frequency)
    {
      if (i.Value % 2 != 0)
      {
        countFreq ++;
      }
    }
    if (countFreq != 1)
      result = 0;
  }
 
  // Printing the
  // readonly result
  if (result == 0)
    Console.Write("longest connected " +
                  "component is Palindrome");
  else
    Console.Write("longest connected " +
                  "component not a Palindrome");
}
 
// Function to check that longest
// connected component forms a palindrome
static void longestConnectionPalin(List []graph,
                                   int vertices,
                                   List values)
{
   
  // maxChain stores the ;
  // maximum chain size
  int maxChain = 0;
 
  // Container to store each chain
  List maxStoreChain = new List(); 
   
  // Following loop invokes
  // DFS algorithm
  for(int i = 1; i <= vertices; i++)
  {
    if (visited[i] == false)
    {
       
      // Variable to hold
      // temporary length
      int sizeChain;
       
      // DFS algorithm
      depthFirst(i, graph);
 
      // Variable to hold each chain size
      sizeChain = storeChain.Count;
 
      if (sizeChain > maxChain)
      {
        maxChain = sizeChain;
        maxStoreChain = storeChain;
      }
    }
  }
   
  // Container to store values
  // of vertices of longest chain
  int []longChainValues = new int[maxChain + 1];
   
  // Storing the values of
  // longest chain
  for(int i = 0; i < maxChain; i ++)
  {
    int temp = values[maxStoreChain[i] - 1];
    longChainValues[i] = temp;
  }
 
  // Function call to check
  // for Palindrome
  checkPalin(longChainValues,
             maxChain);
}
 
// Driver code
public static void Main(String[] args)
{
   
  // Initializing graph in the
  // form of adjacency list
  List []graph = new List[1001];
   
  for(int i = 0; i < graph.Length; i++)
    graph[i] = new List();
   
  // Defining the number of
  // edges and vertices
  //int E;
  //E = 4;
   
  int V;
  V = 7;
 
  // Assigning the values
  // for each vertex of
  // the undirected graph
  List values = new List();
  values.Add(10);
  values.Add(25);
  values.Add(5);
  values.Add(15);
  values.Add(5);
  values.Add(20);
  values.Add(0);
   
  // Constructing the
  // undirected graph
  graph[1].Add(2);
  graph[2].Add(1);
  graph[3].Add(4);
  graph[4].Add(3);
  graph[3].Add(5);
  graph[5].Add(3);
  graph[6].Add(7);
  graph[7].Add(6);
 
  longestConnectionPalin(graph, V, values);
}
}
 
// This code is contributed by aashish1995


输出:
Longest connected component is Palindrome

时间复杂度: O(V + E)