实验四:图的构造与遍历

#include<bits/stdc++.h>
#define MVnum 100
#define MaxInt 32767
using namespace std;

///邻接矩阵
typedef struct{
    char vexs[MVnum];
    int arcs[MVnum][MVnum];
    int vexnum,arcnum;
}AMGraph;

///邻接表
typedef struct ArcNode{
    int adjvex;
    struct ArcNode *nextarc;
    int info;
}ArcNode;
typedef struct VNode{
    char data;
    ArcNode *firstarc;
}VNode,AdjList[MVnum];
typedef struct{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;

void menu()///菜单
{
    cout<<"      ***************************\n";
    cout<<"          实验4:图的构造与遍历  \n";
    cout<<"      ***************************\n";
    cout<<"1.邻接矩阵构造无向网\t\t";cout<<"2.输出邻接矩阵\n";
    cout<<"3.深度优先遍历无向图邻接表\t";
    cout<<"4.广度优先遍历无向图邻接表\n";
    cout<<"0.退出\n\n";
}
int LocateVex(AMGraph G,char v)///邻接矩阵中确定v点在图中的位置
{
    for(int i=0;i<G.vexnum;i++)
        if(v==G.vexs[i])
            return i;
    return -1;
}
int LocateVex2(ALGraph G,char v)///邻接表中确定v点在图中的位置
{
    for(int i=0;i<G.vexnum;i++)
        if(v==G.vertices[i].data)
            return i;
    return -1;
}
bool CreateUDN(AMGraph &G1)///邻接矩阵构造无向网
{
    char v1,v2;
    int w,i,j;
    cout<<"请输入总顶点数和总边数:";
    cin>>G1.vexnum>>G1.arcnum;
    cout<<"请输入顶点信息:";
    for(int i=0;i<G1.vexnum;i++)
        cin>>G1.vexs[i];
    for(i=0;i<G1.vexnum;i++)
        for(j=0;j<G1.vexnum;j++)
            if(i==j)G1.arcs[i][j]=0;
            else G1.arcs[i][j]=MaxInt;
    cout<<"请输入边的信息:\n";
    for(int k=0;k<G1.arcnum;k++){
        cin>>v1>>v2>>w;
        i=LocateVex(G1,v1);j=LocateVex(G1,v2);
        if(i==-1||j==-1){
            if(i==-1)cout<<v1<<"不存在邻接矩阵中!";
            else cout<<v2<<"不存在邻接矩阵中!";
            cout<<"请重新输入:\n";
            k--;
            continue;
        }
        G1.arcs[i][j]=w;
        G1.arcs[j][i]=G1.arcs[i][j];
    }
    return true;
}
void OutputUDN(AMGraph G1)///输出邻接矩阵
{
    for(int i=0;i<G1.vexnum;i++)
        cout<<'\t'<<G1.vexs[i];
    cout<<endl;
    for(int i=0;i<G1.vexnum;i++){
        cout<<G1.vexs[i]<<'\t';
        for(int j=0;j<G1.vexnum;j++)
            if(G1.arcs[i][j]==MaxInt)cout<<"∞"<<'\t';
            else cout<<G1.arcs[i][j]<<'\t';
        cout<<endl;
    }
}
bool CreateUDG(ALGraph &G2)///创建邻接表
{
    char v1,v2;
    int w,i,j;
    cout<<"请输入总顶点数和总边数:";
    cin>>G2.vexnum>>G2.arcnum;
    cout<<"请输入顶点信息:";
    for(i=0;i<G2.vexnum;i++){
        cin>>G2.vertices[i].data;
        G2.vertices[i].firstarc=NULL;
    }
    cout<<"请输入边的信息:\n";
    for(int k=0;k<G2.arcnum;k++){
        cin>>v1>>v2;
        i=LocateVex2(G2,v1);j=LocateVex2(G2,v2);
        if(i==-1||j==-1){
            if(i==-1)cout<<v1<<"不存在邻接矩阵中!";
            else cout<<v2<<"不存在邻接矩阵中!";
            cout<<"请重新输入:\n";
            k--;
            continue;
        }
        ArcNode *p1,*p2;
        p1=new ArcNode;
        p1->adjvex=j;
        p1->nextarc=G2.vertices[i].firstarc;
        G2.vertices[i].firstarc=p1;
        p2=new ArcNode;
        p2->adjvex=i;
        p2->nextarc=G2.vertices[j].firstarc;
        G2.vertices[j].firstarc=p2;
    }
    return true;
}
bool vis1[MVnum];
int o;
void DFS_AL(ALGraph G2,int v)///深度优先遍历
{
    o?printf("->"):o=1;
    cout<<G2.vertices[v].data;
    vis1[v]=true;
    ArcNode *p;
    int w;
    p=G2.vertices[v].firstarc;
    while(p!=NULL){
        w=p->adjvex;
        if(!vis1[w])
            DFS_AL(G2,w);
        p=p->nextarc;
    }
}
bool vis2[MVnum];
void BFS_AL(ALGraph G2,int v)///广度优先遍历
{
    o=0;
    o?printf("->"):o=1;
    cout<<G2.vertices[v].data;
    vis2[v]=true;
    int head=1,tail=1,que[MVnum]={};
    que[tail++]=v;
    while(head<tail){
        int k=que[head],w;
        ArcNode *p;
        p=G2.vertices[k].firstarc;
        while(p!=NULL){
            w=p->adjvex;
            if(!vis2[w]){
                o?printf("->"):o=1;
                cout<<G2.vertices[w].data;
                que[tail++]=w;
                vis2[w]=true;
            }
            p=p->nextarc;
        }
        head++;
    }
}
int main()
{
    AMGraph Graph1;///邻接矩阵
    ALGraph Graph2;///邻接表
    menu();
    int choose=-1,v,flag=0;
    while(choose){
        cout<<"请输入指令:";
        cin>>choose;
        switch(choose){
            case 1: if(CreateUDN(Graph1))
                        cout<<"邻接矩阵构造无向网成功!\n\n";
                    flag=1;
                    break;
            case 2: if(!flag)cout<<"未创建邻接矩阵!\n\n";
                    else{
                        OutputUDN(Graph1);
                        cout<<"输出邻接矩阵成功!\n\n";
                    }
                    break;
            case 3: CreateUDG(Graph2);
                    memset(vis1,false,sizeof(vis1));
                    cout<<"深度优先遍历结果:";
                    for(v=0;v<Graph2.vexnum;v++){
                        if(!vis1[v]){
                            o=0;
                            DFS_AL(Graph2,v);
                            cout<<"  ";
                        }
                    }
                    cout<<"\n\n";
                    break;
            case 4: CreateUDG(Graph2);
                    memset(vis2,false,sizeof(vis2));
                    cout<<"广度优先遍历结果:";
                    for(v=0;v<Graph2.vexnum;v++)
                        if(!vis2[v]){
                            BFS_AL(Graph2,v);
                            cout<<"  ";
                        }
                    cout<<"\n\n";
                    break;
        }
    }
    cout<<"退出成功!\n";
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值