コード例 #1
0
ファイル: Graph.cs プロジェクト: FuGangqiang/algorithms
        public void DirectedCycleTest()
        {
            var g   = DigraphSample();
            var dfs = new DirectedCycle(g);

            Assert.True(dfs.HasCycle());
        }
コード例 #2
0
    public Topological(DirectedGraph g)
    {
        DirectedCycle cycleFinder = new DirectedCycle(g);

        if (!cycleFinder.HasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(g);
            m_order = dfs.ReversePost();
        }
    }
コード例 #3
0
        public Topological_Queue(IDirectedGraph g)
        {
            DirectedCycle directedCycle = new DirectedCycle(g);

            if (!directedCycle.HasCycle())
            {
                //DAG
                solve(g);
            }
        }
コード例 #4
0
            public Topological(DirectedGraph graph)
            {
                var cycleFinder = new DirectedCycle(graph);

                if (!cycleFinder.HasCycle())
                {
                    var depthFirstOrder = new DepthFirstOrder(graph);
                    order = depthFirstOrder.ReversePost();
                }
            }
コード例 #5
0
    public Topological(Digraph g)
    {
        DirectedCycle cycle = new DirectedCycle(g);

        if (!cycle.HasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(g);

            order = dfs.Pre();
        }
    }
コード例 #6
0
        private IEnumerable <int> order;//顶点的拓扑排序
        public Topological(IDirectedGraph g)
        {
            DirectedCycle directedCycle = new DirectedCycle(g);

            if (!directedCycle.HasCycle())
            {
                //DAG
                DepthFirstOrder dfs = new DepthFirstOrder(g);
                order = dfs.ReversePost();
            }
        }
コード例 #7
0
        public TopologicalAOE(IDirectedGraph g)
        {
            DirectedCycle directedCycle = new DirectedCycle(g);

            if (!directedCycle.HasCycle())
            {
                m_ve = new int[g.V];
                //DAG
                solve(g);
            }
        }
コード例 #8
0
        public void HasCycleTest()
        {
            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);
            }
        }
コード例 #9
0
        private IEnumerable <int> order;//顶点的拓扑排序
        public Topological(IDirectedGraph g)
        {
            DirectedCycle directedCycle = new DirectedCycle(g);

            if (!directedCycle.HasCycle())
            {
                //DAG
                DepthFirstOrder dfs = new DepthFirstOrder(g);
                order = dfs.ReversePost();
            }
            else
            {
                //
                throw new ArgumentException("不是 DAG!");
            }
        }
コード例 #10
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();
            }
        }
コード例 #11
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();
        }
コード例 #12
0
        //public bool CreateGrapByBlkObjects(out string error)
        //{
        //	error = null;
        //	try
        //	{
        //		_graph = new Digraph(_blockingObjects.Length + _threads.Length);

        //		for (int i = 0, icnt = _blockingObjects.Length; i < icnt; ++i)
        //		{
        //			ClrtBlkObject blk = _blockingObjects[i];
        //			if (blk.Owner != Constants.InvalidIndex) _graph.AddDistinctEdge(i,blk.Owner+_threadNdxStart);
        //			for (int j = 0, jcnt = blk.Owners.Length; j < jcnt; ++j)
        //			{
        //				_graph.AddDistinctEdge(i, blk.Owners[j] + _threadNdxStart);
        //			}
        //			for (int j = 0, jcnt = blk.Waiters.Length; j < jcnt; ++j)
        //			{
        //				_graph.AddDistinctEdge(blk.Waiters[j] + _threadNdxStart,i);
        //			}


        //		}

        //		List<KeyValuePair<int,int[]>> waitList = new List<KeyValuePair<int, int[]>>(_threads.Length/2);
        //		List<int> blkList = new List<int>();
        //		for (int i = 0, icnt = _threads.Length; i < icnt; ++i)
        //		{
        //			ClrtThread thrd = _threads[i];
        //			if (thrd.BlockingObjects.Length < 1) continue;
        //			blkList.Clear();
        //			for (int j = 0, jcnt = thrd.BlockingObjects.Length; j < jcnt; ++j)
        //			{
        //				blkList.Add(thrd.BlockingObjects[j]);
        //			}
        //			waitList.Add(new KeyValuePair<int, int[]>(i,blkList.ToArray()));
        //		}

        //		_threadWaitingLists = waitList.Count > 0 ? waitList.ToArray() : Utils.EmptyArray<KeyValuePair<int, int[]>>.Value;

        //		return true;
        //	}
        //	catch (Exception ex)
        //	{
        //		error = Utils.GetExceptionErrorString(ex);
        //		return false;
        //	}
        //}


        public bool HasCycle(out string error)
        {
            error = null;
            try
            {
                DirectedCycle dcycle = new DirectedCycle(_graph);
                if (dcycle.HasCycle())
                {
                    var threadNdxStart = _blockingObjects.Length;
                    _cycle = dcycle.GetCycle();
                    return(true);
                }
                _cycle = Utils.EmptyArray <int> .Value;
                return(false);
            }
            catch (Exception ex)
            {
                error = Utils.GetExceptionErrorString(ex);
                return(false);
            }
        }
コード例 #13
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();
        }