楼主: paradox
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford Design and Analysis of Algorithm I 第二期 Week#3 自由讨论

 关闭
🔗
endless 2012-7-18 11:21:40 | 只看该作者
全局:
tc1220 发表于 2012-7-17 22:39
才看到week3...
看到你的解释还有疑问

可以看一下8垅,涉及cut定义的讨论~
回复

使用道具 举报

全局:
拖了两周才把代码贴出来。。。用了array,缺点是运算速度很慢。随机vertex的选取方法也很naive。过两天有时间的话会改进一下,再把改进的代码贴出来吧。
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <time.h>
  8. using namespace std;

  9. #define SIZE 200
  10. #define MAX 10000

  11. int CountVertices(int list[][SIZE]){
  12.         int count=0;
  13.         for (int i=0;i<SIZE;i++)
  14.                 for (int j=0;j<SIZE;j++)
  15.                         if (list[i][j] !=0){
  16.                                 count++;
  17.                                 break;
  18.                         }
  19.         return count;
  20. };

  21. int CountEdges(int list[][SIZE]){
  22.         int count=0;
  23.         for (int i=0;i<SIZE;i++)
  24.                 for (int j=0;j<SIZE;j++)
  25.                         if (list[i][j] !=0){
  26.                                 count = list[i][j];
  27.                                 return count;
  28.                         }
  29.         return count;
  30. };

  31. void MergeVertices(int list[][SIZE],int u, int v){  // merge nodes u and v into u
  32.                
  33.         for (int i=0;i<SIZE;i++){
  34.                 list[u][i] = list[u][i] +list[v][i];
  35.                 list[v][i] = 0;
  36.                 list[i][u] = list[i][u] +list[i][v];
  37.                 list[i][v] = 0;
  38.         }
  39. };

  40. void RemoveSelfLoop(int list[][SIZE]){
  41.         for (int i=0;i<SIZE;i++)
  42.                 list[i][i] = 0;
  43.        
  44. };

  45. int RandomContraction(int list[][SIZE]){
  46.         while (CountVertices(list)>2)
  47.         {
  48.                 int random_u=0, random_v=0;
  49.                 do{
  50.                         //
  51.                         random_v = rand()%SIZE;
  52.                         random_u = rand()%SIZE;
  53.                 }while(random_u == random_v ||list[random_u][random_v] ==0 );
  54.                 MergeVertices(list,random_u,random_v);
  55.                 RemoveSelfLoop(list);
  56.         }
  57.         return CountEdges(list);
  58. };

  59. void print(int list[][SIZE]){
  60.         for (int i=0;i<SIZE;i++){
  61.                 for (int j=0;j<SIZE;j++)
  62.                         cout<<list[i][j]<<" ";
  63.                 cout<<endl;
  64.         }
  65. }


  66. int main()
  67. {
  68.         int adjList[SIZE][SIZE] ={0};
  69.         ifstream myFile("kargerMinCut.txt");
  70.         if (myFile.is_open())
  71.         {
  72.                 string line;
  73.         while (!myFile.eof()){
  74.             getline(myFile, line);
  75.             stringstream str(line);
  76.             int vertex =0;
  77.                         str>>vertex;
  78.             int adjVertices =0;
  79.             while (str>>adjVertices)
  80.                                 adjList[vertex-1][adjVertices-1]=1;
  81.         }
  82.                 myFile.close();
  83.         }
  84.         else
  85.                 cout<<"Unable to open file."<<endl;
  86.         srand((unsigned int) time(NULL));
  87.        
  88.         int runtime = SIZE*SIZE;
  89.         int min_cut = MAX;
  90.         for (int i=0;i<runtime;i++){
  91.                 int copy[SIZE][SIZE];
  92.                 for (int j=0;j<SIZE;j++)
  93.                         for (int k=0;k<SIZE;k++)
  94.                                 copy[j][k]= adjList[j][k];
  95.                 int cut = RandomContraction(copy);
  96.                 if (cut<min_cut)
  97.                         min_cut = cut;
  98.         }
  99.         cout<<"Minimum cut of the given graph:"<<min_cut<<endl;
  100.        
  101.         system("pause");
  102.         return 0;
  103. }
复制代码


回复

使用道具 举报

🔗
midfirefly 2012-7-30 06:28:15 | 只看该作者
全局:
上期才40个node,这次的200个,理论值N=n^2logn个,21万多次random contraction ,可以把失败率降低 1/200以下。也就是说假如200个学生都用一模一样的code做这个习题,里面大约能有一个人无法得到正确答案。

机器慢+同时还在搞n多其他事情,如果一直跑理论值会运行无穷久啊。。。

不过幸亏RP不错。。。。
1000 18 Min= 20
2000 35 Min= 20
3000 52 Min= 17
4000 71 Min= 17

只运行4000次就找到最小值的发生概率很小很小。。。。。
另外,建议楼上一些同学看看基本概率统计入门。。。
still python: 已经比从前做题顺溜多了。。。。

  1. f = open('kargerMinCut.txt')
  2. Gin={}
  3. for each in f.readlines():
  4.     each=map(int, each.split())
  5.     for e in each[1:]:
  6.         #print each[0], e
  7.         if each[0] not in Gin: Gin[each[0]]={}
  8.         Gin[each[0]][e]=1

  9. n = len(Gin)  #m=sum(Gin.values())/2
  10. N = int(n**2*log(n))
  11. print N
  12. time1=time()
  13. GlobalMin=10000
  14. for _nouse_ in range(N):
  15.     if _nouse_ % 1000 == 0:
  16.         print 'N=',_nouse_, 'Time=',int(time()-time1), 'Min=',GlobalMin

  17.     G=copy.deepcopy(Gin)
  18.     rs = random.seed()
  19.     while len(G.keys()) >2:
  20.         u=random.choice(G.keys())
  21.         v=random.choice(G[u].keys())

  22.         for x in G[u].keys():
  23.             if x in G[v]:
  24.                 G[u][x]=G[x][u]=G[u][x]+G[v][x]
  25.                 del G[v][x]
  26.                 del G[x][v]
  27.         
  28.         for x in G[v].keys():
  29.             if x != u:
  30.                 G[u][x]=G[x][u]=G[v][x]
  31.             del G[x][v]
  32.         del G[v]

  33.     gmin = G.values().pop().values().pop()
  34.     GlobalMin=min([gmin, GlobalMin])
  35. print 'MinCut=',GlobalMin
  36. print 'n=',n,'N=',N,'Time=',time()-time1
复制代码
回复

使用道具 举报

🔗
Shuang7 2013-3-1 10:34:16 | 只看该作者
全局:
本帖最后由 Shuang7 于 2013-3-1 10:36 编辑

算是将将赶上进度了。。
C++ release下测试1000次用时3.33 s, 共18次测到最小值,这样看来概率大约是1.8% (3.2GHz 主机)


评分

参与人数 1学分 +1 收起 理由
m4reiiy + 1

查看全部评分

回复

使用道具 举报

🔗
wangyf1990 2013-7-26 10:31:43 | 只看该作者
全局:
小K 发表于 2012-7-30 06:28
上期才40个node,这次的200个,理论值N=n^2logn个,21万多次random contraction ,可以把失败率降低 1/200以 ...

我100次就能出正确结果。。 而且随便试了10次都能出。。
回复

使用道具 举报

全局:
Week 3的作业:

选择题:


编程题:


评分

参与人数 1学分 +1 收起 理由
Shuang7 + 1

查看全部评分

回复

使用道具 举报

🔗
yzm4213 2013-7-29 13:27:43 | 只看该作者
全局:
圆梦梦剧场 发表于 2013-7-29 13:03
Week 3的作业:

选择题:

求编程题答案。。
回复

使用道具 举报

🔗
myprincessss 2013-12-25 05:47:03 | 只看该作者
全局:
wangyf1990 发表于 2013-7-26 10:31
我100次就能出正确结果。。 而且随便试了10次都能出。。

me too...
回复

使用道具 举报

🔗
zorroxue 2014-6-1 00:53:50 | 只看该作者
全局:
答案~~~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表