Category: graph-connectivity

Print all Hamiltonian Cycles in an Undirected Graph

  import java.util.ArrayList;class GFG {                  boolean isSafe(int v, int graph[][],                   ArrayList path,                   int pos)    {                                if (graph[path.get(pos – 1)][v]            == 0)            return false;                          for (int i = 0; i < pos; i++)            if (path.get(i) == v)                return false;                          return true;    }              boolean hasCycle;              void hamCycle(int graph[][])    {                        hasCycle = false;                  ArrayList path            = new ArrayList();        path.add(0);                          boolean[] visited            = new boolean[graph.length];          for (int i = 0;             i < visited.length; i++)            visited[i] = false;          visited[0] = true;                          FindHamCycle(graph, 1, path,                     visited);          if (!hasCycle) {                                                  System.out.println(                "No Hamiltonian […]

Continue Reading

Minimum degree of three nodes forming a triangle in a given Graph

  #include using namespace std;  int minTrioDegree(int N,                  vector& Edges){            int degree[N + 1] = { 0 };              int adj[N + 1][N + 1] = { 0 };              for (int i = 0; i < Edges.size(); i++) {                          int u = Edges[i][0];        int v = Edges[i][1];                          adj[u][v] = adj[u][v] = 1;                          degree[u]++;        degree[v]++;    }          int ans = INT_MAX;          for (int i = 1; i

Continue Reading

Union and Intersection of two Graphs

#include using namespace std;  void find_union(    vector G1,    vector G2){        map added;          vector G;              for (auto p : G1) {          string a = get(p);                  int b = get(p);        int c = get(p);                          G.push_back(            make_tuple(a, b, c));        added[a] = { b, c };    }              for (auto p : G2) {          string a = get(p);        int b = get(p);        int c = get(p);          pair x = { b, c };        pair y = { c, b […]

Continue Reading

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum

#include using namespace std;    int maximumAND(int arr[], int n, int m) {               int tot = 1 > i) & 1) {                                  if (count == 0) {                                          andans = arr[i];                 }                 else {                                                               andans = andans & arr[i];                 }                                                      count++;             }         }                              if […]

Continue Reading

Find a set of at most N/2 nodes from a Graph such that all remaining nodes are directly connected to one of the chosen nodes

#include using namespace std;    void addEdge(vector& adj,              int u, int v) {     adj[u].push_back(v);     adj[v].push_back(u); }    vector BFS(     int N, int source,     vector adjlist) {               int dist[N + 1];        vector vertex_set;                  memset(dist, -1, sizeof dist);                  vertex_set.assign(2, vector(0));             queue Q;             Q.push(source);     dist […]

Continue Reading