• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            lzm

            who dare win.
            posts - 14, comments - 29, trackbacks - 0, articles - 0

            Kruskal算法

            Posted on 2009-04-10 19:41 lzmagic 閱讀(4565) 評論(1)  編輯 收藏 引用 所屬分類: Algorithm
            /**
             * KRUSKAL 最小生成樹算法 (Minimum Spanning Tree) 
             * 輸入:圖g;                 // 有向圖或者無向圖 
             * 輸出:(1)最小生成樹長sum; 
             *         (2)最小生成樹mst。
             * 結構: 圖g用頂點數n和邊集edges(優先隊列)表示,兩點是否連通用并查集實現。 
             * 算法:Kruskal算法  
             * 復雜度:O(|E|*log|E|)~O(|E|*log|V|) 
             
            */
             
            #include 
            <iostream>
            #include 
            <string>
            #include 
            <vector>
            #include 
            <deque>
            #include 
            <list>
            #include 
            <stack>
            #include 
            <queue>
            #include 
            <set>
            #include 
            <map>
            #include 
            <bitset>
            #include 
            <iterator>
            #include 
            <algorithm>
            #include 
            <numeric>
            #include 
            <functional>
            #include 
            <climits>
            using namespace std;

            struct Edge
            {
                
            int u, v, w;        // u、v:兩個頂點  w:權 
                Edge() { }
                Edge(
            int u0, int v0, int w0):u(u0), v(v0), w(w0) { }
            }
            ;

            int n;                    // n : 頂點個數 
            vector<Edge> edges;        // edges : 圖g的所有邊 
            int sum;                // sum : 最小生成樹長 
            vector<Edge> mst;        // mst : 最小生成樹(用邊集表示) 

            class DisjSets
            {
                vector
            <int> s;
                
            public:
                    DisjSets(
            int n) : s(n, -1{ };
                    
            int find (int x)
                    
            {
                        
            if (s[x] < 0return x;
                        
            else return s[x] = find(s[x]);   // 壓縮路徑。 
                    }
            ;
                    
            void unionSets(int root1, int root2)
                    
            {
                        
            if (s[root1] > s[root2])    // 按個數求并(個數用負數表示)。 
                        {
                            s[root2] 
            += s[root1];
                            s[root1] 
            = root2;
                        }

                        
            else
                        
            {
                            s[root1] 
            += s[root2];
                            s[root2] 
            = root1;
                        }

                    }
            ;
            }


            bool Cmp(const Edge &lhs, const Edge &rhs)
            {
                
            return lhs.w > rhs.w;
            }
             

            bool Kruskal()
            {
                DisjSets ds(n);
                make_heap(edges.begin(), edges.end(), Cmp);    
            // 對邊集建堆。 
                int root1, root2;
                Edge e;
                
            while (!edges.empty())    // 遍歷所有邊, 
                {
                    e 
            = edges.front(); pop_heap(edges.begin(), edges.end(), Cmp); edges.pop_back();// 從未選邊集中尋找最小權的邊e。 
                    root1 = ds.find(e.u), root2 = ds.find(e.v);    // 獲取u、v所在的點集, 
                    if (root1 != root2)                // 如果u、v不是同一個點集, 
                    {
                        sum 
            += e.w;                 // 調整最小生成樹長 
                        mst.push_back(e);             // 把邊e放入最小生成樹mst中, 
                        ds.unionSets(root1, root2);    // 合并兩點所在的點集。 
                    }

                    
            if (mst.size() == n - 1return true// 如果選取邊個數為n - 1,成功。
                }

                
            return false;  
            }


            int main()
            {
                n 
            = 7;
                edges.clear();
                edges.push_back(Edge(
            012)); edges.push_back(Edge(024));  edges.push_back(Edge(031));
                edges.push_back(Edge(
            133)); edges.push_back(Edge(1410));
                 edges.push_back(Edge(
            232)); edges.push_back(Edge(255));
                 edges.push_back(Edge(
            347)); edges.push_back(Edge(358)); edges.push_back(Edge(364));
                 edges.push_back(Edge(
            466));
                 edges.push_back(Edge(
            561));
                 
                 sum 
            = 0;
                 mst.clear();
                 
            if(Kruskal())
                 
            {
                    cout 
            << sum << endl;
                    
            for (vector<Edge>::iterator it = mst.begin(); it != mst.end(); ++it)
                        cout 
            << it-><< "->" << it-><< endl;
                }

                
            else
                
            {
                    cout 
            << "Some vertex cann't be reached." << endl; 
                }

                 
                system(
            "pause");
                
            return 0;
            }

            Feedback

            # re: Kruskal算法  回復  更多評論   

            2010-12-06 22:14 by mwxjm
            這個程序是不是有個bug:
            如果節點數量為1,邊數量為0
            則應該是有生成樹的,但是kruskal函數返回結果為false吧
            個人意見
            久久精品国产亚洲av高清漫画 | 久久AV高潮AV无码AV| 亚洲精品乱码久久久久久蜜桃| 久久综合九色综合久99| 久久天天躁狠狠躁夜夜2020一 | 久久精品免费网站网| 久久久久无码精品| 国产精品VIDEOSSEX久久发布| 国产精品日韩欧美久久综合| 亚洲国产成人精品久久久国产成人一区二区三区综 | 激情伊人五月天久久综合 | 欧美激情精品久久久久久久九九九 | 久久综合色老色| 久久99精品久久久久久| 亚洲国产成人久久综合区| 成人免费网站久久久| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 亚洲中文久久精品无码ww16| 久久免费小视频| 日日噜噜夜夜狠狠久久丁香五月| 国产精品xxxx国产喷水亚洲国产精品无码久久一区| 99精品国产免费久久久久久下载| 久久精品草草草| 精品久久久久久无码专区| 国产亚洲精久久久久久无码77777| 久久久久久免费一区二区三区 | 99精品国产综合久久久久五月天| AA级片免费看视频久久| 青草国产精品久久久久久| 欧美日韩精品久久久免费观看| 久久久久无码国产精品不卡| 97超级碰碰碰碰久久久久| 久久久噜噜噜久久中文福利| 伊人久久大香线蕉AV色婷婷色| 一本久久免费视频| 伊人久久无码精品中文字幕| 人人狠狠综合久久亚洲| 久久国产免费直播| 久久人人爽人人精品视频| 四虎国产精品成人免费久久| 久久免费视频一区|