Esempio n. 1
0
        //Input File:
        // 8
        // 15
        // 4 5 0.35
        // 5 4 0.35
        // 4 7 0.37
        // 5 7 0.28
        // 7 5 0.28
        // 5 1 0.32
        // 0 4 0.38
        // 0 2 0.26
        // 7 3 0.39
        // 1 3 0.29
        // 2 7 0.34
        // 6 2 0.40
        // 3 6 0.52
        // 6 0 0.58
        // 6 4 0.93

        static void Main(string[] args)
        {
            const string filePath = @"C:\Users\1\Desktop\tinyEWD.txt";

            var graph = new EdgeWeightedDigraph(filePath);

            DisplayEWDGraph(graph);

            Console.WriteLine();

            var dj = new Dijkstra(graph, 0);

            PrintShortestPath(dj, graph, 0, graph.V());

            Console.WriteLine();

            var bf = new NaiveBellmanFord(graph, 0);

            PrintShortestPath(bf, graph, 0, graph.V());

            Console.WriteLine();

            var a = new Astar(graph, 0, 3);

            PrintShortestPath(a, graph, 0, 3);
        }
        private Stack<DirectedEdge> _cycle; // directed cycle (or null if no such cycle)

        #endregion Fields

        #region Constructors

        /**
         * Determines whether the edge-weighted digraph <tt>G</tt> has a directed cycle and,
         * if so, finds such a cycle.
         * @param G the edge-weighted digraph
         */
        public EdgeWeightedDirectedCycle(EdgeWeightedDigraph G)
        {
            Console.Write("Graph size cycle finder {0}", G.V());
            marked  = new Boolean[G.V()];
            onStack = new Boolean[G.V()];
            edgeTo  = new DirectedEdge[G.V()];
            for (int v = 0; v < G.V(); v++)
            if (!marked[v]) dfs(G, v);
        }
Esempio n. 3
0
 private void SPBellmanFord(EdgeWeightedDigraph graph)
 {
     for (int pass = 0; pass < graph.V(); pass++)
     {
         for (int v = 0; v < graph.V(); v++)
         {
             foreach (var edge in graph.Adj(v))
             {
                 Relax(edge);
             }
         }
     }
 }
Esempio n. 4
0
        public Astar(EdgeWeightedDigraph graph, int source, int target)
        {
            pq     = new MinPQ <double>(graph.V());
            edgeTo = new DirectedWeightedEdge[graph.V()];
            distTo = new double[graph.V()];

            for (int i = 0; i < graph.V(); ++i)
            {
                distTo[i] = double.MaxValue;
            }
            distTo[source] = 0.0;

            AstarSP(graph, 0, target);
        }
Esempio n. 5
0
        public Dijkstra(EdgeWeightedDigraph graph, int source)
        {
            pq     = new MinPQ <double>(graph.V());
            edgeTo = new DirectedWeightedEdge[graph.V()];
            distTo = new double[graph.V()];

            for (int i = 0; i < graph.V(); ++i)
            {
                distTo[i] = double.MaxValue;
            }
            distTo[source] = 0.0;

            dijkstra(graph, 0);
        }
Esempio n. 6
0
        public static void DisplayEWDGraph(EdgeWeightedDigraph g)
        {
            Console.WriteLine("{0} verticies, {1} edges", g.V(), g.E());

            for (int i = 0; i < g.V(); ++i)
            {
                Console.Write("{0}: ", i);
                foreach (var u in g.Adj(i))
                {
                    Console.Write("[{0}-{1},{2}] ", u.From(), u.To(), u.Weight());
                }
                Console.WriteLine();
            }
        }
Esempio n. 7
0
        public NaiveBellmanFord(EdgeWeightedDigraph graph, int source)
        {
            _edgeTo = new DirectedWeightedEdge[graph.V()];
            _distTo = new double[graph.V()];

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

            _distTo[source] = 0.0;

            SPBellmanFord(graph);
        }
Esempio n. 8
0
        public static void PrintShortestPath(IShortestPath sp, EdgeWeightedDigraph g, int source, int destination)
        {
            // print shortest path
            for (int t = 0; t < g.V(); t++)
            {
                if (t == destination)
                {
                    break;
                }

                if (sp.HasPathTo(t))
                {
                    Console.Write("{0} to {1} is {2}  ", source, t, sp.DistTo(t));
                    foreach (var e in sp.PathTo(t))
                    {
                        Console.Write("[{0}-{1}, {2}] ", e.From(), e.To(), e.Weight());
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.Write("{0} to {1} no path\n", source, t);
                }
            }
        }