-
Notifications
You must be signed in to change notification settings - Fork 0
/
KargerMinCuts.cpp
142 lines (118 loc) · 3.41 KB
/
KargerMinCuts.cpp
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
#include <iterator>
#include <sstream>
#include <map>
#include <string>
using namespace std;
int Karger(map<int, vector<int>> data)
{
int rand1, rand2;
int v1, v2;
// Loop while there are more than 2 nodes (the entire graph has not been randomly contracted completely
while (data.size() > 2)
{
//Retreive a random key/node from the graph
auto it = data.begin();
std::advance(it, rand() % data.size());
rand1 = it->first;
//Retreive a random node from the list associated with the first node
rand2 = rand() % data[rand1].size();
//2 random verticies to merge
v1 = rand1;
v2 = data[rand1][rand2];
//Append nodes from v2 adjacency list to the v1 node list
data[v1].reserve(data[v1].size() + distance(data[v2].begin() , data[v2].end()));
data[v1].insert(data[v1].end(), data[v2].begin() , data[v2].end());
//Iterate through all nodes and replace all instances of v2 with v1
map < int, vector < int> >::iterator map_it;
for (map_it = data.begin(); map_it != data.end(); ++map_it){
for (int i = 0; i < (*map_it).second.size(); i++)
{
if ((*map_it).second[i] == v2){
(*map_it).second.erase((*map_it).second.begin() + i);
(*map_it).second.shrink_to_fit();
i--;
}
}
(*map_it).second.push_back(v1);
}
// Remove all self loop nodes in the list for the node
for (int k = 0; k < (int)data[v1].size(); k++)
{
// Remove self loops
if (data[v1][k] == v1)
{
data[v1].erase(data[v1].begin() + k);
data[v1].shrink_to_fit();
//Iterator has changed when element was erased so backtrack 1 element
k--;
}
}
//Remove the contracted unused node
data.erase(v2);
}
//Return the number of cross links
return data[v1].size();
}
//Function used to split a string into a vector
vector<std::string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
//Function used to split a string into a vector
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
// Main function reads input from file and runs quicksort
int _tmain(int argc, _TCHAR* argv[])
{
ifstream file("C:/Users/sean/Downloads/test.txt", ios::in);
string tmp;
int minCut = 0;
int bestMinCut = 0;
int numTrials = 100;
// Change the random seed value
srand((int)time(NULL));
// Create graph data structure
map<int,vector<int>> graph;
//Read file into graph structure
while (!file.eof())
{
getline(file, tmp);
vector<string> splitLine = split(tmp, ' ');
if (splitLine.size() == 0)
continue;
for (int i = 1; i < (int)splitLine.size(); i++)
{
graph[stoi(splitLine[0])].push_back(stoi(splitLine[i]));
}
}
file.close();
// Iterate over the algorithm many times to get a higher probability of getting the correct number of cuts
for (int j = 0; j < numTrials; j++)
{
map<int, vector<int>> temp = graph;
//Perform the random contraction algorithm
minCut = Karger(graph);
cout << minCut << endl;
//If first iteration, take this value as the minum
if (j == 0)
bestMinCut = minCut;
//If not first iteration, check for lower value and set that to the best
else if (minCut < bestMinCut)
bestMinCut = minCut;
}
cout << "The Minimum number of cuts is: " << bestMinCut << endl;;
getchar();
return 0;
}