First, the basic concept of FIG.
1, adjacent point: For the FIG None None v 1 and v 2 has an arc between v1 and v2 is said mutually adjacent point ; For the purposes of FIG. <V 1 , v 2 > v from representatives of a 1 to V 2 arc, called V 2 to V . 1 is a neighbor.
2, the degree: is the number of the vertex of the arc interrelated.
3, FIG communication: None are reachable paths between each of the vertices of a graph, called the communication undirected graph in FIG. Each has a directed graph between the vertices <V . 1 , V 2 > and <V 2 , V . 1 >, this Digraph called strong graph .
Second, the storage structure
1, the adjacency matrix storage (Adjacency Matrix)
FIG not entitled to have an arc between the vertices labeled 1, no arc labeled 0;
FIG on the right, there is an arc between the vertices of the right superscript, subscript no arc infinity;
2, adjacency list (read it, do not want to draw a finite energy)
Three, DFS and BFS
In fact, depth-first and breadth-first, most important thing is to grasp the idea, rather than a specific implementation, because the original aim.
1, DFS depth-first
① starting from v0 v0 access
② find the vertex just a nice visit, a visit that did not visit a nice adjacent points. ② repeated until no abutment position.
③ back, but there are still a been visited not access a nice neighbor vertex before returning to continue to access its next neighbor. ② Repeat until complete coverage.
Maybe I describe is not accurate enough, but there is no way, it should really only be felt to some things. Computer science must be combined with concrete examples of view.
Original:
DFS
In fact, it simply: that is, if the node has access to the adjacent access point has been down, otherwise it back. First Traversal same tree is very similar.
Data storage structure:
String[] vertex = new String[] {"v0","v1","v2","v3","v4","v5"}; int[][] matrix = new int[][] { {0,1,0,1,0,0}, {1,0,1,0,1,0}, {0,1,0,0,0,0}, {1,0,0,0,0,1}, {0,1,0,0,0,0}, {0,0,0,1,0,0} }; int [] = visited new new int [. 6]; // tag access node nice
Recursive:
void matrixDFS(int v0) { System.out.print(vertex[v0] + " "); visited [v0] =. 1 ; // iterate to find v0 abutment point for ( int I = 0; I <vertex.length; I ++ ) { IF (Matrix [v0] [I]. 1 && visited == [I] == 0 ) { matrixDFS(i); } } }
Non-recursive implementation:
// stack-based non-recursive void matrixDFS1 ( int V0) { Stack<Integer> stack = new Stack<Integer>(); stack.push(v0); the while (! stack.isEmpty ()) { int V = stack.pop (); IF (visited [V] == 0) { // stack is likely to have duplicate elements System.out.print (vertex [v] + "" ); visited[v] = 1; for(int i = 0; i < matrix.length; i++) { if(matrix[v][i] == 1 && visited[i] == 0) { stack.push(i); } } } } }
2, BFS depth-first traversal
① visit v0
② access v0 so not visited neighbors
③ respectively adjacent to these points (in the adjacent point ②) starting to visit their neighbors. Repeat ③ know traverse end.
BFS is more like a tree hierarchy traversal
Code:
void matrixBFS2(int v0) { Queue<Integer> queue = new LinkedList<>(); queue.offer(v0); while(!queue.isEmpty()) { int v = queue.poll(); if(visited[v] == 0) { System.out.print(vertex[v] + " "); visited[v] = 1; for(int i = 0; i < vertex.length; i++) { if(matrix[v][i] == 1 && visited[i] == 0) { queue.offer(i); } } } } }
We can see the difference between this code and the code DFS recursive with only a stack with a queue.
DFS: Access v0, neighbors such as stacks
BFS: Access v0, neighbor into the team
Our goal is not to master these specific implementation, but to understand thought DFS and BFS algorithms, which learn and use.