Exemple #1
0
        public static void Main()
        {
            // tests simple model presented on https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
            var graph_nodes = new List <Vertex>();

            var v1 = new Vertex()
            {
                Id = 1, Name = "One"
            };
            var v2 = new Vertex()
            {
                Id = 2, Name = "Two"
            };
            var v3 = new Vertex()
            {
                Id = 3, Name = "Three"
            };

            /*
             * var v4 = new Vertex() { Id = 4, Name = "Four" };
             * var v5 = new Vertex() { Id = 5, Name = "Five" };
             * var v6 = new Vertex() { Id = 6, Name = "Six" };
             * var v7 = new Vertex() { Id = 7, Name = "Seven" };
             * var v8 = new Vertex() { Id = 8, Name = "Eight" };
             */
            v1.Dependencies.Add(v2);
            v2.Dependencies.Add(v1);

            /*   v3.Dependencies.Add(v1);
             * v4.Dependencies.Add(v3);
             * v4.Dependencies.Add(v5);
             * v5.Dependencies.Add(v4);
             * v5.Dependencies.Add(v6);
             * v6.Dependencies.Add(v3);
             * v6.Dependencies.Add(v7);
             * v7.Dependencies.Add(v6);
             * v8.Dependencies.Add(v7);
             * v8.Dependencies.Add(v5);
             * v8.Dependencies.Add(v8);
             */
            graph_nodes.Add(v1);
            graph_nodes.Add(v2);
            graph_nodes.Add(v3);

            /*  graph_nodes.Add(v4);
             * graph_nodes.Add(v5);
             * graph_nodes.Add(v6);
             * graph_nodes.Add(v7);
             * graph_nodes.Add(v8);
             */
            var tcd        = new TarjanCycleDetectStack();
            var cycle_list = tcd.DetectCycle(graph_nodes);

            Console.WriteLine("Total number of cycles: {0}", cycle_list.Count);


            foreach (var vertices in cycle_list)
            {
                //Console.WriteLine(v);

                foreach (var v in vertices)
                {
                    Console.Write(v.ToString());
                    // Console.WriteLine(string.Join(" ", v.Dependencies));
                }
                Console.Write("\n");
            }
            Console.ReadKey();
            //Assert.IsTrue(cycle_list.Count == 4);
        }
        static StringBuilder genStrongCompF(List <string> filepaths, AdjL e)
        {
            StringBuilder msg = new StringBuilder();

            msg.Append(Environment.NewLine + "     Generating all the Components");
            msg.Append(Environment.NewLine + "  =======================");
            var           graph_nodes = new List <Vertex>();
            var           v           = new List <Vertex>();;
            List <string> files       = new List <string>();

            //List<string> filepaths = ProcessCommandline(args);
            foreach (string file in filepaths)
            {
                string[] fullname = file.Split('\\');
                files.Add(fullname[fullname.Length - 1]);
            }
            for (int i = 0; i < files.Count; i++)
            {
                Vertex ver = new Vertex()
                {
                    Id = i, Name = files[i]
                };
                v.Add(ver);
            }

            foreach (var node in v)                  // my nodes
            {
                foreach (var n in e)                 // the file name in tuple list
                {
                    if (node.Name == n.Item1)        //if node name == name in tuple
                    {
                        foreach (var targenode in v) // found the target node
                        {
                            if (targenode.Name == n.Item2)
                            {
                                if (!node.Dependencies.Any(child => child.Name == targenode.Name))
                                {
                                    node.Dependencies.Add(targenode);
                                }
                            }
                        }
                    }
                }
            }

            foreach (var node in v) // my nodes
            {
                graph_nodes.Add(node);
            }

            var tcd        = new TarjanCycleDetectStack();
            var cycle_list = tcd.DetectCycle(graph_nodes);

            msg.Append(Environment.NewLine + "Total number of components: " + cycle_list.Count + Environment.NewLine);


            foreach (var vertices in cycle_list)
            {
                //Console.WriteLine(v);

                foreach (var ver in vertices)
                {
                    msg.Append(ver.ToString() + " ");
                    // Console.WriteLine(string.Join(" ", v.Dependencies));
                }
                msg.Append(Environment.NewLine);
            }
            //Console.ReadKey();
            return(msg);
        }