图——邻接矩阵和邻接表的实现

//1图的邻接矩阵表示法和邻接表创建无向图存储

#include<iostream>
using namespace std;
#define MaxNum 100		//最大顶点数
//1.1图的邻接矩阵表示法创建无向图存储
#define MaxInt  32767	//无穷
/*
typedef char VerType;//数据类型
typedef int ArcType;
//图结构类型
typedef struct {
	VerType  v[MaxNum];//顶点表
	ArcType a[MaxNum][MaxNum];//邻接矩阵
	int vernum, arcnum;	//点数,边数
}AMGraph;
//定位
int locate(AMGraph G, VerType v1) {
	for (int i = 0; i < G.vernum; i++) {
		if (G.v[i] == v1)
			return i;
	}
	return -1;
}
//创建无向图
bool createUDN(AMGraph &G) {
	cout << "Please input the vernum and arcnum" << endl;
	cin >> G.vernum >> G.arcnum;
	int i,j,p,q;
	VerType v1, v2;
	ArcType w;
	//初始化点的名字
	cout << "Please input the name of vernum in order " << endl;
	for (i = 0; i < G.vernum; i++) cin >> G.v[i];
	//初始化邻接矩阵
	for (i = 0; i < G.vernum; i++) {
		for (j = 0; j < G.vernum; j++) {
			if (i != j)
				G.a[i][j] = MaxInt;
			else 
				G.a[i][j] = 0;
		}	
	}
	//开始操作,构造邻接表
	cout << "Please input arcnum graph " << endl;
	for (i = 0; i < G.arcnum; i++) {
		cin >> v1 >> v2 >> w;
		p = locate(G, v1); q = locate(G, v2);
		G.a[p][q] = w;
		G.a[q][p] = G.a[p][q];
	}
	return true;
}
void show(AMGraph G) {
	int i, j;
	for (i = 0; i < G.vernum; i++) {
		for (j = 0; j < G.vernum; j++) {
//			if (G.a[i][j] == MaxInt)  printf("INF\t");
//			else printf("%-3d\t", G.a[i][j]);
		}
		cout << endl;
	}
}
int main() {
	AMGraph G;
	createUDN(G);
	show(G);
}
*/


//1.2 图的邻接表创建无向图存储
/*
邻接表:图的一种链式存储结构,在邻接表中,对每一个顶点都建立一个单链表,把于vi有关的点放在这个表里。
邻接表的第一个结点存放有关结点的信息,作为表头,
其余存放与边有关信息,邻接表由2部分组成,表头结点表和边表
(1)表头结点表:有所有表头结点以顺序形式存储,以便可以随机访问。表头结点包括数据域和链域,数据域存放顶点vi的名称或其他信息,
链域指向链表的第一个结点。
(2)边表:包括邻接点域(只是与顶点邻接的点在图中的位置)、数据域(权值等于边相关的信息)、链域(与顶点邻接的下一条边的结点)

*/
/*
typedef char VerTexType;
typedef int OtherType;
//边结构
typedef struct ArcNode {
	int nextPoint;//边指向顶点的位置
	struct ArcNode*nextarc;
	OtherType info;
}ArcNode;
//点信息
typedef struct VNode {
	VerTexType data;//数据
	ArcNode *firstarc;//由该点出发的第一条直线
}VNode,AdjList[MaxNum];
//图信息
typedef struct {
	AdjList vertices;
	int vernum, arcnum;//点数目,边数
}ALGraph;
//定位函数,返回的序号
int Locate(ALGraph G, VerTexType e) {
	for (int i = 0; i < G.vernum; i++) {
		if (G.vertices[i].data == e) {
			return i;
		}
	}
}
//采用邻接表表示法创建无向图
bool createUDG(ALGraph &G) {
	cout << "Please input  vernum and arcnum" << endl;
	cin >> G.vernum >> G.arcnum;
	int i = 0;
	cout << "Please input  name of Points" << endl;
	for (i = 0; i < G.vernum; i++) {
		cin >> G.vertices[i].data;//输入顶点值,例如名字或带的数字
		G.vertices[i].firstarc = NULL;//初始化为NULL
	}
	//输入各边
	VerTexType v1, v2;
	ArcNode *p1, *p2;
	int weight;
	int p, q;//v1,v2值所在的结点的下表
	cout << "Please input value1,value2,weight" << endl;
	for (i = 0; i < G.arcnum; i++) {
		cin >> v1 >> v2>>weight;//输入一条边依附的2个点的名字或数字
		p = Locate(G, v1); 
		q = Locate(G, v2);//序号
		//分别产生两个结点的第一条边
		//重要思想是,利用边的下一条边和下一个点的属性,把邻接点串通
		p1 = new ArcNode;
		p1->nextPoint = q; //邻接点序号
		p1->nextarc = G.vertices[p].firstarc;//下一条邻接的边作为自己的下一条边
		G.vertices[p].firstarc = p1;//和结点的第一条临近的边交换,相当于边的前插入法

		
		p2 = new ArcNode;
		p2->nextPoint = p;
		p2->nextarc = G.vertices[q].firstarc;
		G.vertices[q].firstarc = p2;
		p1->info = p2->info=weight;
	}
	return true;
}
void show(ALGraph &G) {
	ArcNode *p;	
	for (int i = 0; i < G.vernum; i++) {
		p = G.vertices[i].firstarc;
		while (p) {//以边为依据输出邻接点
			cout << "(" << i << "," << p->nextPoint << ") weight=" << p->info << ends;
			p = p->nextarc;
		}
		cout << endl;
	}
}
int main() {
	ALGraph G;
	createUDG(G);
	show(G);
}
*/

/*测试案例1:
7 9
0 1 2 3 4 5 6
0 1 28
0 5 10
1 2 16
1 6 14
5 4 25
4 6 24
4 3 22
2 3 12
3 6 18
*/

/*测试案例2:
7 9
a b c d e f g
a b 28
a f 10
b c 16
b g 14
f e 25
e g 24
e d 22
c d 12
d g 18
*/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值