• 图的顺序存储结构


    图的顺序存储结构

    图的顺序存储结构C语言实现

    #include<iostream>
    using namespace std;
    #define matrix_size 20
    typedef struct {
        int weight;
    }AdjMatrix[matrix_size][matrix_size];
    
    struct MGraph{
        int vex[matrix_size];
        AdjMatrix arcs;
        int vexnum,arcnum;
        int kind;
    };
    
    typedef enum{DG,DN,UDG,UDN}GraphKind;
    
    
    int LocateVex(MGraph *G ,int v){
        int i;
        for ( i = 0; i < G->vexnum; i++)
        {
            if (G->vex[i]==v)
            {
                break;
            }
        }
        if (i>G->vexnum)
        {
            cout<<"not such vertex"<<endl;
            return -1;
        }
        return i;
    }
    //构造有向图
    void CreateDG(MGraph *G){
        cin>>G->vexnum>>G->arcnum;
        for (int i = 0; i < G->vexnum; i++)
        {
            cin>>G->vex[i];
        }
        for (int i=0; i<G->vexnum; i++) {
            for (int j=0; j<G->vexnum; j++) {
                G->arcs[i][j].weight=0;
            }
        }
        for (int i = 0; i < G->arcnum; i++)
        {
            int v1,v2;
            cin>>v1>>v2;
            int n=LocateVex(G,v1);
            int m=LocateVex(G,v2);
            if (m==-1||n==-1)
            {
                cout<<"not this vertex"<<endl;
                return ;
            }
            G->arcs[n][m].weight=1;
        }
        return ;
    }
    
    //构造无向图
    void CreateDN(MGraph *G){
        cin>>G->vexnum>>G->arcnum;
        for (int i = 0; i < G->vexnum; i++)
        {
            cin>>G->vex[i];
        }
        for (int i=0; i<G->vexnum; i++) {
            for (int j=0; j<G->vexnum; j++) {
                G->arcs[i][j].weight=0;
            }
        }
        for (int i = 0; i < G->arcnum; i++)
        {
            int v1,v2;
            cin>>v1>>v2;
            int n=LocateVex(G,v1);
            int m=LocateVex(G,v2);
            if (m==-1||n==-1)
            {
                cout<<"not this vertex"<<endl;
                return ;
            }
            G->arcs[n][m].weight=1;
            G->arcs[m][n].weight=1;
        }
        return ;
    }
    //构造有向网,和有向图不同的是二阶矩阵中存储的是权值
    void CreateUDG(MGraph *G){
        cin>>G->vexnum>>G->arcnum;
        for (int i = 0; i < G->vexnum; i++)
        {
            cin>>G->vex[i];
        }
        for (int i=0; i<G->vexnum; i++) {
            for (int j=0; j<G->vexnum; j++) {
                G->arcs[i][j].weight=0;
            }
        }
        for (int i = 0; i < G->arcnum; i++)
        {
            int v1,v2,w;
            cin>>v1>>v2>>w;
            int n=LocateVex(G,v1);
            int m=LocateVex(G,v2);
            if (m==-1||n==-1)
            {
                cout<<"not this vertex"<<endl;
                return ;
            }
            G->arcs[n][m].weight=w;
        }
        return ;
    }
    
    //构造无向网,和无向图不同的是二阶矩阵中存储的是权值
    void CreateUDN(MGraph *G){
        cin>>G->vexnum>>G->arcnum;
        for (int i = 0; i < G->vexnum; i++)
        {
            cin>>G->vex[i];
        }
        for (int i=0; i<G->vexnum; i++) {
            for (int j=0; j<G->vexnum; j++) {
                G->arcs[i][j].weight=0;
            }
        }
        for (int i = 0; i < G->arcnum; i++)
        {
            int v1,v2,w;
            cin>>v1>>v2>>w;
            int n=LocateVex(G,v1);
            int m=LocateVex(G,v2);
            if (m==-1||n==-1)
            {
                cout<<"not this vertex"<<endl;
                return ;
            }
            G->arcs[n][m].weight=w;
            G->arcs[m][n].weight=w;
        }
        return ;
    }
    
    
    
    
    //输出函数
    void PrintGrapth(MGraph G)
    {
        for (int i = 0; i < G.vexnum; i++)
        {
            for (int j = 0; j < G.vexnum; j++)
            {
                cout<<G.arcs[i][j].weight<<" ";
            }
            cout<<endl;
        }
    }
    
    
    void CreateGraph(MGraph *G){
        //选择图的类型
        cin>>G->kind; 
        //根据所选类型,调用不同的函数实现构造图的功能
        switch (G->kind) {
            case DG:
                return CreateDG(G);
                break;
            case DN:
                return CreateDN(G);
                break;
            case UDG:
                return CreateUDG(G);
                break;
            case UDN:
                return CreateUDN(G);
                break;
            default:
                break;
        }
    }
    int main() {
        MGraph G;//建立一个图的变量
        CreateGraph(&G);//调用创建函数,传入地址参数
        PrintGrapth(G);//输出图的二阶矩阵
        return 0;
    }

     

    相应地运行结果为: 

    2
    6 10
    1
    2
    3
    4
    5
    6
    1 2 5
    2 3 4
    3 1 8
    1 4 7
    4 3 5
    3 6 9
    6 1 3
    4 6 6
    6 5 1
    5 4 5
    0 5 0 7 0 0
    0 0 4 0 0 0
    8 0 0 0 0 9
    0 0 5 0 0 6
    0 0 0 5 0 0
    3 0 0 0 1 0

     

    因上求缘,果上努力~~~~ 作者:每天卷学习,转载请注明原文链接:https://www.cnblogs.com/BlairGrowing/p/13639584.html

  • 相关阅读:
    pku夏令营面试
    机器学习实验一SVM分类实验
    面试相关-转载-well,yzl——持续更新
    2715:谁拿了最多奖学金-poj
    1005:I Think I Need a Houseboat-poj
    2810:完美立方-poj
    2943:小白鼠排队-poj
    rem+媒体查询---移动端 设计稿以375
    微信小程序 + mock.js 实现后台模拟及调试
    一个div 实现纸张阴影效果
  • 原文地址:https://www.cnblogs.com/BlairGrowing/p/13639584.html
Copyright © 2020-2023  润新知