public void Run()
        {
            // create random DAG with V vertices and E edges; then add F random edges
            const int vv = 50;
            const int e = 100;
            const int f = 20;
            var digraph = new EdgeWeightedDigraph(vv);
            var vertices = new int[vv];
            for (var i = 0; i < vv; i++)
                vertices[i] = i;
            StdRandom.Shuffle(vertices);
            for (var i = 0; i < e; i++)
            {
                int v, w;
                do
                {
                    v = StdRandom.Uniform(vv);
                    w = StdRandom.Uniform(vv);
                } while (v >= w);
                var weight = StdRandom.Uniform();
                digraph.AddEdge(new DirectedEdge(v, w, weight));
            }

            // add F extra edges
            for (var i = 0; i < f; i++)
            {
                var v = StdRandom.Uniform(vv);
                var w = StdRandom.Uniform(vv);
                var weight = StdRandom.Uniform(0.0, 1.0);
                digraph.AddEdge(new DirectedEdge(v, w, weight));
            }

            Console.WriteLine(digraph);

            // find a directed cycle
            var finder = new EdgeWeightedDirectedCycle(digraph);
            if (finder.HasCycle())
            {
                Console.Write("Cycle: ");
                foreach (var edge in finder.Cycle())
                {
                    Console.Write(edge + " ");
                }
                Console.WriteLine();
            }

            // or give topologial sort
            else
            {
                Console.WriteLine("No directed cycle");
            }

            Console.ReadLine();
        }
Exemple #2
0
        // by finding a cycle in predecessor graph
        private void FindNegativeCycle()
        {
            var vv  = _edgeTo.Length;
            var spt = new EdgeWeightedDigraph(vv);

            for (var v = 0; v < vv; v++)
            {
                if (_edgeTo[v] != null)
                {
                    spt.AddEdge(_edgeTo[v]);
                }
            }

            var finder = new EdgeWeightedDirectedCycle(spt);

            _cycle = finder.Cycle();
        }
Exemple #3
0
 /// <summary>
 /// Returns a negative cycle, or <tt>null</tt> if there is no such cycle.
 /// </summary>
 /// <returns>a negative cycle as an iterable of edges, or <tt>null</tt> if there is no such cycle</returns>
 public IEnumerable <DirectedEdge> NegativeCycle()
 {
     for (var v = 0; v < _distTo.Length; v++)
     {
         // negative cycle in v's predecessor graph
         if (_distTo[v][v] < 0.0)
         {
             var spt = new EdgeWeightedDigraph(_edgeTo.Length);
             for (var w = 0; w < _edgeTo.Length; w++)
             {
                 if (_edgeTo[v][w] != null)
                 {
                     spt.AddEdge(_edgeTo[v][w]);
                 }
             }
             var finder = new EdgeWeightedDirectedCycle(spt);
             return(finder.Cycle());
         }
     }
     return(null);
 }
 /// <summary>
 /// Returns a negative cycle, or <tt>null</tt> if there is no such cycle.
 /// </summary>
 /// <returns>a negative cycle as an iterable of edges, or <tt>null</tt> if there is no such cycle</returns>
 public IEnumerable<DirectedEdge> NegativeCycle()
 {
     for (var v = 0; v < _distTo.Length; v++)
     {
         // negative cycle in v's predecessor graph
         if (_distTo[v][v] < 0.0)
         {
             var spt = new EdgeWeightedDigraph(_edgeTo.Length);
             for (var w = 0; w < _edgeTo.Length; w++)
                 if (_edgeTo[v][w] != null)
                     spt.AddEdge(_edgeTo[v][w]);
             var finder = new EdgeWeightedDirectedCycle(spt);
             return finder.Cycle();
         }
     }
     return null;
 }
        // by finding a cycle in predecessor graph
        private void FindNegativeCycle()
        {
            var vv = _edgeTo.Length;
            var spt = new EdgeWeightedDigraph(vv);
            for (var v = 0; v < vv; v++)
                if (_edgeTo[v] != null)
                    spt.AddEdge(_edgeTo[v]);

            var finder = new EdgeWeightedDirectedCycle(spt);
            _cycle = finder.Cycle();
        }