图的基本遍历算法的实现(BFS & DFS)复习

#include <stdio.h>

#define INF 32767
typedef struct MGraph{
    char vertexs[10];
    int edge[10][10];
    int ver_num, edge_num;
}MGraph;

void create_graph(MGraph *graph)
{
    int i, j;
    getchar();
    for(i = 0; i < graph->ver_num; ++i)
        scanf("%c%*c",&(graph->vertexs[i]));

    for(i = 0; i < graph->ver_num; ++i)
    {
        for(j = 0; j < graph->ver_num; ++j)
        {
            if(i == j)
                graph->edge[i][i] = 0;
            else
                graph->edge[i][j] = INF;
        }
    }
    int vi, vj, val;
    for(i = 0; i < graph->edge_num; ++i)
    {
        scanf("%d %d %d", &vi, &vj, &val);
        graph->edge[vi][vj] = graph->edge[vj][vi] = val;
    }
}

void print_graph(MGraph graph)
{
    int i,j;
    for(i = 0; i < graph.ver_num; ++i)
    {
        for(j = 0; j < graph.ver_num; ++j)
        {
            printf("%d\t", graph.edge[i][j]);
        }
        puts("");
    }
}

int vis[10];
void dfs(MGraph graph, int vertex)
{
    int i;
    vis[vertex] = 1;         //将该结点标记为访问过
    printf("%d -> %c\t", vertex, graph.vertexs[vertex]);    //打印结点信息,当然也可以是其他操作

    for(i = 0; i < graph.ver_num; ++i)
    {   //从vertex 到 i 有路径存在,而且 i 结点未被访问过
        if(graph.edge[vertex][i] > 0 && graph.edge[vertex][i] != INF && !vis[i])
            dfs(graph, i);   //进行dfs遍历
    }
}

void dfstraverse(MGraph graph)
{
    int i;
    for(i = 0; i < graph.ver_num; ++i)  //将所有结点标记为未访问过
        vis[i] = 0;
    for(i = 0; i < graph.ver_num; ++i)  //从每个未被访问过的结点开始进行dfs遍历
        if(!vis[i])
            dfs(graph, i);
}

typedef struct Queue{
    int store[1000];
    int front, rear;
}Queue;

void InitQueue(Queue *queue)
{
    queue->front = queue->rear = 0;
}
void EnQueue(Queue *queue , int i)
{
    if(queue->rear == 1000)
        puts("the queue is full!!");
    else{
        queue->store[queue->rear] = i;
        queue->rear++;
    }
}

int DeQueue(Queue *queue)
{
    if(queue->rear == queue->front)
        printf("the queue is empty!!");
    else{
        queue->front++;
        return queue->store[queue->front-1];
    }
}

Queue queue; //定义队列
void bfs(MGraph graph, int vertex)
{
    int i, tmp;
    vis[vertex] = 1;
    printf("%d -> %c\t",vertex, graph.vertexs[vertex]);
    EnQueue(&queue, vertex);

    while(queue.front != queue.rear)
    {
        tmp = DeQueue(&queue);
        for(i = 0; i < graph.ver_num; ++i)
        {
            if(graph.edge[tmp][i] > 0 && graph.edge[tmp][i] != INF && !vis[i])
            {
                vis[i] = 1;
                printf("%d -> %c\t", i, graph.vertexs[i]);
                EnQueue(&queue, i);
            }
        }
    }
}

void bfstraverse(MGraph graph)
{
    int i;
    InitQueue(&queue);
    for(i = 0; i < graph.ver_num; ++i)
        vis[i] = 0;
    for(i = 0; i < graph.ver_num; ++i)
        if(!vis[i])
            bfs(graph, i);
}

int main()
{
    MGraph graph;
    scanf("%d %d", &(graph.ver_num), &(graph.edge_num));
    create_graph(&graph);
    print_graph(graph);
    dfstraverse(graph);
    puts("");
    bfstraverse(graph);
    return 0;
}
/*
4 4
0 1 2 3
0 1 9
1 2 8
2 3 7
3 0 6
 * */
基于邻接矩阵的 DFS & BFS

 

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 typedef struct ArcNode{
  6     int adjVex;             //邻接点域
  7     struct ArcNode *next;   //指针域
  8 }ArcNode;                   //边表
  9 
 10 typedef struct VertexNode{
 11     int data;               //顶点域
 12     struct VertexNode *firstArc;//边表头指针
 13 }VertexNode, AdjList[10000];    //顶点表
 14 
 15 typedef struct {
 16     AdjList adjlist;
 17     int ArcNum, VertexNum;      //图中的顶点数和边数
 18 }AdjListGraph;
 19 /*
 20  * 创建的是无向图,采用头插法建立边表
 21  * */
 22 int create_graph(AdjListGraph *graph)
 23 {
 24     int i, j;
 25     for(i = 0; i < graph->VertexNum; ++i)
 26     {
 27         graph->adjlist[i].data = i;
 28         graph->adjlist[i].firstArc = NULL;
 29     }
 30     int m, n, val;
 31     for(i = 0; i < graph->ArcNum; ++i)
 32     {
 33         puts("the every arc");
 34         scanf("%d %d", &m, &n);
 35         ArcNode *tmp1 = (ArcNode*)malloc(sizeof(ArcNode));
 36         if(tmp1 == NULL)
 37             return 0;
 38         tmp1->adjVex = n;
 39         tmp1->next = graph->adjlist[m].firstArc;
 40         graph->adjlist[m].firstArc = tmp1;
 41 
 42         ArcNode *tmp2 = (ArcNode*)malloc(sizeof(ArcNode));
 43         if(tmp2 == NULL)
 44             return 0;
 45         tmp2->adjVex = m;
 46         tmp2->next = graph->adjlist[n].firstArc;
 47         graph->adjlist[n].firstArc = tmp2;
 48     }
 49     return 1;
 50 }
 51 int vis[10000];
 52 void dfs(AdjListGraph *graph, int vertex)
 53 {
 54     ArcNode *p;
 55     vis[vertex] = 1;
 56     printf("%d\t", graph->adjlist[vertex].data);
 57     p = graph->adjlist[vertex].firstArc;
 58 
 59     while(p)
 60     {
 61         if(!vis[p->adjVex])
 62             dfs(graph, p->adjVex);
 63         p = p->next;
 64     }
 65 }
 66 void dfstreverse(AdjListGraph *graph)
 67 {
 68     int i;
 69     for(i = 0; i < graph->VertexNum; ++i)
 70     {
 71         vis[i] = 0;
 72     }
 73     for(i = 0; i < graph->VertexNum; ++i)
 74     {
 75         if(!vis[i])
 76             dfs(graph, i);
 77     }
 78 }
 79 
 80 typedef struct Queue{
 81     int store[1000];
 82     int front, rear;
 83 }Queue;
 84 
 85 void InitQueue(Queue *queue)
 86 {
 87     queue->front = queue->rear=0;
 88 }
 89 void EnQueue(Queue *queue , int i)
 90 {
 91     if(queue->rear == 1000)
 92         puts("the queue is full!!");
 93     else{
 94         queue->store[queue->rear] = i;
 95         queue->rear++;
 96     }
 97 }
 98 
 99 int DeQueue(Queue *queue)
100 {
101     if(queue->rear == queue->front)
102         printf("the queue is empty!!");
103     else{
104         queue->front++;
105         return queue->store[queue->front-1];
106     }
107 }
108 
109 Queue queue;
110 void bfs(AdjListGraph *graph, int vertex)
111 {
112     int i, tmp;
113     ArcNode *p;
114     vis[vertex] = 1;
115     printf("%d\t", graph->adjlist[vertex].data);
116 
117     EnQueue(&queue, vertex);
118     while(queue.front != queue.rear)
119     {
120         tmp = DeQueue(&queue);
121         p = graph->adjlist[tmp].firstArc;
122         while(p)
123         {
124             if(!vis[p->adjVex])
125             {
126                 vis[p->adjVex] = 1;
127                 printf("%d\t", graph->adjlist[p->adjVex].data);
128                 EnQueue(&queue, p->adjVex);
129             }
130             p = p->next;
131         }
132 
133     }
134 }
135 
136 void bfstreverse(AdjListGraph *graph)
137 {
138 
139     InitQueue(&queue);
140     int i;
141     for(i = 0; i < graph->VertexNum; ++i)
142         vis[i] = 0;
143     for(i = 0; i < graph->VertexNum; ++i)
144         if(!vis[i])
145             bfs(graph, i);
146 }
147 int main()
148 {
149     AdjListGraph graph;
150     int arc_num, ver_num;
151     puts("input the arc_num & ver_num\n");
152     scanf("%d %d", &arc_num, &ver_num);
153     graph.ArcNum = arc_num;
154     graph.VertexNum = ver_num;
155     create_graph(&graph);
156     dfstreverse(&graph);
157     bfstreverse(&graph);
158     return 0;
159 }
基于邻接表的 DFS & BFS

 

手写又不熟了,多多看,多多练!

posted @ 2016-11-20 18:03  unicoe  阅读(224)  评论(0编辑  收藏  举报