Breadth-first traversal & depth-first traversal of graphs

Breadth-first traversal of graphs

Breadth-First Search (Breadth_First Search) Traversal is similar to the process of hierarchical traversal of a tree.

Suppose starting from a vertex v in the graph, after visiting v, visit each of the unvisited and adjacent points of v in turn, and then visit their adjacent points in turn from these adjacent points, and make the "adjacency of the vertex visited first" The "point" is visited before the "adjacent points of the later visited vertex" until all the adjacent points of the visited vertex in the graph have been visited. If there are still unvisited vertices in the graph at this time, another unvisited vertex in the graph is selected as the starting point, and the above process is repeated until all vertices in the graph are visited. In other words, in the process of breadth-first search traversing the graph, v is the starting point, and from near to far, the vertices that have a path connected to v and have a path length of 1, 2, ... are sequentially accessed.

//图的广度优先遍历
    void BFS(const V& value)
    {
        cout << "广度优先遍历序列:";
        queue<V> q;
        int size = _v.size();
        vector<bool> visted;//标记顶点是否被遍历
        int index = GetIndexOfV(value);

        visted.resize(size, false);//初始化为都未访问过
        q.push(index);
        _BFS(q, visted);

        //遍历与当前子图不连通的子图
        for (size_t i = 0; i < size; ++i)
        {
            if(visted[i] == false)
            {
                q.push(i);
                _BFS(q, visted);
            }
        }
        cout << endl;
    }

void _BFS(queue<V>& q, vector<bool>& visted)
    {
        while (!q.empty())
        {
            int index = q.front();
            if (visted[index] == false)//未被遍历
            {
                cout << _v[index] << " ";//遍历
                visted[index] = true;//更新visted为true

                PNode pCur = _linkEdges[index];//指向当前结点
                while (pCur)
                {
                    q.push(pCur->_dst);
                    pCur = pCur->_pNext;
                }
            }
            q.pop();
        }
    }

Depth-first traversal of graphs

Depth-first search (Depth_Fisrst Search) traversal is similar to the first root traversal of a tree, and it is a generalization of the first root traversal of the tree.

Assuming that the initial state is that all vertices in the graph have not been visited, the depth-first search can start from a vertex v in the graph, visit this vertex, and then sequentially start from the unvisited adjacent points of v to traverse the graph in depth first, until the graph All vertices in the graph that have a path connected to v have been visited; if there are still vertices that have not been visited in the graph at this time, another unvisited vertex in the graph is selected as the starting point, and the above process is repeated until all vertices in the graph are have been accessed so far.

//图的深度优先遍历
    void DFS(const V& value)
    {
        cout << "深度优先遍历序列:";
        int size = _v.size();
        size_t index = GetIndexOfV(value);
        vector<bool> visted;
        visted.resize(size, false);
        _DFS(index, visted);

        for (size_t i = 0; i < size; ++i)
        {
            if (visted[i] == false)
                _DFS(i, visted);
        }
        cout << endl;
    }

    void _DFS(size_t index,vector<bool>& visted)
    {
        cout << _v[index] << " ";
        visted[index] = true;

        PNode pCur = _linkEdges[index];
        while (pCur)
        {
            if (visted[pCur->_dst] == false)
                _DFS(pCur->_dst, visted);
            pCur = pCur->_pNext;
        }
    }

Test function:

void test2()
{
    char array[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' };
    Graph<char, int> g(array, sizeof(array) / sizeof(array[0]));

    g.AddEdge('A', 'B', 10);
    g.AddEdge('A', 'C', 20);
    g.AddEdge('A', 'D', 30);
    g.AddEdge('B', 'E', 40);
    g.AddEdge('B', 'C', 50);
    g.AddEdge('C', 'F', 60);
    g.AddEdge('D', 'F', 70);
    g.AddEdge('F', 'H', 80);
    g.AddEdge('H', 'I', 90);
    g.AddEdge('E', 'G', 100);
    g.AddEdge('J', 'K', 10);

    g.BFS('A');
    g.DFS('A');
}

The constructed graph is:

Breadth-first traversal:
write picture description here

Depth-first traversal:
write picture description here

Program result:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643710&siteId=291194637