dfs - practice demo1 (provided by the 20th Zhou Xinjie)

 


 

The traversal process of deep search is to search the branches of the tree as deep as possible. When all the child nodes of a node have been explored, the search will trace back to the starting node of the edge where the node was found.
This process will continue until until all nodes have been found to be reachable.
If there are still undiscovered nodes, the process will randomly select an undiscovered node and repeat the above
process until all nodes have been visited.

Depth-first search traversal process
Starting from a, you can see that the child nodes of a have c, d, and f. The system will perform a depth-first search
process on them first. The child nodes of c are searched, and it can be seen that c has two child nodes b, d
can see that b has no child nodes, but node d has not been accessed as a child node of c. At this time, the program will go to the position of d,
but d has no child nodes. At this time, the process will backtrack to the edge of d. The position of the starting node a and then search for it in
the child nodes of a, only f has not been traversed, so the process can only go to the position of f and then traverse it, it can be seen that the two child nodes of f have no children. Node
So the process ends after traversing g and e.

    static String b[] = { "a", "b", "c", "d", "e", "f", "g" };
    static int [][]arr= {
            {0,0,1,1,0,1,0},
            {0,0,1,0,0,0,0},
            {1,1,0,1,0,0,0},
            {1,0,1,0,0,0,0},
            {0,0,0,0,0,0,1},
            {1,0,0,0,0,0,1},
            {0,0,0,0,1,1,0}
    };

Do a depth search and a breadth search on this 2D array, and iterate over the results.

package test;

public class dfs {
    // 构造图的边
    private int[][] edges = { 
    		{0,0,1,1,0,1,0},
			{0,0,1,0,0,0,0},
			{1,1,0,1,0,0,0},
			{1,0,1,0,0,0,0},
			{0,0,0,0,0,0,1},
			{1,0,0,0,0,0,1},
			{0,0,0,0,1,1,0}
            };
	// 构造图的顶点
	private String[] vertexs = { "a", "b", "c", "d", "e", "f", "g" };
	// 记录被访问顶点
	private boolean[] verStatus;
	// 顶点个数
	private int vertexsNum = vertexs.length;

	public void DFSTra() {
		verStatus = new boolean[vertexsNum];
		for (int i = 0; i < vertexsNum; i++) {
			if (verStatus[i] == false) {
				DFS(i);
			}
		}
	}

	// 递归深搜
	private void DFS(int i) {
		System.out.print(vertexs[i] + " ");
		verStatus[i] = true;
		// 深度搜索子节点
		for (int j = firstAdjVex(i); j >= 0; j = nextAdjvex(i, j)) {
			if (!verStatus[j]) {
				DFS(j);
			}
		}
	}

	// 返回与i相连的第一个顶点
	private int firstAdjVex(int i) {
		for (int j = 0; j < vertexsNum; j++) {
			if (edges[i][j] > 0) {
				return j;
			}
		}
		return -1;
	}

	// 返回与i相连的下一个顶点
	private int nextAdjvex(int i, int k) {
		for (int j = (k + 1); j < vertexsNum; j++) {
			if (edges[i][j] == 1) {
				return j;
			}
		}
		return -1;
	}

	// 测试
	public static void main(String[] args) {
		new dfs().DFSTra();
	}

}

Deep search results

a c b d f g e 

Guess you like

Origin blog.csdn.net/feng8403000/article/details/123591813