图的邻接表存储,以及广度深度遍历

graph.h

#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
/**< 图的邻接矩阵实现 */

#define  MAXVERTEX  100
#define  INFINITY    65535

typedef  char  Vertex;

struct  graph;
typedef  struct  graph  *Graph;
struct  graph
{
    Vertex vertex[MAXVERTEX];
    int  numvertex, numedge;
    int  weight[MAXVERTEX][MAXVERTEX];
};

int visited[MAXVERTEX];
Graph  CreatGraph(int numvertex,int numedge);
void  PrintGraph(Graph G);
void  BreadTraverseGraph(Graph G);
void  DeepTraverseGraph(Graph G);
void  DFS(Graph G,int i);


#endif // GRAPH_H_INCLUDED

graph.c

#include  <stdio.h>
#include  <malloc.h>
#include  "graph.h"
#include  "queue.c"

Graph  CreatGraph(int  numvertex,int numedge)
{
    int  i,j,k,w;
    i=j=0;
    Graph G=(Graph)malloc(sizeof(struct graph));
    if(G==NULL)
        printf("Error: out of space!!!");

     G->numvertex=numvertex;
     G->numedge =numedge;

     printf("input the vertex of graph:\n");
     for(i=0;i <G->numvertex;i++)
        G->vertex[i]=getchar();

    /**< initalize */
    for(i=0;i <G->numvertex;i++)
        for(j=0;j<G->numvertex;j++)
        G->weight[i][j]=INFINITY;

     for(k=0;k <G->numedge;k++)
     {
         printf("输入边的下标和权值:\n");
         scanf("%d %d %d",&i,&j,&w);
         G->weight[i][j]=w;
         G->weight[j][i]=w;
     }

     return  G;
}


void  PrintGraph(Graph G)
{
    int i,j;
    for(i=0;i <G->numvertex;i++)
    {
         for(j=0;j<G->numvertex;j++)
           {
                if(G->weight[i][j]==INFINITY)
                        printf("no\t");
                else
                        printf("%d\t",G->weight[i][j]);
           }
           printf("\n");
    }
}


void  BreadTraverseGraph(Graph G)
{
    int i,j;
    Queue Q;

    for(i=0; i<G->numvertex;i++)
        visited[i]=0;

    Q=CreatQueue(G->numvertex);

    for(i=0;i<G->numvertex;i++)
    {
        if(!visited[i])
           {
                visited[i]=1;
                printf("%c\t",G->vertex[i]);
                Q=EnQueue(i,Q);

                while(!IsEmpty(Q))
                {
                    Q=DeQueue(Q);
                    for(j=0;j <G->numvertex;j++)
                    {
                        if(!visited[j] && G->weight[i][j]!=INFINITY)
                        {
                            visited[j]=1;
                            printf("%c\t",G->vertex[j]);
                            Q=EnQueue(j,Q);
                        }
                    }
                }
           }

    }

}


void  DeepTraverseGraph(Graph G)
{
    int i;
    for(i=0; i<G->numvertex;i++)
        visited[i]=0;

     for(i=0; i<G->numvertex;i++)
        if(!visited[i])
          DFS(G,i);
}



void  DFS(Graph G,int i)
{
    int j;
    visited[i]=1;
    printf("%c \t",G->vertex[i]);

    for(j=0;j<G->numvertex;j++)
        if(!visited[j])
           DFS(G,j);

}

main.c

#include <stdio.h>
#include <stdlib.h>
#include  "graph.c"


int main()
{
    Graph G=(Graph)malloc(sizeof(struct graph));

    G=CreatGraph(8,10);
    PrintGraph(G);

    DeepTraverseGraph(G);
    printf("\n");
    BreadTraverseGraph(G);
    return 0;
}

队列的头文件与之前发的队列数组实现相同。

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/lelieven/article/details/17168871
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞