logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Check whether Graph is a Bipartite using DFS

Posted on December 31, 2013 by Manish
This C++ Program checks whether Graph is a Bipartite using DFS

Here is source code of the C++ Program to check whether Graph is a Bipartite using DFS. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Check whether Graph is a Bipartite using DFS
  3.  */
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <stack>
  7. #include <list>
  8. using namespace std;
  9. /*
  10.  * Class Declaration
  11.  */
  12. class Graph
  13. {
  14.     public:
  15.         int V;
  16.         list<int> *adj;
  17.         Graph(int V);
  18.         void addEdge(int v, int w);
  19. };
  20. /*
  21.  * Constructor
  22.  */
  23. Graph::Graph(int V)
  24. {
  25.     this->V = V;
  26.     adj = new list<int>[V];
  27. }
  28. /*
  29.  * Adding Edge to Graph
  30.  */
  31. void Graph::addEdge(int v, int w)
  32. {
  33.     adj[v].push_back(w);
  34.     adj[w].push_back(v);
  35. }
  36. /*
  37.  * Class Bipartite Declaration
  38.  */
  39. class Bipartite
  40. {
  41.     private:
  42.         bool isBipartite;
  43.         bool *color;
  44.         bool *marked;
  45.         int *edgeTo;
  46.         stack<int> cycle;
  47.     public:
  48.         Bipartite(Graph G)
  49.         {
  50.             isBipartite = true;
  51.             color = new bool [G.V];
  52.             marked = new bool [G.V];
  53.             edgeTo = new int [G.V];
  54.             for (int v = 0; v < G.V; v++)
  55.             {
  56.                 if (!marked[v])
  57.                 {
  58.                     color[v] = false;
  59.                     dfs(G, v);
  60.                 }
  61.             }
  62.         }
  63.         /*
  64.          * DFS
  65.          */
  66.         void dfs(Graph G, int v)
  67.         {
  68.             marked[v] = true;
  69.             list<int>::iterator w;
  70.             for (w = G.adj[v].begin(); w != G.adj[v].end(); w++)
  71.             {
  72.                 if (!cycle.empty())
  73.                     return;
  74.                 if (!marked[*w])
  75.                 {
  76.                     edgeTo[*w] = v;
  77.                     color[*w] = !color[v];
  78.                     dfs(G, *w);
  79.                 }
  80.                 else if (color[*w] == color[v])
  81.                 {
  82.                     isBipartite = false;
  83.                     cycle.push(*w);
  84.                     for (int x = v; x != *w; x = edgeTo[x])
  85.                     {
  86.                         cycle.push(x);
  87.                     }
  88.                     cycle.push(*w);
  89.                 }
  90.             }
  91.         }
  92.         /* 
  93.          * Returns true if graph is Bipartite
  94.          */
  95.         bool isBi()
  96.         {
  97.             return isBipartite;
  98.         }
  99. };
  100.  
  101. /*
  102.  * Main Contains Menu
  103.  */
  104. int main()
  105. {
  106.     Graph g1(4);
  107.     g1.addEdge(0, 1);
  108.     g1.addEdge(0, 3);
  109.     g1.addEdge(1, 0);
  110.     g1.addEdge(1, 2);
  111.     g1.addEdge(2, 1);
  112.     g1.addEdge(2, 3);
  113.     g1.addEdge(3, 2);
  114.     g1.addEdge(3, 0);
  115.     Bipartite b(g1);
  116.     if (b.isBi())
  117.         cout<<"The graph is Bipartite"<<endl;
  118.     else
  119.         cout<<"The graph is not Bipartite"<<endl;
  120. }

$ g++ bipartite_dfs.cpp
$ a.out
The graph is Bipartite
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.
« Prev Page - C++ Program to Check whether Graph is a Bipartite using BFS
» Next Page - C++ Program to Check whether Graph is a Bipartite using 2 Color Algorithm
« C++ Program to Check whether Graph is a Bipartite using BFS
C++ Program to Check whether Graph is a Bipartite using 2 Color Algorithm »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples on Data Structures
  2. Java Algorithms, Problems & Programming Examples
  3. C++ Algorithms, Problems & Programming Examples
  4. C Algorithms, Problems & Programming Examples
  5. C Programming Examples on Graph Problems & Algorithms
  6. Java Programming Examples on Graph Problems & Algorithms
  7. C++ Programming Examples on Graph Problems & Algorithms
  8. Java Programming Examples on Hard Graph Problems & Algorithms
  9. C++ Programming Examples on Hard Graph Problems & Algorithms
  10. C Programming Examples on Hard Graph Problems & Algorithms
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry