0% found this document useful (0 votes)
2 views3 pages

Graph Traversal Bipartite Code

The document contains C++ code implementing graph traversal algorithms including Depth-First Search (DFS) using both recursion and stack, and Breadth-First Search (BFS) using a queue. It also includes a function to check if a graph is bipartite using BFS. The main function reads the graph's edges, performs the traversals, and checks for bipartiteness, outputting the results accordingly.

Uploaded by

Saif Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Graph Traversal Bipartite Code

The document contains C++ code implementing graph traversal algorithms including Depth-First Search (DFS) using both recursion and stack, and Breadth-First Search (BFS) using a queue. It also includes a function to check if a graph is bipartite using BFS. The main function reads the graph's edges, performs the traversals, and checks for bipartiteness, outputting the results accordingly.

Uploaded by

Saif Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// Traversal algorithms for graphs

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6;

vector<bool> vis(N+1,false);
vector<int> adjlist[N+1];

// DFS using recursion


void DFS(int vertex) {
vis[vertex] = true;
cout << vertex << "\n";
for(int child : adjlist[vertex]) {
if(!vis[child]) {
DFS(child);
}
}
}

// DFS using stack


void DFS_stack(int vertex) {
stack<int> s;
s.push(vertex);
vis[vertex] = true;
while(!s.empty()) {
int current = s.top();
s.pop();
cout << current << "\n";
for(int child : adjlist[current]) {
if(!vis[child]) {
vis[child] = true;
s.push(child);
}
}
}
}

// BFS using queue


void BFS(int vertex) {
queue<int> q;
q.push(vertex);
vis[vertex] = true;
while(!q.empty()) {
int current = q.front();
q.pop();
cout << current << "\n";
for(int child : adjlist[current]) {
if(!vis[child]) {
vis[child] = true;
q.push(child);
}
}
}
}

// Finding if graph is bipartite or not using BFS


bool isBipartiteCheck(int vertex) {
vector<int> color(N+1, -1);
queue<int> q;
q.push(vertex);
color[vertex] = 0;

while(!q.empty()) {
int current = q.front();
q.pop();
for(int child : adjlist[current]) {
if(color[child] == -1) {
color[child] = 1 - color[current];
q.push(child);
} else if(color[child] == color[current]) {
return false;
}
}
}
return true;
}

int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adjlist[u].push_back(v);
adjlist[v].push_back(u);
}

for(int i = 1; i <= n; i++) {


if(!vis[i]) {
DFS(i);
}
}

fill(vis.begin(), vis.end(), false);

for(int i = 1; i <= n; i++) {


if(!vis[i]) {
BFS(i);
}
}

fill(vis.begin(), vis.end(), false);


bool isBipartite = true;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
if(!isBipartiteCheck(i)) {
isBipartite = false;
break;
}
}
}

if(isBipartite) cout << "Graph is Bipartite\n";


else cout << "Graph is Not Bipartite\n";

return 0;
}

You might also like