コード例 #1
0
        private void FindNegtiveCycle()
        {
            int V = edgeTo.Length;//图的节点数量
            DirectedWeightedGraph spt = new DirectedWeightedGraph(V);

            for (int v = 0; v < V; ++v)
            {
                if (edgeTo[v] != null)
                {
                    spt.AddEdge(edgeTo[v]);
                }
            }
            DirectedCycle cf = new DirectedCycle(spt);
            //需要转换形式
            Queue <DirectedEdge> edges = new Queue <DirectedEdge>();//没有还
            var list = cf.Cycle().ToList();

            if (list.Count == 0)
            {
                return;
            }
            for (int i = 0; i < list.Count - 1; ++i)
            {
                edges.Enqueue(new DirectedEdge(list[i], list[i + 1], spt.GetWeight(list[i], list[i + 1])));
            }
            cycle = edges;
        }
コード例 #2
0
        public void CycleTest()
        {
            using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyDG.txt"))
            {
                Digraph dg = new Digraph(sr);

                DirectedCycle dcycle = new DirectedCycle(dg);

                Assert.AreEqual(dcycle.HasCycle(), true);

                IEnumerable <int> cycle = dcycle.Cycle();
                foreach (int i in cycle)
                {
                    Console.Write(i + " -> ");
                }
                Console.WriteLine();
            }
        }
コード例 #3
0
        void DigraphTest()
        {
            //var strArr = FileHandler.ReadFileAsStrArr("tinyG.txt");
            //var strArr = FileHandler.ReadFileAsStrArr("tinyDG.txt");
            var strArr = FileHandler.ReadFileAsStrArr("tinyDAG.txt");
            var intArr = Array.ConvertAll(strArr, s => int.Parse(s));
            var dg     = new Digraph(intArr);
            //var dgr = dg.Reverse();
            var dc = new DirectedCycle(dg);

            if (dc.HasCycle())
            {
                foreach (var v in dc.Cycle())
                {
                    Console.WriteLine(v);
                }
            }
            Console.ReadKey();
        }
コード例 #4
0
    void Start()
    {
        Digraph G = new Digraph(txt);

        DirectedCycle finder = new DirectedCycle(G);

        if (finder.hasCycle())
        {
            string str = "Directed cycle: ";

            foreach (int v in finder.Cycle())
            {
                str += (v + " ");
            }
            print(str);
        }

        else
        {
            print("No directed cycle");
        }
    }
コード例 #5
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyDG.txt");  // Prompt
            Console.WriteLine("2 - tinyDAG.txt"); // Prompt
            //Console.WriteLine("3 - largeG.zip"); // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyDG.txt";
                break;

            case "2":
                fieName = "tinyDAG.txt";
                break;

            //case "3":
            //    fieName = "largeG.zip";
            //    break;
            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fieName}");
            var lines = !fieName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeD>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Digraph(v, e, edges);

            if (fileNumber != "3")
            {
                Console.WriteLine(graph);
            }


            var finder = new DirectedCycle(graph);

            if (finder.HasCycle())
            {
                Console.Write("Directed cycle: ");
                foreach (int vi in finder.Cycle())
                {
                    Console.Write($"{vi} ");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No directed cycle");
            }


            Console.ReadLine();
        }