Category: graph-cycle

Check if a cycle exists between nodes S and T in an Undirected Graph with only S and T repeating

  #include using namespace std;  void createGraph(vector& graph,                 vector& edges){    for (auto x : edges) {                          graph[x[0]].push_back(x[1]);        graph[x[1]].push_back(x[0]);    }}  bool findSimpleCycle(int cur,                     vector& graph,                     int start, int dest,                     vector& visited,                     bool flag){          if (!flag and cur == dest) {        dest = start;        flag = 1;    }                  if (flag and cur == dest) {        return true;    }      bool ans = 0;    visited[cur] = 1;    for (auto child : graph[cur]) {                                  if (!visited[child] or child == dest) {            ans = ans                  | […]

Continue Reading

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