-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphTheory.cpp
More file actions
191 lines (188 loc) · 4.39 KB
/
GraphTheory.cpp
File metadata and controls
191 lines (188 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <bits/stdc++.h>
using namespace std;
int dis [2000+5]; //distance array
bool vis[2000+5]; //visited array
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
vector<int> AdjList[100000+5];
vector<int> ToboSort;//topological sort vector
int n,e,u,v; //n is nodes //e is edges //u is perent //v is child
int shortestPath[100000+5];//shortest path array
bool relaxed[100000+5];//relaxed array
vector<pair<int,int>> djiAdjList[100000+5];//djikstra graph
int shortest,minDis;//shortest child,minimum distance
int CC=0;
void CreatingAdjListGraph(){
cin>>n>>e;
for(int i=0;i<e;++i){
cin>>u>>v;
AdjList[u].push_back(v);
//AdjList[v].push_back(u); //undirected graph
}
}
void PreGraph(int n=0){ //Graph preparation
//memset(dis,-1,sizeof(dis)); //BFS distance array preparation
//memset(vis,0,sizeof(vis)); //DFS visited array preparation
// memset(relaxed, 0, sizeof(relaxed)); //djikstra relaxed array preparation
// fill(shortestPath,shortestPath+n, 1e9); //djikstra shortestPath array preparation
}
void djikstra_k(int root){ //O(nlogn) cuz of priority_queue
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
shortestPath[root]=0;
relaxed[root]=1;
for(auto v:djiAdjList[root]){
shortestPath[v.first]=v.second;
pq.push({v.second,v.first});
}
while(!pq.empty()){
minDis=pq.top().first,shortest=pq.top().second;
pq.pop();
if(!relaxed[shortest]){
relaxed[shortest]=1;
for(auto v:djiAdjList[shortest]){
if(shortestPath[v.first]>shortestPath[shortest]+v.second){
shortestPath[v.first]=v.second;
pq.push({v.second,v.first});
}
}
}
}
}
void dijkstra(int root){
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
fill(shortestPath,shortestPath+n, 1e9);
shortestPath[root]=0;
pq.push({0,root});
while(!pq.empty()){
minDis=pq.top().first,shortest=pq.top().second;
pq.pop();
if(minDis>shortestPath[shortest])continue;
for(auto v:djiAdjList[shortest]){
if(shortestPath[v.first]>shortestPath[shortest]+v.second){
shortestPath[v.first]=shortestPath[shortest]+v.second;
pq.push({shortestPath[v.first],v.first});
}
}
}
}
void DFSRec(int root){ //Recursive
vis[root]=1;
for(auto v:AdjList[root]){
if(!vis[v]) DFSRec(v);
}
//ToboSort.push_back(root); //topological sort
}
void DFSItr(int root){ //Iterative
stack<int> stk;
stk.push(root);
vis[root]=1;
while(!stk.empty()){
u=stk.top();
stk.pop();
for(auto v:AdjList[u]){
if(!vis[v]){
stk.push(v);
vis[v]=1;
}
}
}
}
void DFS2D(int stx , int sty) {
vis[stx][sty] = 1;
for(int i = 0; i < 4; ++i) {
int nx = stx + dx[i] , ny = sty + dy[i];
if(!vis[nx][ny] && valid(nx , ny))
DFS(nx , ny);
}
}
int Connectedcomponents(){ //counting number of connectedcomponents
for(int i=1;i<=n;++i){
if(!vis[i]){
DFSItr(i);
CC++;
}
}
return CC;
}
void BFS(int root){
queue<int> qu;
qu.push(root);
dis[root]=0;
while(!qu.empty()){
u=qu.front();
qu.pop();
for(auto v:AdjList[u]){
if(dis[v]==-1){
dis[v]=dis[u]+1;
qu.push(v);
}
}
}
}
bool bipartite(int s, int c = 0){
queue<int> q;
int u, v;
color[s] = c;
q.push(s);
while(!q.empty()){
u = q.front();
q.pop();
for(int i = 0; i < adj[u].size(); i++){
v = adj[u][i];
if(color[v] == -1){
color[v] = 1 - color[u];
q.push(v);
}
else {
if (color[v] != 1-color[u])return 0;
}
}
}
return 1;
}
int main(){
CreatingAdjListGraph();
BFS(0);
}
//-------- DSU ----------
int parent[100005], height[100005], forests;
void Init(int n){
for(int i = 0; i < n; i++){
parent[i] = i;
height[i] = 0;
}
forests = n;
}
int find_set(int x){
if (parent[x] == x)return x;
return parent[x] = find_set(parent[x]);
}
void link(int x, int y){
if(height[x] > height[y])swap(x,y);
parent[x] = y;
if(height[x] == height[y])height[y]++;
}
bool union_sets(int x, int y){
int u = find_set(x), v = find_set(y);
if (u != y){
link(x,y);
forests--;
}
return x != y;
}
int kruskal() {
int n, m, cost = 0, u, v, c;
vector<pair<int, pair<int,int> > > edges;
cin >> n >> m;
for (int i = 0; i < m; i++){
cin >> u >> v >> c;
edges.push_back({c,{u,v}});
}
Init(n);
sort(edges.begin(), edges.end());
for (auto p : edges){
if (forests == 1)break;
if (union_sets(p.second.first,p.second.second))cost += p.first;
}
return cost;
}