Graph

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…

Find regions with most common region size in a given boolean matrix

#include using namespace std;#define ROW 4#define COL 5  int isSafe(int M[][COL], int row, int col,           bool visited[][COL]){    return (row >= 0) && (row <…

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…

Construct a Graph from size of components for each node

#include using namespace std;  int constructConnectedComponent(int A[], int N){              bool flag = false;                  map mp;    for (int i = 0; i < N; i++)…

Shortest distance between two nodes in Graph by reducing weight of an edge by half

#include using namespace std;  vector graph[100001];  vector edges;  void add_edge(int u, int v, int w){    graph[u].push_back({ v, w });    graph[v].push_back({ u, w });    edges.push_back({ u, v,…

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&…

Difference between the shortest and second shortest path in an Unweighted Bidirectional Graph

#include using namespace std;  void get_edges(int s, vector& edges, vector p){    if (s == -1)        return;    get_edges(p[s], edges, p);    edges.push_back(s);}  void dist_helper(vector graph, vector& d,                 int v1,…

Print Adjacency List for a Directed Graph

An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of…