Data Structure -- graph -- Array Storage representation of the graph, depth-first search traversal and breadth-first search Traversal

Source: Internet
Author: User

The graph has four storage structures: array, adjacent table, cross linked list, and multiple adjacent tables. The following uses an array as the storage structure to achieve deep-first search traversal and breadth-first search traversal of graphs. Among them, the queue in STL is used in the breadth-first search traversal. Note the header file inclusion. The Code is as follows:

// Graph array (Adjacent matrix) Storage representation and depth first traversal const int max_vertex_num = 20; // maximum number of vertices typedef Enum {DG, dn, udg, UDN} graphkind; // (directed graph, directed graph, undirected graph, undirected graph) typedef int vrtype; typedef char infotype; typedef char vertextype; typedef struct arccell {vrtype adj; // vrtype is the vertex link type, for a no-Permission graph, 1 or 0 indicates whether the vertex is adjacent or not. For a right graph, the value type is infotype info. // arccell () {adj = 0; info = 0 ;}} arccell, adjmatrix [max_vertex_num] [max_vertex_num]; typedef struct mgraph {vertext Ype vexs [max_vertex_num]; // vertex vector adjmatrix arcs; // adjacent matrix int vexnum, arcnum; // current vertex count and arc count of the graph graphkind kind; // graph type flag} mgraph; int locatevex (mgraph g, char V1) {for (INT I = 0; I <max_vertex_num; ++ I) {If (G. vexs [I] = V1) return I;} return max_vertex_num + 1;} status createudn (mgraph & G) {// array (Adjacent matrix) representation, build an undirected network G. kind = UDN; // manually assign a value to the undirected network int vexnumber = 0, arcnumber = 0; char Info; cout <"Please input the vexnumber arcnumber And info: "; CIN> vexnumber> arcnumber> Info; G. vexnum = vexnumber; G. arcnum = arcnumber; For (INT I = 0; I <G. vexnum; ++ I) {// construct the vertex vector cout <"Please input the vertex of number" <I <"(type char)"; CIN> G. vexs [I];} For (INT I = 0; I <G. vexnum; ++ I) // initialize the adjacent matrix for (Int J = 0; j <G. vexnum; ++ J) {G. ARCs [I] [J]. adj = 0; G. ARCs [I] [J]. info = 0;} Char V1, V2; int Weight = 0, I = 0, j = 0; char infomation; For (int K = 0; k <G. arcnum; ++ K) {// initialize the adjacent Cout matrix <"Please input the two vertexs of the arc and it's weight" <k + 1 <""; cin> V1> V2> weight; I = locatevex (G, V1); j = locatevex (G, V2); G. ARCs [I] [J]. adj = weight; G. ARCs [J] [I]. adj = weight; If (info! = 48) {// the ASCII code of 0 is 48 cout <"Please input infomation:"; CIN> infomation; G. ARCs [I] [J]. info = infomation; G. ARCs [J] [I]. info = infomation ;}} Return OK;} void dismgraph (mgraph m) {for (INT I = 0; I <m. vexnum; ++ I) {for (Int J = 0; j <m. vexnum; ++ J) {cout <m. ARCs [I] [J]. adj <";}cout <Endl ;}// depth-first traversal of the tree and breadth-first traversal of bool visited [max_vertex_num]; // access flag array status visitfunc (mgraph g, int v) {// function variable if (G. vexs [v]) {cout <G. vexs [V] <"; Return OK;} return error;} int firstadjvex (mgraph g, int V) {// returns the first adjacent vertex if (G. vexnum = 0) Return-1; // determine whether graph G exists for (Int J = 0; j <G. vexnum; ++ J) {If (G. ARCs [v] [J]. adj) return J;} return-1;} int nextadjvex (mgraph g, int V, int W) {// returns the next adjacent vertex if (G. vexnum = 0) Return-1; // determine whether graph G exists for (Int J = W + 1; j <G. vexnum; ++ J) {If (G. ARCs [v] [J]. adj) return J;} return-1;} void DFS (mgraph g, int v) {// output from the V Vertex Send, depth first search traverse graph G visited [v] = true; visitfunc (G, V); // access the V node for (int w = firstadjvex (G, v); W> = 0; W = nextadjvex (G, V, W) {If (! Visited [w]) DFS (G, W); // call DFS recursively for the unaccessed adjacent vertex W of V} void dfstraverse (mgraph g, status (* visit) (mgraph g, int V) {// performs depth-first traversal on the graph for (int v = 0; v <G. vexnum; ++ v) {// access flag array initialization visited [v] = false;} For (int v = 0; v <G. vexnum; ++ v) {If (! Visited [v]) DFS (G, V); // call DFS} void bfstraverse (mgraph g, status (* visit) (mgraph G, int V) {// search the traversal graph for (int v = 0; v <G. vexnum; ++ v) visited [v] = false; queue <char> que; char tempvex = 0; For (int v = 0; v <G. vexnum; ++ v) {If (! Visited [v]) {visited [v] = true; visit (G, V); Que. Push (G. vexs [v]); While (! Que. empty () {tempvex = que. front (); que. pop (); For (int w = firstadjvex (G, tempvex); W> = 0; W = nextadjvex (G, tempvex, W) {If (! Visited [w]) {visited [w] = true; visit (G, W); que. push (G. vexs [w]) ;}}}} int main () {mgraph m; createudn (m); dismgraph (m); cout <"DFS Result :"; dfstraverse (M, visitfunc); cout <Endl <"BFS Result:"; bfstraverse (M, visitfunc); Return 0 ;}

Running result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.