Check if longest connected component forms a palindrome in undirected graph

Given an undirected graph with V vertices and E edges, the task is to check that if the largest connected component of the graph forms a palindrome in the undirected graph.



Longest connected component is Palindrome
Longest connected component is {5, 15, 5}
which forms an palindrome by values.


Longest connected component is not a Palindrome
Longest chain is {2, 3, 4, 5} which is not a palindrome.

Approach: The idea is to use Depth First Search Traversal to keep track of the connected components in the undirected graph. At each traversal, if the current length of the connected component is greater than the length of the global length of the connected component then update the longest connected component. Finally, check the longest connected component forms a palindrome.

Below is the implementation of the above approach:






// C++ implementation to check if
// longest connected component is 
// palindrome in undirected graph
#include <bits/stdc++.h>
using namespace std;
// Function to traverse the undirected
// graph using the Depth first traversal
void depthFirst(int v, vector<int> graph[],
                vector<bool>& visited,
                vector<int>& storeChain)
    // Marking the visited 
    // vertex as true
    visited[v] = true;
    // Store the connected chain
    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<int,int> frequency;
    // Container to visit elements
    unordered_set<int> 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});
            frequency[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;
        int countFreq = 0;
        for(auto i: frequency)
            if(i.second % 2 != 0)
                countFreq ++;
        if(countFreq != 1)
            result = 0;
    // Printing the final result
        cout << "Longest connected component is Palindrome";
        cout << "Longest connected component not a Palindrome";
// Function to check that longest connected
// component forms a palindrome
void longestConnectionPalin(
    vector<int> graph[], int vertices,
                   vector<int> values)
    // Initializing boolean array
    // to mark visited vertices
    vector<bool> visited(10001, false);
    // maxChain stores the 
    // maximum chain size
    int maxChain = 0;
    // Container to store longest chain
    vector<int> 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<int> 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<int> 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<int> values;
    // Constructing the undirected graph
    longestConnectionPalin(graph, V, values);
    return 0;



Longest connected component is Palindrome

Time Complexity: O(V + E)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.