Ejemplo n.º 1
0
        protected override void visit(EdgeWeightedGraph graph, int v)
        {
            marked[v] = true;

            foreach (Edge edge in graph.Adj(v))
            {
                int w = edge.Other(v);

                if (marked[w]) continue;
                if (edge.Weight < diestTo[w])
                {
                    edgeTo[w] = edge;
                    diestTo[w] = edge.Weight;

                    if (pq.Contains(w))
                    {
                        pq.Change(w, diestTo[w]);
                    }
                    else
                    {
                        pq.Insert(w, diestTo[w]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void MinPQTest()
        {
            string[] graphItems = {
                "4 5 0.35",
                "4 7 0.37",
                "5 7 0.28",
                "0 7 0.16",
                "1 5 0.32",
                "0 4 0.38",
                "2 3 0.17",
                "1 7 0.19",
                "0 2 0.26",
                "1 2 0.36",
                "1 3 0.29",
                "2 7 0.34",
                "6 2 0.40",
                "3 6 0.52",
                "6 0 0.58",
                "6 4 0.93"
            };
            var graph = new EdgeWeightedGraph(8, 16, graphItems);

            var pq = new MinPQ<Edge>();

            foreach (Edge edge in graph.Edges())
            {
                pq.insert(edge);
            }

            while (pq.isEmpty() == false)
            {
                pq.delMin();
            }
        }
Ejemplo n.º 3
0
        public void PrimMSTTest()
        {
            string[] graphItems = {
                "4 5 0.35",
                "4 7 0.37",
                "5 7 0.28",
                "0 7 0.16",
                "1 5 0.32",
                "0 4 0.38",
                "2 3 0.17",
                "1 7 0.19",
                "0 2 0.26",
                "1 2 0.36",
                "1 3 0.29",
                "2 7 0.34",
                "6 2 0.40",
                "3 6 0.52",
                "6 0 0.58",
                "6 4 0.93"
            };

            var graph = new EdgeWeightedGraph(8, 16, graphItems);

            MST mst = new PrimMST(graph);

            var builder = new StringBuilder();
            foreach (Edge edge in mst.edges())
            {
                builder.AppendFormat("Edge {0}", edge);
            }

            Assert.AreEqual(1.81, mst.Weight);
        }
Ejemplo n.º 4
0
        protected override void visit(EdgeWeightedGraph graph, int v)
        {
            marked[v] = true;

            foreach (Edge edge in graph.Adj(v))
            {
                if (!marked[edge.Other(v)]) pq.insert(edge);
            }
        }
Ejemplo n.º 5
0
        public LazyPrimMST(EdgeWeightedGraph graph)
        {
            pq = new MinPQ<Edge>();
            marked = new bool[graph.V];
            mst = new Queue<Edge>();

            visit(graph, 0);

            while (!pq.isEmpty())
            {
                Edge e = pq.delMin();

                int v = e.Either, w = e.Other(v);

                if (marked[v] && marked[w]) continue;
                mst.Enqueue(e);
                if (!marked[v]) visit(graph, v);
                if (!marked[w]) visit(graph, w);
            }
        }
Ejemplo n.º 6
0
        public PrimMST(EdgeWeightedGraph graph)
        {
            edgeTo = new Edge[graph.V];
            diestTo = new double[graph.V];
            marked = new bool[graph.V];

            for (int v = 0; v < graph.V; v++)
            {
                diestTo[v] = double.MaxValue;
            }

            pq = new IndexMinPq<double>(graph.V);

            diestTo[0] = 0;
            pq.Insert(0, 0);

            while (!pq.IsEmpty())
            {
                visit(graph, pq.DelMin());
            }
        }
Ejemplo n.º 7
0
 protected abstract void visit(EdgeWeightedGraph graph, int v);