コード例 #1
0
ファイル: Chap42.cs プロジェクト: jerry82/JData
        public bool RouteDFS(string v1, string v2, Graph graph)
        {
            bool canRoute = false;

            GNode root = graph.GetNode(v1);
            Stack<GNode> stack = new Stack<GNode>();
            List<string> vList = new List<string>();

            stack.Push(root);

            while (stack.Count > 0) {
                GNode cur = stack.Peek();

                if (!vList.Contains(cur.Name)) {
                    vList.Add(cur.Name);
                    cur.Visited = true;
                }

                Dictionary<string, int> neighbors = cur.Neighbors;
                if (neighbors == null) {
                    GNode tmp = stack.Pop();
                    Console.WriteLine("pop: {0}", tmp.Name);

                }
                else {
                    bool noNeighbor = true;

                    foreach (string n in neighbors.Keys) {
                        if (!graph.Nodes[n].Visited) {
                            stack.Push(graph.Nodes[n]);

                            noNeighbor = false;
                            break;
                        }
                    }

                    if (noNeighbor) {
                        stack.Pop();
                    }
                }

                Console.ReadLine();

            }

            foreach (string name in vList) {
                Console.Write(" {0} ", name);
            }
            Console.WriteLine();

            return canRoute;
        }
コード例 #2
0
ファイル: DataFeeder.cs プロジェクト: jerry82/JData
        //data: A>B>C>E>F; E>D>B
        public Graph GetSampleGraph1()
        {
            Graph graph = new Graph();

            List<string> nodes = new List<string>() { "A", "B", "C", "D", "E", "F" };

            graph.BuildNodes(nodes);
            graph.AddDirectedEdge("A", "B", 1);
            graph.AddDirectedEdge("B", "C", 1);
            graph.AddDirectedEdge("C", "E", 1);
            graph.AddDirectedEdge("E", "F", 1);

            graph.AddDirectedEdge("E", "D", 1);
            graph.AddDirectedEdge("D", "B", 1);

            return graph;
        }
コード例 #3
0
ファイル: Chap42.cs プロジェクト: jerry82/JData
        public bool RouteBFS(string v1, string v2, Graph graph)
        {
            bool canRoute = false;

            Queue<GNode> queue = new Queue<GNode>();

            GNode root = graph.GetNode(v1);

            if (root == null)
                throw new Exception("cannot find 1st node");

            queue.Enqueue(root);

            while (queue.Count > 0) {
                GNode cur = queue.Dequeue();
                cur.Visited = false;

                Dictionary<string, int> neighbors = cur.Neighbors;
                if (neighbors != null) {
                    foreach (string name in neighbors.Keys) {
                        GNode tmpNode = graph.Nodes[name];

                        if (name.Equals(v2)) {
                            canRoute = true;
                            Console.WriteLine("found:{0}", name);
                            break;
                        }
                        else {

                            if (!tmpNode.Visited) {
                                queue.Enqueue(tmpNode);
                                tmpNode.Visited = true;
                                Console.WriteLine("enqueue:{0}", tmpNode.Name);
                            }
                        }
                    }
                }
            }

            return canRoute;
        }