The first three points:
1. Graph traversal is to access each vertex of a graph only once in a certain order.
2. A problem that needs to be solved to implement BFS and DFS is how to store graphs. Generally, there are two methods: the adjacent matrix and the adjacent table. This is simple.
As shown in the preceding figure, the adjacent matrix is used for storage. In other words, the two-dimensional array is used.
3. The test examples of the small test section in this article are:
I. in-depth search Traversal
1. Deep graph G traversal algorithm starting from vertex v
① Access v
② Start in-depth traversal from the neighboring points not accessed by vertex v in sequence.
2. A bit of experience: the biggest feature of DFS algorithms lies in its recursive features, making the algorithm code concise. However, recursion makes the algorithm hard to understand.
Recursion makes it hard for beginners to grasp where the program is running! One of the suggestions is to learn recursion first and grasp the various types of function calls.
3. algorithm code:
# Include <iostream> using namespace STD; int A [11] [11]; bool visited [11]; void store_graph () // The storage graph of the adjacent matrix {int I, J; for (I = 1; I <= 10; I ++) for (j = 1; j <= 10; j ++) cin> A [I] [J];} void dfs_graph () // deep traversal graph {void DFS (INT V); memset (visited, false, sizeof (visited); For (INT I = 1; I <= 10; I ++) // traverse each vertex to prevent access to each vertex if (visited [I] = false) DFS (I);} void DFS (INT v) when the graph is not connected) // traverse vertices in depth {int adj (int x); cout <v <""; // access the vertex vvisited [v] = true; int adj = adj (V); While (adj! = 0) {If (visited [adj] = false) DFS (adj); // recursive call is the key to deep traversal );}} int adj (int x) // calculate the adjacent contact {for (INT I = 1; I <= 10; I ++) if (A [x] [I] = 1 & visited [I] = false) return I; return 0 ;} int main () {cout <"initialization diagram:" <Endl; store_graph (); cout <"DFS traversal result:" <Endl; dfs_graph (); Return 0 ;}
4. Small Test
Ii. breadth-first search Traversal
1. The algorithm used to traverse graph G from vertex v is described as follows:
① Access v
② Assume that the access vertices of the last layer are vi1, vi2, vi3. .. Vik, then the unaccessed adjacent points of vi1, vi2, vi3. .. Vik are accessed in sequence.
③ Repeat ② know that there are no unaccessed adjacent contacts
2. A bit of experience: the BFS algorithm is actually a hierarchical traversal algorithm. The algorithm description shows that the algorithm uses the data structure of the queue. Me
In STL. Because this algorithm is not a recursive algorithm, the program flow is clear.
3. algorithm code:
# Include <iostream> # include <queue> using namespace STD; int A [11] [11]; bool visited [11]; void store_graph () {for (INT I = 1; I <= 10; I ++) for (Int J = 1; j <= 10; j ++) cin> A [I] [J];} void bfs_graph () {void BFS (INT V); memset (visited, false, sizeof (visited )); for (INT I = 1; I <= 10; I ++) if (visited [I] = false) BFS (I);} void BFS (INT V) {int adj (int x); queue <int> myqueue; int adj, temp; cout <v <""; visited [v] = true; myqueue. push (V); While ( ! Myqueue. empty () // The queue is not empty, indicating that the vertex has not been traversed to {temp = myqueue. front (); // get the queue Header element myqueue. pop (); // The Header element is directed! = 0) {If (visited [adj] = false) {cout <adj <""; visited [adj] = true; myqueue. push (adj); // incoming pair} adj = adj (temp) ;}} int adj (int x) {for (INT I = 1; I <= 10; I ++) if (a [x] [I] = 1 & visited [I] = false) return I; return 0;} int main () {cout <"initialization diagram:" <Endl; store_graph (); cout <"BFS traversal result:" <Endl; bfs_graph (); Return 0 ;}
4. Small test: