Wednesday, 1 Dec 2021
Category: Graph Traversals

#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 […]

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 […]

Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect squareGiven a positive integer N, the task is to find the number of unique permutations of first N natural numbers having sum of the adjacent elements equal to a perfect square.Examples:Input: N = 17Output: 2Explanation:Following permutations have sum of […]

#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 = […]

#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

#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 […]

#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 […]

#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 […]