邻接矩阵
(知顶点个数,边的条数, 再插入顶点比较难,修改边的权值容易)
//
//邻接矩阵
//
//邻接矩阵存储使用2个数组存储图的信息:1个以为数组存储顶点,一个二维数组存储边的信息
//(1)二维数组中的对角线为0,以为不存在顶点到自身的边
//(2)要知道某个点的出度,就是顶点vi在第i行的元素之和,入度就是该顶点所在列的元素之和
//(3)顶点vi的所有邻接点就是吧矩阵中第i行元素扫描一遍
#include<iostream>
using namespace std;
typedef char VertexType;
typedef int EdgeType;
#define MAXVEX 100
#define IUNFINITY 0
typedef struct
{
VertexType vexs[MAXVEX]; //顶点表 char
EdgeType coordinates[MAXVEX][MAXVEX]; ///邻接矩阵
int vnum, edgenum; //顶点个数,边个数
}MGraph;
void CreateMGraph(MGraph *G,int v,int e)
{
G->vnum=v;
G->edgenum=e;
cout << "请输入顶点字符" << endl;
for (int i = 0; i < G->vnum; i++)
{
cin >> G->vexs[i];
}
//初始化数组
for (int i = 0; i < G->vnum; i++)
for (int j = 0; j < G->vnum; j++)
G->coordinates[i][j] = IUNFINITY;
cout << "请输入边坐标以及该边的权值" << endl;
int n, m,k;
for (int i=0; i < G->edgenum; i++)
{
cin >> n;
cin >> m;
cin >> k;
G->coordinates[n - 1][m - 1] = k;
//G->coordinates[m - 1][n - 1] = k; //无向图
}
}
void Print(MGraph *G,int v)
{
for (int i = 0; i < v; i++)
{
for (int j = 0; j < v; j++)
{
cout << G->coordinates[i][j] << " ";
}
cout << endl;
}
}
int main()
{
MGraph G;
int vnum;
int edgenum;
cout << "请输入顶点个数,边的条数" << endl;
cin >> vnum;
cin >>edgenum;
CreateMGraph(&G,vnum,edgenum);
Print(&G, vnum);
system("pause");
}
邻接表
(注重点,边,图之间的联系)
边,点,图结构体:
#include<iostream>
using namespace std;
#define MAX 100
typedef char VextexType;
typedef int EdgeType;
//边表
typedef struct EdgeNode {
char adjvex; //边指向的顶点
struct EdgeNode * next; //指向下一条边
}EdgeNode;
//顶点表
typedef struct VextexNode {
VextexType data; //顶点信息
EdgeNode *firstedge; //指向第一条依附该顶点的边
}VextexNode,AdjList[MAX];
//图
typedef struct {
AdjList adjList;
int numvex, numedge;
}Graph;
创建邻接表:
void CreateGraph(Graph *g) {
cout << "请输入顶点数,边的条数:" << endl;
cin >> g->numvex;
cin >> g->numedge;
cout << "请输入各顶点所代表的字符:" << endl;
for (int i = 0; i < g->numvex; i++) { //建立顶点表,并将顶点表所指的第一条边置null
cin >> g->adjList[i].data;
g->adjList[i].firstedge = NULL;
}
/*顶点有下标:*/
//for (int k = 0; k < g->numedge; k++) { //建立边表
// int v1, v2;
// cout << "请输入每条边连接的两个点的下标(v1,v2):" << endl;
// cin >> v1;
// cin >> v2;
// EdgeNode *p;
// p = new EdgeNode;
// p->adjvex = v2;
// p->next = g->adjList[v1].firstedge;
// g->adjList[v1].firstedge = p;
// p = new EdgeNode;
// p->adjvex = v1;
// p->next = g->adjList[v2].firstedge;
// g->adjList[v1].firstedge = p;
//}
/*顶点无下标*/
for (int k = 0; k < g->numedge; k++) { //建立边表
char v1, v2;
cout << "请输入每条边连接的两个点(v1,v2):" << endl;
cin >> v1;
cin >> v2;
int numv1, numv2;
for (int i = 0; i < g->numvex; i++)
{
if (g->adjList[i].data == v1)
numv1 = i;
}
for (int i = 0; i < g->numvex; i++)
{
if (g->adjList[i].data == v2)
numv2 = i;
}
EdgeNode *p;
p = new EdgeNode;
p->adjvex = v2; //有向图
p->next = g->adjList[numv1].firstedge; //前插法
g->adjList[numv1].firstedge = p;
//p = new EdgeNode; //若有这段代码则为无向图
//p->adjvex = v1;
//p->next = g->adjList[numv2].firstedge;
//g->adjList[numv2].firstedge = p;
}
}
打印邻接表:
void Print(Graph *g)
{
EdgeNode *pr;
for (int i = 0; i < g->numvex; i++)
{
pr = g->adjList[i].firstedge;
cout << g->adjList[i].data << " ";
for (pr = g->adjList[i].firstedge; pr != NULL; pr = pr->next)
{
cout << pr->adjvex << " ";
}
cout << endl;
}
}
主程序:
int main()
{
Graph G;
CreateGraph(&G);
Print(&G);
system("pause");
}