Data structures (V) - graph traversal (recursive)

Traversing Graph There are many ways, but here, starting from the basic data structures, or just introduce two basic ways, depth-first traversal and breadth-first traversal.

Depth-first traversal

FIG depth-first search (Depth First Search), and the preamble relatively similar tree traversal.

It's thought: Suppose the initial state is drawing all the vertices have not been visited, starting from a vertex v, the first access to the apex, followed by its neighbors from each unvisited depth-first search graph traversal, until FIG vertices and v have all communication paths are accessible. If this fashion not have access to the other vertex, the vertex alternatively as a starting point has not been accessed, repeating the process until all vertices have been drawing until accessed.

Clearly, the depth-first search is a recursive process. In a word: "right up to the head, do not hit the brick wall does not look back."

Code implements (the adjacency matrix):

public void DFSSelf() {
    boolean[] beTraversed = new boolean[size];
    beTraversed[0] = true;
    System.out.print(vertexs[0] + " ");
    DFSSelf(0, beTraversed);//从头节点开始
}

public void DFSSelf(int x, boolean[] beTraversed) {
    int y = 0;
    while (y <= size - 1) {
        if (matrix[x][y] != 0 && beTraversed[y] == false) { //遍历与当前节点有边的节点
            beTraversed[y] = true;
            System.out.print(vertexs[y] + " ");//遍历
            DFSSelf(y, beTraversed);//递归进行深度遍历
        }
        y++;
    }
}

Adjacency table (thinking exactly the same, just get the relationship between the nodes is not the same):

public void DFS() {
    boolean[] beTraversed = new boolean[size];
    beTraversed[getPosition(vertexList[0].ch)] = true;
    DFS(beTraversed, vertexList[0]);
}

/**
 * 深度优先遍历
 *
 * @param beTraversed
 * @param temp
 */
public void DFS(boolean[] beTraversed, Vertex temp) {
    System.out.print(temp.ch + " ");
    beTraversed[getPosition(temp.ch)] = true;
    while (temp != null) {
        if (beTraversed[getPosition(temp.ch)] == false) {
            DFS(beTraversed, vertexList[getPosition(temp.ch)]);
        }
        temp = temp.next;
    }
}

Breadth-first traversal

Breadth-first search algorithm (Breadth First Search), also known as "BFS" ​​or "breadth-first search", referred to BFS.

The idea is: a vertex v from the graph, and then click Access After visiting v individual never visited neighbors v, and then separately from these neighbors in order to visit their neighbors, and making the "first to be accessed adjacent vertex points adjacent vertex dots before access after being accessed, all vertices have been visited neighbors until the figure are accessed. If at this time the figures there have not been accessed vertex, you need another is selected from a vertex not been visited as a new starting point, repeating the process until all vertices have been drawing until accessed.

In other words, breadth-first search is a traversal of FIG v as a starting point of the process, from near to far, and v has successively access communication path and a path length of 1, 2, ... vertex.

Code implements (the adjacency matrix):

public void BFSSelf() {
    boolean[] beTraversed = new boolean[size];
    beTraversed[0] = true;
    System.out.print(vertexs[0] + " ");
    BFSSelf(0, beTraversed, new LinkedList<>());
}

/**
 * 广度优先搜索遍历
 *
 * @param x
 * @param beTraversed
 */
public void BFSSelf(int x, boolean[] beTraversed, LinkedList<Character> list) {
    int y = 0;
    while (y <= size - 1) {
        if (matrix[x][y] != 0 && beTraversed[y] == false) { //依次将与当前节点有联系的节点遍历,然后入队。
            beTraversed[y] = true;
            System.out.print(vertexs[y] + " ");
            list.add(vertexs[y]);
        }
        y++;
    }
    if (!list.isEmpty()) { //出队进行递归操作
        Character node = list.pop();
        int pos = getPosition(node);
        BFSSelf(pos, beTraversed, list);
    }
}

Adjacency list

public void BFS(){
    boolean[] beTraversed = new boolean[size];
    beTraversed[getPosition(vertexList[0].ch)] = true;
    System.out.print(vertexList[0].ch+" ");
    int startPos = getPosition(vertexList[0].ch);
    BFS(startPos,beTraversed);
}

/**
 * 广度优先搜素遍历
 * @param beTraversed
 */
public void BFS(int x,boolean[] beTraversed){
    Vertex temp = vertexList[x];
    LinkedList<Vertex> list = new LinkedList<>();
    while(temp!=null){
        if(beTraversed[getPosition(temp.ch)] == false){
            System.out.print(temp.ch+" ");
            beTraversed[getPosition(temp.ch)] = true;
            list.add(vertexList[getPosition(temp.ch)]);
        }
        temp = temp.next;
    }

    while(!list.isEmpty()){
        Vertex node = list.pop();
        int pos = getPosition(node.ch);
        BFS(pos,beTraversed);
    }
}
Published 133 original articles · won praise 37 · views 90000 +

Guess you like

Origin blog.csdn.net/liman65727/article/details/104311866