邻接矩阵求顶点度
【摘要】
#include <iostream>//蓝多多算法实验六
#include<malloc.h>
using namespace std;
#define MAXVEX 100//...
#include <iostream>//蓝多多算法实验六
#include<malloc.h>
using namespace std;
#define MAXVEX 100//最大顶点数
typedef char VertexType;//顶点类型
typedef int EdgeType;//边的权值
typedef struct
{
VertexType vexs[MAXVEX];//顶点表
EdgeType edges[MAXVEX][MAXVEX];//邻接矩阵
int n, e;//顶点数和边数
}MGraph;
MGraph CreateMGraph(int pd)//建立邻接矩阵
{
MGraph G;
int i, j, k, n;
cout << "请输入顶点数和边数:";
cin >> G.n >> G.e;
cout << "请输入顶点信息:";
for (i = 0; i < G.n; i++)
cin >> G.vexs[i];
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++)
G.edges[i][j] = 0;//初始化邻接矩阵
cout << "请输入每条边对应的两个顶点的序号及权重:\n例:0 1 2 表示标注的第0个顶点和第1个顶点之间有边且权重为2 (注意序号从0开始)\n";
for (n = 0; n < G.e; n++)
{
cin >> i >> j >> k;//输入边的信息
G.edges[i][j] = k;
if (pd == 0)//无向图,边是双向的
G.edges[j][i] = k;
}
return G;
}
void DisplayMGraph(MGraph G, int pd)//分行输出
{
for (int i = 0; i < G.n; i++)//第i个顶点(行)
{
cout << "第" << i + 1 << "行:";
for (int j = 0; j < G.n; j++)//第j列
if (pd == 0 && G.edges[i][j] == 0)
cout << "∞" << "\t";
else
cout << G.edges[i][j] << "\t";
cout << "\n";
}
}
int OutDegree(MGraph G, int i)//(出)度(求第i个顶点表中的结点数)
{
int degree = 0;
for (int j = 0; j < G.n; j++)
if (G.edges[i][j] != 0)
degree++;
return degree;
}
int InDegree(MGraph G, int i)//入度
{
int degree = 0;
for (int j = 0; j < G.n; j++)
if (G.edges[j][i] != 0)
degree++;
return degree;
}
void PrintOut(MGraph G, int pd)//度
{
int all;
if (pd == 0)//无向图
for (int i = 0; i < G.n; i++)
cout << "第" << i << "个顶点" << G.vexs[i] << "的度是" << OutDegree(G, i) << endl;
else//有向图
for (int i = 0; i < G.n; i++)
{
cout << "第" << i << "个顶点" << G.vexs[i] << "的出度是" << OutDegree(G, i) << ",入度是" << InDegree(G, i) << endl;
all = OutDegree(G, i) + InDegree(G, i);
cout << "第" << i << "个顶点" << G.vexs[i] << "的度是" << all << endl;
}
}
int main()
{
//判断是有向图还是无向图//
cout << "如果是无向图,请输入0;如果是有向图,请输入1:";
int pd;
cin >> pd;
if (pd != 0 && pd != 1)
{
cout << "输入有误,请退出重新输入0或1。";
return 0;
}
MGraph G = CreateMGraph(pd);
cout << "\n分行输出该邻接矩阵为:\n";
DisplayMGraph(G, pd);
PrintOut(G, pd);
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
文章来源: chuanchuan.blog.csdn.net,作者:川川菜鸟,版权归原作者所有,如需转载,请联系作者。
原文链接:chuanchuan.blog.csdn.net/article/details/118554858
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)