I haven't summarized one thing very carefully for a long time,
I really don't want to learn again this time. I will definitely review it later. Otherwise, I 'd better not do it,
I forget it later. Isn't it the same as I didn't ??? I can't jump out of the forgetting rule, so I hope I can learn it well,
Make better use of the time. This is a very careful summary of the graph,
Let's take a look:
# Include <stdio. h> # include <string. h> # include <iostream> # include <string>/** how to create a graph using an adjacent table * and the simplest BFS traversal */using namespace STD; const int maxv = 1111; const int MaxE = 1111; // generally, the number of undirected edges is less than C (2, n). Although it is the number of so many edges, the undirected edge must be set to int N; int m; struct edge {int V; int Wight; int next;} e [MaxE]; int head [maxv]; // header node int vis [maxv]; // indicates whether the vertex has been accessed. int idx; void Init () // Initialization is also required! It is also a good choice to encapsulate them together. In the future, the initialization work will be completed in Init () {//. It looks very good at encapsulation! Memset (Head,-1, sizeof (head); memset (VIS, 0, sizeof (VIS); idx = 0;} void addedge (int A, int B, int value) // start point A, end point B, and Edge Weight Value {e [idx]. wight = value; E [idx]. V = B; E [idx]. next = head [a]; head [a] = idx ++; // when creating a graph, be sure to pay attention to the problem. When simulating an array, the bottom mark value of the edge is the index. You can find the edge by finding the index. Therefore, the index is the medium you are looking for and can be used as a pointer, // This is clever .} void visit_bfs (int x) // then add a for loop outside. This is the BFS traversing all vertices {If (vis [x]) {return ;} vis [x] = 1; int P = hea D [X]; // do some thing for visiting; printf ("Access Point V: % d \ n", x); While (P! =-1) {int v = E [p]. v; // indicates the vertex you will traverse. printf ("Access vertex v: % d \ n", V); vis [v] = 1; // mark that you have accessed this point; P = E [p]. next; // continue to access the next adjacent vertex;} void visit_dfs (int x) // Of course, if the graph is a connected graph, you do not need to add a for loop. If not, if you want to traverse all {//, you also need to add a for loop; If (vis [x]) {return;} // do some thing for visiting; vis [x] = 1; printf ("Access Point V: % d \ n", x); int P = head [X]; cout <"P =" <p <Endl; while (P! =-1) // determine if the next node you want to access has been accessed. If yes, it will no longer be accessed {int v = E [p]. v; visit_dfs (V); vis [v] = 1; P = E [p]. next; // do not trace back, so vis [v] = 0; do not need to be cleared .}} int main () {int A, B, Val; while (scanf ("% d", & N, & M )! = EOF) {Init (); For (INT I = 1; I <= m; I ++) {scanf ("% d", &, & B, & Val); addedge (a, B, Val); // If an undirected graph is created, add // addedge (B, A, Val );} printf ("BFS traversal result: \ n"); For (INT I = 1; I <= N; I ++) {visit_bfs (I);} memset (VIS, 0, sizeof (VIS); printf ("DFS traversal result: \ n"); For (INT I = 1; I <= N; I ++) {visit_dfs (I) ;}} system ("pause"); Return 0 ;}/ * 5 31 2 11 3 13 4 1 */