【C语言学习】图的操作模板

//图
#include<stdio.h>
#include<stdlib.h>

#define MaxInt 35500//极大值
#define MaxNum 100//最大顶点数

typedef char VerTexType;//数据类型
typedef int  ArcType;//权值类型(有可能是浮点数)

//邻接矩阵
typedef struct 
{
    VerTexType vexs[MaxNum];//顶点数组
    ArcType arcs[MaxNum][MaxNum];//边数组
    int vexnum,arcnum;//点数,边数
}AmGraph;

//邻接表
typedef struct ArcNode//边结点
{
    int adjvex;
    ArcType info;
    struct ArcNode* nextarc;
}ArcNode;

typedef struct VexNode //顶点节点
{
    VerTexType data;
    ArcNode* firstArc;
}VexNode;

typedef struct//邻接表
{
    VexNode vertices[MaxNum];
    int vexnum,arcnum;
}Algraph;

//最小生成树,普利姆算法辅助数组
struct
{
    VerTexType adjvex;
    ArcType lowcost;//最小边的权值
}closeedge[MaxNum];

int LocateMatrixVex(AmGraph* mygraph,VerTexType c)
{
    for(int i=0;i<mygraph->vexnum;i++)
    {
        if(mygraph->vexs[i]==c)
            return i;
    }
}

int LocateLinkVex(Algraph* mygraph,VerTexType c)
{
    for(int i=0;i<mygraph->vexnum;i++)
    {
        if(mygraph->vertices->data==c)
            return i;
    }
}

void CreatUDNByMatrix(AmGraph* mygraph)//邻接矩阵无向网
{
    //初始化,读入点数边数,将矩阵置为极大值(不连通)
    scanf("%d %d",&mygraph->vexnum,&mygraph->arcnum);
    for(int i=0;i<mygraph->vexnum;i++)
    {
        scanf("%c",&mygraph->vexs[i]);
    }
    for(int i=0;i<mygraph->vexnum;i++)
        for(int j=0;j<mygraph->vexnum;j++)
        {
            mygraph->arcs[i][j]==MaxInt;
        }
    //链接矩阵
    for(int k=0;k<mygraph->arcnum;k++)//对每条边输入端点权值,储存
    {
        VerTexType v1,v2;
        ArcType w;
        scanf("%c %c %d",&v1,&v2,&w);
        int i=LocateMatrixVex(mygraph,v1),j=LocateMatrixVex(mygraph,v2);
        mygraph->arcs[i][j]=w;
        mygraph->arcs[j][i]=w;
    }
}

void CreatUDNByLink(Algraph* mygraph)//邻接表创建无向网
{
    //初始化,读入顶点数边数,
    scanf("%d %d",&mygraph->vexnum,&mygraph->arcnum);
    for(int i=0;i<mygraph->vexnum;i++)
    {
        scanf("%c",&mygraph->vertices[i].data);
        mygraph->vertices[i].firstArc=NULL;
    }
    for(int k=0;k<mygraph->arcnum;k++)
    {
        VerTexType v1,v2;
        ArcType w;
        scanf("%c %c %d",&v1,&v2,&w);
        int i=LocateLinkVex(mygraph,v1),j=LocateLinkVex(mygraph,v2);
        ArcNode* p1=(ArcNode*)malloc(sizeof(ArcNode)),*p2=(ArcNode*)malloc(sizeof(ArcNode));;
        p1->adjvex=j;
        p1->info=w;
        p1->nextarc=mygraph->vertices[i].firstArc;
        mygraph->vertices[i].firstArc=p1;
        p2->adjvex=i;
        p2->info=w;
        p2->nextarc=mygraph->vertices[j].firstArc;
        mygraph->vertices[j].firstArc=p1;
    }
}

//深度优先搜索
void DFS_AM(AmGraph* mygraph,int k,int Visited[])//深度优先搜索邻接矩阵图,Visited为标记数组,需要自己创建
{
    printf("%c ",mygraph->vexs[k]);
    Visited[k]=1;
    for(int w=0;w<mygraph->vexnum;w++)
    {
        if(mygraph->arcs[k][w]!=MaxInt&&!Visited[w])//w是k的邻接点,w没有被访问过
            DFS_AM(mygraph,w,Visited);
    }
}

void DFS_LM(Algraph* mygraph,int k,int Visited[])//深度优先搜索邻接表图,Visited为标记数组,需要自己创建
{
    printf("%c ",mygraph->vertices[k].data);
    ArcNode* p=(ArcNode*)malloc(sizeof(ArcNode));
    p=mygraph->vertices[k].firstArc;
    while(p!=NULL)
    {
        int w=p->adjvex;//w与k邻接
        if(!Visited[w])//且w没被访问过
            {
                DFS_LM(mygraph,w,Visited);
            }
        p=p->nextarc;
    }
}

//广度优先搜索
void BFS_AM(AmGraph* mygraph,int k,int Visited[])//广度优先搜索邻接矩阵图,Visited为标记数组,需要自己创建
{
    int Queue[MaxNum];
    int base=0,top=0,u;
    printf("%c ",mygraph->vexs[k]);
    Visited[k]=1;
    Queue[top]=k;
    top=(top+1)%MaxNum; 
    while(base!=top)
    {
        u=Queue[base];
        base=(base+1)%MaxInt;
        for(int w=0;w<mygraph->vexnum;w++)
        {
            if(mygraph->arcs[u][w]!=MaxInt&&!Visited[w])//w是k的邻接点,w没有被访问过
            {
                printf("%c ",mygraph->vexs[w]);
                Visited[w]=1;
                Queue[top]=w;
                top=(top+1)%MaxInt;
            }
        }
    }
}

void BFS_LM(Algraph* mygraph,int k,int Visited[])//广度优先搜索邻接表图,Visited为标记数组,需要自己创建
{
    int Queue[MaxNum];
    int base=0,top=0,u;
    ArcNode* p=(ArcNode*)malloc(sizeof(ArcNode));
    printf("%c ",mygraph->vertices[k].data);
    Visited[k]=1;
    Queue[top]=k;
    top=(top+1)%MaxInt;
    while(base!=top)
    {
        u=Queue[base];
        base=(base+1)%MaxInt;
        p=mygraph->vertices[u].firstArc;
        while(p!=NULL)
        {
            int w=p->adjvex;
            if(!Visited[w])
            {
                printf("%c ",mygraph->vertices[w].data);
                Visited[w]=1;
                Queue[top]=w;
                top=(top+1)%MaxInt;
            }
            p=p->nextarc;
        }
    }
}

//最小生成树,普利姆算法
void MiniSpanTree_Prim(AmGraph* mygraph,VerTexType u)
{
    int k=LocateMatrixVex(mygraph,u);
    for(int i=0;i<mygraph->vexnum;i++)
    {
        if(i!=k)
        {
            closeedge[i]->adjvex=u;
            closeedge[i]->lowcost=mygraph->arcs[k][i];
        }
    }
    closeedge[k].lowcost=0;//权值记零,这个边不再访问
    for(int i=1;i<mygraph->vexnum;i++)
    {
        int min=MaxInt;
        for(int j=0,m=k;j<mygraph->vexnum;j++)//k=MIN(closeedge)找出下一个最近点,
            if(closeedge[j].lowcost<min&&closeedge[j].lowcost!=0)
            {
                min=closeedge[j].lowcost;
                k=j;
            }
        printf("%c-%c %d\n",closeedge[k].adjvex,mygraph->vexs[k],min);
        closeedge[k].lowcost=0;
        for(int j=0;j<mygraph->vexnum;j++)
        {
            if(mygraph->arcs[k][j]<closeedge[j].lowcost)
            {
                closeedge[j].adjvex=mygraph->vexs[k];
                closeedge[j].lowcost=mygraph->arcs[k][j];
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值