我是靠谱客的博主 无限蜜蜂,最近开发中收集的这篇文章主要介绍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;
}


最后

以上就是无限蜜蜂为你收集整理的Stanford 算法入门 week 3 Assignment的全部内容,希望文章能够帮你解决Stanford 算法入门 week 3 Assignment所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部