Storing undirected graph adjacency matrix, the four vertices, four sides

 

//

//  main.cpp

//  Graph_wuxiang

//

//  Created by duanqibo on 2019/6/29.

//  Copyright © 2019年 duanqibo. All rights reserved.

// store undirected graph adjacency matrix, four vertices, four edges, v0 to v1: Right 30; v0 to v2: Right 80;

// v1 to v2: Right 50; v2 to v3: Right 40; 32767 represents a point between the other non-contiguous;

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

    typedef char VertexType;

    typedef int WeightType;

    const int a = 4;

    const int MAX_INT=32767;

    typedef struct gp

    {

        VertexType vexS [one];

        WeightType arcs [a] [a];

        int vexnum,arcnum;

    }WGraph;

    void CreateGraph(WGraph *g)

    {

        int i,j,n,e,w,k;

        char ch;

        printf ( "Enter the number of vertices and the number of edges:");

        scanf("%d%d",&n,&e);

        g->vexnum=n;

        g->arcnum=e;

        for(i=0;i<g->vexnum;i++)

        {

            getchar();

            printf ( "% d of vertices: \ n", i);

            scanf("%c",&ch);

            g->vexs[i]=ch;

        }      

        for(i=0;i<g->vexnum;i++)

            for(j=0;j<g->vexnum;j++)

                g->arcs[i][j]=MAX_INT;

        

        for(k=0;k<g->arcnum;k++)

        {

            scanf("%d%d%d",&i,&j,&w);

            g->arcs[i][j]=w;

            g->arcs[j][i]=w;

        }

    }

    void OutGraph(WGraph *g)

    {

        int i,j;

        for(i=0;i<g->vexnum;i++)

        {

            for(j=0;j<g->vexnum;j++)

                printf("%d ",g->arcs[i][j]);

            printf("\n");

        }

    }  

    int main(int argc, const char * argv[]) {  

        WGraph g;

        CreateGraph(&g);

        printf ( "output matrix: \ n");

        OutGraph(&g);  

}

 Results are as follows:

 

Guess you like

Origin www.cnblogs.com/duanqibo/p/11106815.html