Category: BFS

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

Maximize difference between pair of nodes in a given rooted tree such that one node is ancestor of another

#include using namespace std;  vector tree;vector visited;vector ancestorNum;  int maxDiff = INT_MIN;  void dfs(int src, int val, vector& W){          visited[src] = true;          for (auto neighbour : tree[src]) {                                  if (!visited[neighbour]            && (ancestorNum[neighbour]                > ancestorNum[src])) {                          maxDiff = max(                val – W[neighbour – 1],                maxDiff);                          dfs(neighbour, val, W);        }    }}  void bfs(int src, int N){            visited.assign(N, false);          queue q;              ancestorNum[src] = 0;          visited[src] = true;          q.push(src);          while (!q.empty()) {                  int cur = q.front();        q.pop();                  for (auto neighbour : tree[cur]) {                                      if […]

Continue Reading

Minimize steps required to convert number N to M using arithmetic operators

  #include using namespace std;  string changeNtoM(int N, int M){        if (N == M) {        return ” “;    }          if (M == 0) {        return “-“;    }          queue q;          unordered_map visited;          q.push({ N, “” }), visited[N] = 1;          q.push({ 1, “/” }), visited[1] = 1;          while (!q.empty()) {                pair cur = q.front();        q.pop();                                  if (cur.first == M) {                        return cur.second;        }                          if (!visited[cur.first + cur.first]            && cur.first + cur.first

Continue Reading

Find all the Mother Vertices of a graph

  #include using namespace std;  void dfs_helper(int u,                vector& adj,                bool visited[]){    if (visited[u])        return;      visited[u] = true;      for (auto v : adj[u]) {        if (!visited[v])            dfs_helper(v, adj, visited);    }}  void getTransposeGraph(    vector& adj,    vector& trans_adj){    for (int u = 0; u < adj.size(); u++) {        for (auto v : adj[u]) {            trans_adj[v].push_back(u);        }    }}  void initialize_visited(bool visited[], int n){    for (int u = 0; u < n; u++)        visited[u] = false;}  vector findAllMotherVertices(    vector& adj){    int n = adj.size();    bool […]

Continue Reading

Test Case Generation | Set 6 (Random Unweighted Binary Tree)

  #include using namespace std;    vector generateBinaryTree(int n){                        vector options = { 0, 1, 2 };                        map mp;                queue q;            q.push(1);            vector v;                int count = 1;                mp[1] = 1;            while (!q.empty() or count < n) {                                            int front;                  if(!q.empty()) {                                                    front = q.front();                                                                q.pop();        }                                                          int numberOfChilds          = options[rand() % (options.size())];                            if (count >= n)            continue;                                        while (numberOfChilds–) {                                                                int child = rand() % n + 1;                                        while (mp[child]) {                child++;                if (child > n) {                    child = 1;                }            }                                        count++;                            mp[child] […]

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

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

Construct a Tree whose sum of nodes of any path is not divisible by the count of nodes in that path

   #include using namespace std;    void assignValues(int Edges[][2], int n) {          vector tree[n + 1];             for (int i = 0; i < n - 1; i++) {            int u = Edges[i][0];         int v = Edges[i][1];         tree[u].push_back(v);         tree[v].push_back(u);     }                  vector visited(n + 1, false);             vector answer(n […]

Continue Reading