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);
        }
Beispiel #2
0
        // Relax(u, v, w):
        // if(d[v] > d[u] + w(u, v))
        //    d[v] = d[u] + w(u, v)

        private void dijkstra(EdgeWeightedDigraph graph, int vertex)
        {
            pq.Insert(vertex, 0.0);

            while (!pq.IsEmpty())
            {
                int v = pq.DelMin();
                foreach (DirectedWeightedEdge edge in graph.Adj(v))
                {
                    Relax(edge);
                }
            }
        }
Beispiel #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);
             }
         }
     }
 }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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();
            }
        }
Beispiel #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);
        }
Beispiel #8
0
        private void AstarSP(EdgeWeightedDigraph graph, int vertex, int target)
        {
            pq.Insert(vertex, 0.0);

            while (!pq.IsEmpty())
            {
                int v = pq.DelMin();

                if (v == target)
                {
                    break;
                }

                foreach (DirectedWeightedEdge edge in graph.Adj(v))
                {
                    Relax(edge, target);
                }
            }
        }
        // certify that digraph is either acyclic or has a directed cycle
        private Boolean check(EdgeWeightedDigraph G)
        {
            // edge-weighted digraph is cyclic
            if (hasCycle()) {
            // verify cycle
            DirectedEdge first = null, last = null;
            foreach (DirectedEdge e in cycle()) {
                if (first == null) first = e;
                if (last != null) {
                    if (last.to() != e.from()) {
                        return false;
                    }
                }
                last = e;
            }

            if (last.to() != first.from()) {
                return false;
            }
            }

            return true;
        }
        // check that algorithm computes either the topological order or finds a directed cycle
        private void dfs(EdgeWeightedDigraph G, int v)
        {
            // Console.Out.WriteLine(cost++);
            onStack[v] = true;
            marked[v] = true;
            foreach(DirectedEdge e in G.adj(v))
            {
            DirectedEdge resultEdge = e;
            int w = resultEdge.to();

            // short circuit if directed cycle found
            if (_cycle != null) return;

            //found new vertex, so recur
            else if (!marked[w]) {
                edgeTo[w] = resultEdge;
                dfs(G, w);
            }

            // trace back directed cycle
            else if (onStack[w]) {
                _cycle = new Stack<DirectedEdge>();
                while (e.from() != w) {
                    _cycle.Push(resultEdge);
                    resultEdge = edgeTo[e.from()];
                }
                _cycle.Push(resultEdge);
            }
            }

            onStack[v] = false;
        }
Beispiel #11
0
        // by finding a cycle in predecessor graph
        private void findNegativeCycle()
        {
            int V = edgeTo.Length;
            EdgeWeightedDigraph spt = new EdgeWeightedDigraph(V);
            for (int v = 0; v < V; v++)
            if (edgeTo[v] != null)
                spt.addEdge(edgeTo[v]);

            // EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(spt);
            //cycle = finder.cycle();
            if (first)
            {
            cycle = null;
            first = !first;
            }
            else
            cycle = new List<DirectedEdge>();
        }