The file contains the adjacency list representation of a simple undirected graph. There are 200 vertices labeled 1 to 200. The first column in the file represents the vertex label, and the particular row (other entries except the first column) tells all the vertices that the vertex is adjacent to. So for example, the 6th row looks like : "6 155 56 52 120 ......". This just means that the vertex with label 6 is adjacent to (i.e., shares an edge with) the vertices with labels 155,56,52,120,......,etc
Your task is to code up and run the randomized contraction algorithm for the min cut problem and use it on the above graph to compute the min cut (i.e., the minimum-possible number of crossing edges). (HINT: Note that you'll have to figure out an implementation of edge contractions. Initially, you might want to do this naively, creating a new graph from the old every time there's an edge contraction. But you should also think about more efficient implementations.) (WARNING: As per the video lectures, please make sure to run the algorithm many times with different random seeds, and remember the smallest cut that you ever find.) Write your numeric answer in the space provided. So e.g., if your answer is 5, just type 5 in the space provided.
#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <random>
#include <time.h>
using namespace std;
void FindThEdges(vector<vector<int>> &graph, int r, int &rr, int &cc)
{
rr = -1; // indicate error
cc = -1;
int numv = graph.size();
int acc = 0;
for (int i=0; i<numv; i++)
{
if (graph[i].empty())
{
continue;
}
if (acc+graph[i].size() >= r)
{
rr = i;
cc = r - acc-1;
break;
}
else
{
acc += graph[i].size();
}
}
}
void NumOfVectex(vector<vector<int>> &graph, int &v, int &numV, int &numE)
{
numV = 0;
numE = 0;
int numG = graph.size();
for (int i=0; i < numG; i++)
{
if (!graph[i].empty())
{
numV++;
v = i;
numE += graph[i].size();
}
}
}
int RandomContraction(vector<vector<int>> &graph, default_random_engine &generator)
{
int numV;
int numE;
int se;
NumOfVectex(graph, se, numV, numE);
//cout << numV << " (" << numE << ")" << endl;
// ------------------
if (numV == 2)
return graph[se].size();
if (numV < 2)
return -1; // indicate error
// ------ Contraction --------
uniform_int_distribution<int> distribution(1, numE);
int m = distribution(generator);
int rr;
int cc;
FindThEdges(graph, m, rr, cc);
if (rr == -1 || cc == -1)
return -1;
// ----- union the vertex -------
int otherV = graph[rr][cc]-1;
for (int i=0; i<graph[otherV].size(); i++)
{
int t = graph[otherV][i]-1;
graph[rr].push_back(t+1); // direct the t
for (int j=0; j< graph[t].size(); j++)
{
if (graph[t][j] == otherV+1)
{
graph[t][j] = rr+1; // direct the rr
break;
}
}
}
graph[otherV].clear();
// ------ eliminate the loop -----
int n=0;
while(n < graph[rr].size())
{
if (graph[rr][n] == rr+1)
{
graph[rr].erase(graph[rr].begin()+n);
continue;
}
n++;
}
return RandomContraction(graph, generator);
}
int main()
{
ifstream infile;
infile.open("kargerMinCut.txt");
// ------------- Read the Graph --------------
vector<vector<int>> graph;
string line;
std::stringstream ss;
vector<int> verline;
while(getline(infile, line))
{
ss << line;
int n;
ss >> n;
int temp;
while(ss >> temp)
{
verline.push_back(temp);
}
graph.push_back(verline);
verline.clear();
ss.clear();
}
// ------------------------------------
int num = 1000;
int temp;
vector<vector<int>> cgraph;
// ----
for (int i=0; i<200; i++)
{
for (int j=0; j<graph.size(); j++)
{
cgraph.push_back(graph[j]);
}
default_random_engine rd((unsigned)time(NULL));
temp = RandomContraction(cgraph, rd);
num = min(num, temp);
cgraph.clear();
}
// ------------------------------------
infile.close();
return 0;
}