博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

图的深度优先搜索

Posted on 2012-10-19 17:30  皇星客栈--Linux  阅读(188)  评论(0编辑  收藏  举报

这个写法我认为精简

View Code
 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #include<stdlib.h>
 4 
 5 typedef struct node
 6 {
 7     int vertex;
 8     struct node *nextnode;
 9 }*graph,Linknode;
10 
11 struct node head[9];
12 int visited[9];
13 
14 void creategraph( int *node,int num )
15 {
16     graph newnode;
17     graph ptr;
18     int from;
19     int to;
20     int i;
21     
22     for( i=0; i<num; i++ )
23     {
24         from = node[i*2];
25         to = node[i*2+1];
26         
27         newnode = ( graph ) malloc( sizeof( Linknode ) );
28         newnode -> vertex = to;
29         newnode -> nextnode = NULL;
30         
31         ptr = &(head[from]);
32         while( ptr->nextnode!=NULL )
33             ptr = ptr -> nextnode;
34         ptr->nextnode = newnode;
35     }
36 }
37         
38 void DFS ( int current )
39 {
40     graph ptr;
41     visited[current] = 1;
42     printf("顶点[%d] ",current);
43      ptr = head[current].nextnode;
44     while( ptr!=NULL )
45     {
46         if( visited[ptr->vertex]!=1 )
47             DFS(ptr->vertex);
48         ptr = ptr -> nextnode;
49     }    
50 }    
51     
52 int main( )
53 {
54     graph ptr;    
55     int i;
56     
57     int node[20][2]={                                                        
58        {1,2},{2,1},
59        {1,3},{3,1},
60        {2,4},{4,2},
61        {2,5},{5,2},
62        {3,6},{6,3},
63        {3,7},{7,3},
64        {4,8},{8,4},
65        {5,8},{8,5},
66        {6,8},{8,6},
67        {7,8},{8,7}  
68     };
69 
70     for( i=1; i<=8; i++ )
71     {
72         head[i].vertex = i;
73         head[i].nextnode = NULL;
74         visited[i] = 0;
75     }
76     
77     creategraph(*node,20);
78     
79     printf("图的邻接表内容:\n");
80     for( i=1; i<=8; i++ )
81     {
82         ptr = head[i].nextnode;
83         printf("[%d]:",i);
84         while( ptr != NULL )
85         {
86             printf("%d ",ptr->vertex);
87             ptr=ptr->nextnode;
88         }
89         printf("\n");
90     }
91     printf("\n深度优先搜索遍历的结果为:\n");     
92     DFS( 1 );
93     printf("\n\n");
94     system("pause");
95     return 0;    
96 }

 这个写法比较粗糙

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<malloc.h>
 4 
 5 typedef int  VertexType;
 6 #define MAX_VERTEX_NUM 20
 7 
 8 typedef struct ArcNode{
 9     int                data;
10     struct ArcNode *nextarc;
11 }ArcNode;
12 
13 typedef struct VNode{
14     VertexType data;
15     ArcNode *fistarc;
16 }VNode,AdjList[MAX_VERTEX_NUM];
17 
18 typedef struct{
19     AdjList vertices;
20     int vexnum,arcnum;
21 }ALGraph;
22 
23 typedef enum{FALSE,TRUE} Boolean;
24 Boolean visited[MAX_VERTEX_NUM];
25 
26 void CreatALGraph( ALGraph *G )
27 {
28     int i;
29     int j;
30     int k;
31     ArcNode *S;
32     printf("请输入顶点数和边数:\n");
33     scanf("%d %d",&G->vexnum,&G->arcnum);
34     printf("请输入定点编号:\n"); 
35     for( i=1; i<=G->vexnum; i++ )
36     {
37         scanf("%d",&G->vertices[i].data);
38         G->vertices[i].fistarc = NULL;
39     }
40     printf("请输入由两个顶点构成的边,如(0,1)\n");
41     for( k=0; k<G->arcnum; k++ )
42     {
43         scanf("%d %d",&i,&j);       
44         S = (ArcNode *)malloc(sizeof(ArcNode));
45         S->data = j;
46         S->nextarc = G->vertices[i].fistarc;
47         G->vertices[i].fistarc = S;
48         S = (ArcNode *)malloc(sizeof(ArcNode));
49         S->data = i;
50         S->nextarc = G->vertices[j].fistarc;
51         G->vertices[j].fistarc = S;
52     }          
53 }  
54 
55 void DFSM( ALGraph *G, int i )
56 {
57     ArcNode *p;
58     printf("%d\n",G->vertices[i].data);
59     visited[i] = TRUE;
60     p = G->vertices[i].fistarc;
61     while( p ) {
62         if( !visited[p->data] )
63             DFSM(G,p->data);
64     p = p->nextarc;
65   }
66 }
67 
68 void DFS( ALGraph *G )
69 {
70     for( int i=1; i<=G->vexnum; i++ )
71         visited[i] = FALSE;
72     for( int i=1; i<=G->vexnum; i++ )
73     {
74         if( !visited[i])
75             DFSM(G,i);
76     }
77 }    
78         
79 int main( )
80 {
81     ALGraph *G;
82     G = (ALGraph *)malloc(sizeof(ALGraph));
83     CreatALGraph(G);
84     printf("遍历的结果与构造的顺序有关!\n");
85     DFS(G);
86     system("pause");
87     return 0;
88 }