Data Structure BASICS (21), data structure basics 21
DFS
Start from a vertex V0, access the vertex, and then search for the traversal graph from each unaccessed adjacent vertex of V0 in sequence, until all vertices having the same path with V0 in the figure are accessed (using the stack ).
// The depth of the undirected Graph stored using the adjacent matrix preferentially traverses the template <typename Type> void Graph <Type >:: DFS () {stack <int> iStack; showVertex (0 ); vertexList [0]-> wasVisted = true; iStack. push (0); while (! IStack. empty () {int top = iStack. top (); int v = getAdjUnvisitedVertex (top); if (v =-1) {iStack. pop () ;}else {showVertex (v); vertexList [v]-> wasVisted = true; iStack. push (v) ;}/// enable deep/wide search for (int I = 0; I <nVerts; ++ I) vertexList [I]-> wasVisted = false ;}
BFS
Starting from a vertex V0, and then accessing all the unaccessed neighboring contacts of V0 in turn after the vertex is accessed, and then accessing their neighboring contacts in order according to the order in which these vertices are accessed, until all vertices having the same path with V0 in the figure are accessed.
If there are still unaccessed vertices in the graph, select an unaccessed vertex as the starting point and repeat the above process until all vertices in the graph are accessed (using a queue ).
// The breadth of the undirected Graph stored using the adjacent matrix preferentially traverses the template <typename Type> void Graph <Type >:: BFS () {queue <int> iQueue; showVertex (0 ); vertexList [0]-> wasVisted = true; iQueue. push (0); while (! IQueue. empty () {int front = iQueue. front (); iQueue. pop (); int v = getAdjUnvisitedVertex (front); while (v! =-1) {showVertex (v); vertexList [v]-> wasVisted = true; iQueue. push (v); v = getAdjUnvisitedVertex (front) ;}}for (int I = 0; I <nVerts; ++ I) vertexList [I]-> wasVisted = false ;}
Appendix-complete code
Const int MAX_VERTS = 20; // Vertex template <typename Type> class Vertex {public: Vertex (const Type & _ node = Type (): node (_ node ), wasVisted (false) {} public: bool wasVisted; // Add an access bit Type node;}; // figure template <typename Type> class Graph {public: Graph ();~ Graph (); void addVertex (const Type & vertex); void addEdge (int start, int end); void printMatrix (); void showVertex (int v); void DFS (); void BFS (); private: int getAdjUnvisitedVertex (int v); private: Vertex <Type> * vertexList [MAX_VERTS]; int nVerts; int adjMatrix [MAX_VERTS] [MAX_VERTS];}; template <typename Type> void Graph <Type>: DFS () {stack <int> iStack; showVertex (0); vertexList [0]-> wasVisted = true; I Stack. push (0); while (! IStack. empty () {int top = iStack. top (); int v = getAdjUnvisitedVertex (top); if (v =-1) {iStack. pop () ;}else {showVertex (v); vertexList [v]-> wasVisted = true; iStack. push (v) ;}/// enable deep search for (int I = 0; I <nVerts; ++ I) vertexList [I]-> wasVisted = false;} template <typename Type> void Graph <Type >:: BFS () {queue <int> iQueue; showVertex (0 ); vertexList [0]-> wasVisted = true; iQueue. push (0); whi Le (! IQueue. empty () {int front = iQueue. front (); iQueue. pop (); int v = getAdjUnvisitedVertex (front); while (v! =-1) {showVertex (v); vertexList [v]-> wasVisted = true; iQueue. push (v); v = getAdjUnvisitedVertex (front) ;}}for (int I = 0; I <nVerts; ++ I) vertexList [I]-> wasVisted = false ;} // obtain the next inaccessible connected node template <typename Type> int Graph <Type>: getAdjUnvisitedVertex (int v) {for (int j = 0; j <nVerts; ++ j) {// The first is the adjacent and if (adjMatrix [v] [j] = 1) that has not been accessed) & (vertexList [j]-> wasVisted = false) return j;} retu Rn-1;} // print the node Information template <typename Type> void Graph <Type >:: showVertex (int v) {cout <vertexList [v]-> node <'';} template <typename Type> Graph <Type >:: Graph (): nVerts (0) {for (int I = 0; I <MAX_VERTS; ++ I) for (int j = 0; j <MAX_VERTS; ++ j) adjMatrix [I] [j] = 0;} template <typename Type> Graph <Type> ::~ Graph () {for (int I = 0; I <nVerts; ++ I) delete vertexList [I];} template <typename Type> void Graph <Type> :: addVertex (const Type & vertex) {vertexList [nVerts ++] = new Vertex <Type> (vertex) ;}template <typename Type> void Graph <Type> :: addEdge (int start, int end) {// undirected graph adjMatrix [start] [end] = 1; adjMatrix [end] [start] = 1 ;} template <typename Type> void Graph <Type>: printMatrix () {for (int I = 0; I <nVerts; ++ I) {for (int j = 0; j <nVerts; ++ j) cout <adjMatrix [I] [j] <''; cout <endl ;}// test code int main () {Graph <char> g; g. addVertex ('A'); // 0g. addVertex ('B'); // 1g. addVertex ('C'); // 2g. addVertex ('D'); // 3g. addVertex ('E'); // 4g. addEdge (0, 1); // A-B g. addEdge (0, 3); // A-D g. addEdge (1, 0); // B-A g. addEdge (1, 4); // B-E g. addEdge (2, 4); // C-E g. addEdge (3, 0); // D-A g. addEdge (3, 4); // D-E g. addEdge (4, 1); // E-B g. addEdge (4, 2); // E-C g. addEdge (4, 3); // E-D g. printMatrix (); cout <"DFS:"; g. DFS (); cout <"\ nBFS:"; g. BFS (); return 0 ;}