static void Main(string[] args)
        {
            //string[] dataFiles = new string[]
            //{@"tinyEWG.txt",
            //    @"mediumEWG.txt"
            //};
            //foreach (var file in dataFiles)
            //{
            //    var input = File.ReadAllLines(file);
            //    EdgeWeightedGraph ewg = new EdgeWeightedGraph(input);
            //    LazyPrimMST mst = new LazyPrimMST(ewg);
            //    foreach (var e in mst.Edges())
            //    {
            //        Console.WriteLine(e);
            //    }
            //    Console.WriteLine(mst.Weight());
            //}

            var file = @"tinyEWD.txt";
            var data = File.ReadAllLines(file);
            var ewd  = new EdgeWeightedDiGraph(data);
            DijkstrasShortestPath dsp = new DijkstrasShortestPath(ewd, 0);
            var sp = dsp.PathTo(5);

            foreach (var p in sp)
            {
                Console.WriteLine(p);
            }
        }
Example #2
0
 //ctor inits DSs, builds shortest path tree and computes distances
 //takes a built DiGraph and a source vertex
 public DijkstrasShortestPath(EdgeWeightedDiGraph g, int s)
 {
     _pq     = new IndexMinPQ <double>(g.V);
     _edgeTo = new DirectedEdge[g.V];
     _distTo = new double[g.V];
     //load initial values
     for (int i = 1; i < g.V; i++)
     {
         _distTo[i] = Double.PositiveInfinity;
     }
     _distTo[s] = 0.0;
     _edgeTo[s] = null;
     //loads a starting value onto PQ to start process
     _pq.Insert(s, _distTo[s]);
     //start business logic
     //start cycle of deleting min edges and checking adj edges for 'eligability'
     while (!_pq.IsEmpty())
     {
         var minV = _pq.DelMin();
         Relax(g, minV);
     }
 }
Example #3
0
        private void Relax(EdgeWeightedDiGraph g, int v)
        {
            foreach (var edge in g.Adj(v))
            {
                //test if current weight of path to W is more than going through v; ie v 'relaxes' the path

                int    w        = edge.To();
                double distViaV = _distTo[v] + edge.Weight();
                //if current distTo w is greater than (distTo v + this.edge weight), then add/or replace on arrays and PQ
                if (distViaV < _distTo[w])
                {
                    _distTo[w] = distViaV;
                    _edgeTo[w] = edge;
                    if (_pq.Contains(w))
                    {
                        _pq.Change(w, distViaV);
                    }
                    else
                    {
                        _pq.Insert(w, distViaV);
                    }
                }
            }
        }
        static int minimumMovesDij(string[] grid, int startX, int startY, int goalX, int goalY)
        {
            int from = -1;
            int to   = -1;
            int n    = grid.Length;

            int[][] matrix  = new int[n][];
            int     tracker = 0;

            for (int i = 0; i < n; i++)
            {
                matrix[i] = new int[n];
                for (int j = 0; j < n; j++)
                {
                    if (grid[i][j] != 'X')
                    {
                        matrix[i][j] = tracker;
                    }
                    else
                    {
                        matrix[i][j] = -1;
                    }

                    tracker++;
                    if (startX == i && startY == j)
                    {
                        from = matrix[i][j];
                    }

                    if (goalX == i && goalY == j)
                    {
                        to = matrix[i][j];
                    }
                }
            }

            EdgeWeightedDiGraph ewdg = new EdgeWeightedDiGraph(tracker);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i < n - 1 && matrix[i][j] != -1 && matrix[i + 1][j] != -1)
                    {
                        ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j], matrix[i + 1][j], 1), "V");
                        ewdg.AddEdge(new DirectedEdgeAPI(matrix[i + 1][j], matrix[i][j], 1), "V");
                    }

                    if (j < n - 1 && matrix[i][j] != -1 && matrix[i][j + 1] != -1)
                    {
                        ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j], matrix[i][j + 1], 1), "H");
                        ewdg.AddEdge(new DirectedEdgeAPI(matrix[i][j + 1], matrix[i][j], 1), "H");
                    }
                }
            }

            BellmanFordShortestPath djkForward = new BellmanFordShortestPath(ewdg, from);

            //DijkstraShortestPath djkBackward = new DijkstraShortestPath(ewdg, from);

            //var count = djkForward.DistTo[to] > djkBackward.DistTo[from] ? djkBackward.DistTo[from] : djkForward.DistTo[to];
            //List<int> list = djk.PathTo(from, to).ToList();
            //string previousAllign = string.Empty;
            //int count = 0;
            //for (int i = 0; i < list.Count() - 1; i++)
            //{
            //    string key = list[i] < list[i + 1] ? list[i] + "|" + list[i + 1] : list[i + 1] + "|" + list[i];
            //    var item = ewdg.dict[key];

            //    if (previousAllign == string.Empty || previousAllign != item)
            //    {
            //        count++;
            //        previousAllign = item;
            //    }
            //}

            return((int)0);
        }