public TopologicalSortShortestPath(WeightedDiGraph G, int s)
        {
            this.s = s;
            int V = G.V();

            marked = new bool[V];
            edgeTo = new Edge[V];
            cost   = new double[V];

            for (var i = 0; i < V; ++i)
            {
                cost[i] = Double.MaxValue;
            }

            cost[s] = 0;

            DepthFirstPostOrder dfo = new DepthFirstPostOrder(G.ToDiGraph());

            foreach (var v in dfo.PostOrder())
            {
                foreach (var e in G.adj(v))
                {
                    Relax(G, e);
                }
            }
        }
Example #2
0
        public Dijkstra(WeightedDiGraph G, int s)
        {
            this.s = s;
            int V = G.V();

            marked = new bool[V];
            edgeTo = new Edge[V];
            cost   = new double[V];

            for (var i = 0; i < V; ++i)
            {
                cost[i] = Double.MaxValue;
            }

            cost[s] = 0;

            pq = new IndexMinPQ <Double>(V);


            pq.Insert(s, 0);

            while (!pq.IsEmpty)
            {
                var v = pq.DelMin();
                marked[v] = true;
                foreach (var e in G.adj(v))
                {
                    Relax(G, e);
                }
            }
        }
Example #3
0
        public BellmanFulkerson(WeightedDiGraph G, int s)
        {
            this.s = s;
            var V = G.V();

            cost = new double[V];
            for (var v = 0; v < V; ++v)
            {
                cost[v] = Double.MaxValue;
            }

            edgeTo  = new Edge[V];
            cost[s] = 0;

            for (var j = 0; j < V; ++j)
            {
                for (var v = 0; v < V; ++v)
                {
                    foreach (var e in G.adj(v))
                    {
                        Relax(G, e);
                    }
                }
            }

            negativeCycles = false;
            for (var v = 0; v < V; ++v)
            {
                foreach (var e in G.adj(v))
                {
                    if (Relax(G, e))
                    {
                        negativeCycles = true;
                    }
                }
            }
        }