Пример #1
0
        string Ops;                                 //目标顶点标识

        //Findpath方法 查找两顶点间定常路径(递归体)
        private void Findpath(AdjNode n, int level)
        {
            if (level == Len)         //搜索层级等于路径长度
            {
                if (n.Content == Ops) //当前顶点为目标顶点
                {
                    //将路径堆栈元素转换为一条路径输出
                    string[] output = path.ToArray();
                    for (int i = output.Length - 1; i >= 0; i--)
                    {
                        Console.Write(output[i]);
                        if (i != 0)
                        {
                            Console.Write("->");
                        }
                    }
                    Console.WriteLine();
                }
                path.Pop(); //弹出栈顶顶点
                return;
            }
            AdjNode temp = n.Next;

            while (temp != null)
            {
                if (path.Contains(temp.Content)) //若路径中已含有此顶点(成环)
                {
                    temp = temp.Next;            //跳过此顶点
                    continue;
                }
                path.Push(temp.Content);                                            //压入此顶点
                Findpath(Vertices.Find(v => v.Content == temp.Content), level + 1); //递归搜索此顶点的邻接顶点
                temp = temp.Next;
            }
        }
Пример #2
0
        //Bfs方法 广度优先搜索递归体
        private void Bfs(AdjNode n)
        {
            if (n.Weight == (int)Status.NotDetected) //若顶点未被探测
            {
                Console.Write(n.Content + " ");      //输出当前顶点标识
            }
            n.Weight = (int)Status.Visited;          //标记顶点为访问结束
            AdjNode temp = n.Next;

            while (temp != null)
            {
                if (Vertices.Find(v => v.Content == temp.Content).Weight != (int)Status.Visited)      //若顶点未完成访问
                {
                    if (Vertices.Find(v => v.Content == temp.Content).Weight != (int)Status.Detected) //若顶点未被探测
                    {
                        Vertices.Find(v => v.Content == temp.Content).Weight = (int)Status.Detected;  //标记顶点为已探测
                        Console.Write(temp.Content + " ");                                            //输出当前顶点标识
                    }
                }
                temp = temp.Next;
            }
            temp = n.Next;
            while (temp != null)
            {
                if (Vertices.Find(v => v.Content == temp.Content).Weight == (int)Status.Detected) //若顶点已被探测
                {
                    Bfs(Vertices.Find(v => v.Content == temp.Content));                           //递归搜索顶点
                }
                temp = temp.Next;
            }
        }
Пример #3
0
        //Dfs方法 深度优先搜索递归体
        private void Dfs(AdjNode n)
        {
            n.Weight = (int)Status.Visited; //标记顶点为访问结束 访问状态存储于Vertices中顶点元素的Weight变量
            Console.Write(n.Content + " "); //输出当前顶点标识
            AdjNode temp = n.Next;

            while (temp != null)
            {
                if (Vertices.Find(v => v.Content == temp.Content).Weight == (int)Status.NotDetected) //若顶点未被探测
                {
                    Dfs(Vertices.Find(v => v.Content == temp.Content));                              //递归搜索顶点
                }
                temp = temp.Next;
            }
        }
Пример #4
0
        //Add方法 新增一条边
        public void Add(string input)
        {
            string x      = input.Split(' ')[0];                        //始顶点
            string y      = input.Split(' ')[1];                        //终顶点
            int    weight = int.Parse(input.Split(' ')[2]);             //边权重

            if (Vertices.Find(v => v.Content == x) != default(AdjNode)) //此处使用Lambda表达式从Vertices中获得始顶点x的位置
            //若Vertices中已经存在顶点x则直接向其所指向的邻接链表的末端增加一个节点
            {
                AdjNode temp = Vertices.Find(v => v.Content == x);
                while (temp.Next != null)
                {
                    temp = temp.Next;
                }
                temp.Next = new AdjNode(y, weight);
            }
            else
            //若Vertices中不存在顶点x则新增一个顶点并在其所指向的邻接链表末端增加一个节点
            {
                AdjNode temp = new AdjNode(x);
                Vertices.Add(temp);
                temp.Next = new AdjNode(y, weight);
            }
        }