Skip to content

Add Bipartite checking algorithm #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/main/cpp/algorithms/graphtheory/BipartiteChecking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

#include <iostream>
#include <queue>
#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 <int> 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;
}
Binary file not shown.
Binary file not shown.
112 changes: 112 additions & 0 deletions src/main/cpp/algorithms/other/boruvka_minimum_spanning_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <iostream>
#include <vector>

const int MAXN = 10000;
const int inf = 0x3fffffff;

int nodeCount, edgesCount;
std::vector<std::pair<int, int>> 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<int, int> 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<int, int> 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<int, int> 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;
}