Beispiel #1
0
        public CPM(int N, List <string> edgeText)
        {
            var g = new EdgeWeightedDigraph(2 * N + 2);

            int s = 2 * N, t = 2 * N + 1;

            for (int i = 0; i < N; i++)
            {
                var    e        = edgeText[i].Split(' ');
                double duration = Convert.ToDouble(e[0]);
                g.AddEdge(new DirectedEdge(i, i + N, duration));
                g.AddEdge(new DirectedEdge(s, i, 0.0));
                g.AddEdge(new DirectedEdge(i + N, t, 0.0));
                for (int j = 1; j < e.Length; j++)
                {
                    int successor = Convert.ToInt32(e[j]);
                    g.AddEdge(new DirectedEdge(i + N, successor, 0.0));
                }
            }


            AcylicSP acy = new AcylicSP(g, s, AcylicSP.PathType.Lagrest);

            Console.WriteLine("start\ttimes");
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine($"{i}\t{acy.DistTo[i]}");
            }
            Console.WriteLine($"Total Times:{acy.DistTo[t]}");
        }
Beispiel #2
0
        private void FindNegativeCycle()
        {
            int v = _edgeTo.Length;
            EdgeWeightedDigraph spt = new EdgeWeightedDigraph(v);

            for (int s = 0; s < v; s++)
            {
                if (_edgeTo[s] != null)
                {
                    spt.AddEdge(_edgeTo[s]);
                }
            }
            EdgeWeightedCycleFinder finder = new EdgeWeightedCycleFinder(spt);

            _cycle = finder.Cycle;
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            EdgeWeightedDigraph g = new EdgeWeightedDigraph(8);

            g.AddEdge(new DirectedEdge(4, 5, 0.35));
            g.AddEdge(new DirectedEdge(5, 4, 0.35));
            g.AddEdge(new DirectedEdge(4, 7, 0.37));
            g.AddEdge(new DirectedEdge(5, 7, 0.28));
            g.AddEdge(new DirectedEdge(7, 5, 0.28));
            g.AddEdge(new DirectedEdge(5, 1, 0.32));
            g.AddEdge(new DirectedEdge(0, 4, 0.38));
            g.AddEdge(new DirectedEdge(0, 2, 0.26));
            g.AddEdge(new DirectedEdge(7, 3, 0.39));
            g.AddEdge(new DirectedEdge(1, 3, 0.29));
            g.AddEdge(new DirectedEdge(2, 7, 0.34));
            g.AddEdge(new DirectedEdge(6, 2, 0.40));
            g.AddEdge(new DirectedEdge(3, 6, 0.52));
            g.AddEdge(new DirectedEdge(6, 0, 0.58));
            g.AddEdge(new DirectedEdge(6, 4, 0.93));

            DijkstraSP dsp = new DijkstraSP(g, 0);

            Console.WriteLine("DijkstraSP");
            foreach (var e in dsp.PathTo(6))
            {
                Console.WriteLine($"{e} ");
            }

            var edges = new List <string>()
            {
                "41 1 7 9",
                "51 2",
                "50",
                "36",
                "38",
                "45",
                "21 3 8",
                "32 3 8",
                "32 2",
                "29 4 6"
            };
            var cpm = new CPM(10, edges);

            var dedges = new List <string>()
            {
                "4 5  0.35",
                "5 4 -0.66",
                "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"
            };
            int N = 8;
            EdgeWeightedDigraph spt = new EdgeWeightedDigraph(N);

            for (int i = 0; i < N; i++)
            {
                var    e        = dedges[i].Split(' ');
                double duration = Convert.ToDouble(e[e.Length - 1]);
                int    v        = Convert.ToInt32(e[0]);
                int    w        = Convert.ToInt32(e[1]);
                spt.AddEdge(new DirectedEdge(v, w, duration));
            }

            BellmanFordSP bsp = new BellmanFordSP(spt, 0);

            Console.WriteLine(bsp.HasNegativeCycle());
            Console.WriteLine("Hello World!");
        }