Exemplo n.º 1
0
 /// <summary>
 /// add all edges e incident to v onto pq if the other endpoint has not yet been scanned
 /// </summary>
 /// <param name="g"></param>
 /// <param name="v"></param>
 private void scan(EdgeWeightedGraph g, int v)
 {
     this._marked[v] = true;
     foreach (Edge e in g.Adj[v])
         if (!this._marked[e.Other(v)])
             this._pq.Insert(e);
 }
Exemplo n.º 2
0
 public LazyPrimMST(EdgeWeightedGraph g)
 {
     this.MST = new Queue<Edge>();
     this._pq = new MinPQ<Edge>();
     this._marked = new bool[g.V()];
     for (int v = 0; v < g.V(); v++)
         if (!this._marked[v])
             this.prim(g, v);
 }
Exemplo n.º 3
0
 private void prim(EdgeWeightedGraph g, int s)
 {
     this._distTo[s] = 0.0d;
     this._pq.Insert(s, this._distTo[s]);
     while (!this._pq.IsEmpty())
     {
         int v = this._pq.DelMin();
         this.scan(g, v);
     }
 }
Exemplo n.º 4
0
        public PrimMST(EdgeWeightedGraph g)
        {
            this._edgeTo = new Edge[g.V()];
            this._distTo = new double[g.V()];
            this._marked = new bool[g.V()];
            this._pq = new IndexMinPQ<double>(g.V());
            for (int v = 0; v < g.V(); v++)
                this._distTo[v] = double.PositiveInfinity;

            for (int v = 0; v < g.V(); v++)
                if (!this._marked[v])
                    this.prim(g, v);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new edge-weighted graph that is a deep copy of G.
 /// </summary>
 /// <param name="G"></param>
 public EdgeWeightedGraph(EdgeWeightedGraph G)
     : this(G.V())
 {
     this._e = G.E();
     for (int v = 0; v < G.V(); v++)
     {
         // reverse so that adjacency list is in same order as original
         Part1.Stack<Edge> reverse = new Part1.Stack<Edge>();
         foreach (Edge e in G.Adj[v])
             reverse.Push(e);
         foreach (Edge e in reverse)
             this.Adj[v].Add(e);
     }
 }
Exemplo n.º 6
0
        private void scan(EdgeWeightedGraph g, int v)
        {
            this._marked[v] = true;
            foreach (Edge e in g.Adj[v])
            {
                int w = e.Other(v);
                if (this._marked[w])
                    continue;

                if (e.Weight() < this._distTo[w])
                {
                    this._distTo[w] = e.Weight();
                    this._edgeTo[w] = e;
                    if (this._pq.Contains(w))
                        this._pq.DecreaseKey(w, this._distTo[w]);
                    else
                        this._pq.Insert(w, this._distTo[w]);
                }
            }
        }
Exemplo n.º 7
0
        public KruskalMST(EdgeWeightedGraph g)
        {
            MinPQ<Edge> pq = new MinPQ<Edge>();
            foreach (Edge e in g.Edges())
                pq.Insert(e);

            Part1.QuickUnion uf = new Part1.QuickUnion(g.V());
            while (!pq.IsEmpty() && this._mst.Count < g.V() - 1)
            {
                Edge e = pq.DelMin();
                int v = e.Either();
                int w = e.Other(v);
                if (!uf.IsConnected(v, w))
                {
                    uf.Union(v, w);
                    this._mst.Enqueue(e);
                    this.Weight += e.Weight();
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// run Prim's algorithm
        /// </summary>
        /// <param name="g"></param>
        /// <param name="s"></param>
        private void prim(EdgeWeightedGraph g, int s)
        {
            this.scan(g, s);

            while (!this._pq.IsEmpty())
            {
                Edge e = this._pq.DelMin();
                int v = e.Either(), w = e.Other(v);
                if (this._marked[v] && this._marked[w])
                {
                    // lazy, both v and w already scanned
                    continue;
                }

                this.MST.Enqueue(e);
                this.Weight += e.Weight();

                if (!this._marked[v])
                    this.scan(g, v);
                if (!this._marked[w])
                    this.scan(g, w);
            }
        }
Exemplo n.º 9
0
        public void TestKruskalMST()
        {
            EdgeWeightedGraph g = new EdgeWeightedGraph(8);
            g.AddEdge(new Edge(0, 2, 0.26d));
            g.AddEdge(new Edge(0, 4, 0.38d));
            g.AddEdge(new Edge(0, 7, 0.16d));
            g.AddEdge(new Edge(1, 3, 0.29d));
            g.AddEdge(new Edge(1, 2, 0.36d));
            g.AddEdge(new Edge(1, 5, 0.32d));
            g.AddEdge(new Edge(1, 7, 0.19d));
            g.AddEdge(new Edge(2, 3, 0.17d));
            g.AddEdge(new Edge(2, 7, 0.34d));
            g.AddEdge(new Edge(3, 6, 0.52d));
            g.AddEdge(new Edge(4, 5, 0.35d));
            g.AddEdge(new Edge(4, 7, 0.37d));
            g.AddEdge(new Edge(5, 7, 0.28d));
            g.AddEdge(new Edge(6, 0, 0.58d));
            g.AddEdge(new Edge(6, 2, 0.40d));
            g.AddEdge(new Edge(6, 4, 0.93d));
            Debug.WriteLine(g);

            KruskalMST mst = new KruskalMST(g);
            Debug.WriteLine(mst);
        }