示例#1
0
        static void Main(string[] args)
        {
            List <string> scc = new List <string>();
            List <CsNode <string, string> > nodes = new List <CsNode <string, string> >();

            Console.Write("\n  Testing Graph class");
            Console.WriteLine("\n =======================");
            TestGraph t = new TestGraph();

            CsNode <string, string> node1 = new CsNode <string, string>("node1");
            CsNode <string, string> node2 = new CsNode <string, string>("node2");
            CsNode <string, string> node3 = new CsNode <string, string>("node3");
            CsNode <string, string> node4 = new CsNode <string, string>("node4");
            CsNode <string, string> node5 = new CsNode <string, string>("node5");

            node1.addChild(node2, "edge12");
            node2.addChild(node1, "edge21");
            node2.addChild(node3, "edge23");
            node2.addChild(node4, "edge24");
            node3.addChild(node1, "edge31");
            node5.addChild(node1, "edge51");
            node5.addChild(node4, "edge54");

            CsGraph <string, string> graph = new CsGraph <string, string>("Fred");

            graph.addNode(node1);
            graph.addNode(node2);
            graph.addNode(node3);
            graph.addNode(node4);
            graph.addNode(node5);
            nodes.Add(node1);
            nodes.Add(node2);
            nodes.Add(node3);
            nodes.Add(node4);
            nodes.Add(node5);

            scc = t.tarjan(nodes);
            Console.WriteLine("Graph with parent and nodes");
            foreach (var node in nodes)
            {
                Console.Write("\n  {0}", node.name);
                for (int i = 0; i < node.children.Count; ++i)
                {
                    Console.Write("\n    {0}", node.children[i].targetNode.name);
                }
            }
            Console.WriteLine("SCCs:\n");
            foreach (var s in scc)
            {
                Console.Write(s);
            }
            Console.Read();
        }
        // show the result of SCC
        public CsGraph <string, string> show_strong(string[] args)
        {
            DepAnalysis test = new DepAnalysis();

            test.match(args);
            CsGraph <string, string>        dep_graph        = new CsGraph <string, string>("dep_name");
            CsNode <string, string>         graph_start_node = new CsNode <string, string>("start_graph");
            List <CsNode <string, string> > allnode          = new List <CsNode <string, string> >();

            for (int i = 0; i < args.Length; ++i)
            {
                CsNode <string, string> nodes = new CsNode <string, string>(Path.GetFileName(args[i]));
                graph_start_node = nodes;
                allnode.Add(nodes);
            }

            foreach (var ele in test.depentable)
            {
                foreach (var ls in allnode)
                {
                    if (ls.name == ele.Key)
                    {
                        foreach (var item in ele.Value)
                        {
                            foreach (var ls2 in allnode)
                            {
                                if (ls2.name == item)
                                {
                                    ls.addChild(ls2, "XXX");
                                }
                            }
                        }
                    }
                }
            }
            foreach (var ls in allnode)
            {
                dep_graph.addNode(ls);
            }
            //    dep_graph.showDependencies();
            dep_graph.startNode = graph_start_node;

            //   Console.WriteLine("\n\n");
            Console.WriteLine("\n----------------------------show strong component -----------------------");

            dep_graph.tarjan();
            return(dep_graph);
        }