コード例 #1
0
ファイル: Graph.cs プロジェクト: iamkrish/Learning
        public bool DetectCycle(Graph graph)
        {
            //Detect Cycle
            for (int i = 0; i < graph.GraphEdges; i++)
            {
                int src = graph.Edges[i].Source;
                int des = graph.Edges[i].Destination;

                int xLeader = graph.FindPathByCompression(graph.DisJointSets, src);
                int yleader = graph.FindPathByCompression(graph.DisJointSets, des);

                if (xLeader == yleader)
                {
                    return true;
                }
                graph.UnionByRank(xLeader, yleader);

            }
            return false;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: iamkrish/Learning
        static void Main(string[] args)
        {
            Graph graph = new Graph(3, 3);
            graph.Edges[0].Source = 0;
            graph.Edges[0].Destination = 1;

            graph.Edges[1].Source = 1;
            graph.Edges[1].Destination = 2;

            graph.Edges[2].Source = 0;
            graph.Edges[2].Destination = 2;

            for (int i = 0; i < graph.GraphVertices; i++)
            {
                graph.DisJointSets[i].Parent = i;
                graph.DisJointSets[i].Rank = 0;
            }

            bool cycleExists = graph.DetectCycle(graph);

            Console.WriteLine(cycleExists);
            Console.ReadLine();
        }