图的邻接矩阵表示

#include <iostream>
#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
int vis[105]
//邻接矩阵表示图
struct ENode
{
    int V1,V2;
    int weight;
};
struct GNode
{
    int Nv;//顶点数
    int Ne;//边数
    int G[100][100];//邻接矩阵
    char Data[100];//存定点的数据
};
struct GNode *CreateGraph(int VertexNum)
{
    //初始化一个有VertexNum个顶点但没有边的图
    struct GNode *Graph;
    int v,w;
    Graph=(struct GNode *)malloc(sizeof(struct GNode));
    Graph->Nv=VertexNum;
    Graph->Ne=0;
    for(v=0; v<Graph->Nv; v++)
        for(w=0; w<Graph->Nv; w++)
        {
            Graph->G[v][w]=0;
        }
    return Graph;
}
void InsertEdge(struct GNode *Graph,struct ENode *E)
{
    Graph->G[E->V1][E->V2]=1;
    Graph->G[E->V2][E->V1]=1;
}
struct GNode *BuildGraph(int k,int m)
{
    struct GNode *Graph;
    struct ENode *E;
    int V;
    int Nv,i;
    //cin>>Nv;//读入顶点个数
    Graph=CreateGraph(k);//初始化有Nv个顶点但没有边的图
    Graph->Ne=m;//读入边数
    if(Graph->Ne!=0)
    {
        E=(struct ENode *)malloc(sizeof(struct ENode));
        for(i=0; i<Graph->Ne; i++)
        {
            cin>>E->V1>>E->V2;
            InsertEdge(Graph,E);
        }
    }
    for(V=0; V<Graph->Nv; V++)
        cin>>Graph->Data[V];
    return Graph;
}

猜你喜欢

转载自blog.csdn.net/cpx17852033609/article/details/80671177