Graph and graph Traversal

Source: Internet
Author: User

Since I have never studied Discrete Mathematics, I can only make an axe to the class here:

A graph G = (V, E) is composed of vertex set V (vertex) and edge set E (EGGE. Each edge is a point (V, W), where V, W, V. If the point-to-point is ordered, the graph is directed, otherwise it becomes an undirected graph. If two vertices (V1, V2) and E are associated, they are adjacent to each other. The degree of a vertex refers to the number of vertices associated with the vertex. Digraphs can also be divided into inbound and outbound degrees. If there is a sequence (V1, V2 ,...... (VN). If two adjacent elements (Vi, vi + 1) and E constitute the path. If V1 = Vn, it is called a loop or loop. If we can move the path from point V1 to V2, we call these two points Unicom. If any two points in the graph are connected, they are called connected graphs. If a graph G' = (V ', e') and v'ε V, E 'ε E, then G' is a subgraph of G. In particular, if the vertices of the two graphs are identical, but the edge set of the second graph is the subset of the first graph, the second graph is called the subgraph of the first graph.

There are so many basic concepts of graphs. In fact, these concepts are very clear after being drawn.

How can we express this complicated data structure, that is, a bit of data with edges? People come up with many kinds of representation: the adjacent matrix, the adjacent table, and so on. Here we will only introduce the simplest adjacent matrix: it is to write n vertices in the graph into a n * n matrix. If the two vertices are associated, the value of the matrix is 1, if not, it is 0.

# Include <stdio. h> # include <malloc. h> // stack # include "Stack is required for deep priority search. H "// For breadth-first traversal, the queue # include" queue must be used. H "# define Max 8 // adjacent matrix typedef int ** adjacentmatrix; // data structure of the graph typedef struct graph {// adjacent matrix adjacentmatrix matrix; // vertex vector char * vextex; // Number of vertex vectors int vextexnum; // Number of edges int arcnum;} graph; // basic graph operations // initialize graph initgraph (INT); // destroy graph void destroygraph (graph *); // Add an edge bool addarc (graph *, Char, char); // delete an edge bool deletearc (graph *, Char, char); // display the graph void showgraph (graph *); // graph traversal: // implement the void do_dfs (graph *, INT); void DFS (graph *); // implement the int * findadjacentvertex (graph *, int, int *); void dfs_bystack (graph *, char); // void bfs_byqueue (graph *, char) for the queue with breadth-first search );

A data structure. The most important operation is to traverse it. There are two common methods for graph traversal: depth-first traversal and breadth-first traversal. The depth-first traversal is similar to the first-order traversal of a binary tree. The breadth-first traversal is similar to the layer-by-layer traversal tree.

The basic steps for the breadth-first traversal are as follows:

1. queue the initial node

2. When the queue is not empty, it leaves the queue. The nodes that leave the queue are placed in VX.

3. Locate the nodes adjacent to VX that have not been accessed and queue them.

4. Switch 2.

Note that you can perform corresponding operations after each release, such as printing.

The basic steps for deep traversal are as follows:

1. Press the Start Node to stack

2. If the stack is not empty, the stack will pop up; the pop-up node will be placed in VX.

3. Locate the nodes adjacent to VX that have not been accessed, and press them on the stack.

4. Switch 2.

 

The following code is provided:

# Include "graph. H "// basic graph operations // initialize graph initgraph (int n) {graph G; G. vextexnum = N; G. arcnum = 0; G. matrix = (INT **) malloc (sizeof (int *) * n); For (INT I = 0; I <n; ++ I) g. matrix [I] = (int *) malloc (sizeof (INT) * n); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) g. matrix [I] [J] = 0; G. vextex = (char *) malloc (sizeof (char) * n); char a = 'a'; for (INT I = 0; I <n; ++ I) g. vextex [I] = a + I; return g;} // destroy the void destroygr APH (graph * g) {free (G-> vextex); G-> vextex = NULL; For (INT I = 0; I <G-> vextexnum; ++ I) free (G-> matrix [I]); free (G-> matrix); G-> matrix = NULL; G-> arcnum =-1; g-> vextexnum =-1;} // Add an edge bool addarc (graph * g, char vex1, char vex2) {int M = vex1-'A '; int n = vex2-'A'; If (M <0 | M> G-> vextexnum | n <0 | n> G-> vextexnum) {printf ("2 vertexes must in the graph \ n"); Return false;} else {G-> matrix [m] [N] = 1; G-> matri X [N] [m] = 1; ++ G-> arcnum; return true ;}/// delete an edge bool deletearc (graph * g, char vex1, char vex2) {int M = vex1-'A'; int n = vex2-'A'; If (0 = G-> matrix [m] [N]) {printf ("this arc does not exsit! \ N "); Return false;} else {G-> matrix [m] [N] = 0; G-> matrix [N] [m] = 0; g-> arcnum --; return true ;}// display the void showgraph (graph * g) {printf (""); For (INT I = 0; I <G-> vextexnum; ++ I) printf ("% C", G-> vextex [I]); For (INT I = 0; I <G-> vextexnum; ++ I) {printf ("\ n"); printf ("% C", G-> vextex [I]); for (Int J = 0; j <G-> vextexnum; ++ J) printf ("% d", G-> matrix [I] [J]);} printf ("\ n");} // graph traversal: // Recursive Implementation of deep Priority Search // you need to use global variables to record whether a vertex has been accessed bool vi Sited_dfs [Max]; void DFS (graph * g) {printf ("use recursive depth first traversal: \ n sequential access node:"); // before the start of access, none of the nodes have been accessed for (INT I = 0; I <G-> vextexnum; ++ I) visited_dfs [I] = false; // For each node, if it is not accessed, the deep search for (INT I = 0; I <G-> vextexnum; ++ I) {If (false = visited_dfs [I]) is called first. do_dfs (G, I);} printf ("\ n");} void do_dfs (graph * g, int I) {visited_dfs [I] = true; printf ("% C", G-> vextex [I]); // search for the adjacent contacts of I in sequence for (Int J = 0; j <G-> vextexnum; ++ J) {// If J is the adjacent contact of I and is not accessed If (visited_dfs [J] = false & 1 = G-> matrix [I] [J]) do_dfs (G, j) ;}} void dfs_bystack (graph * g, char start) {int start = start-'A'; printf ("Use Queue for depth-first traversal: \ n sequentially accesses the node: "); // creates an array to mark whether the node has been accessed by bool * is_visited = (bool *) malloc (sizeof (bool) * g-> vextexnum); For (INT I = 0; I <G-> vextexnum; ++ I) is_visited [I] = false; // create a stack S; S = initstack (G-> vextexnum); push (& S, start); is_visited [start] = true; while (! Is_empty (& S) {int vertex; POP (& S, & vertex); int adjacentnumber; int * adjacentvertex = findadjacentvertex (G, vertex, & adjacentnumber ); for (INT I = 0; I <adjacentnumber; ++ I) {If (false = is_visited [adjacentvertex [I]) {push (& S, adjacentvertex [I]); is_visited [adjacentvertex [I] = true ;}} printf ("% C", G-> vextex [vertex]);} printf ("\ n"); destroystack (& S); free (is_visited);} // search for nodes adjacent to Vex and take out the number of nodes through N, returns the int * findad array composed of these nodes. Jacentvertex (graph * g, int Vex, int * n) {// counter int CNT = 0; int * adj = (int *) malloc (sizeof (INT) * g-> vextexnum); For (INT I = 0; I <G-> vextexnum; ++ I) {if (1 = G-> matrix [Vex] [I]) adj [CNT ++] = I;} * n = CNT; return adj ;} void bfs_byqueue (graph * g, char start) {int start = start-'A'; printf ("using stacks for breadth-first traversal: \ n sequential access to nodes :"); // create an array to mark whether the node has been accessed by bool * is_visited = (bool *) malloc (sizeof (bool) * g-> vextexnum); For (INT I = 0; I <G-> ve Xtexnum; ++ I) is_visited [I] = false; queue Q; q = initqueue (G-> vextexnum); enqueue (& Q, & START ); is_visited [start] = true; while (! Is_empty (& Q) {int vertex; dequeue (& Q, & vertex); int adjacentnumber; int * adjacentvertex = findadjacentvertex (G, vertex, & adjacentnumber ); for (INT I = 0; I <adjacentnumber; ++ I) {If (false = is_visited [adjacentvertex [I]) {enqueue (& Q, & adjacentvertex [I]); is_visited [adjacentvertex [I] = true ;}} printf ("% C", G-> vextex [vertex]);} free (is_visited); destroyqueue (& Q );}

Note that the depth-first traversal of an image can be implemented either by using the stack or by returning the image. This is consistent with our previous understanding.

The stack and queue code used in the program will not be posted, as described in previous blogs. The main difference is that a stack or queue is obtained through the return value of the function. In the past, the pointer pointing to them was uploaded to the function. However, their ideas are similar.

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.