-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErdosRenyi.h
80 lines (66 loc) · 1.92 KB
/
ErdosRenyi.h
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
//
// Created by kopcion on 30.07.2020.
//
#ifndef GOMORYHU_ERDOSRENYI_H
#define GOMORYHU_ERDOSRENYI_H
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <fstream>
using namespace std;
void ER(string input, string output){
vector<unordered_map<int,int> > graph;
int n,m;
ifstream file("/home/kopcion/CLionProjects/GomoryHu/" + input, std::ios_base::in);
file>>n>>m;
graph.resize(n+1);
for(int i=0; i < m; ++i){
int a, b, c;
file>>a>>b>>c;
graph[a][b] = c;
graph[b][a] = c;
}
priority_queue<pair<int,int> > pq;
vector<int> component(n+1, -1);
int comp = 1;
int size = 0;
for(int i=1; i < component.size(); ++i){
if(component[i] != -1) continue;
component[i] = comp;
queue<int> q;
q.push(i);
while(!q.empty()){
int u = q.front(); q.pop();
size++;
for(auto v : graph[u]){
if(component[v.first] != -1) continue;
component[v.first] = comp;
q.push(v.first);
}
}
pq.push(pair<int,int>(size, comp));
size = 0;
comp++;
}
std::ofstream ofs ("../ErdosRenyi" + to_string(n/100) + ".txt", std::ofstream::out);
int edges = 0;
for(int i=1; i < graph.size(); ++i){
if(component[i] != pq.top().second) continue;
for(auto v : graph[i]){
if(component[v.second] != pq.top().second) continue;
edges++;
}
}
ofs << pq.top().first << " " << edges<<endl;
for(int i=1; i < graph.size(); ++i){
if(component[i] != pq.top().second) continue;
for(auto v : graph[i]){
if(component[v.second] != pq.top().second) continue;
ofs << i << " " << v.first<<" "<<v.second<<endl;
}
}
cout<<pq.top().first << " " << edges<<endl;
ofs.close();
}
#endif //GOMORYHU_ERDOSRENYI_H