Exemplo n.º 1
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.º 2
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.º 3
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.º 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);
        }