Exemplo n.º 1
0
 private void Visit(EdgeWeightedGraph g, int v)
 {
     marked[v] = true;
     foreach (var e in g.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;
         }
         if (e.Weight < distTo[w])
         {
             edgeTo[w] = e;
             distTo[w] = e.Weight;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Exemplo n.º 2
0
    private void Visit(EdgeWeightedGraph g, int v)
    {
        marked[v] = true;

        ListBag <Edge> list = g.Adj()[v];
        BagNode <Edge> node = list.first;

        while (node != null)
        {
            int w = node.t.Other(v);

            if (!marked[w])
            {
                //更新权值最小的边
                if (node.t.Weight() < distTo[w])
                {
                    edgeTo[w] = node.t;
                    distTo[w] = node.t.Weight();

                    //记录最近的顶点
                    if (same)
                    {
                        pq[pq.Count - 1] = w;
                    }
                    else
                    {
                        same = true;
                        pq.Add(w);
                    }
                }
            }
            node = node.next;
        }
    }
Exemplo n.º 3
0
 private void scan(EdgeWeightedGraph G, int v)
 {
     marked[v] = true;
     foreach (Edge e in G.Adj(v))
     {
         int w = e.other(v);
         if (marked[w])
         {
             continue;                    // v-w is obsolete edge
         }
         if (e.Weight() < distTo[w])
         {
             distTo[w] = e.Weight();
             edgeTo[w] = e;
             if (pq.contains(w))
             {
                 pq.decreaseKey(w, distTo[w]);
             }
             else
             {
                 pq.insert(w, distTo[w]);
             }
         }
     }
 }
Exemplo n.º 4
0
        private void Scan(EdgeWeightedGraph G, int v)
        {
            _marked[v] = true;

            foreach (Edge e in G.Adj(v))
            {
                int w = e.Target(v);
                if (_marked[w])
                {
                    continue;
                }
                if (e.Weight() < _distTo[w])
                {
                    _distTo[w] = e.Weight();
                    _edgeTo[w] = e;
                    if (_pq.Contains(w))
                    {
                        _pq.ChangeKey(w, _distTo[w]);
                    }
                    else
                    {
                        _pq.Insert(w, _distTo[w]);
                    }
                }
            }
        }
 void Visit(EdgeWeightedGraph g, int v)
 {
     m_marked[v] = true;
     foreach (var e in g.Adj(v))
     {
         int w = e.Other(v);
         if (m_marked[w])
         {
             continue;
         }
         if (e.Weight() < m_distTo[w])
         {
             m_edgeTo[w] = e;
             m_distTo[w] = e.Weight();
             if (m_pq.contains(w))
             {
                 m_pq.change(w, m_distTo[w]);
             }
             else
             {
                 m_pq.insert(w, m_distTo[w]);
             }
         }
     }
 }
Exemplo n.º 6
0
 private void Scan(EdgeWeightedGraph g, int v)
 {
     _marked[v] = true;
     foreach(var x in g.Adj(v))
         if(!_marked[v.TheOther(v)])
             _minPQ.Insert(x);
 }
 private void Visit(EdgeWeightedGraph g, int v)
 {
     marked[v] = true;
     foreach (var e in g.Adj(v))
     {
         if (!marked[e.Other(v)])
         {
             pq.Insert(e);
         }
     }
 }
Exemplo n.º 8
0
 private void scan(EdgeWeightedGraph G, int v)
 {
     marked[v] = true;
     foreach (Edge e in G.Adj(v))
     {
         if (!marked[e.other(v)])
         {
             pq.insert(e);
         }
     }
 }
Exemplo n.º 9
0
 private void dfs(EdgeWeightedGraph G, int v)
 {
     marked[v] = true;
     id[v]     = count;
     size[count]++;
     foreach (Edge e in G.Adj(v))
     {
         int w = e.other(v);
         if (!marked[w])
         {
             dfs(G, w);
         }
     }
 }
Exemplo n.º 10
0
        private void Visit(EdgeWeightedGraph g, int v)
        {
            _marked[v] = true;
            foreach (var e in g.Adj(v))
            {
                var notV = e.Other(v);

                if (e.Weight() < _distTo[notV])
                {
                    _edgeTo[notV] = e;
                    _distTo[notV] = e.Weight();
                    _pq.Insert(_distTo[notV]);
                }
            }
        }
Exemplo n.º 11
0
    private void Visit(EdgeWeightedGraph g, int v)
    {
        marked[v] = true;

        ListBag <Edge> list = g.Adj()[v];
        BagNode <Edge> node = list.first;

        while (node != null)
        {
            if (!marked[node.t.Other(v)])
            {
                pq.Add(node.t);
            }
            node = node.next;
        }
    }
Exemplo n.º 12
0
        private void Scan(EdgeWeightedGraph G, int v)
        {
            _marked[v] = true;

            foreach (Edge e in G.Adj(v))
            {
                int w = e.Target(v);
                if (_marked[w]) continue;
                if (e.Weight() < _distTo[w])
                {
                    _distTo[w] = e.Weight();
                    _edgeTo[w] = e;
                    if (_pq.Contains(w)) _pq.ChangeKey(w, _distTo[w]);
                    else _pq.Insert(w, _distTo[w]);
                }
            }
        }
Exemplo n.º 13
0
        void MinimumSpanningTreesTest()
        {
            var strArr = FileHandler.ReadFileAsStrArr("tinyEWG.txt");
            var ewg    = new EdgeWeightedGraph(strArr);

            foreach (var e in ewg.Adj(0))
            {
                Console.WriteLine(e);
            }
            Console.WriteLine();
            var lpm = new LazyPrimMST(ewg);

            foreach (var e in lpm.Edges())
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();
        }
Exemplo n.º 14
0
    public KruskalMST(EdgeWeightedGraph g)
    {
        mst = new List <Edge>();
        pq  = new List <Edge>();

        ListBag <Edge>[] list = g.Adj();

        for (int i = 0; i < list.Length; i++)
        {
            ListBag <Edge> e    = list[i];
            BagNode <Edge> node = e.first;

            while (node != null)
            {
                //Debug.Log(node.t.Weight());
                pq.Add(node.t);
                node = node.next;
            }
        }

        //每次添加一条权值最小的边
        //多花一个数组空间检测是否形成环
        while (pq.Count != 0 && mst.Count < g.V() - 1)
        {
            int min = GetMinWeight();

            Edge e = pq[min];
            pq.RemoveAt(min);

            if (!Connected(e))
            {
                int v = e.Either();
                int w = e.Other(v);

                //Debug.Log(v + "," + w);

                mst.Add(e);
            }
        }
    }