Category: cpp-queue

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

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