DFS搜索,用邻接表存储图

  1 //DFSTraverse.cpp
2 //This function is to traver ALGraph by DFS Algorithm
3 # include <iostream>
4 # include <malloc.h>
5 # include <conio.h>
6
7 # define MAX_VERTEX_NUM 20
8 # define OK 1
9
10 using namespace std;
11
12 typedef int VertexType;
13 typedef int InfoType;
14
15 typedef struct ArcNode //define structure ALGraph
16 { int adjvex;
17 struct ArcNode *nextarc;
18 InfoType *info;
19 }ArcNode;
20
21 typedef struct VNode
22 { VertexType data;
23 ArcNode *firstarc;
24 }VNode,AdjList[MAX_VERTEX_NUM];
25
26 typedef struct
27 { AdjList vertices;
28 int vexnum,arcnum;
29 int kind;
30 }ALGraph;
31
32 int CreateDG(ALGraph &G) //CreateDG() sub-fuction
33 { int IncInfo,i,j,k,v1,v2,w;
34 cout<<endl<<"Please input the number of G.vexnum (eg. G.vexnum=4): ";
35 cin>>G.vexnum; //input the number of vex
36 cout<<"Please input the number of G.arcnum (eg. G.arcnum=4): ";
37 cin>>G.arcnum; //input the number of arc
38 cout<<"Please input the number of IncInfo (0 for none) : ";
39 cin>>IncInfo;
40 for(i=0;i<G.vexnum;++i) //initial G.vertices
41 { G.vertices[i].data=i;
42 G.vertices[i].firstarc=NULL;
43 }
44 cout<<"Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...";
45 for(k=0;k<G.arcnum;++k) //input arc(v1,v2)
46 { cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum): ";
47 cin>>v1;
48 cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
49 cin>>v2;
50 i=v1;
51 j=v2;
52 while(i<1||i>G.vexnum||j<1||j>G.vexnum) //if (v1,v2) illegal,again
53 { cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum) : ";
54 cin>>v1;
55 cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
56 cin>>v2;
57 i=v1;
58 j=v2;
59 } //while end
60 i--;
61 j--;
62 ArcNode *p;
63 p=(ArcNode *)malloc(sizeof(ArcNode)); //allocate memory
64 if(!p)
65 { cout<<"Overflow!"; //if overflow
66 return (0);
67 }
68 p->adjvex=j; //assign p
69 p->nextarc=G.vertices[i].firstarc;
70 p->info=NULL;
71 G.vertices[i].firstarc=p;
72 if(IncInfo)
73 { cout<<"Please input the info :";
74 cin>>*(p->info); //input information
75 } //if end
76 } //for end
77 return (OK);
78 } //CreateDG() end
79
80 void DFS(ALGraph G,int v,int *visited) //DFS() sub-fuction
81 { int w;
82 visited[v]=1;
83 cout<<v+1<<"->";
84 ArcNode *p;
85 for(p = G.vertices[v].firstarc;p;p = p ->nextarc)
86
87 {
88
89 w = p->adjvex;
90 if(!visited[w]) DFS(G,w,visited);
91
92 }
93 //call DFS()
94 } //DFS() end
95
96 void DFSTraverse(ALGraph G) //DFSTraverse() sub-function
97 { int v;
98 int visited[MAX_VERTEX_NUM];
99 for(v=0;v<G.vexnum;++v)
100 visited[v]=0; //initial visited[v]
101 for(v=0;v<G.vexnum;++v)
102 if(visited[v]==0)
103 DFS(G,v,visited); //call DFS()
104 } //DFSTraverse() end
105
106 void main() //main() function
107 { ALGraph G;
108 cout<<endl<<endl<<"DFSTraverse.cpp";
109 cout<<endl<<"==============="<<endl;
110 CreateDG(G); //call CreateDG()
111 cout<<"DFS Traverse is as follows :";
112 cout<<endl<<endl<<"Begin->";
113 DFSTraverse(G); //call DFSTraverse()
114 cout<<"End !"<<endl<<endl<<"...OK!...";
115 getch();
116 } //main() end

posted @ 2012-02-22 14:52  uniquews  阅读(1372)  评论(0编辑  收藏  举报