The previous article "BFs and Dfs" finished, suddenly realized that this might deviate from the "data structure" theme, so come back to introduce a storage: adjacency table and adjacency matrix.
There are two ways to save a diagram, the adjacency matrix is strictly a bool type of two-dimensional array, map[i][j] means I to J there is no one-way edge, the adjacency table is to 1~n each point in a linked list, the list e[i] Each point j is stored in a one-way edge of I to J. There are pros and cons in both of these ways, and in sparse graphs, adjacency tables are better, save time and save space; in dense graphs, adjacency matrices are better, without wasting time and eliminating the space of the pointer domain.
When actually writing the code, for the adjacency matrix, we may consider using the adjacency matrix of int to express the weight of the edge at the same time, depending on the situation; for adjacency tables, we can actually assign a one-dimensional array as the header when we pull out a linked list for each point, to simplify the code when the edges are deleted. At the same time, it is convenient to save the information of each point, as well as to use pointers as the header in the code of this article, save some space.
This article only gives the relative basic code, the information on the edge only has a weight, presumably this is enough. If information increases, you can add information in the same location. In addition, the critical table in many cases can use static memory instead of dynamic memory, this method of this article does not repeat the code, the method is described in the article "Linear table".
Attention! For adjacency table and adjacency matrix, I did not try to write with the class, here just give a very ugly class version of the code, not for everyone to reference, but instead, I hope that a master can give a better class version of the implementation code, greatly appreciated!
Refreshing version:
Const intMAXN =10000;//adjacency Matrixstructedge{BOOLP//p indicates that the edge is not available, or it can be substituted with a value that cannot be taken in a topic by C instead of the function of P intC; Edge ():p (false) {}}MAP[MAXN+1][maxn+1];voidClear () { for(intI=1; i<=maxn;++i) for(intj=1; j<=maxn;++j) map[i][j].p=false;}voidAddedge (intUintVintc) {MAP[U][V].P=true; Map[u][v].c=C;}voidDeledge (intUintv) {MAP[U][V].P=false;}//adjacency Tablestructedge{intv; intC; Edge*Next; Edge (int_v=0,int_c=0): V (_v), C (_c) {}}*e[maxn+1];//global definition, the initial is 0, if the local definition, you should first clear 0voidClear () {Edge*p; for(intI=1; i<=maxn;++i) while(E[i]) {p=e[i]; E[i]=p->Next; Delete p; }}voidAddedge (intUintVintc) {Edge*p=NewEdge (V,C); P->next=e[u]; e[u]=p;}voidDeledge (intUintv) { if(E[U]->V==V) {e[u]=e[u]->next;return; } for(Edge *p=e[u],*q=p->next;q;p=q,q=p->next)if(q->v==v) {p->next=q->Next; Delete q; return;//If there is a heavy edge, it should not be returned here, and should be returned after the loop }}
Class Edition:
//adjacency Tablestructedge{intv; intC; Edge*Next; Edge (int_v=0,int_c=0): V (_v), C (_c) {}};classmap{Static Const intMAXN =10000; Edge*e[maxn+1]; Public: Map () { for(intI=1; i<=maxn;++i) e[i]=0; } voidClear () {Edge*p; for(intI=1; i<=maxn;++i) while(E[i]) {p=e[i]; E[i]=p->Next; Delete p; } } voidAddintUintVintc) {Edge*p=NewEdge (V,C); P->next=e[u]; e[u]=p; } voidDelintUintv) {if(E[U]->V==V) {e[u]=e[u]->next;return; } for(Edge *p=e[u],*q=p->next;q;p=q,q=p->next)if(q->v==v) {p->next=q->Next; Delete q; return;//If there is a heavy edge, it should not be returned here, and should be returned after the loop}} Edge* BEGIN (intu) {returne[u];} Edge* Next (Edge *p) {returnP->Next;}} G;
"Data Structure" C + + code adjacency table and adjacency matrix