Graph Thoery
Graph Thoery
// preorder
visited[node] = true;
cout<<node<<" ";
if(visited[*it] == false)
dfs(*it, v);
// postorder
// cout<<node<<" ";
queue<int> pq;
pq.push(node);
visited[node] = true;
while(!pq.empty())
int t= pq.front();
pq.pop();
cout<<t<<" ";
vector<int> :: iterator it;
if(visited[*it]);
else{
visited[*it] = true;
pq.push(*it);
int main(){
int n, m;
vector<vector<int>> v(n);
int x, y;
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
dfs(0, v);
cout<<endl;
bfs(0, v);
return 0;
}
Topology sort (Kahn's Algorithm)
int n, m;
cin>>n>>m;
vector<vector<int>> ad(n);
int x, y;
cin>>x>>y;
ad[x].push_back(y);
indeg[y]++;
queue<int> pq;
if(indeg[i] == 0)
pq.push(i);
while(!pq.empty())
int t = pq.front();
pq.pop();
cout<<t<<" ";
for(auto it : ad[t])
indeg[it]--;
if(indeg[it] == 0)
pq.push(it);
return 0;}
int n, m;
vector<vector<int>> v(n);
int x, y;
v[x].push_back(y);
indeg[y]++;
queue<int> pq;
if (indeg[i] == 0)
pq.push(i);
int count = 0;
while (!pq.empty())
int t = pq.front();
count++;
pq.pop();
{
indeg[it]--;
if (indeg[it] == 0)
pq.push(it);
if (count == n)
return false;
return true;
return 0;
By DFS
st[i] = true;
if(!vis[i])
vis[i] = true;
for(auto it : ad[i])
return true;
if(st[it])
return true;
st[i] = false;
return false;
}
int main(){
int n, m;
cin>>n>>m;
vector<vector<int>> ad(n);
int x, y;
cin>>x>>y;
ad[x].push_back(y);
cycle = true;
if(cycle)
cout<<"cycle is present"<<endl;
else
return 0;
//dfs**************************
visited[i] = true;
for(auto it : ad[i])
if(it != parent)
if(visited[it])
return true;
return true;
return false;
visited[i] = true;
pq.push({i, -1});
while(!pq.empty())
int a = p.first;
int b = p.second;
pq.pop();
for(auto it : adj[a])
if(it != b)
if(visited[it])
return true;
else
visited[it] = true;
pq.push({it, a});
}
}
return false;
int main(){
int n, m;
cin>>n>>m;
vector<vector<int>> ad(n);
int x,y;
cin>>x>>y;
ad[x].push_back(y);
ad[y].push_back(x);
cycle = true;
if(cycle)
cout<<"cycle is present"<<endl;
else
return 0;
}
Component of graphs
if(visited[i])
return 0;
visited[i] = true;
int ans = 1;
for(auto it : ad[i])
if(!visited[it])
ans += get_comp(it);
visited[it] = true;
return ans;
}int main(){
int n, m;
cin>>n>>m;
ad = vector<vector<int>> (n);
int x, y;
cin>>x>>y;
ad[x].push_back(y);
ad[y].push_back(x);
}
if(!visited[i])
component.push_back(get_comp(i));
for(auto it : component)
cout<<it<<" ";
cout<<endl;
return 0;
Bipartite
bipart = false;
return;
col[i] = curr;
if(visited[i])
return;
visited[i] = true;
for(auto it : ad[i])
color(it, curr^1);
}int main(){
int n, m;
cin>>n>>m;
ad = vector<vector<int>> (n);
bipart = true;
int x, y;
cin>>x>>y;
ad[x].push_back(y);
ad[y].push_back(x);
if(!visited[i])
color(i, 0);
if(bipart)
cout<<"graph is bipartite"<<endl;
else
return 0;
}
Cycle detection by disjoint and union
int find_set(int v)
if(parent[v] == v)
return v;
int a = find_set(x);
int b = find_set(y);
if(a != b)
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
int main()
parent[i] = i;
sz[i] = 1;
int n, m;
cin>>n>>m;
vector<vector<int>> edges;
int x, y;
cin>>x>>y;
edges.push_back({x, y});
for(auto it : edges)
int x = it[0];
int y = it[1];
int a = find_set(x);
int b = find_set(y);
if(a == b)
cycle = true;
else
union_set(a, b);
if(cycle)
cout<<"cycle is present"<<endl;
else
return 0;
}
Kruskal algorithm (Minimum spanning tree)
int find_set(int v)
if (parent[v] == v)
return v;
int a = find_set(x);
int b = find_set(y);
if (a != b)
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
int main()
parent[i] = i;
sz[i] = 1;
int n, m;
int x, y, w;
edges.push_back({w, x, y});
sort(edges.begin(), edges.end());
int cost = 0;
for(auto it : edges)
int w = it[0];
int x = it[1];
int y = it[2];
int a = find_set(x);
int b = find_set(y);
if(a == b)
continue;
else
union_set(x, y);
cout<<x<<"-->"<<y<<endl;
cost += w;
return 0;
}
Dijkstra algorithm (will not work for negative cycle)
int n, m;
cin>>n>>m;
int x, y, w;
cin>>x>>y>>w;
edges[x].push_back({y, w});
edges[y].push_back({x, w});
int source;
cin>>source;
set<pair<int, int>> s;
dist[source] = 0;
s.insert({0, source});
while(!s.empty())
auto x = *(s.begin());
s.erase(x);
for(auto it : edges[x.second])
s.erase({dist[it.first], it.first});
dist[it.first] = it.second+dist[x.second];
s.insert({dist[it.first], it.first});
}
if(dist[i] != INT_MAX)
cout<<dist[i]<<" ";
else
cout<<"can't find";
cout<<endl;
return 0;
Bellman's ford Algorithm (it can work for negative as well as positive edge weight)
Bellman-Ford algorithm does not work for graphs that contains a negative weight cycle.
int n,m;
cin>>n>>m;
vector<vector<int>> edges;
int x, y, w;
cin>>x>>y>>w;
edges.push_back({x, y, w});
int source;
cin>>source;
vector<int> dist(n, INF);
dist[source] = 0;
for(auto x : edges)
int u = x[0];
int v = x[1];
int w = x[2];
for(auto x : dist)
cout<<x<<" ";
cout<<endl;
return 0;
int n, m;
vector<vector<int>> edges;
int x, y, w;
edges.push_back({x, y, w});
}
// int source;
// cin>>source;
dist[1] = 0;
int u = x[0];
int v = x[1];
int w = x[2];
int u = x[0];
int v = x[1];
int w = x[2];
flag = true;
break;
if (flag)
else
return 0;
vector<vector<int>> graph = {{0, 5, N, 10}, {N, 0, 3, N}, {N, N, 0, 1}, {N, N, N, 0}};
int n = graph.size();
if (dist[i][j] == N)
else
return 0;
Prim's Algorithm
pq.push({0, 0});
while(!pq.empty()){
pq.pop();
if(!visited[p.second]){
visited[p.second] = true;
cout<<p.second<<"->"<<p.first<<endl;
for(auto it : edges[p.second]){
pq.push({it.second, it.first});
}int main(){
int n, m;
cin>>n>>m;
int x, y, w;
cin>>x>>y>>w;
edges[x].push_back({y, w});
edges[y].push_back({x, w});
prims(edges, n);
return 0;
}
visited[idx] = true;
if (!visited[it])
visited[idx] = true;
if (!visited[it])
st.push(idx);
int main(){
int n, m;
vector<vector<int>> edges(n);
int x, y;
edges[x].push_back(y);
stack<int> st;
if (!visited[i])
vector<vector<int>> newedges(n);
newedges[it].push_back(i);
int count = 0;
while (!st.empty())
int t = st.top();
st.pop();
if (!visited[t])
count++;
}
return 0;
0-1 BFS
int n, m;
int x, y;
if (x == y)
continue;
edges[x].push_back({y, 0});
edges[y].push_back({x, 1});
deque<int> pq;
pq.push_back(1);
level[1] = 0;
while (!pq.empty())
int t = pq.front();
pq.pop_front();
int wt = it.second;
{
level[node] = level[t] + wt;
if (wt == 1)
pq.push_back(node);
else
pq.push_front(node);
if (level[n] == INT_MAX)
else
return 0;
Articulation Points
Time complexity = O(Node+Edges)
Space complexity = O(n);
time++;
int count = 0;
visited[idx] = true;
if (it == parent)
continue;
if (visited[it] == true)
else
check[idx] = true;
count++;
if (parent == -1)
if (count >= 2)
check[idx] = true;
}int main(){
int n, m;
int x, y;
edges[x].push_back(y);
edges[y].push_back(x);
disc.resize(n + 1);
low.resize(n + 1);
visited.resize(n + 1, false);
check.resize(n + 1, false);
int time = 0;
if (!visited[i])
int ans = 0;
if (check[i] == true)
ans++;
return 0;
time++;
visited[idx] = true;
if (it == parent)
continue;
if (visited[it] == true)
else
ans.push_back({idx, it});
int main(){
int n, m;
cin>>n>>m;
vector<vector<int>> edges(n);
int u,v;
cin>>u>>v;
edges[u].push_back(v);
edges[v].push_back(u);
disc.resize(n);
low.resize(n);
visited.resize(n, false);
int time = 0;
if (!visited[i])
for(auto it : ans){
cout<<"["<<it[0]<<","<<it[1]<<"]"<<endl;
return 0;