Skip to content

Commit

Permalink
added bipartite check in graph class
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Jan 20, 2024
1 parent 9bad71a commit 83a1a02
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ int main() {
std::cout << x << " ";
}
std::cout << '\n';

graph<char> g3("directed", { {'a', 'b'}, {'c', 'd'}, {'b','c'} });
std::cout << "Graph g3 is: " << g3 << '\n';
}
92 changes: 91 additions & 1 deletion src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ template <typename T> class graph {
__elements.insert(v);
}

size_t size();

std::vector<T> dfs(T start);

std::vector<T> bfs(T start);
Expand All @@ -55,12 +57,27 @@ template <typename T> class graph {

std::vector<T> topological_sort();

bool bipartite();

friend std::ostream & operator <<(std::ostream &out, graph<T> &g){
out << '{';

std::vector<T> elements = g.topological_sort();
for(T &x : elements){
out << x << ' ';
}
out << '}' << '\n';
return out;
}

private:
std::unordered_map<T, std::vector<T>> adj;
std::unordered_set<T> __elements;
std::string __type;
};

template<typename T> size_t graph<T>::size(){return __elements.size();}

template <typename T> std::vector<T> graph<T>::dfs(T start) {
std::stack<T> s;
std::vector<T> path;
Expand Down Expand Up @@ -197,6 +214,35 @@ template <typename T> std::vector<T> graph<T>::topological_sort() {
return top_sort;
}

template <typename T> bool graph<T>::bipartite(){
std::unordered_map<T, int> color;
std::queue<std::pair<T, int> > q;

for(T x : __elements){
if(color.find(x) == color.end()){
q.push({x, 0});
color[x] = 0;
while(!q.empty()){
std::pair<T, int> current = q.front();
q.pop();
T v = current.first;
int col = current.second;
for(T & x : adj[v]){
if(color.find(x) != color.end() && color[x] == col){
return false;
}
if(color.find(x) == color.end()){
color[x] = (col) ? 0 : 1;
q.push({x, color[x]});
}
}
}
}
}
return true;
}


template <typename T> class weighted_graph {
public:
weighted_graph(std::string __type,
Expand Down Expand Up @@ -232,6 +278,8 @@ template <typename T> class weighted_graph {
__elements.insert(v);
}

size_t size();

std::vector<T> dfs(T start);

std::vector<T> bfs(T start);
Expand All @@ -246,12 +294,26 @@ template <typename T> class weighted_graph {

int64_t prim(T start);

bool bipartite();

friend std::ostream &operator <<(std::ostream &out, weighted_graph<T> &g){
out << '{';
std::vector<T> elements = g.topological_sort();
for(T &x : elements){
out << x << ' ';
}
out << '}' << '\n';
return out;
}

private:
std::unordered_map<T, std::vector<std::pair<T, int64_t>>> adj;
std::string __type;
std::unordered_set<T> __elements;
};

template <typename T> size_t weighted_graph<T>::size(){return __elements.size();}

template <typename T> int64_t weighted_graph<T>::shortest_path(T start, T end) {
if (__elements.find(start) == __elements.end()) {
std::cout << "Element: " << start << " is not found in the Graph" << '\n';
Expand Down Expand Up @@ -475,4 +537,32 @@ template <typename T> int64_t weighted_graph<T>::prim(T __temp) {
return cost;
}

#endif
template <typename T> bool weighted_graph<T>::bipartite(){
std::unordered_map<T, int> color;
std::queue<std::pair<T, int> > q;

for(T x : __elements){
if(color.find(x) == color.end()){
q.push({x, 0});
color[x] = 0;
while(!q.empty()){
std::pair<T, int> current = q.front();
q.pop();
T v = current.first;
int col = current.second;
for(std::pair<T, int64_t> & x : adj[v]){
if(color.find(x.first) != color.end() && color[x.first] == col){
return false;
}
if(color.find(x.first) == color.end()){
color[x.first] = (col) ? 0 : 1;
q.push({x.first, color[x.first]});
}
}
}
}
}
return true;
}

#endif
9 changes: 9 additions & 0 deletions tests/graph/graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ TEST_CASE("testing topological sorting") {
std::vector<int> v1 = {2, 0, 1, 3, 4};
REQUIRE(g.topological_sort() == v1);
}

TEST_CASE("testing bipartite check"){
graph<char> g("undirected");
g.add_edge('a', 'b');
g.add_edge('b', 'c');
g.add_edge('c', 'd');
g.add_edge('d', 'a');
REQUIRE(g.bipartite() == true);
}
12 changes: 11 additions & 1 deletion tests/graph/weighted_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ TEST_CASE("testing mst with prim's algo") {
g.add_edge(1, 6, 1);
g.add_edge(3, 2, 4);
REQUIRE(g.prim(0) == 3);
}
}

TEST_CASE("testing bipartite graph"){
weighted_graph<char> g("undirected");
g.add_edge('a', 'b', 1);
g.add_edge('b', 'c', 2);
g.add_edge('c', 'd', 3);
g.add_edge('d', 'a', 4);
REQUIRE(g.bipartite() == true);

}

0 comments on commit 83a1a02

Please sign in to comment.