Example #1
0
        public BFS(string xmlPath, int start)
        {
            //检查start起点
            if (start < 0)
            {
                Console.WriteLine("start不可以为负数");
                return;
            }

            //根据XML文件生成图(MyGraph即默认构造的图)
            MyGraph graph = new MyGraph(xmlPath);

            //再次检查图的数据
            if (start > graph.GetVerticesCount() ||
                graph.GetVerticesCount() == 0 ||
                graph.GetEdgeCount() == 0)
            {
                return;
            }

            this.start  = start;
            this.marked = new bool[graph.GetVerticesCount()];
            this.from   = new int[graph.GetVerticesCount()];

            //起点自身和自身连通
            this.marked[start] = true;

            //初始值的from[]全部设置为-1,即标记未找到最近的上一个顶点
            for (int i = 0; i < graph.GetVerticesCount(); i++)
            {
                this.from[i] = -1;
            }

            //绑定图中与start连通的marked[]和from[]
            BondingConnect_bfs(graph);

            //起点的from[start]即自身(先绑定其他最后再绑定自身)
            this.from[start] = start;
        }