Category: Graph

DSatur Algorithm for Graph Coloring

  #include #include #include #include using namespace std;  struct nodeInfo {    int sat;     int deg;     int vertex; };struct maxSat {    bool operator()(const nodeInfo& lhs,                    const nodeInfo& rhs) const    {                                        return tie(lhs.sat, lhs.deg, lhs.vertex)               > tie(rhs.sat, rhs.deg, rhs.vertex);    }};  class Graph {          int n;          vector adj;  public:        Graph(int numNodes)    {        n = numNodes;        adj.resize(n, vector());    }    ~Graph() { adj.clear(); }          void addEdge(int u, int v);              void DSatur();};  void Graph::addEdge(int u, int v){    adj[u].push_back(v);    adj[v].push_back(u);}  void Graph::DSatur(){    int u, i;    vector used(n, false);    vector c(n), […]

Continue Reading

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

Count of distinct groups of strings formed after performing equivalent operation

#include using namespace std;  int Find(vector& parent, int a){    return parent[a]           = (parent[a] == a ? a : Find(parent, parent[a]));}  void Union(vector& parent,           vector& rank, int a,           int b){          a = Find(parent, a);    b = Find(parent, b);          if (rank[a] == rank[b])        rank[a]++;    if (rank[a] > rank[b])        parent[b] = a;    else        parent[a] = b;}  void numOfDistinctStrings(string arr[],                          int N){            vector parent(27);          vector rank(27, 0);      for (int j = 0; j < 27; j++) {                parent[j] = […]

Continue Reading

Maximum element in connected component of given node for Q queries

  #include using namespace std;  int Find(vector& parent, int a){    return parent[a] = (parent[a] == a)                           ? a                           : (Find(parent, parent[a]));}  void Union(vector& parent,           vector& rank,           vector& maxValue,           int a, int b){      a = Find(parent, a);    b = Find(parent, b);      if (a == b)        return;          if (rank[a] == rank[b]) {        rank[a]++;    }      if (rank[a] < rank[b]) {        int temp = a;        a = b;        b = temp;    }      parent[b] = a;          maxValue[a] = max(maxValue[a],                      maxValue[b]);}  void findMaxValueOfSet(    vector& arr,    vector& queries, int […]

Continue Reading

Implementation of Johnson’s algorithm for all-pairs shortest paths

import java.util.ArrayList;import java.util.Arrays;  public class Graph {    private static class Neighbour {        int destination;        int weight;          Neighbour(int destination, int weight)        {            this.destination = destination;            this.weight = weight;        }    }      private int vertices;    private final ArrayList        adjacencyList;                  public Graph(int vertices)    {        this.vertices = vertices;          adjacencyList = new ArrayList(vertices);        for (int i = 0; i < vertices; i++)            adjacencyList.add(new ArrayList());    }                  public Graph(int vertices, int[][] adjacencyMatrix)    {        this(vertices);          for (int i = 0; i < vertices; i++) {            for (int j = […]

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

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 < ROW) && (col >= 0)           && (col < COL)           && (M[row][col] && !visited[row][col]);}  void DFS(int M[][COL], int row, int col,         bool visited[][COL], int& count){            static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };    static int colNbr[] = { -1, […]

Continue Reading