Download the text file
here. (Right click and save link as)
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. (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.
我的代码——编译成功,运行到没几次就出现 pair[0] = 201,怎么会出现编号超过200的顶点呢?郁闷死了……
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define NUM 200
struct edge {
int vertex;
struct edge * next;
};
void contraction(struct edge ** graph);
int * randomPick(struct edge ** graph);
void mergeVertices(struct edge ** graph, int * pair);
struct edge ** fetchGraph();
void deleteSelfLoop(struct edge ** graph);
int solveMincut(struct edge ** graph);
int main() {
srand(time(0));
//read data, build graph
int j, newAns;
int MinCut = 100000;
for(j = 0; j < 1000; j++) {
struct edge ** graph = fetchGraph();
newAns = solveMincut(graph);
MinCut = (MinCut < newAns)
? MinCut : newAns;
free(graph);
}
cout<<MinCut<<endl;
return 0;
}
int solveMincut(struct edge ** graph) {
int i, k, m = 0 ;
struct edge * node;
for(i = NUM; i > 2; i--) {
contraction(graph);
}
for(k = 1; k <= NUM; k++) {
for(node = graph[k]; node->next; node = node->next) {
m++;
}
}
return m / 2;
}
void deleteSelfLoop(struct edge ** graph) {
struct edge * node;
int i;
for(i = 1; i <= NUM; i++) {
for(node = graph[i]; node->next; )
if(node->next->vertex == graph[i]->vertex) {
node->next = node->next->next;
} else {
node = node->next;
}
}
}
void mergeVertices(struct edge ** graph, int * pair) {
struct edge * node;
int i;
for(i = 1; i <= NUM; i++) {
for(node = graph[i]; node->next; node = node->next)
if(node->next->vertex == pair[0])
node->next->vertex = pair[1];
}
node = graph[pair[1]];
while(node->next) {
node = node->next;
}
node->next = graph[pair[0]]->next;
graph[pair[0]]->next = NULL;
}
void contraction(struct edge ** graph) {
int * pair;
pair = randomPick(graph);
mergeVertices(graph, pair);
deleteSelfLoop(graph);
free(pair);
}
int * randomPick(struct edge ** graph) {
int * pair = (int *) calloc(2, sizeof(int));
int i, j, m = 0, pick = 0;
struct edge * node;
for(i = 1; i <= NUM; i++) {
for(node = graph[i]; node->next; node = node->next)
m++;
}
pick = rand() % m + 1;
node = (struct edge*) calloc(1, sizeof(struct edge));
node->next = NULL;
for(i = 1, j = 0; j < pick; ) {
if(node->next) {
node = node->next;
j++;
} else {
node = graph[i++];
}
}
pair[0] = i;
pair[1] = node->vertex;
return pair;
}
//读入一行数据,存入string,再用stringstream <sstream>
struct edge ** fetchGraph() {
struct edge ** graph = (struct edge **) calloc(NUM + 1, sizeof(struct edge *));
struct edge *node;
ifstream fin("kargerMinCut.txt");
string line;
int row;
while(!fin.eof()) {
getline(fin, line);
stringstream st(line);
st>>row;
node = (struct edge *) malloc(sizeof(struct edge));
node->vertex = row;
node->next = NULL;
graph[row] = node;
int token;
while(st>>token) {
//free(node);
node = (struct edge*) calloc(1, sizeof(struct edge));
node->vertex = token;
node->next = graph[row]->next;
graph[row]->next = node;
}
}
fin.close();
//free(node);
return graph;
}
发表评论 取消回复