Given an undirected graph consisting of V vertices and a 2d array E[][2] denoting edges between pairs of nodes. Given another array arr[] representing values assigned to each node, the task is to find the maximum GCD among the GCD of all connected components in the graph.
Examples:
Input: V = 5, E[][2] = {{1, 3}, {2, 3}, {1, 2}}, arr[] = {23, 43, 123, 54, 2}
Output: 54
Explanation:Â
Â
Connected component {1, 2, 3}: GCD(arr[1], arr[2], arr[3]) = GCD(23, 43, 123) = 1.
Connected component {4}: GCD = 54.
Connected component {5}: GCD = 2.
Therefore, the maximum GCD is 54.Input: V = 5, E = {{1, 2}, {1, 3}, {4, 5}}, arr[] = { 10, 10, 10, 15, 15 }
Output: 15
Approach: The given problem can be solved by performing Depth First Search traversal on the given graph and then find the maximum GCD among all the connected components. Follow the steps below to solve the problem:
- Initialize a variable, say maxGCD as INT_MIN, to store the maximum GCD among all the connected components.
- Initialize another variable, say currentGCD as 0, to store the GCD of each connected component independently.
- Initialize an auxiliary array visited[] as false to store the visited nodes in the DFS Traversal.
- Iterate over each vertex over the range [1, V] and perform the following steps:
- If the current vertex is not visited, i.e. visited[i] = false, then initialize currentGCD as 0.
- Perform DFS Traversal from the current vertex with the currentGCD value and update currentGCD value as the GCD of currentGCD and arr[i – 1] in each recursive call.
- If the value of currentGCD is greater than maxGCD, then update maxGCD as currentGCD.
- After completing the above steps, print the value of maxGCD as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the GCD of two// numbers a and bint gcd(int a, int b){    // Base Case    if (b == 0)        return a;Â
    // Recursively find the GCD    return gcd(b, a % b);}Â
// Function to perform DFS Traversalvoid depthFirst(int v, vector<int> graph[],                vector<bool>& visited,                int& currGCD,                vector<int> values){    // Mark the visited vertex as true    visited[v] = true;Â
    // Update GCD of current    // connected component    currGCD = gcd(currGCD, values[v - 1]);Â
    // Traverse all adjacent nodes    for (auto child : graph[v]) {Â
        if (visited[child] == false) {Â
            // Recursive call to perform            // DFS traversal            depthFirst(child, graph, visited,                       currGCD, values);        }    }}Â
// Function to find the maximum GCD// of nodes among all the connected// components of an undirected graphvoid maximumGcd(int Edges[][2], int E,                int V, vector<int>& arr){    vector<int> graph[V + 1];Â
    // Traverse the edges    for (int i = 0; i < E; i++) {Â
        int u = Edges[i][0];        int v = Edges[i][1];Â
        graph[u].push_back(v);        graph[v].push_back(u);    }Â
    // Initialize boolean array    // to mark visited vertices    vector<bool> visited(V + 1, false);Â
    // Stores the maximum GCD value    int maxGCD = INT_MIN;Â
    // Traverse all the vertices    for (int i = 1; i <= V; i++) {Â
        // If node is not visited        if (visited[i] == false) {Â
            // Stores GCD of current            // connected component            int currGCD = 0;Â
            // Perform DFS Traversal            depthFirst(i, graph, visited,                       currGCD, arr);Â
            // Update maxGCD            if (currGCD > maxGCD) {                maxGCD = currGCD;            }        }    }Â
    // Print the result    cout << maxGCD;}Â
// Driver Codeint main(){Â Â Â Â int E = 3, V = 5;Â Â Â Â vector<int> arr = { 23, 43, 123, 54, 2 };Â Â Â Â int Edges[][2] = { { 1, 3 }, { 2, 3 }, { 1, 2 } };Â
    maximumGcd(Edges, E, V, arr);Â
    return 0;} |
Java
// Java program for the above approachÂ
import java.io.*;import java.util.*;Â
class GFG {Â
  static int currGCD;Â
  // Function to find the GCD of two  // numbers a and b  static int gcd(int a, int b)  {    // Base Case    if (b == 0)      return a;Â
    // Recursively find the GCD    return gcd(b, a % b);  }Â
  // Function to perform DFS Traversal  static void depthFirst(int v,                         ArrayList<Integer> graph[],                         boolean visited[], int values[])  {    // Mark the visited vertex as true    visited[v] = true;Â
    // Update GCD of current    // connected component    currGCD = gcd(currGCD, values[v - 1]);Â
    // Traverse all adjacent nodes    for (int child : graph[v]) {Â
      if (visited[child] == false) {Â
        // Recursive call to perform        // DFS traversal        depthFirst(child, graph, visited, values);      }    }  }Â
  // Function to find the maximum GCD  // of nodes among all the connected  // components of an undirected graph  static void maximumGcd(int Edges[][], int E, int V,                         int arr[])  {Â
    ArrayList<Integer> graph[] = new ArrayList[V + 1];Â
    // Initialize the graph    for (int i = 0; i < V + 1; i++)      graph[i] = new ArrayList<>();Â
    // Traverse the edges    for (int i = 0; i < E; i++) {Â
      int u = Edges[i][0];      int v = Edges[i][1];Â
      graph[u].add(v);      graph[v].add(u);    }Â
    // Initialize boolean array    // to mark visited vertices    boolean visited[] = new boolean[V + 1];Â
    // Stores the maximum GCD value    int maxGCD = Integer.MIN_VALUE;Â
    // Traverse all the vertices    for (int i = 1; i <= V; i++) {Â
      // If node is not visited      if (visited[i] == false) {Â
        // Stores GCD of current        // connected component        currGCD = 0;Â
        // Perform DFS Traversal        depthFirst(i, graph, visited, arr);Â
        // Update maxGCD        if (currGCD > maxGCD) {          maxGCD = currGCD;        }      }    }Â
    // Print the result    System.out.println(maxGCD);  }     // Driver Code  public static void main(String[] args)  {Â
    int E = 3, V = 5;    int arr[] = { 23, 43, 123, 54, 2 };    int Edges[][] = { { 1, 3 }, { 2, 3 }, { 1, 2 } };Â
    maximumGcd(Edges, E, V, arr);  }}Â
// This code is contributed by Kingash. |
Python3
# Python 3 program for the above approachfrom math import gcdimport sysÂ
# Function to find the GCD of two# numbers a and bcurrGCD = 0Â
# Function to perform DFS Traversaldef depthFirst(v, graph, visited, values):    global currGCD         # Mark the visited vertex as true    visited[v] = TrueÂ
    # Update GCD of current    # connected component    currGCD = gcd(currGCD, values[v - 1])Â
    # Traverse all adjacent nodes    for child in graph[v]:        if (visited[child] == False):                       # Recursive call to perform            # DFS traversal            depthFirst(child, graph, visited, values)Â
# Function to find the maximum GCD# of nodes among all the connected# components of an undirected graphdef maximumGcd(Edges, E, V, arr):Â Â Â Â global currGCDÂ Â Â Â graph = [[] for i in range(V + 1)]Â
    # Traverse the edges    for i in range(E):        u = Edges[i][0]        v = Edges[i][1]Â
        graph[u].append(v)        graph[v].append(u)Â
    # Initialize boolean array    # to mark visited vertices    visited = [False for i in range(V+1)]Â
    # Stores the maximum GCD value    maxGCD = -sys.maxsize - 1Â
    # Traverse all the vertices    for i in range(1, V + 1, 1):               # If node is not visited        if (visited[i] == False):                       # Stores GCD of current            # connected component            currGCD = 0Â
            # Perform DFS Traversal            depthFirst(i, graph, visited, arr)Â
            # Update maxGCD            if (currGCD > maxGCD):                maxGCD = currGCDÂ
    # Print the result    print(maxGCD)Â
# Driver Codeif __name__ == '__main__':Â Â Â Â E = 3Â Â Â Â V = 5Â Â Â Â arr =Â [23, 43, 123, 54, 2]Â Â Â Â Edges =Â [[1, 3 ], [2, 3], [1, 2]]Â Â Â Â maximumGcd(Edges, E, V, arr)Â
    # This code is contributed by ipg2016107. |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG {         static int currGCD;    // Function to find the GCD of two  // numbers a and b  static int gcd(int a, int b)  {    // Base Case    if (b == 0)      return a;      // Recursively find the GCD    return gcd(b, a % b);  }    // Function to perform DFS Traversal  static void depthFirst(int v, List<List<int>> graph, bool[] visited, int[] values)  {    // Mark the visited vertex as true    visited[v] = true;      // Update GCD of current    // connected component    currGCD = gcd(currGCD, values[v - 1]);      // Traverse all adjacent nodes    foreach(int child in graph[v]) {        if (visited[child] == false) {          // Recursive call to perform        // DFS traversal        depthFirst(child, graph, visited, values);      }    }  }    // Function to find the maximum GCD  // of nodes among all the connected  // components of an undirected graph  static void maximumGcd(int[,] Edges, int E, int V, int[] arr)  {      List<List<int>> graph = new List<List<int>>();      // Initialize the graph    for (int i = 0; i < V + 1; i++)      graph.Add(new List<int>());      // Traverse the edges    for (int i = 0; i < E; i++) {        int u = Edges[i,0];      int v = Edges[i,1];        graph[u].Add(v);      graph[v].Add(u);    }      // Initialize boolean array    // to mark visited vertices    bool[] visited = new bool[V + 1];      // Stores the maximum GCD value    int maxGCD = Int32.MinValue;      // Traverse all the vertices    for (int i = 1; i <= V; i++) {        // If node is not visited      if (visited[i] == false) {          // Stores GCD of current        // connected component        currGCD = 0;          // Perform DFS Traversal        depthFirst(i, graph, visited, arr);          // Update maxGCD        if (currGCD > maxGCD) {          maxGCD = currGCD;        }      }    }      // Print the result    Console.WriteLine(maxGCD);  }       static void Main() {    int E = 3, V = 5;    int[] arr = { 23, 43, 123, 54, 2 };    int[,] Edges = { { 1, 3 }, { 2, 3 }, { 1, 2 } };      maximumGcd(Edges, E, V, arr);  }}Â
// This code is contributed by rameshtravel07. |
Javascript
<script>    // Javascript program for the above approach         let currGCD;       // Function to find the GCD of two    // numbers a and b    function gcd(a, b)    {      // Base Case      if (b == 0)        return a;Â
      // Recursively find the GCD      return gcd(b, a % b);    }Â
    // Function to perform DFS Traversal    function depthFirst(v, graph, visited, values)    {      // Mark the visited vertex as true      visited[v] = true;Â
      // Update GCD of current      // connected component      currGCD = gcd(currGCD, values[v - 1]);Â
      // Traverse all adjacent nodes      for(let child = 0; child < graph[v].length; child++) {Â
        if (visited[graph[v][child]] == false) {Â
          // Recursive call to perform          // DFS traversal          depthFirst(graph[v][child], graph, visited, values);        }      }    }         // Function to find the maximum GCD    // of nodes among all the connected    // components of an undirected graph    function maximumGcd(Edges, E, V, arr)    {Â
      let graph = [];Â
      // Initialize the graph      for (let i = 0; i < V + 1; i++)        graph.push([]);Â
      // Traverse the edges      for (let i = 0; i < E; i++) {Â
        let u = Edges[i][0];        let v = Edges[i][1];Â
        graph[u].push(v);        graph[v].push(u);      }Â
      // Initialize boolean array      // to mark visited vertices      let visited = new Array(V + 1);      visited.fill(false);Â
      // Stores the maximum GCD value      let maxGCD = Number.MIN_VALUE;Â
      // Traverse all the vertices      for (let i = 1; i <= V; i++) {Â
        // If node is not visited        if (visited[i] == false) {Â
          // Stores GCD of current          // connected component          currGCD = 0;Â
          // Perform DFS Traversal          depthFirst(i, graph, visited, arr);Â
          // Update maxGCD          if (currGCD > maxGCD) {            maxGCD = currGCD;          }        }      }Â
      // Print the result      document.write(maxGCD + "</br>");    }         let E = 3, V = 5;    let arr = [ 23, 43, 123, 54, 2 ];    let Edges = [ [ 1, 3 ], [ 2, 3 ], [ 1, 2 ] ];       maximumGcd(Edges, E, V, arr);         // This code is contributed by decode2207.</script> |
54
Â
Time Complexity: O((V + E) * log(M)), where M is the smallest element of the given array arr[].
Auxiliary Space: O(V)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


… [Trackback]
[…] Read More here on that Topic: geeksforgeeks.org/maximum-gcd-of-all-nodes-in-a-connected-component-of-an-undirected-graph/ […]