06-Figure 1 list connected sets (25 points) (implemented in the C language neighbor list) and 06-adjacent

Source: Internet
Author: User

06-Figure 1 list connected sets (25 points) (implemented in the C language neighbor list) and 06-adjacent

Address: https://pta.patest.cn/pta/test/558/exam/4/question/9495

Because the number of edges E <(n * (n-1)/2, I chose the adjacent table implementation. The priority queue is implemented using the cyclic queue;

DFS basic ideas:

1: select a vertex to indicate that the vertex has been accessed;

2: select the next vertex to determine whether other neighboring contacts of the vertex (the access sequence is from small to large) have been accessed;

3: Repeat 2nd to know that all vertices have been accessed.

The pseudocode is as follows:

DFS( Vertex v ){       Visit( V );     Visited[V] = true;     foreach(  v->neighbor )        if ( Visited[v->neighbor ] == false )                DFS(v->neighbor );}

BFS basic ideas:

1: select a point to join the team and visit this point, marking that the site has been visited;

2: Leave the team and place all the neighboring points not visited at this pointAccessAnd join the team;

3: Repeat the second point until the queue is empty;

The pseudocode is as follows:

BFS ( Vertex v ){       Visit( v );    Visited[v] = true;    EnQueue(Q, v);           while ( !IsEmpty(Q) )     {        w = DeQueue(Q);          foreach ( w->neighor )            if ( Visited[w->neighor] == false )             {                 Visit( w->neighor );                Visited[w->neighor] = true;                EnQueue(Q, w->neighor);            }    } }        

Code

1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <stdbool. h> 4 # include <string. h> 5 # include <dos. h> 6 7 # define MaxVertexNum 10 8 9 typedef int ElementType; 10 typedef int Position; 11 12 typedef struct QNode {13 ElementType * Data; /* array of storage elements */14 Position Front, Rear;/* queue header and tail pointer */15 int MaxSize;/* maximum queue capacity */16} QNode, * Queue; 17 18 19 typedef struct ENode 20 {21 int ivex; // The vertex position pointed to by the edge 22 struct ENode * next; 23} ENode, * PENode; 24 25 typedef int VertexType; 26 27 typedef struct VNode 28 {29 VertexType data; 30 ENode * first_edge; 31} VNode; 32 33 typedef struct LGraph 34 {35 int vexnum; // Number of vertices in the graph 36 int edgnum; // Number of edges in the graph 37 VNode * vexs; // array of vertex storage 38} LGraph; 39 40 bool TAG; // output format 41 bool visited [MaxVertexNum]; 42 43 LGraph * LGraph_new (); 44 void LGraph_destroy (LGraph * G); 45 void LGraph_insert (ENode ** head, int ivex); 46 void ResetVisit (); 47 void DFS (LGraph * G, int vex); 48 void BFS (LGraph * G, int vex); 49 void ListComponents (LGraph * G, void (* func) (LGraph * G, int vex )); 50 51 // basic operations of the priority Queue 52 Queue CreateQueue (int MaxSize); 53 bool IsFull (Queue Q); 54 bool AddQ (Queue Q, ElementType X ); 55 bool IsEmpty (Queue Q); 56 ElementType DeleteQ (Queue Q); 57 5 8 int main () 59 {60 LGraph * G = LGraph_new (); 61 ResetVisit (); 62 ListComponents (G, DFS); 63 ResetVisit (); 64 ListComponents (G, BFS); 65 system ("pause"); 66 return 0; 67} 68 69 void ListComponents (LGraph * G, void (* func) (LGraph * G, int vex )) 70 {71 int I; 72 for (I = 0; I <G-> vexnum; ++ I) 73 {74 if (visited [I] = false) 75 {76 (* func) (G, I); 77 printf ("} \ n"); 78 TAG = false; 79} 80} 8 1} 82 83 LGraph * LGraph_new () 84 {85 86 LGraph * G = (LGraph *) malloc (sizeof (LGraph); 87 scanf ("% d ", & G-> vexnum, & G-> edgnum); 88G-> vexs = (VNode *) malloc (G-> vexnum * sizeof (VNode); 89 int I, v1, v2; 90 for (I = 0; I <G-> vexnum; ++ I) 91 {92G-> vexs [I]. data = I; 93G-> vexs [I]. first_edge = NULL; 94} 95 for (I = 0; I <G-> edgnum; ++ I) 96 {97 scanf ("% d", & v1, & v2); 98 // because the vertex information is the vertex Coordinate 99 LGraph_insert (& G-> vexs [v1]. first_edge, v2); 100 LGraph_insert (& G-> vexs [v2]. first_edge, v1); 101} 102 return G; 103} 104 void ResetVisit () 105 {106 TAG = false; 107 memset (visited, false, sizeof (visited )); 109} 110 111 void LGraph_insert (ENode ** head, int ivex) 112 {113 ENode * pnew, * p1, * p2; 114 pnew = (ENode *) malloc (sizeof (ENode); 115 pnew-> ivex = ivex; 116 if (* head = NULL) 117 {118 * head = p New; 119 120 pnew-> next = NULL; 121} 122 else 123 {124 if (* head)-> ivex) // trouble with no header node 125 {126 pnew-> next = * head; 127 * head = pnew; 128} 129 else130 {131 for (p1 = * head, p2 = p1-> next; p2! = NULL; p1 = p2, p2 = p1-> next) 132 {133 if (p2-> ivex) 134 break; 135} 136 pnew-> next = p2; 137 p1-> next = pnew; 138} 139} 140 141 void DFS (LGraph * G, int vex) 142 {143 if (TAG = false) 145 {146 TAG = true; 147 printf ("{"); 148} 149 printf ("% d", vex); 150 visited [vex] = true; 151 ENode * temp = G-> vexs [vex]. first_edge; 152 while (temp! = NULL) 153 {154 if (visited [temp-> ivex] = false) 155 {156 DFS (G, temp-> ivex ); 157} 158 temp = temp-> next; 159} 160} 161 void BFS (LGraph * G, int vex) 162 {163 Queue Q = CreateQueue (G-> vexnum ); 164 if (TAG = false) 165 {166 TAG = true; 167 printf ("{"); 168} 169 printf ("% d", vex ); 170 visited [vex] = true; 171 AddQ (Q, vex); 172 while (! IsEmpty (Q) 173 {174 vex = DeleteQ (Q); 175 ENode * temp = G-> vexs [vex]. first_edge; 176 while (temp! = NULL) 177 {178 if (visited [temp-> ivex] = false) 179 {180 printf ("% d", temp-> ivex ); 181 visited [temp-> ivex] = true; 182 AddQ (Q, temp-> ivex); 183} 184 temp = temp-> next; 185} 186} 187 free (Q-> Data); 188 free (Q); 189} 190 191 192 // Queue basic operation 193 Queue CreateQueue (int MaxSize) 194 {195 Queue Q = (Queue) malloc (sizeof (struct QNode); 196 Q-> Data = (ElementType *) malloc (MaxSize * sizeof (ElementType )); 197 Q-> Front = Q-> Rear = 0; 198 Q-> MaxSize = MaxSize; 199 return Q; 200} 201 202 bool IsFull (Queue Q) 203 {204 return (Q-> Rear + 1) % Q-> MaxSize = Q-> Front); 205} 206 207 bool AddQ (Queue Q, ElementType X) 208 {209 if (IsFull (Q) {210 printf ("queue full"); 211 return false; 212} 213 else {214 Q-> Rear = (Q-> Rear + 1) % Q-> MaxSize; 215 Q-> Data [Q-> Rear] = X; 216 return true; 217} 218} 219 220 bool IsEmpty (Queue Q) 221 {222 return (Q-> Front = Q-> Rear ); 223} 224 225 ElementType DeleteQ (Queue Q) 226 {227 if (IsEmpty (Q) {228 printf ("Queue empty"); 229 return 0; 230} 231 else {232 Q-> Front = (Q-> Front + 1) % Q-> MaxSize; 233 return Q-> Data [Q-> Front]; 234} 235}View Code

 

Related Article

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.