Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebddd41

Browse files
authored
Create 图.md
1 parent a6da724 commit ebddd41

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

图.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
###
2+
3+
**邻接矩阵表示法**
4+
5+
*图的定义*
6+
7+
~~~cpp
8+
struct AMGraph
9+
{
10+
//图的顶点向量
11+
char Vexs[MAXSIZE];
12+
//图的邻接矩阵
13+
int Arcs[MAXSIZE];
14+
//图的总顶点数和总边数
15+
int vexnum, arcnum;
16+
};
17+
~~~
18+
19+
*创建无向网*
20+
21+
~~~cpp
22+
void CreatUDN(AMGraph &G)
23+
{
24+
//第一步:输入无向图的顶点数目
25+
cout << "input num" << endl;
26+
cin >> G.arcnum >> G.arcnum; //输入总顶点数和总边数
27+
//第二步:输入结点的名称,保存在一维数组中
28+
cout << "input vexs" << endl;
29+
for (int i = 0; i < G.vexnum; ++i)
30+
{
31+
cin >> G.vexs[i];
32+
}
33+
//第三步:将邻接矩阵的元素值置为无穷大
34+
for (int i = 0; i < G.arcnum; ++i)
35+
{
36+
for (int j = 0; j < G.arcnum; ++j)
37+
{
38+
G.arcs[i][j] = INT32_MAX;
39+
}
40+
}
41+
//第四步:输入顶点相互关系以及权重
42+
for (int k = 0; k < G.arcnum;++k)
43+
{
44+
int i, j, weight;
45+
char a, b;
46+
cin >> a >> b >> weight;
47+
//由输入的顶点a和b查找到对应的下标i,j
48+
i = LocateVex(G,a)
49+
j = LocateVex(G,b)
50+
G.arcs[i][j] = G.arcs[j][i] = weight;
51+
}
52+
}
53+
~~~
54+
55+
*LocateVex函数*
56+
57+
~~~cpp
58+
int LocateVex(AMGraph &G, const char &e)
59+
{
60+
for (int i = 0; i < G.vexnum;++i)
61+
{
62+
if(G.vexs[i]==e)
63+
return i;
64+
}
65+
return -1;
66+
}
67+
~~~
68+
69+
**邻接表表示法**
70+
71+
*定义*
72+
73+
~~~cpp
74+
//边表的定义
75+
struct ArcNode
76+
{
77+
int adjvex; //保存顶点的下标
78+
int weight; //保存边的权重
79+
ArcNode *nextarc; //指向下一个边结点
80+
};
81+
//顶点表的定义
82+
struct VNode
83+
{
84+
//数据域,存放顶点
85+
VecTexType data;
86+
//指针域,用于保存邻接表的
87+
ArcNode *firstarc;
88+
};
89+
//图的定义
90+
struct ALGraph
91+
{
92+
//定义一个数组,保存图的顶点
93+
VNode vexs[MAXSIZE];
94+
//定义两个变量,保存当前图的顶点个数以及边的条数
95+
int vexnum, arcnum;
96+
};
97+
~~~
98+
99+
*定位算法*
100+
101+
~~~cpp
102+
int LocateVex(ALGraph &G, const char &v)
103+
{
104+
for (int i = 0; i < G.vexnum; ++i)
105+
{
106+
if (G.vexs[i].data == v)
107+
return i;
108+
}
109+
return -1;
110+
}
111+
~~~
112+
113+
*创建无向图算法*
114+
115+
~~~cpp
116+
void CreatUDG(ALGraph &G)
117+
{
118+
//第一步:输入图的顶点个数以及边的条数
119+
cout << "info" << endl;
120+
cin >> G.vexnum >> G.arcnum;
121+
//第二步:给顶点向量赋值
122+
for (int i = 0; i < G.vexnum; ++i)
123+
{
124+
cin >> G.vexs[i].data;
125+
G.vexs[i].firstarc = nullptr;
126+
}
127+
//给每个顶点所含的边赋值
128+
for (int j = 0; j < G.arcnum; ++j)
129+
{
130+
cout << "input info about arc" << endl;
131+
char a, b;
132+
//int w;
133+
cin >> a >> b;
134+
int i = LocateVex(G, a);
135+
int j = LocateVex(G, b);
136+
//申请动态内存
137+
ArcNode *p = new ArcNode;
138+
p->adjvex = j;
139+
//p->weight = w;
140+
p->nextarc = G.vexs[i].firstarc;
141+
G.vexs[i].firstarc = p;
142+
143+
ArcNode *q = new ArcNode;
144+
q->adjvex = i;
145+
//q->weight = w;
146+
q->nextarc = G.vexs[j].firstarc;
147+
G.vexs[j].firstarc = q;
148+
}
149+
}
150+
~~~
151+

0 commit comments

Comments
 (0)