0% found this document useful (0 votes)
3 views9 pages

Graph

The document provides various algorithms for graph traversal and cycle detection, including Breadth First Search (BFS) and Depth First Search (DFS) implementations. It also discusses methods for detecting cycles in both undirected and directed graphs, as well as techniques for determining if a graph is bipartite. Additionally, it outlines approaches for topological sorting using both DFS and BFS, and mentions the criteria for bipartiteness and finding the shortest cycle in undirected cyclic graphs.

Uploaded by

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

Graph

The document provides various algorithms for graph traversal and cycle detection, including Breadth First Search (BFS) and Depth First Search (DFS) implementations. It also discusses methods for detecting cycles in both undirected and directed graphs, as well as techniques for determining if a graph is bipartite. Additionally, it outlines approaches for topological sorting using both DFS and BFS, and mentions the criteria for bipartiteness and finding the shortest cycle in undirected cyclic graphs.

Uploaded by

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

Algorithms:

[1] Simple BFS:

class Solution {
public:
// Function to return Breadth First Traversal of given graph.
vector<int> bfsOfGraph(int V, vector<int> adj[]) {
int vis[V] = {0};
vis[0] = 1;
queue<int> q;
// push the initial starting node
q.push(0);
vector<int> bfs;
// iterate till the queue is empty
while(!q.empty()) {
// get the topmost element in the queue
int node = q.front();
q.pop();
bfs.push_back(node);
// traverse for all its neighbours
for(auto it : adj[node]) {
// if the neighbour has previously not been visited,
// store in Q and mark as visited
if(!vis[it]) {
vis[it] = 1;
q.push(it);
}
}
}
return bfs;
}
};

[2]Simple DFS:

class Solution {
private:
void dfs(int node, vector<int> adj[], int vis[], vector<int>
&ls) {
vis[node] = 1;
ls.push_back(node);
// traverse all its neighbours
for(auto it : adj[node]) {
// if the neighbour is not visited
if(!vis[it]) {
dfs(it, adj, vis, ls);
}
}
}

[3] Cycle detection (undirected graph) using bfs

class Solution {
private:
bool detect(int src, vector<int> adj[], int vis[]) {
vis[src] = 1;
// store <source node, parent node>
queue<pair<int,int>> q;
q.push({src, -1});
// traverse until queue is not empty
while(!q.empty()) {
int node = q.front().first;
int parent = q.front().second;
q.pop();

// go to all adjacent nodes


for(auto adjacentNode: adj[node]) {
// if adjacent node is unvisited
if(!vis[adjacentNode]) {
vis[adjacentNode] = 1;
q.push({adjacentNode, node});
}
// if adjacent node is visited and is not it's own parent
node
else if(parent != adjacentNode) {
// yes it is a cycle
return true;
}
}
}
// there's no cycle
return false;
}

[4] cycle detection(undirected) dfs:


[5] cycle detection using dfs(directed graph):

class Solution {
private:
bool dfsCheck(int node, vector<int> adj[], int vis[], int
pathVis[]) {
vis[node] = 1;
pathVis[node] = 1;

// traverse for adjacent nodes


for (auto it : adj[node]) {
// when the node is not visited
if (!vis[it]) {
if (dfsCheck(it, adj, vis, pathVis) ==
true)
return true;
}
// if the node has been previously visited
// but it has to be visited on the same path
else if (pathVis[it]) {
return true;
}
}

pathVis[node] = 0;
return false;
}

[6] bipartite graph using dfs:

class Solution {
private:
bool dfs(int node, int col, int color[], vector<int> adj[]) {
color[node] = col;

// traverse adjacent nodes


for(auto it : adj[node]) {
// if uncoloured
if(color[it] == -1) {
if(dfs(it, !col, color, adj) == false) return false;
}
// if previously coloured and have the same colour
else if(color[it] == col) {
return false;
}
}

return true;
}
[7] bipartite graph using bfs:

bool bfs(vector<vector<int>>&adj,int start){


int n=adj.size();
vector<int>color(n,-1);

queue<int>q;
color[start]=0;
q.push(start);
while(!q.empty()){
int node=q.front();
q.pop();
for(auto nbr:adj[node]){
if(color[nbr]==-1){
color[nbr]=!color[node];
q.push(nbr);
}
else if(color[nbr]==color[node]){
return false;
}
}
}
return true;
}

[8] toposort using dfs:

class Solution {
private:
void dfs(int node, int vis[], stack<int> &st,
vector<int> adj[]) {
vis[node] = 1;
for (auto it : adj[node]) {
if (!vis[it]) dfs(it, vis, st, adj);
}
st.push(node);
}

[9] toposort using bfs:

class Solution {
public:
//Function to return list containing vertices in Topological
order.
vector<int> topoSort(int V, vector<int> adj[])
{
int indegree[V] = {0};
for (int i = 0; i < V; i++) {
for (auto it : adj[i]) {
indegree[it]++;
}
}

queue<int> q;
for (int i = 0; i < V; i++) {
if (indegree[i] == 0) {
q.push(i);
}
}
vector<int> topo;
while (!q.empty()) {
int node = q.front();
q.pop();
topo.push_back(node);
// node is in your topo sort
// so please remove it from the indegree

for (auto it : adj[node]) {


indegree[it]--;
if (indegree[it] == 0) q.push(it);
}
}

return topo;
}
};

[1] for a bipartite graph if nodes can be partitioned into two independent sets A and sets B such that
every edge in the graph connects a node A in set A and a node B in set B

-> no two adjacent nodes do not have same node

->if cycle length is even it is bipartite graph

-> if graph have no cycle than it is bipartite graph

-> odd length cycle is not bipartite graph

[2] Shortest cycle length in undirected cyclic graph:

To find shortest length we can’t find with normal dfs we have to use logic of directed graph cycle
detection otherwise go with bfs traversal with formula dist[node]+dist[nbr]+1;

You might also like