Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6fa300e

Browse files
Added Graph Theory Algorithms
1 parent d035b0c commit 6fa300e

File tree

3 files changed

+355
-0
lines changed

3 files changed

+355
-0
lines changed

‎25-Graph Theory/bipartiteGraphTest.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
#define ld long double
5+
#define rep(i,a,b) for(ll i=a;i<b;i++)
6+
#define repp(i,a,b) for(ll i=a;i<=b;i++)
7+
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
8+
#define endl "\n"
9+
#define deb(x) cerr << #x << "=" << x << endl
10+
#define debb(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
11+
#define mod ((ll)1e18)
12+
#define N ((ll)2e6)
13+
#define pb push_back
14+
#define in insert
15+
#define F first
16+
#define S second
17+
#define mp make_pair
18+
#define lb lower_bound
19+
#define ub upper_bound
20+
#define PI 3.1415926535897932384626
21+
#define all(x) x.begin(), x.end()
22+
#define rall(x) x.rbegin(), x.rend()
23+
#define speed ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
24+
#define test ll t; cin>>t; while(t--)
25+
typedef pair<ll, ll> pll;
26+
typedef vector<pll> vpll;
27+
ll dr[] = { -1, 1, 0, 0};
28+
ll dc[] = {0, 0, 1, -1};
29+
30+
/*Functions*/
31+
ll gcd(ll a, ll b);
32+
ll exp(ll x, ll y, ll p);
33+
ll exp(ll x, ll y);
34+
void InverseofNumber();
35+
void InverseofFactorial();
36+
void factorial();
37+
ll Binomial(ll n, ll R);
38+
39+
40+
ll factorialNumInverse[N + 1];
41+
ll naturalNumInverse[N + 1];
42+
ll fact[N + 1];
43+
44+
void fileIO() {
45+
#ifndef ONLINE_JUDGE
46+
freopen("input.txt", "r", stdin);
47+
freopen("output.txt", "w", stdout);
48+
#endif
49+
}
50+
ll n, m; //no of vertices and number of edges
51+
vector<ll> v[N]; //the graph representation
52+
ll vis[N]; //visited array
53+
ll color[N]; //color array
54+
bool dfs(ll x, ll c) {
55+
vis[x] = 1;
56+
color[x] = c;
57+
for (auto i : v[x]) {
58+
if (vis[i] == 0) {
59+
if (dfs(i, c ^ 1) == false) //invert color
60+
return false;
61+
}
62+
else {
63+
if (color[x] == color[i])
64+
return false;
65+
}
66+
}
67+
return true;
68+
}
69+
int main() {
70+
speed
71+
// InverseofNumber(),InverseofFactorial(),factorial();
72+
fileIO();
73+
cin >> n >> m;
74+
while (m--) {
75+
ll xx, yy; cin >> xx >> yy;
76+
v[xx].pb(yy);
77+
v[yy].pb(xx);
78+
}
79+
ll ans = 0;
80+
bool answer;
81+
for (ll i = 1; i <= n; i++) {
82+
if (!vis[i]) {
83+
answer = dfs(i, 0); //dfs calls for unvisited nodes
84+
if (!answer) {
85+
cout << "NOT BIPARTITE" << endl;
86+
exit(0);
87+
}
88+
}
89+
}
90+
cout << "BIPARTITE" << endl;
91+
92+
return 0;
93+
}
94+
95+
96+
/*All Functions*/
97+
ll gcd(ll a, ll b) {
98+
if (b == 0)
99+
return a;
100+
return gcd(b, a % b);
101+
}
102+
103+
ll exp(ll x, ll y, ll p) {
104+
ll res = 1;
105+
while (y) {
106+
if (y % 2)
107+
res = (res * x % p) % p;
108+
x = (x * x) % p;
109+
y /= 2;
110+
}
111+
return res;
112+
}
113+
ll exp(ll x, ll y) {
114+
ll res = 1;
115+
while (y) {
116+
if (y % 2)
117+
res = (res * x ) ;
118+
x = (x * x) ;
119+
y /= 2;
120+
}
121+
return res;
122+
}
123+
void InverseofNumber() {
124+
naturalNumInverse[0] = naturalNumInverse[1] = 1;
125+
for (ll i = 2; i <= N; i++)
126+
naturalNumInverse[i] = naturalNumInverse[mod % i] * (mod - mod / i) % mod;
127+
}
128+
void InverseofFactorial() {
129+
factorialNumInverse[0] = factorialNumInverse[1] = 1;
130+
for (ll i = 2; i <= N; i++)
131+
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % mod;
132+
}
133+
void factorial() {
134+
fact[0] = 1;
135+
for (ll i = 1; i <= N; i++)
136+
fact[i] = (fact[i - 1] * i) % mod;
137+
}
138+
ll Binomial(ll n, ll R) {
139+
ll ans = ((fact[n] * factorialNumInverse[R]) % mod * (factorialNumInverse[n - R]) % mod) % mod;
140+
return ans;
141+
}

‎25-Graph Theory/connectedComponents.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include<bits/stdc++.h>
2+
// #warning x&1=0 if x is even and x&1=1 if x is odd
3+
// #include<ext/pb_ds/assoc_container.hpp>
4+
// #include <ext/pb_ds/tree_policy.hpp>
5+
// using namespace __gnu_pdbs;
6+
using namespace std;
7+
#define ll long long
8+
#define rep( i, a, b) for(ll i=a;i<b;i++)
9+
#define repp(i,a,b) for(ll i=a;i<=b;i++)
10+
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
11+
#define ld long double
12+
#define endl "\n"
13+
#define mod ((ll)1e18)
14+
#define N ((ll)2e6+10)
15+
#define pb push_back
16+
#define in insert
17+
#define f first
18+
#define s second
19+
#define lb lower_bound
20+
#define ub upper_bound
21+
#define all(x) x.begin(), x.end()
22+
#define speed ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
23+
#define test ll t; cin>>t; while(t--)
24+
ll gcd(ll a, ll b) {if (b == 0)return a; return gcd(b, a % b);}
25+
ll exp(ll x, ll y, ll p) { ll res = 1; while (y) {if (y % 2) res = (res * x % p) % p; x = (x * x) % p; y /= 2; } return res;}
26+
ll exp(ll x, ll y) { ll res = 1; while (y) {if (y % 2) res = (res * x ) ; x = (x * x) ; y /= 2; } return res;}
27+
ll sv[1000000] = {0};
28+
ll factorialNumInverse[N + 1];
29+
ll naturalNumInverse[N + 1];
30+
ll fact[N + 1];
31+
void InverseofNumber() {naturalNumInverse[0] = naturalNumInverse[1] = 1; for (ll i = 2; i <= N; i++)naturalNumInverse[i] = naturalNumInverse[mod % i] * (mod - mod / i) % mod;}
32+
void InverseofFactorial() {factorialNumInverse[0] = factorialNumInverse[1] = 1; for (ll i = 2; i <= N; i++)factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % mod;}
33+
void factorial() {fact[0] = 1; for (ll i = 1; i <= N; i++) {fact[i] = (fact[i - 1] * i) % mod;}}
34+
ll Binomial(ll n, ll R) {ll ans = ((fact[n] * factorialNumInverse[R]) % mod * (factorialNumInverse[n - R]) % mod) % mod; return ans;}
35+
/*TOIENT FUNCTION*/ll phi(ll n) {ll ans = n; for (ll i = 2; i * i <= n; i++) {if (n % i == 0) {while (n % i == 0) n /= i; ans -= ans / i;}} if (n > 1) ans -= ans / n; return ans;}
36+
37+
38+
void fileIO() {
39+
#ifndef ONLINE_JUDGE
40+
freopen("input.txt", "r", stdin);
41+
freopen("output.txt", "w", stdout);
42+
#endif
43+
}
44+
ll n, k; ll vis[N] ;
45+
ll counter = 0;
46+
ll component[N];
47+
vector<ll> v[N];
48+
void dfs(ll at) {
49+
if (vis[at] == 1) return;
50+
vis[at] = 1;
51+
component[at] = counter;
52+
for (auto i : v[at]) {
53+
dfs(i);
54+
}
55+
}
56+
57+
int main() {
58+
speed
59+
// InverseofNumber(),InverseofFactorial(),factorial();
60+
fileIO();
61+
cin >> n >> k;
62+
rep(i, 0, k) {
63+
ll xx, yy; cin >> xx >> yy;
64+
v[xx].pb(yy);
65+
v[yy].pb(xx);
66+
}
67+
fill(vis, vis + N, 0);
68+
repp(i, 1, n) {
69+
if (!vis[i]) {
70+
counter++;
71+
dfs(i);
72+
}
73+
}
74+
repp(i, 0, n) {
75+
cout << component[i] << " ";
76+
}
77+
cout << endl;
78+
79+
80+
return 0;
81+
}

‎25-Graph Theory/dijkstras.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define ll long long
4+
#define ld long double
5+
#define rep(i,a,b) for(ll i=a;i<b;i++)
6+
#define repp(i,a,b) for(ll i=a;i<=b;i++)
7+
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
8+
#define endl "\n"
9+
#define deb(x) cerr << #x << "=" << x << endl
10+
#define debb(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
11+
#define mod ((ll)1e18)
12+
#define N ((ll)2e6+10)
13+
#define pb push_back
14+
#define in insert
15+
#define F first
16+
#define S second
17+
#define lb lower_bound
18+
#define ub upper_bound
19+
#define PI 3.1415926535897932384626
20+
#define all(x) x.begin(), x.end()
21+
#define speed ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
22+
#define test ll t; cin>>t; while(t--)
23+
typedef pair<ll, ll> pll;
24+
typedef vector<pll> vpll;
25+
26+
/*Functions*/
27+
ll gcd(ll a, ll b);
28+
ll exp(ll x, ll y, ll p);
29+
ll exp(ll x, ll y);
30+
void InverseofNumber();
31+
void InverseofFactorial();
32+
ll Binomial(ll n, ll R);
33+
34+
35+
ll factorialNumInverse[N + 1];
36+
ll naturalNumInverse[N + 1];
37+
ll fact[N + 1];
38+
39+
void fileIO() {
40+
#ifndef ONLINE_JUDGE
41+
freopen("input.txt", "r", stdin);
42+
freopen("output.txt", "w", stdout);
43+
#endif
44+
}
45+
46+
ll n, m;
47+
vector<pll> v[N];
48+
ll ids[N] = {0};
49+
ll dist[N] = {0};
50+
ll vis[N] = {0};
51+
52+
int main() {
53+
speed
54+
// InverseofNumber(),InverseofFactorial(),factorial();
55+
fileIO();
56+
57+
cin >> n >> m;
58+
rep(i, 0, m) {
59+
ll xx, yy, wt; cin >> xx >> yy >> wt;
60+
v[xx].pb({yy, wt});
61+
}
62+
fill(dist, dist + N, mod);
63+
dist[1] = 0;
64+
multiset < pair < ll , ll > > s;
65+
s.in({1, 0});
66+
while (!s.empty()) {
67+
pll x = *s.begin();
68+
s.erase(s.begin());
69+
vis[x.F] = 1;
70+
for (auto i : v[x.F]) {
71+
ll d = dist[x.F] + i.S;
72+
if (d < dist[i.F]) {
73+
dist[i.F] = d;
74+
s.in({i.F, d});
75+
}
76+
}
77+
78+
}
79+
repp(i, 1, n) {
80+
cout << dist[i] << " ";
81+
}
82+
cout << endl;
83+
return 0;
84+
}
85+
86+
87+
/*All Functions*/
88+
ll gcd(ll a, ll b) {
89+
if (b == 0)
90+
return a;
91+
return gcd(b, a % b);
92+
}
93+
94+
ll exp(ll x, ll y, ll p) {
95+
ll res = 1;
96+
while (y) {
97+
if (y % 2)
98+
res = (res * x % p) % p;
99+
x = (x * x) % p;
100+
y /= 2;
101+
}
102+
return res;
103+
}
104+
ll exp(ll x, ll y) {
105+
ll res = 1;
106+
while (y) {
107+
if (y % 2)
108+
res = (res * x ) ;
109+
x = (x * x) ;
110+
y /= 2;
111+
}
112+
return res;
113+
}
114+
void InverseofNumber() {
115+
naturalNumInverse[0] = naturalNumInverse[1] = 1;
116+
for (ll i = 2; i <= N; i++)
117+
naturalNumInverse[i] = naturalNumInverse[mod % i] * (mod - mod / i) % mod;
118+
}
119+
void InverseofFactorial() {
120+
factorialNumInverse[0] = factorialNumInverse[1] = 1;
121+
for (ll i = 2; i <= N; i++)
122+
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % mod;
123+
}
124+
void factorial() {
125+
fact[0] = 1;
126+
for (ll i = 1; i <= N; i++)
127+
fact[i] = (fact[i - 1] * i) % mod;
128+
}
129+
ll Binomial(ll n, ll R) {
130+
ll ans = ((fact[n] * factorialNumInverse[R]) % mod * (factorialNumInverse[n - R]) % mod) % mod;
131+
return ans;
132+
}
133+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /