圖(Graph)鄰接矩陣表示

圖是一種表示多對多的關系
包含:
一組頂點:通常用V(Vertex)表示頂i點集合
一組邊:通常用E(Edge)表示邊的集合
邊是頂點對:(v,w)<-E
有向邊<v,w>表示從v指向w的邊(單行線) v->w

鄰接矩陣圖表示

關于無向圖的存儲,可以使用一個長度為N(N+1)/2的一維數組A存儲{G00,G01,G11,...,Gn-1 0,...,Gn-1 n-1}則Gij在A中對應的下標是(i*(i+1)/2+j)
在網絡中,只要把G[i][j]的值定義為邊<Vi,Vj>的權重即可。
關于圖的度:從該點出發的邊數為出度,指向該點的邊數為入度。
無向圖:對應行或者列非0元素的個數
有向圖:對應行非0元素的個數是出度,對應列非0元素的個數是入度

/* 圖的鄰接矩陣表示法 */

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

#define MaxVertexNum 100 //最大頂點數
#define INFINITY 0   

typedef int Vertex;     //頂點下標表示頂點
typedef int WeightType; //邊的權值設置為整型
typedef char DataType;  //頂點存儲的數據類型設為字符型

/* 邊的定義 */
typedef struct ENode{
    Vertex V1,V2;   //有向邊<V1,V2>
    WeightType Weight;  //權重
}*Edge;

/* 圖節點的定義 */
typedef struct GNode{
    int Nv; //頂點數
    int Ne; //邊數
    WeightType G[MaxVertexNum][MaxVertexNum];   //鄰接矩陣
    //DataType Data[MaxVertexNum];  //存儲頂點的數據
    /* 若頂點無數據,則Data[]不必出現 */
}*MGraph; //以鄰接矩陣存儲的圖的類型

/* 初始化一個有VertexNum個頂點但是沒有邊的圖 */
MGraph CreateGraph(int VertexNum); 

void InsertEdge(MGraph Graph,Edge E);   //圖的插入
MGraph BuildGraph();    //圖的建立
void ShowGraph(MGraph Graph);   //顯示圖

int main()
{
    MGraph graph;
    int VertexNum = 8;

    graph=BuildGraph();

    ShowGraph(graph);

    system("pause");
    return 0;
}

/*
    初始化一個圖:VertexNum * VertexNum 的矩陣
    * * ... * *
    . *     * .
    .         .
    . *     * .
    * * ... * *
*/
MGraph CreateGraph(int VertexNum)
{
    Vertex V,W;
    MGraph Graph;

    Graph=(MGraph)malloc(sizeof(struct GNode));
    Graph->Nv=VertexNum;
    Graph->Ne=0;
    /* 初始化鄰接矩陣,從0-(Graph->Nv-1) */
    for(V=0;V<Graph->Nv;V++)
        for(W=0;W<Graph->Nv;W++)
            Graph->G[V][W]=INFINITY;

    return Graph;
}

void InsertEdge(MGraph Graph,Edge E)
{
    /* 插入邊<V1,V2> */
    Graph->G[E->V1][E->V2]=E->Weight;
    /* 若是無向圖,還要插入<V2,V1> */
    Graph->G[E->V2][E->V1]=E->Weight;
}

MGraph BuildGraph()
{
    MGraph Graph;
    Edge E;
    Vertex V;
    int Nv,i;

    printf("please enter the Node:\n");
    scanf_s("%d",&Nv);  //頂點數
    Graph=CreateGraph(Nv);

    printf("please enter the Edge:\n");
    scanf_s("%d",&(Graph->Ne));
    if(Graph->Ne!=0){
        E=(Edge)malloc(sizeof(struct ENode)); /* 建立邊節點 */
        for(i=0;i<Graph->Ne;i++){
            /* 起點、終點、權重 插入鄰接矩陣 */
            printf("please enter V1 V2 Weight:\n");
            scanf_s("%d %d %d",&E->V1,&E->V2,&E->Weight);
            InsertEdge(Graph,E);
        }
    }

    /*
     讀入頂點數據 
    for(V=0;V<Graph->Nv;V++)
        scanf_s("%c",&(Graph->Data[V]));
    */
    return Graph;
}

void ShowGraph(MGraph Graph)
{
    int i, j;
    printf("show the Graph:\n");
    for(i=0;i<Graph->Nv;i++){
        for(j=0;j<Graph->Nv;j++){
            printf("%d ", Graph->G[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

輸入與輸出示例:

please enter the Node:
10
please enter the Edge:
17
please enter V1 V2 Weight:
0 1 1
please enter V1 V2 Weight:
0 3 1
please enter V1 V2 Weight:
3 1 1
please enter V1 V2 Weight:
1 2 1
please enter V1 V2 Weight:
3 7 1
please enter V1 V2 Weight:
3 6 1
please enter V1 V2 Weight:
1 5 1
please enter V1 V2 Weight:
2 5 1
please enter V1 V2 Weight:
2 4 1
please enter V1 V2 Weight:
7 6 1
please enter V1 V2 Weight:
5 6 1
please enter V1 V2 Weight:
5 4 1
please enter V1 V2 Weight:
6 8 1
please enter V1 V2 Weight:
5 8 1
please enter V1 V2 Weight:
5 9 1
please enter V1 V2 Weight:
4 9 1
please enter V1 V2 Weight:
8 9 1
show the Graph:
0 1 0 1 0 0 0 0 0 0
1 0 1 1 0 1 0 0 0 0
0 1 0 0 1 1 0 0 0 0
1 1 0 0 0 0 1 1 0 0
0 0 1 0 0 1 0 0 0 1
0 1 1 0 1 0 1 0 1 1
0 0 0 1 0 1 0 1 1 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 0 1 1 0 0 1
0 0 0 0 1 1 0 0 1 0

請按任意鍵繼續. . .
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容