Skip to content

Commit 9a811c1

Browse files
committed
Added All Graph Algos
1 parent d80ab99 commit 9a811c1

19 files changed

+1460
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long int
3+
#define f first
4+
#define s second
5+
#define pii pair<int,int>
6+
#define pic pair<int,char>
7+
using namespace std;
8+
9+
// TC = O(V+E);
10+
// SC = O(V+E) + O(V) + O(V)
11+
// for adjlist col array Auxilery space for recursion
12+
13+
int bipartite_DFS(int i, int par, vector<int>adj[], vector<int> &col){
14+
15+
if(col[i]==-1)col[i] = 1;
16+
17+
for(auto x: adj[i]){
18+
19+
if(col[x]==-1){ // not colored yet
20+
21+
col[x] = 1-col[i];
22+
23+
if(!bipartite_DFS(x,i,adj,col)) // if one node return false then graph will not remain bipatitite
24+
return 0;
25+
26+
}
27+
else if(col[x]==col[i]){ // x is visited now check x has different color to it's parent i or not
28+
return 0;
29+
}
30+
}
31+
32+
return 1;
33+
34+
}
35+
36+
int main(){
37+
38+
int v,e;cin>>v>>e;
39+
40+
vector<int>adj[v+1];
41+
vector<int>vis(v,-1);
42+
43+
for(int i=0;i<e;i++){
44+
int a,b;cin>>a>>b;
45+
--a,--b;
46+
adj[a].push_back(b);
47+
adj[b].push_back(a);
48+
}
49+
50+
int flag = 0;
51+
52+
for(int i=0;i<v;i++){
53+
54+
if(vis[i]==-1) {// not colored yet
55+
56+
if(!bipartite_DFS(i,-1,adj,vis)){
57+
58+
flag = 1;
59+
break;
60+
61+
}
62+
}
63+
}
64+
65+
if(flag) //no possible
66+
cout<<"IMPOSSIBLE\n";
67+
68+
else{
69+
for(auto &x: vis)
70+
cout<<x+1<<" ";
71+
}
72+
73+
return 0;
74+
}
75+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dense graph is a graph in which the number of edges is close to the maximal number of edges.
2+
Sparse graph is a graph in which the number of edges is close to the minimal number of edges. Sparse graph can be a disconnected graph.
3+
4+
As the names indicate sparse graphs are sparsely connected (eg: Trees). Usually the number of edges is in O(n) where n is the number of vertices. Therefore adjacency lists are preferred since they require constant space for every edge.
5+
6+
Dense graphs are densely connected. Here number of edges is usually O(n^2). Therefore adjacency matrix is preferred.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
4+
#define f first
5+
#define s second
6+
#define pb push_back
7+
#define ll long long
8+
int MOD = 1e9+7;
9+
10+
int ans=0;
11+
int DFS(int i,int par,vector<int>adj[]){
12+
13+
int mx = 0, premax = 0;
14+
for(auto x: adj[i]){
15+
if(x != par){
16+
int height = DFS(x,i,adj);
17+
if(mx < height )
18+
premax = mx, mx = height;
19+
else if(premax < height)
20+
premax = height;
21+
}
22+
}
23+
ans = max(ans, mx+premax+1);
24+
return 1 + mx;
25+
}
26+
int main(){
27+
28+
int n;cin>>n; // number of nodes
29+
vector<int>adj[n+1];
30+
31+
for(int i=0;i<n-1;i++){
32+
int u, v; cin>>v>>u;
33+
--v,--u;
34+
adj[v].emplace_back(u);
35+
adj[u].emplace_back(v);
36+
}
37+
38+
DFS(0,-1,adj);
39+
cout<<ans;
40+
}
41+
42+
/*
43+
44+
Input
45+
11 // numbr of vertices
46+
1 2 // next n-1 lines of edges connected u to v
47+
1 7
48+
1 6
49+
1 4
50+
1 5
51+
2 10
52+
10 11
53+
2 3
54+
7 8
55+
8 9
56+
57+
output
58+
7
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define pb push_back
4+
#define ll long long
5+
6+
vector<int>gr[100002];
7+
int vis[100002],par[100002],c[100002];
8+
9+
void dfs(int a,int p){
10+
par[a]=p;
11+
for(int i=0;i<gr[a].size();i++)
12+
if(gr[a][i]!=p)
13+
dfs(gr[a][i],a);
14+
}
15+
16+
int dfs1(int a,int p){
17+
int ans =- 100000;
18+
for(int i=0;i<gr[a].size();i++)
19+
if(gr[a][i]!=p)
20+
ans=max(ans,dfs1(gr[a][i],a));
21+
if(ans==-100000)ans=0;
22+
return ans+c[a];
23+
}
24+
25+
int main()
26+
{
27+
int n,r,u,v;
28+
cin>>n>>r;
29+
for(int i=1;i<n;i++){
30+
cin>>u>>v;
31+
gr[u].pb(v);
32+
gr[v].pb(u);
33+
}
34+
// int c[n+1];
35+
for(int i=1;i<=n;i++){
36+
cin>>c[i];
37+
vis[i]=0;
38+
}
39+
dfs(1,1);
40+
vector<int>vv; // storing the path
41+
42+
while(r!=1){
43+
vv.pb(r);
44+
r=par[r];
45+
}
46+
47+
vv.pb(1);
48+
49+
for(auto x: vv)cout<<x<<" ";
50+
51+
for(int i=0;i<(vv.size())/2;i++)
52+
c[vv[i]]=0; // half of the path will be of robber
53+
if((vv.size()) % 2 == 1) // if total length of the path is odd then they will definately meet pathLen/2
54+
c[vv[(vv.size())/2]]/=2;
55+
cout<<dfs1(1,1);
56+
57+
return 0;
58+
59+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// start BFS from all rotten oranges initiallly and Cnt time in every level
5+
// until all oranges becomes rottten
6+
int inf = 1e5;
7+
8+
int orangesRotting(vector<vector<int>>& grid) {
9+
10+
int r = grid.size(), c = grid[0].size();
11+
12+
queue<pair<int,int>>q;
13+
vector<vector<int>>dis(r+1,vector<int>(c+1,inf));
14+
int time=0;
15+
16+
// Multisource BFS
17+
for(int i=0;i<r;i++){
18+
for(int j=0;j<c;j++){
19+
if(grid[i][j]==2){
20+
q.push({i,j}); //pushing all the rotten oranges intially in the queue
21+
dis[i][j] = 0; // make 0 for all the starting points
22+
}
23+
}
24+
}
25+
26+
int dx[] = {0,0,-1,1};
27+
int dy[] = {1,-1,0,0};
28+
29+
while(!q.empty()){
30+
31+
int i = q.front().first, j = q.front().second;
32+
q.pop();
33+
34+
for(int k=0;k<4;k++){
35+
int nx = i + dx[k], ny = j + dy[k];
36+
// going for fresh oranges to all its adjacent
37+
if(nx>=0&&nx<r&&ny>=0&&ny<c && grid[nx][ny]==1 && dis[nx][ny]==inf){
38+
39+
// if we get fresh orange then mark it rotten and push into queue
40+
// update the distance of (nx,ny) means we can reach (nx,ny) from (i,j) in one step;
41+
42+
q.push({nx,ny});
43+
dis[nx][ny] = dis[i][j]+1;
44+
time = max(time, dis[nx][ny]);
45+
}
46+
}
47+
}
48+
49+
// If any rotten orange is remaining where we can't reach return -1
50+
for(int i=0;i<r;i++){
51+
for(int j=0;j<c;j++){
52+
if(grid[i][j]==1 && dis[i][j]==inf){
53+
return -1;
54+
}
55+
}
56+
}
57+
return time;
58+
}
59+
60+
61+
62+
// Input:
63+
// grid = [[2,1,1],
64+
// [1,1,0],
65+
// [0,1,1]]
66+
67+
// Output: 4

0 commit comments

Comments
 (0)