图的深度优先搜索(DFS)和图的广度优先搜索(BFS) 代码

图的深度优先搜索

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct node
 5 {
 6     int vertex;
 7     struct node* nextnode;
 8 };
 9 
10 typedef struct node* graph;
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         newnode = (graph)malloc(sizeof(struct node));
27         newnode->vertex = to;
28         newnode->nextnode = NULL;
29         ptr = &(head[from]);
30         while(ptr->nextnode != NULL)
31             ptr = ptr->nextnode;
32         ptr->nextnode = newnode;
33     }
34 }
35 
36 void dfs(int current)
37 {
38     graph ptr;
39     
40     visited[current] = 1;
41     printf("顶点[%d] ",current);
42     ptr = head[current].nextnode;
43     while(ptr != NULL)
44     {
45         if(visited[ptr->vertex] == 0)
46             dfs(ptr->vertex);
47         ptr = ptr->nextnode;
48     }
49 }
50 
51 int main()
52 {
53     graph ptr;
54     int node[20][2] = {
55         { 1, 2}, { 2, 1},
56         { 1, 3}, { 3, 1},
57         { 2, 4}, { 4, 2},
58         { 2, 5}, { 5, 2},
59         { 3, 6}, { 6, 3},
60         { 3, 7}, { 7, 3},
61         { 4, 8}, { 8, 4},
62         { 5, 8}, { 8, 5},
63         { 6, 8}, { 8, 6},
64         { 7, 8}, { 8, 7},
65     };
66     int i;
67     for(i = 1;i <= 8;i++)
68     {
69         head[i].vertex = i;
70         head[i].nextnode = NULL;
71         visited[i] = 0;
72     }
73     creategraph(node,20);
74     printf("图的邻接表内容:\n");
75     for(i = 1;i <= 8;i++)
76     {
77         printf("顶点%d =>",head[i].vertex);
78         ptr = head[i].nextnode;
79         while(ptr != NULL)
80         {
81             printf(" %d ",ptr->vertex);
82             ptr = ptr->nextnode;
83         }
84         printf("\n");
85     }
86     printf("图的深度优先遍历内容:\n");
87     dfs(1);
88     printf("\n");
89     return 0;
90 }

广度优先遍历

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define MAXQUEUE 10
  5 struct node
  6 {
  7     int vertex;
  8     struct node* nextnode;
  9 };
 10 
 11 typedef struct node* graph;
 12 struct node head[9];
 13 int visited[9];
 14 
 15 int queue[MAXQUEUE];
 16 int front = -1;
 17 int rear = -1;
 18 
 19 void creategraph(int* node,int num)
 20 {
 21     graph newnode;
 22     graph ptr;
 23     int from;
 24     int to;
 25     int i;
 26     
 27     for(i = 0;i < num;i++)
 28     {
 29         from = node[i*2];
 30         to = node[i*2+1];
 31         newnode = (graph)malloc(sizeof(struct node));
 32         newnode->vertex = to;
 33         newnode->nextnode = NULL;
 34         ptr = &(head[from]);
 35         while(ptr->nextnode != NULL)
 36             ptr = ptr->nextnode;
 37         ptr->nextnode = newnode;
 38     }
 39 }
 40 
 41 int enqueue(int value)
 42 {
 43     if(rear >= MAXQUEUE)
 44         return -1;
 45     rear++;
 46     queue[rear] = value;
 47 }
 48 
 49 int dequeue()
 50 {
 51     if(front == rear)
 52         return -1;
 53     front++;
 54     return queue[front];
 55 }
 56 
 57 void bfs(int current)
 58 {
 59     graph ptr;
 60     
 61     enqueue(current);
 62     visited[current] = 1;
 63     printf("顶点[%d] ",current);
 64     while(front != rear)
 65     {
 66         current = dequeue();
 67         ptr = head[current].nextnode;
 68         while(ptr != NULL)
 69         {
 70             if(visited[ptr->vertex] == 0)
 71             {
 72                 enqueue(ptr->vertex);
 73                 visited[ptr->vertex] = 1;
 74                 printf("顶点[%d] ",ptr->vertex);
 75             }
 76             ptr = ptr->nextnode;
 77         }
 78     }
 79 }
 80 
 81 int main()
 82 {
 83     graph ptr;
 84     int node[20][2] = {
 85         { 1, 2}, { 2, 1},
 86         { 1, 3}, { 3, 1},
 87         { 2, 4}, { 4, 2},
 88         { 2, 5}, { 5, 2},
 89         { 3, 6}, { 6, 3},
 90         { 3, 7}, { 7, 3},
 91         { 4, 8}, { 8, 4},
 92         { 5, 8}, { 8, 5},
 93         { 6, 8}, { 8, 6},
 94         { 7, 8}, { 8, 7},
 95     };
 96     int i;
 97     for(i = 1;i <= 8;i++)
 98     {
 99         head[i].vertex = i;
100         head[i].nextnode = NULL;
101         visited[i] = 0;
102     }
103     creategraph(node,20);
104     printf("图的邻接表内容:\n");
105     for(i = 1;i <= 8;i++)
106     {
107         printf("顶底%d =>",head[i].vertex);
108         ptr = head[i].nextnode;
109         while(ptr != NULL)
110         {
111             printf(" %d ",ptr->vertex);
112             ptr = ptr->nextnode;
113         }
114         printf("\n");
115     }
116     printf("图的广度优先遍历内容:\n");
117     bfs(1);
118     printf("\n");
119     return 0;
120 }

posted @ 2021-01-02 15:24  互联星空  阅读(343)  评论(0编辑  收藏  举报