---
Basic Graph Theory Short Note
1. Degree Sum
[2 \times (\text{degree counter})]
2. Handshaking Lemma
Even nodes always have an odd number of degrees. In-degree and out-degree satisfy: [ \text{Sum of in-degree} = \text{Sum of out-degree} ]
3. Calculating the Complexity of Graph
[ V = \text{vertices}, E = \text{edges} ] [ \mathcal{O}(V + E) \text{ or } \mathcal{O}(V^2) ]
4. How to Store Graph
Coming soon...
5. Adjacency Matrix
#include<bits/stdc++.h>
using namespace std;
int g[100][100];
int main() {
int n, e; cin >> n >> e;
while(e--) {
int u, v; cin >> u >> v;
g[u][v] = 1;
g[v][u] = 1;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cout << g[i][j] << " ";
}
cout << endl;
}
if(g[4][2]) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
6. Adjacency List
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
vector<int> g[N];
int indgr[N], outdgr[N];
int main() {
int n, e; cin >> n >> e;
while(e--) {
int u, v; cin >> u >> v;
indgr[v]++;
outdgr[u]++;
g[u].push_back(v);
}
for(int i = 0; i <= n; i++) {
cout << indgr[i] << " ";
}
cout << "\n";
for(int i = 0; i <= n; i++) {
cout << outdgr[i] << " ";
}
}
7. DFS
#include<bits/stdc++.h>
#define itsmdshahin ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
using namespace std;
const int N = 1e5 + 9;
bool vis[N];
vector<int> g[N];
void dfs(int s) {
vis[s] = true;
for(auto it : g[s]) {
if(!vis[it]) {
cout << "VISITED " << it << endl;
dfs(it);
}
}
}
int main() {
int n, m; cin >> n >> m;
while(m--) {
int x, y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int s; cin >> s;
dfs(s);
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
cout << "DISCONNECTED GRAPH\n";
return 0;
}
}
}
8. Connected Components
#include<bits/stdc++.h>
#define itsmdshahin ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
using namespace std;
const int N = 1e5 + 9;
bool vis[N];
vector<int> g[N];
void dfs(int s) {
vis[s] = true;
for(auto it : g[s]) {
if(!vis[it]) {
dfs(it);
}
}
}
int main() {
int n, m; cin >> n >> m;
while(m--) {
int x, y; cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int ans = 0;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
dfs(i);
++ans;
}
}
cout << "Connected Components: " << ans << endl;
}
9. BFS Implementation
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> G[N];
bool Vis[N];
int main() {
int n, e; cin >> n >> e;
while(e--) {
int x, y; cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
queue<int> q;
q.push(1);
Vis[1] = true;
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto it : G[u]) {
if(!Vis[it]) {
q.push(it);
Vis[v] = true;
}
}
}
}
10. BFS Component Find in Graph
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> G[N];
bool Vis[N];
queue<int> q;
void bfs(int n) {
q.push(n);
Vis[n] = true;
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto it : G[u]) {
if(!Vis[it]) {
q.push(it);
Vis[it] = true;
}
}
}
}
int main() {
int n, e; cin >> n >> e;
int ans = 0;
for(int i = 0; i < n; i++) {
if(!Vis[i]) {
bfs(i);
++ans;
}
}
cout << ans << endl;
}
11. BFS Distance Find in Graph Theory
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> G[N];
bool Vis[N];
vector<int> dis(N);
int main() {
int n, e; cin >> n >> e;
queue<int> q;
q.push(1);
Vis[1] = true;
dis[1] = 0;
while(!q.empty()) {
int u = q.front();
q.pop();
for(auto it : G[u]) {
if(!Vis[it]) {
q.push(it);
dis[it] = dis[u] + 1;
Vis[it] = true;
}
}
}
for(int i = 1; i <= n; i++) {
cout << dis[i] << " ";
}
cout << endl;
}
---
12. BFS Path Find in Graph Theory
// Implementation will be added soon...
13. Bicoloring and Bipartite Graph
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
vector<int> g[N];
bool vis[N], col[N], ok;
void dfs(int u) {
vis[u] = true;
for(auto v : g[u]) {
if(!vis[v]) {
col[v] = col[u] ^ 1;
dfs(v);
} else {
if(col[u] == col[v]) ok = false;
}
}
}
int main() {
int n, e;
cin >> n >> e;
while(e--) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
ok = true;
for(int i = 1; i <= n; i++) {
if(!vis[i]) dfs(i);
}
if(ok) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
Certainly! Here are the implementations for problems 12 to 18:
12. Articulation Points and Bridges
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> g[N];
bool vis[N];
int in[N], low[N], timer;
set<int> articulationPoints;
set<pair<int, int>> bridges;
void dfs(int u, int par) {
vis[u] = true;
in[u] = low[u] = timer++;
int children = 0;
for (auto v : g[u]) {
if (v == par) continue;
if (vis[v]) {
low[u] = min(low[u], in[v]);
} else {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= in[u] && par != -1) {
articulationPoints.insert(u);
}
if (low[v] > in[u]) {
bridges.insert({min(u, v), max(u, v)});
}
children++;
}
}
if (par == -1 && children > 1) {
articulationPoints.insert(u);
}
}
int main() {
int n, e; cin >> n >> e;
while (e--) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
timer = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, -1);
}
}
cout << "Articulation Points: ";
for (auto point : articulationPoints) {
cout << point << " ";
}
cout << "\n";
cout << "Bridges: ";
for (auto bridge : bridges) {
cout << bridge.first << "-" << bridge.second << " ";
}
cout << "\n";
return 0;
}
13. Topological Sorting
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<int> g[N];
bool vis[N];
stack<int> order;
void dfs(int u) {
vis[u] = true;
for (auto v : g[u]) {
if (!vis[v]) {
dfs(v);
}
}
order.push(u);
}
int main() {
int n, e; cin >> n >> e;
while (e--) {
int u, v; cin >> u >> v;
g[u].push_back(v);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i);
}
}
cout << "Topological Sorting: ";
while (!order.empty()) {
cout << order.top() << " ";
order.pop();
}
cout << "\n";
return 0;
}
14. Dijkstra's Algorithm
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<pair<int, int>> g[N];
vector<int> dis(N, INT_MAX);
void dijkstra(int start) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, start});
dis[start] = 0;
while (!pq.empty()) {
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if (w > dis[u]) continue;
for (auto it : g[u]) {
int v = it.first;
int weight = it.second;
if (dis[u] + weight < dis[v]) {
dis[v] = dis[u] + weight;
pq.push({dis[v], v});
}
}
}
}
int main() {
int n, e; cin >> n >> e;
while (e--) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
int start; cin >> start;
dijkstra(start);
cout << "Shortest Distances from Node " << start << ":\n";
for (int i = 1; i <= n; i++) {
cout << "Node " << i << ": " << dis[i] << "\n";
}
return 0;
}
15. Bellman-Ford Algorithm
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<tuple<int, int, int>> edges;
vector<int> dis(N, INT_MAX);
void bellmanFord(int start) {
dis[start] = 0;
for (int i = 1; i <= edges.size() - 1; i++) {
for (auto edge : edges) {
int u, v, w;
tie(u, v, w) = edge;
if (dis[u] != INT_MAX && dis[u] + w < dis[v]) {
dis[v] = dis[u] + w;
}
}
}
}
int main() {
int n, e; cin >> n >> e;
while (e--) {
int u, v, w; cin >> u >> v >> w;
edges.push_back({u, v, w});
}
int start; cin >> start;
bellmanFord(start);
cout << "Shortest Distances from Node " << start << ":\n";
for (int i = 1; i <= n; i++) {
cout << "Node " << i << ": " << dis[i] << "\n";
}
return 0;
}
16. Floyd-Warshall Algorithm
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int dis[N][N];
void floydWarshall(int n) {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dis[i][k] != INT_MAX && dis[k][j] != INT_MAX && dis[i][k] + dis[k][j] < dis[i][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
}
int main() {
int n, e; cin >> n >> e;
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; i++) dis[i][i] = 0;
while (e--) {
int u, v, w; cin >> u >> v >> w;
dis[u][v] = w;
}
floydWarshall(n);
cout << "Shortest Distances between Nodes:\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dis[i][j] == INT_MAX) cout << "INF ";
else cout << dis[i][j] << " ";
}
cout << "\n";
}
return 0;
}
17. Minimum Spanning Tree (Prim's Algorithm)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
vector<pair<int, int>> g[N];
bool vis[N];
int primMST(int start) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, start});
int mstWeight = 0;
while (!pq.empty()) {
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if (vis[u]) continue;
mstWeight += w;
vis[u] = true;
for (auto it : g[u]) {
int v = it.first;
int weight = it.second;
if (!vis[v]) {
pq.push({weight, v});
}
}
}
return mstWeight;
}
int main() {
int n, e; cin >> n >> e;
while (e--) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
int start; cin >> start;
int mstWeight = primMST(start);
cout << "Minimum Spanning Tree Weight: " << mstWeight << "\n";
return 0;
}
### ```