图的基础应用:使用邻接矩阵存储并进行图的遍历
1.题目信息
在论坛处看到的一个提问,这两道题属于图的应用:题9需要通过邻接矩阵存储图顶点和边,题10可以在题9的基础上直接输出 或者 通过邻接表存储图顶点和边
2. 需要掌握的知识
图的存储和遍历
3.思路
-
题9是图的应用,通过二维数组存储数据
-
分析图顶点的特点,得出边的关系并存储。依据图示,对于顶点(i,j),它的上 下 左 右顶点可以归纳如下;对于有效边,需要满足 0<=i<=6 0<=j<=6
up:(i,j-1)
down:(i,j+1)
left:(i-1,j)
right:(i+1,j)
-
按要求打印
4. 代码框架(重点)
4.1 采用的数据结构
由题目输出决定,需要使用二维数组
#define max 49
int a[max][max];
};
4.2 程序框架
int main() //程序伪码描述
{
1.创建空图
2.获取有效边并存储进图
3.打印
}
4.3 主要分支函数
- CreatGraphic( )函数,创建一个空图,用于存储顶点和边数据
void CreatGraphic()
{
for(int i=0;i<max;i++)
for(int j=0;j<max;j++)
a[i][j]=0;
}
- 上下左右判定函数 是本题的核心函数,用于判定指定顶点(i,j) 是否具备有效的上下左右顶点,为找到有效边打基础
int up(int i,int j)
{
if ( boundary_min<=(j-1) && (j-1)<=boundary_max )
return ((j-1)*column+i);
else return Null;
}
int down(int i,int j)
{
if( boundary_min<=(j+1) && (j+1)<=boundary_max )
return ((j+1)*column+i);
else return Null;
}
int left(int i,int j)
{
if( boundary_min<=(i-1) && (i-1)<=boundary_max )
return (j*column+(i-1));
else return Null;
}
int right(int i,int j)
{
if( boundary_min<=(i+1) && (i+1)<=boundary_max )
return (j*column+(i+1));
else return Null;
}
5. 程序运行结果
题目9
0100000100000000000000000000000000000000000000000
1010000010000000000000000000000000000000000000000
0101000001000000000000000000000000000000000000000
0010100000100000000000000000000000000000000000000
0001010000010000000000000000000000000000000000000
0000101000001000000000000000000000000000000000000
0000010000000100000000000000000000000000000000000
1000000010000010000000000000000000000000000000000
0100000101000001000000000000000000000000000000000
0010000010100000100000000000000000000000000000000
0001000001010000010000000000000000000000000000000
0000100000101000001000000000000000000000000000000
0000010000010100000100000000000000000000000000000
0000001000001000000010000000000000000000000000000
0000000100000001000001000000000000000000000000000
0000000010000010100000100000000000000000000000000
0000000001000001010000010000000000000000000000000
0000000000100000101000001000000000000000000000000
0000000000010000010100000100000000000000000000000
0000000000001000001010000010000000000000000000000
0000000000000100000100000001000000000000000000000
0000000000000010000000100000100000000000000000000
0000000000000001000001010000010000000000000000000
0000000000000000100000101000001000000000000000000
0000000000000000010000010100000100000000000000000
0000000000000000001000001010000010000000000000000
0000000000000000000100000101000001000000000000000
0000000000000000000010000010000000100000000000000
0000000000000000000001000000010000010000000000000
0000000000000000000000100000101000001000000000000
0000000000000000000000010000010100000100000000000
0000000000000000000000001000001010000010000000000
0000000000000000000000000100000101000001000000000
0000000000000000000000000010000010100000100000000
0000000000000000000000000001000001000000010000000
0000000000000000000000000000100000001000001000000
0000000000000000000000000000010000010100000100000
0000000000000000000000000000001000001010000010000
0000000000000000000000000000000100000101000001000
0000000000000000000000000000000010000010100000100
0000000000000000000000000000000001000001010000010
0000000000000000000000000000000000100000100000001
0000000000000000000000000000000000010000000100000
0000000000000000000000000000000000001000001010000
0000000000000000000000000000000000000100000101000
0000000000000000000000000000000000000010000010100
0000000000000000000000000000000000000001000001010
0000000000000000000000000000000000000000100000101
0000000000000000000000000000000000000000010000010
题目10
- 方法一:可以在图9的基础上直接打印
- 方法二:通过邻接表实现(其中判定函数是通用的),后面有时间了需要按方法二实现一次
0 1 7
1 0 2 8
2 1 3 9
3 2 4 10
4 3 5 11
5 4 6 12
6 5 13
7 0 8 14
8 1 7 9 15
9 2 8 10 16
10 3 9 11 17
11 4 10 12 18
12 5 11 13 19
13 6 12 20
14 7 15 21
15 8 14 16 22
16 9 15 17 23
17 10 16 18 24
18 11 17 19 25
19 12 18 20 26
20 13 19 27
21 14 22 28
22 15 21 23 29
23 16 22 24 30
24 17 23 25 31
25 18 24 26 32
26 19 25 27 33
27 20 26 34
28 21 29 35
29 22 28 30 36
30 23 29 31 37
31 24 30 32 38
32 25 31 33 39
33 26 32 34 40
34 27 33 41
35 28 36 42
36 29 35 37 43
37 30 36 38 44
38 31 37 39 45
39 32 38 40 46
40 33 39 41 47
41 34 40 48
42 35 43
43 36 42 44
44 37 43 45
45 38 44 46
46 39 45 47
47 40 46 48
48 41 47
6. 完整代码(通过邻接矩阵存储图)
#include<iostream>
using namespace std;
#define row 7
#define column 7
#define max 49
#define boundary_min 0
#define boundary_max 6
#define Null -1
int a[max][max];
void CreatGraphic();
int up(int i,int j);
int down(int i,int j);
int left(int i,int j);
int right(int i,int j);
void Print_09();
void Print_10();
int main()
{
CreatGraphic();
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(up(i,j)!=Null)
{
a[(j)*column + i][up(i,j)] = 1;
a[up(i,j)][(j)*column + i] = 1;
}
if(down(i,j)!=Null)
{
a[(j)*column + i][down(i,j)] = 1;
a[down(i,j)][(j)*column + i] = 1;
}
if(left(i,j)!=Null)
{
a[(j)*column + i][left(i,j)] = 1;
a[left(i,j)][(j)*column + i] = 1;
}
if(right(i,j)!=Null)
{
a[(j)*column + i][right(i,j)] = 1;
a[right(i,j)][(j)*column + i] = 1;
}
}
}
Print_09();
Print_10();
return 0;
}
void Print_10()
{
for(int k=0;k<max;k++)
{
cout<<k<<' ';
for(int h=0;h<max;h++)
{
if(a[k][h])
cout<<h<<' ';
}
cout<<endl;
}
}
void Print_09()
{
for(int k=0;k<max;k++)
{
for(int h=0;h<max;h++)
cout<<a[k][h];
cout<<endl;
}
}
void CreatGraphic()
{
for(int i=0;i<max;i++)
for(int j=0;j<max;j++)
a[i][j]=0;
}
int up(int i,int j)
{
if ( boundary_min<=(j-1) && (j-1)<=boundary_max )
return ((j-1)*column+i);
else return Null;
}
int down(int i,int j)
{
if( boundary_min<=(j+1) && (j+1)<=boundary_max )
return ((j+1)*column+i);
else return Null;
}
int left(int i,int j)
{
if( boundary_min<=(i-1) && (i-1)<=boundary_max )
return (j*column+(i-1));
else return Null;
}
int right(int i,int j)
{
if( boundary_min<=(i+1) && (i+1)<=boundary_max )
return (j*column+(i+1));
else return Null;
}