JAVA data structure: implement graph breadth first traversal and depth first traversal

The data structure series is a note that I have learned to make, which will be continuously updated. The source code is ...

The data structure series is a note that I have learned to make, which will be continuously updated. The source code is shared in github: data structure Of course, you can also get it from the following code snippet
Note: github code update will be delayed. Pay attention not to get lost A kind of

This article introduces the breadth first search algorithm and depth first search algorithm, and implements them in java

1. Breadth first search

Textbook explanation: breadth first search is similar to tree level traversal, which is the generalization of number level traversal

Algorithm description:
Starting from a vertex v in the graph, access the vertex first, and then access each of the unaccepted adjacency points w1,w2,w3,w4 , and then access the vertices w1,w2 in order The adjacent points that are not accessed are repeated until all the vertices in the graph are accessed.

Brief overview: select a vertex of the graph, top-down, left-right, far and near, one line search

2. Depth first search

Textbook explanation: depth first search is similar to tree first root traversal, which is the extension of tree first root traversal

Algorithm description:
Start from a vertex v in the graph, and then access any of its adjacent points w1. Start from w1, access the vertex w2 which is adjacent to w1 and has not been accessed yet , so that until all the adjacency points have been accessed, then, step back, meet the vertex just visited before, check whether there are any adjacency points that have not been accessed, if so, access this vertex, otherwise continue to step back until all the vertices have been accessed.

Brief overview: start from a vertex, walk with a tendon on the left until it cannot walk, and then back. If there is a way, then walk on the left, and so on, until all the vertices are visited.

3. Java implementation: GraphSearch.java

IGraph interface is involved, see the link for details:
Adjacency table of storage structure of Graphs perhaps Adjacency matrix of storage structure of Graphs

Of course, you can also visit my github to get the complete project code (Note: there is a delay in updating). See the beginning of the blog for the address

package code.Graph; import code.Queue.LinkQueue; /** * Graph search algorithm, including breadth first search algorithm and depth first search algorithm * */ public class GraphSearch { private static boolean [] visited;//Access flag array /** * Breadth first search * */ public static void BFSTraverse(IGraph G)throws Exception{ visited = new boolean[G.getVexNum()];//Instantiate access flag array for (int v = 0;v < G.getVexNum();v++){//Cycle vertex times //Initialize access flag array visited[v] = false; } for (int v = 0;v < G.getVexNum();v++){ if (!visited[v]){ //Vertex not yet accessed BFS(G,v); } } } private static void BFS(IGraph G,int v)throws Exception{ visited[v] = true; System.out.print(G.getVex(v).toString()+", "); LinkQueue Q = new LinkQueue();//Secondary queue Q Q.offer(v); while (!Q.isEmpty()){ int u = (Integer)Q.poll();//Queue head element is queued and assigned to u for (int w = G.firstAdjVex(u);w >= 0;w = G.nextAdjVex(u,w)){ if (!visited[w]){ //w is u's adjacency vertex that has not been accessed visited[w] = true; System.out.print(G.getVex(w).toString()+", "); Q.offer(w); } } } } /** * Depth first search * */ public static void DFSTraverse(IGraph G) throws Exception{ visited = new boolean[G.getVexNum()];//Instantiate access flag array for (int v = 0;v < G.getVexNum();v++){//Cycle vertex times //Initialize access flag array visited[v] = false; } for (int v = 0;v < G.getVexNum();v++){ if (!visited[v]){ //Vertex not yet accessed DFS(G,v); } } } public static void DFS(IGraph G,int v)throws Exception{ //Recursive depth first traversal graph G from the v-th vertex visited[v] = true; System.out.print(G.getVex(v).toString()+", ");//Access vertex v for (int w = G.firstAdjVex(v);w >= 0;w = G.nextAdjVex(v,w)){ if (!visited[w]){ DFS(G,w); //Recursively call DFS to the adjacent vertex w of v that has not been accessed } } } }
Big Qing er Published 19 original articles, won praise 19, visited 1568 Private letter follow

14 February 2020, 08:57 | Views: 4407

Add new comment

For adding a comment, please log in
or create account

0 comments