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

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

全局:
托了两周才把week 4的编程题补上。。。完全按照video上的算法写的,为了图省事用了三个graph,空间复杂度太大,不过不会出现爆栈的情况,而且计算速度比较快。

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <stack>
  5. #include <algorithm>
  6. using namespace std;

  7. #define SIZE 875714

  8. struct Node{
  9.         size_t finishTime;  // finishing time
  10.         size_t leader; // leader node of inclosed SCC
  11.         bool ifExplored;  // flag for DFS exploration
  12.         vector<size_t> tailList; // list of tail nodes
  13. };

  14. Node graph[SIZE],revGraph[SIZE],copyGraph[SIZE];
  15. size_t countFinishTime; //count for finishing time
  16. size_t leaderNode; // index of leader node in a strongly connected component


  17. void DFS(Node testGraph[],int headNode){
  18.         testGraph[headNode-1].ifExplored = true;  // mark head node as explored
  19.         testGraph[headNode-1].leader = leaderNode;  // mark leader node
  20.         for (vector<size_t>::size_type i=0;i!=testGraph[headNode-1].tailList.size();i++)  // traverse all arcs from source node
  21.         {
  22.                 int tail = testGraph[headNode-1].tailList[i];
  23.                 if (!testGraph[tail-1].ifExplored)  // if the tail is unexplored
  24.                         DFS(testGraph,tail);
  25.         }
  26.         testGraph[headNode-1].finishTime = ++countFinishTime;  //mark finishing time
  27. }

  28. void DFSLoop(Node testGraph[]){
  29.         countFinishTime =0;  
  30.         leaderNode = 0;
  31.         for (size_t i=SIZE;i>0;i--){  //traverse nodes in descending order
  32.                 if (!testGraph[i-1].ifExplored){  //if the node unexplored
  33.                         leaderNode = i;  //mark as leader node
  34.                         DFS(testGraph,i);   
  35.                 }
  36.         }
  37. }

  38. void  CountSCCs(Node testGraph[]){
  39.         size_t SCCList[SIZE] ={0};  //array of leader in SCCs
  40.         for (size_t i=0;i<SIZE;i++)  // counting size of SCC
  41.                 SCCList[testGraph[i].leader-1] ++;
  42.        
  43.         sort(SCCList,SCCList+SIZE);  // sort the sizes in ascending order
  44.         for( int i=SIZE;i>SIZE-5;i--) // print the largest 5 sizes
  45.                 cout<<SCCList[i-1]<<" ";
  46. }

  47. void KosarajusAlgorithm(){
  48.         DFSLoop(revGraph);  //run DFSLoop on reversed graph
  49.        
  50.         size_t tempFinishTime;   
  51.         size_t tempTailNode;
  52.         vector<size_t> tempList;
  53.         for (size_t i=0;i<SIZE;i++){  // replace node index with node finishing time
  54.                 tempList.clear();
  55.                 tempFinishTime = revGraph[i].finishTime;
  56.                 for (vector<size_t>::size_type j=0;j<copyGraph[i].tailList.size();j++){
  57.                         tempTailNode = copyGraph[i].tailList.at(j);
  58.                         tempList.push_back(revGraph[tempTailNode-1].finishTime);
  59.                 }
  60.                 graph[tempFinishTime-1].tailList = tempList;
  61.         }
  62.         DFSLoop(graph);  // run DFSLoop on forward-order graph

  63.         CountSCCs(graph); // Count the 5 largest SCCs
  64. }

  65. int main()
  66. {       
  67.         FILE *pFile;
  68.         size_t headNode,tailNode;       
  69.         pFile =  fopen("SCC.txt","r");
  70.         if (pFile ==NULL) perror("Error openning file.");
  71.         else{
  72.                 rewind(pFile);
  73.                 while (!feof(pFile)){
  74.                         fscanf_s(pFile,"%d",&headNode);
  75.                         fscanf_s(pFile,"%d",&tailNode);
  76.                         copyGraph[headNode-1].tailList.push_back(tailNode);
  77.                         revGraph[tailNode-1].tailList.push_back(headNode);
  78.                 }
  79.         }
  80.         fclose(pFile);
  81.         KosarajusAlgorithm();

  82.         return 0;
  83. }
复制代码


回复

使用道具 举报

🔗
lhh_NJU 2012-9-26 15:07:24 | 只看该作者
全局:
蜡笔小丸子 发表于 2012-7-25 12:06
托了两周才把week 4的编程题补上。。。完全按照video上的算法写的,为了图省事用了三个graph,空间复杂度太 ...

哎, 暑假准备T去了。 最近才开始补这门课的作业, 我也做到了这道题哎, 确切答案是多少啊?我已经试过5次了。 可以私信告诉我吗?
回复

使用道具 举报

🔗
lhh_NJU 2012-9-27 15:39:46 | 只看该作者
全局:
蜡笔小丸子 发表于 2012-7-25 12:06
托了两周才把week 4的编程题补上。。。完全按照video上的算法写的,为了图省事用了三个graph,空间复杂度太 ...

我已经算出正确答案了, 之前写的是iterativeDFS, 在小的测试用例上没问题, 现在改了内存设置后可以用递归式DFS了,然后结果就对了。 泪奔啊....
回复

使用道具 举报

🔗
Shuang7 2013-3-3 09:40:39 | 只看该作者
全局:
本帖最后由 Shuang7 于 2013-3-3 09:46 编辑

花了一个下午+晚上才搞定,居然遇到传说中的stack overflow了= = 最后通过调整栈大小解决的。。C++编写,release版运行课程数据用时约6s :) 套用前几周写好的工程,新加入程序约100行,不过封装情况还有待改进~
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

全局:
Week 4的练习题:


Week 4的编程题:

评分

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

查看全部评分

回复

使用道具 举报

🔗
lljllj122 2013-8-5 19:06:35 | 只看该作者
全局:
昨天晚上才开始写,时间很赶,所以就用用python写。。虽然写得很乱,但是好歹弄出来了递归版本。
做小的测试集都能通过,一做作业给的数据我的栈区就傲娇了...后来才知道MacOS 只给8192B的栈区.....
辗转各种运行环境(ubuntu, win)运行结果依然各种killed。 换成非递归版本只怕也来不及了,我决定“cheating”一下..换了 stackless版的解释器

于是。。跑了将近3分钟,终于跑出结果了
膜拜楼上几位10秒之内出结果的大神啊!


我现在正在改非递归的版本

评分

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

查看全部评分

回复

使用道具 举报

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

本版积分规则

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