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

Berkeley CS 61B Data Structures(in Java) lab13

🔗
elyn 2016-6-17 16:22:30 | 只看该作者
全局:
Sophia_Z 发表于 2016-6-17 15:28
被发现了。。。我用了一个boolean记录是否全部遍历过。。。好蛋疼的。。把BFS改了一下。。话说 ...

好囧,原来我的算法也用一重循环就可以了。
把每个点当做开始点BFS,把第N层的点作为结束点,更新到新的G里面就可以了,,,哈哈哈,也不知道当初为什么用了两重循环
回复

使用道具 举报

🔗
zzdsg 2016-8-26 11:18:46 | 只看该作者
全局:
做完了~开心~来这里打卡
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
fishgo 2016-9-30 01:20:15 | 只看该作者
全局:
马上就要完成61B了,开心!
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
闲的时光 2016-12-7 15:47:18 | 只看该作者
全局:

回复

使用道具 举报

🔗
codergoose 2016-12-17 12:13:34 | 只看该作者
全局:

回复

使用道具 举报

全局:
这道题目粗看不难,但是readme.pdf中有几句提示值得玩味:
Try to think of the fastest, simplest code for length2Paths(). It's possible to do it with a relatively simple triply-nested loop.  ... If your TA thinks your algorithm is too slow, you'll be asked to do it again.
那么问题来了,how to define fastest, simplest code?
我用两种不同的思路来解决这个问题:
(1)use a triply-nested loop with modification.
   INSTEAD OF:
  1. for( int u = 0; u < vertices; u++){
  2.             for(int v = 0; v < vertices; v++) {
  3.                          for(int w = 0; w < vertices; w++){
  4.               // set newGraph.adjMatrix[][].
复制代码
The running time is O(n^3). Thus, we need to improve the performance.
IMPROVEMENT:
  1. //STEP 1: loop through the adjMatrix to determine the degree of each vertex, O(n^2).
  2. //STEP 2:  loop through the adjMatrix to form the incidentEdges for each vertex, O(n^2). (NOTE: incidentEdges[i].length == degree(i)  => save memory.)
  3. //STEP 3:
  4.                    for(int u = 0; u < vertices; u++) {
  5.                          for(int v : incidentEdges[u]) {
  6.                                for(int w : incidentEdges[v]){
  7.                                // set newGraph.adjMatrix[][].
复制代码
The running time is O(n^2 + n * max(degree(i))^2). Faster since degree(i) < n.
This implementation is much intuitive and clear.
(2) use BFS as someone suggested.
STEP1  loop through every vertex.
              STEP1.1   start BFS from this vertex.
              STEP1.2   get all vertices in the LEVEL length and set newGraph.adjMatrix.
The crux of this implementation: how to represent one vertex's LEVEL.
Originally, I used an int array to store the level info and update it according to the preceding vertex's.
However, it is WRONG. Because the same vertex can have the DIFFERENT level number and can COEXIST in the queue under certain circumstances.

For example, in the given test code, when length = 5.
Level       0     1    2    3     4     5
vertex      8->4->2->0->8->4
                   8->4->5->7
                   8->4->5->9->1->0
                   8->4->5->9->1->3
                   8->6->4->2->0->8
                   8->6->4->5->7
                   8->6->4->5->9
                   8->6->7
                   8->10->6->4->2
                   8->10->6->4->5
                   8->10->6->7     
The running time is somewhat complicated, which I am unable to give the analysis.

评分

参与人数 1大米 +3 收起 理由
蔚蔚酱 + 3 回答的很好!

查看全部评分

回复

使用道具 举报

🔗
蔚蔚酱 2017-3-18 22:10:08 | 只看该作者
全局:
今天终于get到了递归的一种正确打开方式,不能去用回溯的方法理解递归(因为回溯本质上还是在一层层迭代了),而是用数学归纳法理解递归。只要base case成立(n=0,程序对n=1时成立),n=k时,对k+1也成立,那么久永远成立了。
看了楼上的,只有一句膜可以说。。。。

Lab13.PNG (9.77 KB, 下载次数: 0)

Lab13.PNG
回复

使用道具 举报

🔗
mmyn 2017-5-19 01:44:20 | 只看该作者
全局:
想要得到正确答案并不难,但是看了楼上各种分析之后感觉自己有点儿嫩了,今天晚了,明天起来看看有没有能改进的地方~
回复

使用道具 举报

🔗
Wei Zhang 2017-5-26 03:24:26 | 只看该作者
本楼:
全局:
lab13 done
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
splansher 2017-5-29 11:33:36 | 只看该作者
全局:
elyn 发表于 2016-6-17 16:22
好囧,原来我的算法也用一重循环就可以了。
把每个点当做开始点BFS,把第N层的点作为结束点,更新到新的 ...

你好 。。那个BFS怎么套进去阿。。就是怎么把老师上课的  BFS代码改一下。。。改了好久。。
回复

使用道具 举报

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

本版积分规则

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