Chapter 4 graph-deep and wide search Traversal

Source: Internet
Author: User

Well, the deep and wide search graph traversal is the course on the data structure that day. Then, in the evening, we asked for various online learning methods for the key of the data center, then the next night I changed the library for one night with the printed program, and then I went back to the dormitory to adjust the program. ...... Amount ...... In this case, I also called the teacher and said that I could look for him some afternoon. I was afraid that my code is a half-product, which is also an incentive to motivate myself.

/** This program is used to implement deep and breadth-first searches for undirected graphs respectively. */# Include <stdio. h> # include <stdlib. h> # define true 1 # define false 0 # define maxvertexnum 10/* Maximum number of edges */typedef int vertextype;/* set the top type to an integer, use the vertex number to represent the vertex */typedef int edgetype;/* set the edge weight to an integer, And the edge length? It seems that I cannot use it, right? */Typedef struct mgraph {/* graph type stored in an adjacent matrix */vertextype vexs [maxvertexnum];/* Number of vertices, edge Number */edgetype edges [maxvertexnum] [maxvertexnum];/* edge array */int n, E;} mgraph; int createudg (mgraph * g, int N, int e) {/* Create an undirected graph with n vertex e edges */INT I, j, U, V; G-> N = N; G-> E = E; for (I = 0; I <= n-1; I ++)/* initialize vertex information */g-> vexs [I] = I; for (I = 0; I <= n-1; I ++) {/* initialize the adjacent matrix */For (j = 0; j <= n-1; j ++) g-> edges [I] [J] = 0;} for (I = 0; I <= E-1; I ++) {/* two vertex numbers of an edge */scanf (" % D ", & U, & V); G-> edges [u] [v] = G-> edges [v] [u] = 1 ;} return 0;} int firstadjvex (mgraph g, int v) {/* returns the first adjacent vertex of V */int I; for (I = 0; I <= G. n-1; I ++) {If (G. edges [v] [I] = 1) return I;} return-1;} int nextadjvex (mgraph g, int V, int W) {/* returns the next vertex of V, that is, the next vertex of W */int I; for (I = W + 1; I <= G. n-1; I ++) {/* write W + 1 as W, = | */If (G. edges [v] [I] = 1) return I;} return-1;} int visited [10] = {0};/* access flag array, it turns out that public variables can be written in this way! */Void DFS (mgraph g, int v) {/* Deep Priority Search */int w; visited [v] = true; printf ("% d", V ); for (W = firstadjvex (G, V); W> = 0; W = nextadjvex (G, V, W) {If (visited [w]! = 1) DFS (G, W) ;}# define maxqsize 100 typedef struct sqqueue {/* queue sequential Storage Structure */int * base; int front; int rear ;} sqqueue; int initqueue (sqqueue * q) {/* construct an empty queue */Q-> base = (int *) malloc (maxqsize * sizeof (INT); If (! Q-> base) return 0; q-> front = Q-> rear = 0; return 1;} int enqueue (sqqueue * q, int e) {/* Insert a new team-End Element with E as Q */If (Q-> rear + 1) % maxqsize = Q-> front) return 0; q-> base [q-> rear] = E; q-> rear = (Q-> rear + 1) % maxqsize; return 1;} int dequeue (sqqueue * q) {/* Delete the Header element */int e; E = Q-> base [q-> front]; q-> front = (Q-> front + 1) % maxqsize; Return e;} int queueempty (sqqueue * q) {/* determine whether the queue is empty */If (Q-> front = Q-> rear) return 0; elsereturn 1;} void BF Straverse (mgraph g) {/* breadth-first search */sqqueue Q; int U, V, W; For (V = 0; v <= G. n-1; V ++) visited [v] = false; initqueue (& Q); For (V = 0; v <= G. n-1; V ++) {If (visited [v]! = 1) {visited [v] = true; printf ("% d", V); enqueue (& Q, V); While (! Queueempty (& Q) {u = dequeue (& Q); For (W = firstadjvex (G, U); W> = 0; W = nextadjvex (G, U, w) if (visited [w]! = 1) {visited [w] = true; printf ("% d", W); enqueue (& Q, W) ;}}} int main () {mgraph g; createudg (& G, 5, 5);/* in this line, I realized the role of &, with it */DFS (G, 0 ); /* the result of the variable G operation can be retained after the function is generated. */Printf ("\ n"); bfstraverse (g); printf ("\ n"); Return 0 ;}/ ** test case: (the image is shown in the attachment) ** input: ** 0 1 0 2 1 3 1 4 3 4 * output: ** 01342*01234 */

 

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.