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

# Graph

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

### Corona Virus | TCS Codevita 2020

#include using namespace std;#define row 9#define col 20 void coronaVirus(char arr[row][col]){ char inverted[9][20]; char temp[9][20]; int count = 0; int total = 0; for (int i…

### Hungarian Algorithm for Assignment Problem | Set 2 (Implementation)

Given a 2D array, arr of size N*N where arr[i][j] denotes the cost to complete the jthjob by the ith…