请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

newv的判断是否多余

拓展切分时, 对newv进行了判断, 找到不属于蓝色切分的端点, 然后将其visited设为true. 这个newv = visited[minEdge.getV()] ? minEdge.getW() : minEdge.getV(); 是否多余呢, 因为好像每次往优先队列里面丢入新的横切边的时候, 新的横切边的初始化方式都是v为true的结点, w为false的结点的呀。

正在回答

1回答

把你认为的,去掉冗余的,完整的 Prim 算法的代码贴一下?

0 回复 有任何疑惑可以回复我~
  • 提问者 IT_god #1
    public Prim(WeightedGraph G){
    
            this.G = G;
            mst = new ArrayList<>();
    
            CC cc = new CC(G);
            if(cc.count() > 1) return;
    
            boolean visited[] = new boolean[G.V()];
            visited[0] = true;
            Queue pq = new PriorityQueue<WeightedEdge>();
            for(int w: G.adj(0))
                pq.add(new WeightedEdge(0, w, G.getWeight(0, w)));
    
            while(!pq.isEmpty()){
    
                WeightedEdge minEdge = (WeightedEdge) pq.remove();
                if(visited[minEdge.getV()] && visited[minEdge.getW()])
                    continue;
    
                mst.add(minEdge);
    
                int newv = minEdge.getW();
                visited[newv] = true;
                for(int w: G.adj(newv))
                    if(!visited[w])
                        pq.add(new WeightedEdge(newv, w, G.getWeight(newv, w)));
            }
        }
    回复 有任何疑惑可以回复我~ 2020-10-26 13:14:51
  • liuyubobobo 回复 提问者 IT_god #2
    理解了。可以,没问题的:)
    回复 有任何疑惑可以回复我~ 2020-10-26 13:22:22
  • 提问者 IT_god 回复 liuyubobobo #3
    谢谢老师
    回复 有任何疑惑可以回复我~ 2020-10-26 13:29:34
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号