Stanford 算法入门 week 3 Assignment

Question 5

The minimum s-t cut problem is the following. The input is an undirected graph, and two distinct vertices of the graph are labelled "s" and "t". The goal is to compute the minimum cut (i.e., fewest number of crossing edges) that satisfies the property that s and t are on different sides of the cut.

Suppose someone gives you a subroutine for this s-t minimum cut problem via an API. Your job is to solve the original minimum cut problem (the one discussed in the lectures), when all you can do is invoke the given min s-t cut subroutine. (That is, the goal is to reduce the min cut problem to the min s-t cut problem.)

Now suppose you are given an instance of the minimum cut problem -- that is, you are given an undirected graph (with no specially labelled vertices) and need to compute the minimum cut. What is the minimum number of times that you need to call the given min s-t cut subroutine to guarantee that you'll find a min cut of the given graph?

这题做两遍没理解……


---------------------------------------------------------------------------------------------------

Programming Question - 3

Question 1

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值