Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
is_graph_bipartite.cpp File Reference

Algorithm to check whether a graph is bipartite More...

#include <iostream>
#include <vector>
#include <queue>
Include dependency graph for is_graph_bipartite.cpp:

Classes

class  graph::is_graph_bipartite::Graph
 Class for representing graph as an adjacency list. More...
 

Namespaces

 graph
 Graph algorithms.
 
 is_graph_bipartite
 Functions for checking whether a graph is bipartite or not.
 

Functions

static void test ()
 namespace graph More...
 
int main ()
 

Detailed Description

Algorithm to check whether a graph is bipartite

A graph is a collection of nodes also called vertices and these vertices are connected by edges.A bipartite graph is a graph whose vertices can be divided into two disjoint and independent sets U and V such that every edge connects a vertex in U to one in V.

The given Algorithm will determine whether the given graph is bipartite or not

Example - Here is a graph g1 with 5 vertices and is bipartite
    1   4
   / \ / \
  2   3   5
Example - Here is a graph G2 with 3 vertices and is not bipartite
    1 --- 2
     \   /
       3
Author
Akshat Vaya

Function Documentation

◆ main()

int main ( )

Main function

Testing

160  {
161  test(); ///Testing
162  return 0;
163 }
Here is the call graph for this function:

◆ test()

static void test ( )
static

namespace graph

Function to test the above algorithm

Returns
none

creating graph G1 with 5 vertices

adding edges to the graphs as per the illustrated example

creating graph G2 with 3 vertices

adding edges to the graphs as per the illustrated example

checking whether the graphs are bipartite or not

129  {
130  graph::is_graph_bipartite::Graph G1(5); /// creating graph G1 with 5 vertices
131  /// adding edges to the graphs as per the illustrated example
132  G1.addEdge(1,2);
133  G1.addEdge(1,3);
134  G1.addEdge(3,4);
135  G1.addEdge(4,5);
136 
137  graph::is_graph_bipartite::Graph G2(3); /// creating graph G2 with 3 vertices
138  /// adding edges to the graphs as per the illustrated example
139  G2.addEdge(1,2);
140  G2.addEdge(1,3);
141  G2.addEdge(2,3);
142 
143  /// checking whether the graphs are bipartite or not
144  if(G1.is_bipartite()){
145  std::cout<<"The given graph G1 is a bipartite graph\n";
146  }
147  else{
148  std::cout<<"The given graph G1 is not a bipartite graph\n";
149  }
150  if(G2.is_bipartite()){
151  std::cout<<"The given graph G2 is a bipartite graph\n";
152  }
153  else{
154  std::cout<<"The given graph G2 is not a bipartite graph\n";
155  }
156 }
Here is the call graph for this function:
test
static void test()
namespace graph
Definition: is_graph_bipartite.cpp:129
std::cout
graph::is_graph_bipartite::Graph
Class for representing graph as an adjacency list.
Definition: is_graph_bipartite.cpp:49