Category: Graph Traversals

Level order traversal by converting N-ary Tree into adjacency list representation with K as root node

#include using namespace std;  struct Node {    int data;    vector child;};  Node* newNode(int key){    Node* temp = new Node;    temp->data = key;    return temp;}  vector adj;  void DFS(struct Node* node){        for (auto x : node->child) {        if (x != NULL) {                                      adj[node->data].push_back(x->data);            adj[x->data].push_back(node->data);                          DFS(x);        }    }}  void levelOrderTrav(int s, int N){            queue q;              vector visited(N);      q.push(s);          q.push(-1);    visited[s] = true;    while (!q.empty()) {                  int v = q.front();        q.pop();                  if (v == -1) {            if (!q.empty())                q.push(-1);                          cout child).push_back(newNode(4));    (root->child).push_back(newNode(5));    (root->child[0]->child).push_back(newNode(6));    (root->child[0]->child).push_back(newNode(7));    (root->child[2]->child).push_back(newNode(8));    (root->child[3]->child).push_back(newNode(9));    (root->child[3]->child).push_back(newNode(10));    (root->child[3]->child).push_back(newNode(11));    int N = 11;    int K = 5;    adj.resize(N […]

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

Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1

#include using namespace std;  #define M 3#define N 3  void findHeightMatrixUtil(int mat[][N],                          int height[M][N]){        queue q;          int vis[M][N] = { 0 };          for (int i = 0; i < M; i++) {        for (int j = 0; j < N; j++) {            if (mat[i][j] == 1) {                q.push({ i, j });                height[i][j] = 0;                vis[i][j] = 1;            }        }    }          while (q.empty() == 0) {          pair k = q.front();        q.pop();                          int x = […]

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

Maximum cost path from source node to destination node via at most K intermediate nodes

  #include using namespace std;  int findShortestPath(    int n, vector& edges,    int src, int dst, int K){        vector adjlist(        n, vector());          queue q;      unordered_map mp;              int ans = INT_MIN;          for (int i = 0; i < edges.size(); i++) {          auto edge = edges[i];          adjlist[edge[0]].push_back(            make_pair(edge[1], edge[2]));    }          q.push({ src, 0 });      int level = 0;                  while (!q.empty() && level < K + 2) {                  int sz = q.size();          for (int i = 0; i […]

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