コード例 #1
0
        public GraphDepthFirst(Graph_Adjacency_List G, int s)
        {
            int length = G.numberVertices;

            _hasBeenVisited = new bool[length];
            edgeTo          = new int[length];

            DepthFirstSearch(G, s);
        }
コード例 #2
0
 private void DepthFirstSearch(Graph_Adjacency_List G, int vertex)
 {
     _hasBeenVisited[vertex]         = true;
     _connectedVertexIdGroup[vertex] = _count;
     foreach (var adjacentVertex in G.FindAdjacentVertices(vertex))
     {
         if (!_hasBeenVisited[adjacentVertex])
         {
             DepthFirstSearch(G, adjacentVertex);
         }
     }
 }
コード例 #3
0
        private int _count;                    // # of components

        public GraphConnectedComponents(Graph_Adjacency_List G)
        {
            _hasBeenVisited         = new bool[G.numberVertices];
            _connectedVertexIdGroup = new int[G.numberVertices];
            for (int v = 0; v < G.numberVertices; v++)
            {
                if (!_hasBeenVisited[v])
                {
                    DepthFirstSearch(G, v);
                    _count++;
                }
            }
        }
コード例 #4
0
        private void DepthFirstSearch(Graph_Adjacency_List G, int vertex)
        {
            _hasBeenVisited[vertex] = true;

            foreach (var adjacentVertex in G.FindAdjacentVertices(vertex))
            {
                if (!_hasBeenVisited[adjacentVertex])
                {
                    DepthFirstSearch(G, adjacentVertex);
                    edgeTo[adjacentVertex] = vertex;
                }
            }
        }
コード例 #5
0
        private void BreadthFirstSearch(Graph_Adjacency_List G, int s)
        {
            Queue <int> queue = new Queue <int>();

            queue.Enqueue(s);
            marked[s] = true;

            while (queue.Count > 0)
            {
                int v = queue.Dequeue();

                foreach (var w in G.FindAdjacentVertices(v))
                {
                    if (!marked[w])
                    {
                        queue.Enqueue(w);
                        marked[w] = true;
                        edgeTo[w] = v;
                    }
                }
            }
        }