图邻接矩阵的c语言实现 graph_adj_matrix.cpp

gra_adj_matrix.cpp 文件

#include 
#include 
#include 
using namespace std;

#define  Vertex_MAX 15
#define  Edge_MAX 15*(15-1)
typedef int elemtype;
//typedef float elemtype;
int Vertex_n = 0;
int Edge_n = 0;

struct graph
{
    elemtype V[Vertex_MAX+1];   //顶点
    elemtype arcs[Vertex_MAX+1][Vertex_MAX+1];  //边(i,j)
};

//访问标志位
char visited[Vertex_MAX+1] = {0};

//建立无向图邻接矩阵
void UDcreatadj_matrix(graph& g)
{
    int i,j,k;
    //输入顶点个数和边的个数
    printf("请输入顶点个数:");
    cin>>Vertex_n;
    printf("请输入边的个数:");
    cin>>Edge_n;
    //输入顶点信息
    for (k = 1; k <= Vertex_n; k++) 
    {  
        printf("请输入%d个顶点,第%d个顶点: ",Vertex_n,k);
        cin>>g.V[k];
    }
    //矩阵初始化
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++ )
        {
            g.arcs[i][j] = 0;
        }
    }
    //输入边(i,j)
    for (k = 1; k <= Edge_n; k++)    
    {
        printf("请输入%d个边,第%d个边(i j): ",Edge_n,k);
        cin>>i>>j;
        g.arcs[i][j] = 1;
        g.arcs[j][i] = 1;
    }
    //输出矩阵
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++ )
        {
            printf("%d  ",g.arcs[i][j]);
        }
        printf("\n");
    }
}

//邻接矩阵存储,深度优先搜索算法,无向图连通图,遍历顶点
void UDdfs_adj_matrix(graph& g , int onevertex,int vertex_n)
{
    int j;
    cout<//第一个要访问的顶点
    visited[onevertex] = 1;     //标志置1
    for (j = 1; j<= vertex_n; j++)
    {
        if (g.arcs[onevertex][j] == 1 && !visited[j])   //存在边关系的顶点
        {
            //cout<
            UDdfs_adj_matrix(g,j,vertex_n);
        }
    }
}

//连通图和非连通图,深度搜索算法
void UDdfs_all_adj_matrix(graph& g , int vertex_n)
{
    for (int kk = 1; kk <= vertex_n; kk ++ )
    {
        if (!visited[kk])
        {
            UDdfs_adj_matrix(g , kk , vertex_n);//所有顶点都作为起点遍历
        }

    }
}

//邻接矩阵存储,广度优先搜索算法,无向图连通图,遍历顶点
void UDbfs_adj_matrix(graph& g , int onevertex,int vertex_n)
{
    //int Q[Vertex_MAX + 1];
    int *Q = new int[vertex_n + 1]; //Q为队列
    memset(Q,0,sizeof(int)*(vertex_n + 1)); 
    int f , r, j;   //f,r分别为队列头,尾指针
    f = r = 0;
    cout<//输出访问顶点
    visited[onevertex] = 1; //标志已经访问过
    r++; 
    Q[r] = onevertex;   //入队列
    while(f < r)
    {
        f++;
        onevertex = Q[f];   //出队列
        for (j=1; j<=vertex_n; j++)
        {
            if ((g.arcs[onevertex][j] == 1) && (!visited[j]))
            {
                cout<1;
                r++;
                Q[r] = j;
            }
        }
    }
    if (Q != NULL)
    {
        delete [] Q;
        Q = NULL;
    }

}
//连通图和非连通图,广度搜索算法
void UDbfs_all_adj_matrix(graph& g , int vertex_n)
{
    for (int kk = 1; kk <= vertex_n; kk ++ )
    {
        if (!visited[kk])
        {
            UDbfs_adj_matrix(g ,kk,vertex_n);
        }

    }
}
//建立有向图邻接矩阵
void Dcreateadj_matrix(graph& g)
{
    int i , j, k;
    //输入顶点个数和边的个数
    printf("请输入顶点个数:");
    cin>>Vertex_n;
    printf("请输入边的个数:");
    cin>>Edge_n;
    //输入顶点信息
    for (i = 1; i <= Vertex_n; i ++)
    {
        printf("请输入%d个顶点,第%d个顶点: ",Vertex_n,i);
        cin>>g.V[i];
    }
    //矩阵初始化
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++)
        {
            g.arcs[i][j] = 0;
        }
    }
    //输入弧
    for (k = 1; k <= Edge_n; k++)
    {
        printf("请输入%d个边,第%d个弧(i j): ",Edge_n,k);
        cin>>i>>j;
        g.arcs[i][j] = 1;
    }
    //输出矩阵
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++ )
        {
            printf("%d  ", g.arcs[i][j]);
        }
        printf("\n");
    }
}

//建立无向网的邻接矩阵
void UDNetcreatadj_matrix(graph& g)
{
    int i,j,k;
    //权值
    int w;
    //输入顶点个数和边的个数
    printf("请输入顶点个数:");
    cin>>Vertex_n;
    printf("请输入边的个数:");
    cin>>Edge_n;
    //输入顶点信息
    for (k = 1; k <= Vertex_n; k++) 
    {  
        printf("请输入%d个顶点,第%d个顶点: ",Vertex_n,k);
        cin>>g.V[k];
    }
    //矩阵初始化
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++ )
        {
            if (i == j)
            {
                g.arcs[i][j] = 0;
            }
            else g.arcs[i][j] = 65536; //此处应该是无穷大
        }
    }
    //输入边(i,j)及权值
    for (k = 1; k <= Edge_n; k++)    
    {
        printf("请输入%d个边,第%d个边和权值(i j w): ",Edge_n,k);
        cin>>i>>j>>w;
        g.arcs[i][j] = w;
        g.arcs[j][i] = w;
    }
    //输出矩阵
    for (i = 1; i <= Vertex_n; i ++)
    {
        for (j = 1; j <= Vertex_n; j++ )
        {
            printf("%d      ",g.arcs[i][j]);
        }
        printf("\n");
    }
}



int main()
{

    graph g;
    UDcreatadj_matrix(g);
    //Dcreateadj_matrix(g);
    //UDNetcreatadj_matrix(g);
    UDdfs_adj_matrix(g, 1,Vertex_n);
    //UDbfs_adj_matrix(g,1,Vertex_n);
    system("pause");
    return 0;
}

good luck !

你可能感兴趣的:(数据结构)