MinCut

21 篇文章 0 订阅
9 篇文章 0 订阅

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值