Skip to content

Commit 2b76aed

Browse files
committed
Basic graph algorithms - DFS, DSU, Euler Walk
1 parent 829acc2 commit 2b76aed

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

graph/dfs.sublime-snippet

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<snippet>
2+
<content><![CDATA[
3+
void dfs(int x)
4+
{
5+
visited[x]=true;
6+
7+
for(auto& i : G[x])
8+
{
9+
if(visited[i]==false)
10+
{
11+
dfs(i);
12+
}
13+
}
14+
}
15+
]]></content>
16+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
17+
<tabTrigger>dfs</tabTrigger>
18+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
19+
<!-- <scope>source.python</scope> -->
20+
</snippet>

graph/dsu.sublime-snippet

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// Disjoint Set DataStructure
4+
map< ll,ll > parent; // reserve space for map if n > 10000
5+
map< ll,ll > urank;
6+
7+
void create(ll x)
8+
{
9+
parent[x] = x ;
10+
urank[x] = 0 ; // rank = no. of nodes in its subtree
11+
}
12+
13+
ll find(ll x)
14+
{
15+
if( parent[x] != x ) //path compression
16+
{
17+
parent[x] = find(parent[x]) ;
18+
}
19+
return parent[x];
20+
}
21+
22+
void merge(ll x, ll y)
23+
{
24+
ll xroot = find(x);
25+
ll yroot = find(y);
26+
27+
if( urank[xroot] <= urank[yroot] ) // Union by rank
28+
{
29+
parent[xroot] = yroot ;
30+
urank[yroot] = urank[yroot] + urank[xroot] ;
31+
}
32+
else
33+
{
34+
parent[yroot] = xroot;
35+
urank[xroot] = urank[xroot] + urank[yroot] ;
36+
}
37+
}
38+
39+
]]></content>
40+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
41+
<tabTrigger>dsu</tabTrigger>
42+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
43+
<!-- <scope>source.python</scope> -->
44+
</snippet>

graph/ewalk.sublime-snippet

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<snippet>
2+
<content><![CDATA[
3+
// convert rooted tree to array
4+
// 0 - white(not yet visited), 1- grey(currently being explored), 2-black(explored completely )
5+
6+
vector<int> color(MX+1);
7+
vector<int> parent(MX+1);
8+
9+
int tt=0;
10+
vector<int> tin(MX+1) ; // v was visited at tin[v] time
11+
vector<int> tout(MX+1); // v was left at tout[v] time
12+
vector<int> vis(MX+1); // at time tt, vis[tt] node was visited
13+
14+
void dfs(int v)
15+
{
16+
color[v] = 1 ; // grey
17+
18+
vis[tt] = v ;
19+
tin[v] = tt++ ;
20+
for( auto& u : G[v] )
21+
{
22+
if ( color[u] == 0 ) // white
23+
{
24+
dfs(u) ;
25+
parent[u] = v ;
26+
}
27+
28+
}
29+
tout[v] = tt ;
30+
31+
color[v] = 2 ; // black
32+
}
33+
34+
// vis is the array of the rooted tree
35+
]]></content>
36+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
37+
<tabTrigger>ewalk</tabTrigger>
38+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
39+
<!-- <scope>source.python</scope> -->
40+
</snippet>

graph/graphStarter.sublime-snippet

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<snippet>
2+
<content><![CDATA[
3+
#include <bits/stdc++.h>
4+
#include <cstdio>
5+
#include <cstring>
6+
#include <cmath>
7+
#include <cstring>
8+
#include <complex>
9+
#include <chrono>
10+
#define endl "\n"
11+
#define ll long long int
12+
#define vi vector<int>
13+
#define vll vector<ll>
14+
#define vvi vector < vi >
15+
#define pii pair<int,int>
16+
#define pll pair<long long, long long>
17+
#define mod 1000000007
18+
#define inf 1000000000000000001;
19+
#define all(c) c.begin(),c.end()
20+
#define mp(x,y) make_pair(x,y)
21+
#define mem(a,val) memset(a,val,sizeof(a))
22+
#define eb emplace_back
23+
#define f first
24+
#define s second
25+
26+
using namespace std;
27+
28+
const int MX = 100010; // maximum no. of nodes in graph
29+
vector<bool> visited(MX+1,0);
30+
31+
int n;
32+
vector< vector<pair<int, ll>> > G(MX+1); // adjacency list, (edge,cost) pair
33+
34+
int main()
35+
{
36+
37+
std::ios::sync_with_stdio(false);
38+
39+
int T;
40+
cin>>T;
41+
42+
while(T--)
43+
{
44+
45+
for(int i=0; i<MX+1; i++)
46+
{
47+
G[i].clear();
48+
}
49+
fill(all(visited),0);
50+
51+
int m; // no. of edges
52+
cin>>n>>m;
53+
54+
for(int i = 1 ; i <= m ; i++)
55+
{
56+
57+
int u,v;
58+
ll c;
59+
cin>>u>>v>>c; // 1 based indexing of nodes
60+
61+
G[u].eb(v,c);
62+
G[v].eb(u,c);
63+
64+
}
65+
66+
}
67+
return 0;
68+
}
69+
]]></content>
70+
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
71+
<tabTrigger>graph-starter</tabTrigger>
72+
<!-- Optional: Set a scope to limit where the snippet will trigger -->
73+
<!-- <scope>source.python</scope> -->
74+
</snippet>

0 commit comments

Comments
 (0)