diff --git a/src/main/cpp/algorithms/graphtheory/BipartiteChecking.cpp b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.cpp new file mode 100644 index 0000000..589b0c6 --- /dev/null +++ b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.cpp @@ -0,0 +1,78 @@ + +#include +#include +#define V 4 + +using namespace std; + +bool isBipartite(int G[][V], int src) +{ + int colorArr[V]; + for (int i = 0; i < V; ++i) + colorArr[i] = -1; + + // Assign first color to source + colorArr[src] = 1; + + // Create a queue (FIFO) of vertex + // numbers and enqueue source vertex + // for BFS traversal + queue q; + q.push(src); + + // Run while there are vertices + // in queue (Similar to BFS) + while (!q.empty()) + { + // Dequeue a vertex from queue ( Refer http://goo.gl/35oz8 ) + int u = q.front(); + q.pop(); + + // Return false if there is a self-loop + if (G[u][u] == 1) + return false; + + // Find all non-colored adjacent vertices + for (int v = 0; v < V; ++v) + { + // An edge from u to v exists and + // destination v is not colored + if (G[u][v] && colorArr[v] == -1) + { + // Assign alternate color to this adjacent v of u + colorArr[v] = 1 - colorArr[u]; + q.push(v); + } + + // An edge from u to v exists and destination + // v is colored with same color as u + else if (G[u][v] && colorArr[v] == colorArr[u]) + return false; + } + } + + // If we reach here, then all adjacent + // vertices can be colored with alternate color + return true; +} + +// Driver program to test above function +int main() +{ + int G[][V] = {{0, 1, 0, 1}, + {1, 0, 1, 0}, + {0, 1, 0, 1}, + {1, 0, 1, 0} + }; + + isBipartite(G, 0) ? cout << "Yes\n" : cout << "No\n"; + + int GN[][V] = {{1, 1, 0, 1}, + {1, 0, 1, 1}, + {0, 1, 0, 1}, + {1, 0, 1, 0} + }; + + isBipartite(GN, 0) ? cout << "Yes\n" : cout << "No\n"; + return 0; +} diff --git a/src/main/cpp/algorithms/graphtheory/BipartiteChecking.exe b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.exe new file mode 100644 index 0000000..4dbbca0 Binary files /dev/null and b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.exe differ diff --git a/src/main/cpp/algorithms/graphtheory/BipartiteChecking.o b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.o new file mode 100644 index 0000000..22a25aa Binary files /dev/null and b/src/main/cpp/algorithms/graphtheory/BipartiteChecking.o differ diff --git a/src/main/cpp/algorithms/other/boruvka_minimum_spanning_tree.cpp b/src/main/cpp/algorithms/other/boruvka_minimum_spanning_tree.cpp new file mode 100644 index 0000000..fcbd644 --- /dev/null +++ b/src/main/cpp/algorithms/other/boruvka_minimum_spanning_tree.cpp @@ -0,0 +1,112 @@ +#include +#include + +const int MAXN = 10000; +const int inf = 0x3fffffff; + +int nodeCount, edgesCount; +std::vector> graf[MAXN]; + +void readGraph() +{ + std::cin >> nodeCount >> edgesCount; + for (int i = 0; i < edgesCount; i++) + { + int x, y, w; + std::cin >> x >> y >> w; + graf[x].push_back({y, w}); + graf[y].push_back({x, w}); + } +} + +int parent[MAXN], rank[MAXN]; +// Initialize the forest of trees +void initDisjoint() +{ + for (int i = 0; i < nodeCount; i++) + { + parent[i] = i; + rank[i] = 0; + } +} + +// Get set representative +int getSR(int x) +{ + if (x == parent[x]) + return x; + return parent[x] = getSR(parent[x]); +} + +int areUnited(int x, int y) +{ + return getSR(x) == getSR(y); +} + +void unite(int x, int y) +{ + x = getSR(x); + y = getSR(y); + if (rank[x] > rank[y]) + parent[y] = x; + else + { + parent[x] = y; + if (rank[x] == rank[y]) + rank[y]++; + } +} + +std::pair cheapEdge[MAXN]; +int solve() +{ + int numberOfTrees = nodeCount; + int weightSum = 0; + initDisjoint(); + + while (numberOfTrees > 1) + { + // initializing cheapest edge to "none" + for (int i = 0; i < nodeCount; i++) + cheapEdge[i].first = -1; + for (int i = 0; i < nodeCount; i++) + for (size_t j = 0; j < graf[i].size(); j++) + { + std::pair edge = graf[i][j]; + int xsr = getSR(i); + int ysr = getSR(edge.first); + int w = edge.second; + if (xsr == ysr) + continue; + if (cheapEdge[xsr].first == -1 || + w < graf[cheapEdge[xsr].first][cheapEdge[xsr].second].second) + cheapEdge[xsr] = {i, j}; + if (cheapEdge[ysr].first == -1 || + w < graf[cheapEdge[ysr].first][cheapEdge[ysr].second].second) + cheapEdge[ysr] = {i, j}; + } + for (int i = 0; i < nodeCount; i++) + { + int xsr = getSR(i); + if (cheapEdge[xsr].first == -1) + continue; + std::pair edge = graf[cheapEdge[xsr].first][cheapEdge[xsr].second]; + int ysr = getSR(edge.first); + if (xsr == ysr) + continue; + weightSum += edge.second; + unite(xsr, ysr); + numberOfTrees--; + } + } + + return weightSum; +} + +int main() +{ + readGraph(); + int weight = solve(); + std::cout << "The weight of the minimum spanning tree is " << weight; + return 0; +} \ No newline at end of file