Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Data Structures and Algorithms/Graph/Bipartite_graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <bits/stdc++.h>
#define ll long long int
#define f first
#define s second
#define pii pair<int,int>
#define pic pair<int,char>
using namespace std;

// TC = O(V+E);
// SC = O(V+E) + O(V) + O(V)
// for adjlist col array Auxilery space for recursion

int bipartite_DFS(int i, int par, vector<int>adj[], vector<int> &col){

if(col[i]==-1)col[i] = 1;

for(auto x: adj[i]){

if(col[x]==-1){ // not colored yet

col[x] = 1-col[i];

if(!bipartite_DFS(x,i,adj,col)) // if one node return false then graph will not remain bipatitite
return 0;

}
else if(col[x]==col[i]){ // x is visited now check x has different color to it's parent i or not
return 0;
}
}

return 1;

}

int main(){

int v,e;cin>>v>>e;

vector<int>adj[v+1];
vector<int>vis(v,-1);

for(int i=0;i<e;i++){
int a,b;cin>>a>>b;
--a,--b;
adj[a].push_back(b);
adj[b].push_back(a);
}

int flag = 0;

for(int i=0;i<v;i++){

if(vis[i]==-1) {// not colored yet

if(!bipartite_DFS(i,-1,adj,vis)){

flag = 1;
break;

}
}
}

if(flag) //no possible
cout<<"IMPOSSIBLE\n";

else{
for(auto &x: vis)
cout<<x+1<<" ";
}

return 0;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Dense graph is a graph in which the number of edges is close to the maximal number of edges.
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.

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.

Dense graphs are densely connected. Here number of edges is usually O(n^2). Therefore adjacency matrix is preferred.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define f first
#define s second
#define pb push_back
#define ll long long
int MOD = 1e9+7;

int ans=0;
int DFS(int i,int par,vector<int>adj[]){

int mx = 0, premax = 0;
for(auto x: adj[i]){
if(x != par){
int height = DFS(x,i,adj);
if(mx < height )
premax = mx, mx = height;
else if(premax < height)
premax = height;
}
}
ans = max(ans, mx+premax+1);
return 1 + mx;
}
int main(){

int n;cin>>n; // number of nodes
vector<int>adj[n+1];

for(int i=0;i<n-1;i++){
int u, v; cin>>v>>u;
--v,--u;
adj[v].emplace_back(u);
adj[u].emplace_back(v);
}

DFS(0,-1,adj);
cout<<ans;
}

/*

Input
11 // numbr of vertices
1 2 // next n-1 lines of edges connected u to v
1 7
1 6
1 4
1 5
2 10
10 11
2 3
7 8
8 9

output
7
59 changes: 59 additions & 0 deletions Data Structures and Algorithms/Graph/Robber_&_you.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long

vector<int>gr[100002];
int vis[100002],par[100002],c[100002];

void dfs(int a,int p){
par[a]=p;
for(int i=0;i<gr[a].size();i++)
if(gr[a][i]!=p)
dfs(gr[a][i],a);
}

int dfs1(int a,int p){
int ans =- 100000;
for(int i=0;i<gr[a].size();i++)
if(gr[a][i]!=p)
ans=max(ans,dfs1(gr[a][i],a));
if(ans==-100000)ans=0;
return ans+c[a];
}

int main()
{
int n,r,u,v;
cin>>n>>r;
for(int i=1;i<n;i++){
cin>>u>>v;
gr[u].pb(v);
gr[v].pb(u);
}
// int c[n+1];
for(int i=1;i<=n;i++){
cin>>c[i];
vis[i]=0;
}
dfs(1,1);
vector<int>vv; // storing the path

while(r!=1){
vv.pb(r);
r=par[r];
}

vv.pb(1);

for(auto x: vv)cout<<x<<" ";

for(int i=0;i<(vv.size())/2;i++)
c[vv[i]]=0; // half of the path will be of robber
if((vv.size()) % 2 == 1) // if total length of the path is odd then they will definately meet pathLen/2
c[vv[(vv.size())/2]]/=2;
cout<<dfs1(1,1);

return 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <bits/stdc++.h>
using namespace std;

// start BFS from all rotten oranges initiallly and Cnt time in every level
// until all oranges becomes rottten
int inf = 1e5;

int orangesRotting(vector<vector<int>>& grid) {

int r = grid.size(), c = grid[0].size();

queue<pair<int,int>>q;
vector<vector<int>>dis(r+1,vector<int>(c+1,inf));
int time=0;

// Multisource BFS
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(grid[i][j]==2){
q.push({i,j}); //pushing all the rotten oranges intially in the queue
dis[i][j] = 0; // make 0 for all the starting points
}
}
}

int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};

while(!q.empty()){

int i = q.front().first, j = q.front().second;
q.pop();

for(int k=0;k<4;k++){
int nx = i + dx[k], ny = j + dy[k];
// going for fresh oranges to all its adjacent
if(nx>=0&&nx<r&&ny>=0&&ny<c && grid[nx][ny]==1 && dis[nx][ny]==inf){

// if we get fresh orange then mark it rotten and push into queue
// update the distance of (nx,ny) means we can reach (nx,ny) from (i,j) in one step;

q.push({nx,ny});
dis[nx][ny] = dis[i][j]+1;
time = max(time, dis[nx][ny]);
}
}
}

// If any rotten orange is remaining where we can't reach return -1
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(grid[i][j]==1 && dis[i][j]==inf){
return -1;
}
}
}
return time;
}



// Input:
// grid = [[2,1,1],
// [1,1,0],
// [0,1,1]]

// Output: 4
Loading