示例#1
0
        private void Prim(EdgeWeightedGraph G, int s)
        {
            _distTo[s] = 0.0;

            _pq.Insert(s, _distTo[s]);
            while (!_pq.IsEmpty())
            {

                int v = _pq.DeleteMin();
                Scan(G, v);
            }
        }
示例#2
0
        private IndexMinPriorityQueue<Double> _pq; //the [vertex number]|[weight] key value pairs in our minimum spanning tree

        #endregion Fields

        #region Constructors

        public PrimMST(EdgeWeightedGraph G)
        {
            //initialize the various arrays and the minimum priority queue
            _edgeTo = new Edge[G.V()];
            _distTo = new double[G.V()];
            _marked = new Boolean[G.V()];
            _pq = new IndexMinPriorityQueue<Double>(G.V());

            for (int v = 0; v < G.V(); v++) _distTo[v] = Double.PositiveInfinity;

            for (int v = 0; v < G.V(); v++)
                if (!_marked[v]) Prim(G, v);
        }
示例#3
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]);
                }
            }
        }
示例#4
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWG.txt";
                break;

            case "2":
                fileName = "mediumEWG.txt";
                break;

            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeW>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var weight       = Convert.ToDouble(lineSplitted[2], CultureInfo.InvariantCulture);
                    var edge         = new EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);

            var mst = new LazyPrimMST(edgeWeightedGraph);

            foreach (var edge in mst.Edges())
            {
                Console.WriteLine(edge);
            }
            Console.Write($"{$"{mst.Weight():0.00000}"}{Environment.NewLine}");

            Console.ReadLine();
        }
示例#5
0
    // check optimality conditions (takes time proportional to E V lg* V)
    private bool check(EdgeWeightedGraph G)
    {
        // check total weight
        double total = 0.0;

        foreach (Edge e in edges())
        {
            total += e.Weight();
        }
        if (Mathf.Abs((float)(total - Weight())) > FLOATING_POINT_EPSILON)
        {
            throw new System.Exception("Weight of edges does not equal weight(): " + total + " vs. " + Weight() + "\n");
            return(false);
        }

        // check that it is acyclic
        UF uf = new UF(G.V());

        foreach (Edge e in edges())
        {
            int v = e.either(), w = e.other(v);
            if (uf.connected(v, w))
            {
                throw new System.Exception("Not a forest");
                return(false);
            }
            uf.union(v, w);
        }

        // check that it is a spanning forest
        foreach (Edge e in G.edges())
        {
            int v = e.either(), w = e.other(v);
            if (!uf.connected(v, w))
            {
                throw new System.Exception("Not a spanning forest");
                return(false);
            }
        }

        // check that it is a minimal spanning forest (cut optimality conditions)
        foreach (Edge e in edges())
        {
            // all edges in MST except e
            uf = new UF(G.V());
            foreach (Edge f in mst)
            {
                int x = f.either(), y = f.other(x);
                if (f != e)
                {
                    uf.union(x, y);
                }
            }

            // check that e is min weight edge in crossing cut
            foreach (Edge f in G.edges())
            {
                int x = f.either(), y = f.other(x);
                if (!uf.connected(x, y))
                {
                    if (f.Weight() < e.Weight())
                    {
                        throw new System.Exception("Edge " + f + " violates cut optimality conditions");
                        return(false);
                    }
                }
            }
        }

        return(true);
    }
        public void DrawSkelethMaze()
        {
            EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(25);
              //  int tab[25];
                Random rand;
                    rand = new Random();
                    int xRand = rand.Next();
                for(int i=0;i<5;i++)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        //  for(int x=0;x<4;x++)
                        int dest = ((i + 1) * 5) + j;
                        if (i + 1 < 5)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * 5 + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = ((i - 1) * 5) + j;
                        if (i - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * 5 + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * 5) + (j + 1);
                        if (j + 1 < 5)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * 5 + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * 5) + (j - 1);
                        if (j - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * 5 + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest =( (i + 1) * 5) + (j + 1);
                        if( j + 1 < 5  && i + 1 < 5)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * 5 + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                    }
                }

                    PrimMST MazeBasicTree = new PrimMST(MazeBasic);
                 //   MazeBasicTree.Edges
                        Queue<Edge> MazeDone = new Queue<Edge>();

                    int sorc,desto;
                    String writelienString;
                   foreach (Edge s in MazeBasicTree.Edges())
            {

               // Debug.WriteLine( s.Target.  +"  ");

                       sorc = s.Source();
                       desto = s.Dest();
                       writelienString = sorc.ToString() + "  " + desto.ToString();

                  //     Debug.WriteLine(writelienString);

            }
        }
        public MazeStruct DrawMazeSkelethOfSize(int MazeSize,bool is_fivedirect)
        {
            EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(MazeSize * MazeSize);
                //  int tab[25];
                Random rand;
                rand = new Random();
                int xRand = rand.Next();
                for (int i = 0; i < MazeSize; i++)
                {
                    for (int j = 0; j < MazeSize; j++)
                    {
                        //  for(int x=0;x<4;x++)
                        int dest = ((i + 1) * MazeSize) + j;
                        if (i + 1 < MazeSize)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = ((i - 1) * MazeSize) + j;
                        if (i - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * MazeSize) + (j + 1);
                        if (j + 1 < MazeSize)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * MazeSize) + (j - 1);
                        if (j - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        if (is_fivedirect)
                        {
                            dest = ((i + 1) * MazeSize) + (j + 1);
                            if (j + 1 < MazeSize && i + 1 < MazeSize)
                            {
                                xRand = (rand.Next() % 6) + 1;
                                Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                                MazeBasic.AddEdge(tempEdge);
                            }
                        }

                    }
                }

                PrimMST MazeBasicTree = new PrimMST(MazeBasic);
                //   MazeBasicTree.Edges
                Queue<Edge> MazeDone = new Queue<Edge>();
               // Queue<Edge> MazeDone = new Queue<Edge>();

              //  int sorc, desto;
              //  String writelienString;
                MazeStruct MazeToReturn;
                MazeToReturn = new MazeStruct(MazeSize* MazeSize);
                foreach (Edge s in MazeBasicTree.Edges())
                {

                    // Debug.WriteLine( s.Target.  +"  ");

                    MazeToReturn.setUpByEdge(s,true);

                }
                return MazeToReturn;
        }
        public void DrawMazeOfSize(int MazeSize)
        {
            EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(MazeSize*MazeSize);
                //  int tab[25];
                Random rand;
                rand = new Random();
                int xRand = rand.Next();
                for (int i = 0; i < MazeSize; i++)
                {
                    for (int j = 0; j < MazeSize; j++)
                    {
                        //  for(int x=0;x<4;x++)
                        int dest = ((i + 1) * MazeSize) + j;
                        if (i + 1 < MazeSize)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = ((i - 1) * MazeSize) + j;
                        if (i - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * MazeSize) + (j + 1);
                        if (j + 1 < MazeSize)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = (i * MazeSize) + (j - 1);
                        if (j - 1 >= 0)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                        dest = ((i + 1) * MazeSize) + (j + 1);
                        if (j + 1 < MazeSize && i + 1 < MazeSize)
                        {
                            xRand = (rand.Next() % 6) + 1;
                            Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand);
                            MazeBasic.AddEdge(tempEdge);
                        }

                    }
                }

                PrimMST MazeBasicTree = new PrimMST(MazeBasic);
                //   MazeBasicTree.Edges
                Queue<Edge> MazeDone = new Queue<Edge>();
                //  MazeDone = MazeBasicTree.Edges();

                //Edge TempEdge = new Edge(Center.Y,)
             //   int sorc, desto;
            //    String writelienString;
              //  foreach (Edge s in MazeBasicTree.Edges())
               //     {
                    // Debug.WriteLine( s.Target.  +"  ");
                //    sorc = s.Source();
             //       desto = s.Dest();
               //     writelienString = sorc.ToString() + "  " + desto.ToString();

               //     Debug.WriteLine(writelienString);

             //       }
        }
示例#9
0
        public void Run()
        {
            Console.WriteLine("Choose file:");      // Prompt
            Console.WriteLine("1 - tinyEWG.txt");   // Prompt
            Console.WriteLine("2 - mediumEWG.txt"); // Prompt
            //Console.WriteLine("3 - mediumEWG.txt"); // Prompt

            Console.WriteLine("or quit"); // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;
            int    source;

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyEWG.txt";
                source   = 6;
                break;

            case "2":
                fileName = "mediumEWG.txt";
                source   = 6;
                break;

            //case "3":
            //    fileName = "largeEWG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeW>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var weight       = Convert.ToDouble(lineSplitted[2], CultureInfo.InvariantCulture);
                    var edge         = new EdgeW(ve, we, weight);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var edgeWeightedGraph = new EdgeWeightedGraph(v, e, edges);

            Console.WriteLine(edgeWeightedGraph);

            // compute shortest paths
            var sp = new DijkstraUndirectedSP(edgeWeightedGraph, source);


            // print shortest path
            for (var t = 0; t < edgeWeightedGraph.V; t++)
            {
                if (sp.HasPathTo(t))
                {
                    Console.Write($"{source} to {t} {$"{sp.DistTo(t):0.00000}"}  ");
                    foreach (var edge in sp.PathTo(t))
                    {
                        Console.Write($"{edge}   ");
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine($"{source} to {t}         no path{Environment.NewLine}");
                }
            }

            Console.ReadLine();
        }