邻接矩阵逆邻接矩阵
#include "stdio.h"
#include "stdlib.h"
#define MVN 20
typedef char VerTexType;
typedef int ArcType;
typedef struct
{VerTexType Vexs[MVN];// 頂點(diǎn)數(shù)組ArcType Arcs[MVN][MVN];// 邊/弧int Vexnum,Arcnum;// 當(dāng)前頂點(diǎn)數(shù)和邊數(shù)
}AMGraph;// Adjacency Matrix Graph :鄰接矩陣typedef struct
{VerTexType Vex[MVN];ArcType Arcs[MVN][MVN];
}InAMGraph; // Inverse Adjacency Matrix Grapg 逆鄰接矩陣int get_first(AMGraph* G,VerTexType* vex);
int GetVertex(AMGraph* G);
int LocateVex(AMGraph* G,VerTexType v);int CreateDG(AMGraph* G, InAMGraph* IG)// Create Direction Graph 創(chuàng)建有向圖
{ VerTexType v1;VerTexType v2;//弧的兩個(gè)頂點(diǎn)printf("輸入頂點(diǎn)的數(shù)量:\n");scanf("%d",&(*G).Vexnum);// 指定頂點(diǎn)的數(shù)量printf("輸入邊的數(shù)量:\n");scanf("%d",&(*G).Arcnum);// 指定邊的數(shù)量for(int i = 0; i< (*G).Vexnum; i++)//對(duì)于圖,初始化為0;對(duì)于網(wǎng),初始化為無(wú)窮大{for(int j = 0; j < (*G).Vexnum; j++)(*G).Arcs[i][j] = 0;}GetVertex(&(*G));for (int k = 0; k < (*G).Arcnum; k++)// 數(shù)據(jù)寫入矩陣{ printf("從哪個(gè)頂點(diǎn)到哪個(gè)頂點(diǎn)?\n");printf("弧尾:\n");get_first(&(*G),&v1);printf("弧頭:\n");get_first(&(*G),&v2);int i = LocateVex(&(*G),v1);//行int j = LocateVex(&(*G),v2);//列(*G).Arcs[i][j] = 1;// 鄰接矩陣(*IG).Arcs[j][i] = 1;//逆鄰接矩陣}return 1;}
int GetVertex(AMGraph* G)
{ for (int i = 0; i <(*G).Vexnum; i++)// 逐個(gè)輸入頂點(diǎn)的值{ printf("輸入頂點(diǎn)的值:\n"); get_first(&(*G),&(*G).Vexs[i]);// 寫入到頂點(diǎn)數(shù)組}
}
int LocateVex(AMGraph* G,VerTexType v)//查找頂點(diǎn)在頂點(diǎn)數(shù)組中的下標(biāo)
{if(sizeof(*G) == 0) return 0;for(int i = 0; i < MVN; i++){if((*G).Vexs[i] = v)return i;}return 0;
}
int get_first(AMGraph* G, VerTexType* vex)//去除鍵盤輸入換行符
{(*vex) = getchar();while(getchar() != '\n')continue;return 1;
}
int main()
{ InAMGraph IG;AMGraph G;CreateDG(&G,&IG);return 0;
缺陷:尚未實(shí)現(xiàn)遍歷功能
使用方法:需要參照一個(gè)圖來(lái)輸入
總結(jié)
- 上一篇: 显示器尺寸对照表_怎样知道自己的电脑显示
- 下一篇: 中科大 EPC课程 爬虫(最新,效果良好