/// <summary>
        /// 使用深度优先搜索找到g图中v顶点的所有相遇顶点
        /// </summary>
        /// <param name="g"></param>
        /// <param name="v"></param>
        private void dfs(GraphUndirected g, int v)
        {
            // 把v顶点的 marked标为true
            this.markeds[v] = true;
            IEnumerator ie = g.adjGet(v).GetEnumerator();

            while (ie.MoveNext())
            {
                int w = (int)ie.Current;
                if (!this.markeds[w])
                {
                    dfs(g, w);
                }
            }
            // 相通顶点数量 +1
            this.countNum++;
        }
Ejemplo n.º 2
0
        private void dfs(GraphUndirected g, int v)
        {
            this.markeds[v] = true;
            // 遍历顶点v的邻接表,拿到每一个相邻的顶点继续递归
            IEnumerator ie = g.adjGet(v).GetEnumerator();

            while (ie.MoveNext())
            {
                int w = (int)ie.Current;
                // 如果顶点我没有被搜索,递归搜索
                if (!this.markeds[w])
                {
                    // 到达顶点w的路径上的最后一个顶点是v
                    edgeTo[w] = v;
                    dfs(g, w);
                }
            }
        }
 /// <summary>
 /// 使用广度优先搜索找到g图中v顶点的所有相遇顶点
 /// </summary>
 /// <param name="g"></param>
 /// <param name="v"></param>
 private void bfs(GraphUndirected g, int v)
 {
     // 把v顶点的 marked标为true
     this.markeds[v] = true;
     // 把顶点v进入队列,待搜索
     this.waitSearch.enqueue(v);
     // 通过循环 如果队列不为空,则从队列中弹出一个待搜索的顶点进行搜索
     while (!this.waitSearch.isEmpty())
     {
         int         wait = this.waitSearch.dequeue();
         IEnumerator ie   = g.adjGet(wait).GetEnumerator();
         while (ie.MoveNext())
         {
             int w = (int)ie.Current;
             if (!this.markeds[w])
             {
                 bfs(g, w);
             }
         }
     }
     // 相通顶点数量 +1
     this.countNum++;
 }