1、/* graph1.h:, */int menu_netselect1() /* */char s8;int c;clrscr(); /* */printf(“n“);printf(“n“);printf(“ * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * :a1.txt; . i1.txt; * n“);printf(“ * :a2.txt; . i2.txt; * n“);printf(“ * :a3.txt; . i3.txt; * n“);printf(“ * * n“);printf(“ * n“
2、);printf(“n“);printf(“ 01:()n“);printf(“ 02:()n“);printf(“ 03:n“);printf(“ 04:(-)n“);printf(“ 05:(-)n“);printf(“ 06: DFS n“);printf(“ 07: BFS n“);printf(“ 08:()n“);printf(“ 09:()n“);printf(“ 10:()n“);printf(“ 11:n“);printf(“ 12:n“);do printf(“n“);printf(“ :1-12: “);gets(s);c=atoi(s);while(c12);retur
3、n(c);int LocateVex1(ALGraph G,VertexType u) /* Gu-1 */int i;for(i=1;ivex;+i) /* */p=G-veri.first;while(p)q=p-next;free(p);p=q;G-vex=0; /* 0 */G-arc=0; /* 0 */void setlistunnetwork1(ALGraph *G) /* */int i,j,k,w,l=0; /* w */VertexType va,vb; /* 2 */LNode *p;char str13;FILE *fp;clrscr(); /* */printf(“n
4、“);printf(“n“);printf(“ * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * () * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * n“);printf(“n“);printf(“n“);printf(“ () : “); gets(str);fp=fopen(str,“r“);while(fp=NULL) l+;if(l=3)printf(“n“);printf(“n“);printf(“n“);printf(“n“);printf(
5、“ * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * n“);printf(“n“);printf(“n“);scanf(“%*c“);exit(0);printf(“ %s !n“,str);printf(“ () : “); gets(str);fp=fopen(str,“r“);fscanf(fp,“%d“,fscanf(fp,“%d“,for(i=1;ivex;i+)
6、 /* */fscanf(fp,“%s“,G-veri.data);G-veri.first=NULL; /* */for(k=1;karc;k+) /* */fscanf(fp,“%s%s%d“,va,vb,i=LocateVex1(*G,va); /* , */j=LocateVex1(*G,vb); /* , */p=(LNode *)malloc(sizeof(LNode); /* */p-wig=w; /* s */p-adj=j; /* */p-next=G-veri.first;G-veri.first=p;p=(LNode *)malloc(sizeof(LNode); /*
7、*/p-wig=w; /* s */p-adj=i; /* */p-next=G-verj.first;G-verj.first=p;printf(“n %s !n“,str);fclose(fp); /* */printf(“nn“);printf(“n“);scanf(“%*c“);void setlistnetwork1(ALGraph *G) /* */int i,j,k,w,l=0; /* w */VertexType va,vb; /* 2 */LNode *p;char str13;FILE *fp;clrscr(); /* */printf(“n“);printf(“n“);p
8、rintf(“ * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * () * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * n“);printf(“n“);printf(“n“);printf(“ () : “); gets(str);fp=fopen(str,“r“);while(fp=NULL) l+;if(l=3)printf(“n“);printf(“n“);printf(“n“);printf(“n“);printf(“ * n“);printf(“
9、 * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * * n“);printf(“ * n“);printf(“n“);printf(“n“);scanf(“%*c“);exit(0);printf(“ %s !n“,str);printf(“ () : “); gets(str);fp=fopen(str,“r“);fscanf(fp,“%d“,fscanf(fp,“%d“,for(i=1;ivex;+i) /* */fscanf(fp,
10、“%s“,G-veri.data);G-veri.first=NULL; /* */for(k=1;karc;+k) /* */fscanf(fp,“%s%s%d“,va,vb,i=LocateVex1(*G,va); /* , */j=LocateVex1(*G,vb); /* , */p=(LNode *)malloc(sizeof(LNode); /* */p-wig=w; /* s */p-adj=j; /* */p-next=G-veri.first;G-veri.first=p;printf(“n %s !n“,str);fclose(fp); /* */printf(“nn“);
11、printf(“n“);scanf(“%*c“);void printetwork1(ALGraph G) /* G */int i;Link p;printf(“%dn“,G.vex);printf(“);for(i=1;iadj,p-wig);p=p-next;printf(“n“);printf(“n“);/*/int LocateElem1(Link L,ElemTypes e,int(*compare)(ElemTypes,ElemTypes) /* L1ecompare() */int i=0;Link p=L;while(p)i+;if(compare(*p,e) /* */re
12、turn i;p=p-next;return 0;Link Point1(Link L,ElemTypes e,int(*equal)(ElemTypes,ElemTypes),Link *p) /* L*/int i,j;i=LocateElem1(L,e,equal);if(i) /* */if(i=1) /* */* p=NULL;return L;* p=L;for(j=2;jnext;return (*p)-next;return NULL; /* */int FirstAdjVex1(ALGraph G,VertexType v) /* v */Link p;int v1;v1=L
13、ocateVex1(G,v); /* v1vG */p=G.verv1.first;if(p) return p-adj;else return -1;int equalvex1(ElemTypes a,ElemTypes b) /* DeleteArc()DeleteVex()NextAdjVex() */if(a.adj=b.adj) return 1;else return 0;int NextAdjVex1(ALGraph G,VertexType v,VertexType w) /* v(w) */LNode *p,*p1;ElemTypes e;int v1;v1=LocateVe
14、x1(G,v); /* v1vG */e.adj=LocateVex1(G,w); /* e.adjwG */p=Point1(G.verv1.first,e,equalvex1, /* pvw */if(!p|!p-next) /* ww */return -1;else return p-next-adj; /* v(w) */void PrintBrother1(CSTree bt,int i) /*/if(bt!=NULL)PrintBrother1(bt-nextbroths,i+5);printf(“%*sn“,i,bt-data);PrintBrother1(bt-firstch
15、ild,i+5);i=i-5;void DFSTree1(ALGraph G,int i,CSTree *T) /* vGT */int j,first=1;CSTree p,q;visitedi=1;for(j=FirstAdjVex1(G,G.veri.data);j=0;j=NextAdjVex1(G,G.veri.data,G.verj.data)/* wv */if(!visitedj) /* j */p=(CSTree)malloc(sizeof(CSNode); /* */strcpy(p-data,G.verj.data);p-firstchild=NULL;p-nextbro
16、ths=NULL;if(first) /* ji */(*T)-firstchild=p;first=0; /* */else /* ji */q-nextbroths=p; /* (1q)*/q=p;DFSTree1(G,j, /* jGq */void DFSForest1(ALGraph G,CSTree *T) /* G-()()T */CSTree p,q;int v;* T=NULL;for(v=1;vdata,G.verv.data);p-firstchild=NULL;p-nextbroths=NULL;if(!*T) /* (T) */* T=p;else /* () */q
17、-nextbroths=p; /* 1q */q=p; /* q */DFSTree1(G,v, /* p */void PreOrderTraverse1(CSTree T) /* -T */if(T)printf(“%3s “,T-data); /* */PreOrderTraverse1(T-firstchild); /* */PreOrderTraverse1(T-nextbroths); /* */void IntOrderTraverse(CSTree T) /* -T */if(T)IntOrderTraverse(T-firstchild); /* */printf(“%3s
18、“,T-data); /* */IntOrderTraverse(T-nextbroths); /* */void PosOrderTraverse(CSTree T) /* -T */if(T)PosOrderTraverse(T-firstchild); /* */PosOrderTraverse(T-nextbroths); /* */printf(“%3s “,T-data); /* */void FindInDegree1(ALGraph G,int indegree) /* */int i;LNode *p;for(i=1;iadj+;p=p-next;int Topologica
19、lOrder1(ALGraph G,int T,int *topt) /* ve()T */int i,k,count=0; /* 0 */int indegreeMAX_VER; /* */int tops,SMAX_VER;LNode *p;FindInDegree1(G,indegree); /* indegree */tops=0; /* */printf(“: “);for(i=1;i0) /* S */i=Stops-1; tops-; /* Sj */printf(“%s “,G.veri.data);T*topt=i; (*topt)+; /* jT(1) */+count;
20、/* T */for(p=G.veri.first;p;p=p-next) /* i */k=p-adj; /* k */if(-indegreek=0) /* k10kS */Stops=k; tops+;if(vei+p-wigvek)vek=vei+p-wig; /* ki */ /* .ivei*/if(countj)j=vei; /* j=Max(ve) */for(i=1;i0) /* vl */j=Ttopt-1; topt-;for(p=G.verj.first;p;p=p-next) /* T,j,pjk,k()*/k=p-adj;dut=p-wig; /* dut= */i
21、f(vlk-dutkvlk */printf(“nnve :n“);for(i=1;inext)k=p-adj;dut=p-wig; /* dut= */ee=vej; /* ee=(j) */el=vlk-dut; /* el=(j) */printf(“%s%s %2d ee(%s%s) %3d“,G.verj.data,G.verk.data,dut,G.verj.data,G.verk.data,ee);printf(“n“);printf(“n.“);getch();printf(“nnel :n“);printf(“n el( ) n“);for(j=1;jnext)k=p-adj
22、;dut=p-wig; /* dut= */ee=vej; /* ee=(j) */el=vlk-dut; /* el=(j) */printf(“%s%s %2d el(%s%s) %3d“,G.verj.data,G.verk.data,dut,G.verj.data,G.verk.data,el);printf(“n“);printf(“n.“);getch();printf(“nnee :; el :.n“);printf(“n ee eln“);for(j=1;jnext)k=p-adj;dut=p-wig; /* dut= */ee=vej; /* ee=(j) */el=vlk-
23、dut; /* el=(j) */printf(“%s%s %2d %3d %3d “,G.verj.data,G.verk.data,dut,ee,el);/* */if(ee=el) /* */printf(“!“);printf(“n“);void TopologicalSort1(ALGraph G) /* GG*/int i,k,count=0; /* 0 */int indegreeMAX_VER; /* */int top,StackMAX_VER;LNode *p;FindInDegree1(G,indegree); /* indegree */top=0; /* S */fo
24、r(i=0;i0) /* S */i=Stacktop-1; top-;printf(“%s “,G.veri.data); /* i */+count; /* +1 */for(p=G.veri.first;p;p=p-next) /* i */k=p-adj; /* k */if(!(-indegreek) /* k1,0,kS */Stacktop=k; top+;if(countadj;if(!visitedj) dfs11(G,j);p=p-next;void dfs121(ALGraph G,int i) /* () */int j,top;LNode *p,*stackMAX_V
25、ER;printf(“%3s,“,G.veri.data);visitedi=1;top=0;p=G.veri.first;while(top!=0 | p!=NULL)while(p!=NULL)j=p-adj;if(p!=NULL else printf(“%3s,“,G.verj.data);visitedj=1;stacktop=p; top+;p=G.verj.first;if(top!=0)p=stacktop-1; top-;p=p-next;void dfs111(ALGraph G,int i) /* () */int j,count;count=0;for(j=i;jadj
26、;if(!visitedj)printf(“,“);dfss11(G,j);p=p-next;void dfss111(ALGraph G,int i) /* () */int j,count;count=0;for(j=i;jadj;if(!visitedj)printf(“%3s,“,G.verj.data);visitedj=1;queuerear=j; rear+;p=p-next;void bfs111(ALGraph G,int i) /* () */int j,count;count=0;for(j=i;jadj;if(!visitedj)printf(“,“);visitedj=1;queuerear=j; rear+;p=p-next;void bfss111(ALGraph G,int i) /* () */int j,count;count=0;for(j=i;j=G.vex;j+)if(!visitedj) /* */count=count+1;printf(“: %d :“,count);bfss11(G,j); /* dfs1(G,j); */printf(“n“);for(j=1;j=G.vex;j+)if(!visitedj)count=count+1;printf(“: %d :“,count);bfss11(G,j);printf(“n“);