我想使用深度优先和广度优先方法来遍历图形。我以前在一个简单的节点列表上做过这个,但我从来没有用邻接矩阵尝试过,老实说,我什至不知道从哪里开始。
这是我的矩阵:
999999999 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 999999999 0 3 1 0 0 0 0 0 0 0 0 0 0 0
1 0 999999999 3 0 1 0 0 0 0 0 0 0 0 0 0
0 3 3 999999999 0 0 0 8 0 0 0 0 0 0 0 0
0 1 0 0 999999999 0 1 3 0 0 0 0 0 0 0 0
0 0 1 0 0 999999999 0 3 1 0 0 0 0 0 0 0
0 0 0 0 1 0 999999999 0 0 3 0 1 0 0 0 0
0 0 0 8 3 3 0 999999999 0 8 8 0 3 0 0 0
0 0 0 0 0 1 0 0 999999999 0 3 0 0 1 0 0
0 0 0 0 0 0 3 8 0 999999999 0 0 0 0 3 0
0 0 0 0 0 0 0 8 3 0 999999999 0 0 0 0 3
0 0 0 0 0 0 1 0 0 0 0 999999999 0 0 1 0
0 0 0 0 0 0 0 3 0 0 0 0 999999999 0 1 1
0 0 0 0 0 0 0 0 1 0 0 0 0 999999999 0 1
0 0 0 0 0 0 0 0 0 3 0 1 1 0 999999999 0
0 0 0 0 0 0 0 0 0 0 3 0 1 1 0 999999999
这是我创建此矩阵的方法(C#):
private static int[,] CreateMatrix()
{
int A = 0;
int B = 1;
int C = 2;
int D = 3;
int E = 4;
int F = 5;
int G = 6;
int H = 7;
int I = 8;
int J = 9;
int K = 10;
int L = 11;
int M = 12;
int N = 13;
int O = 14;
int P = 15;
int[,] matrix = new int[16, 16];
matrix[A, B] = 1;
matrix[A, C] = 1;
matrix[B, D] = 3;
matrix[B, E] = 1;
matrix[C, D] = 3;
matrix[C, F] = 1;
matrix[D, H] = 8;
matrix[E, G] = 1;
matrix[E, H] = 3;
matrix[F, H] = 3;
matrix[F, I] = 1;
matrix[G, J] = 3;
matrix[G, L] = 1;
matrix[H, J] = 8;
matrix[H, K] = 8;
matrix[H, M] = 3;
matrix[I, K] = 3;
matrix[I, N] = 1;
matrix[J, O] = 3;
matrix[K, P] = 3;
matrix[L, O] = 1;
matrix[M, O] = 1;
matrix[M, P] = 1;
matrix[N, P] = 1;
matrix[B, A] = 1;
matrix[C, A] = 1;
matrix[D, B] = 3;
matrix[E, B] = 1;
matrix[D, C] = 3;
matrix[F, C] = 1;
matrix[H, D] = 8;
matrix[G, E] = 1;
matrix[H, E] = 3;
matrix[H, F] = 3;
matrix[I, F] = 1;
matrix[J, G] = 3;
matrix[L, G] = 1;
matrix[J, H] = 8;
matrix[K, H] = 8;
matrix[M, H] = 3;
matrix[K, I] = 3;
matrix[N, I] = 1;
matrix[O, J] = 3;
matrix[P, K] = 3;
matrix[O, L] = 1;
matrix[O, M] = 1;
matrix[P, M] = 1;
matrix[P, N] = 1;
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
if (matrix[i, j] == 0)
matrix[i, j] = 0;
if (i == j)
matrix[i, j] = 999999999;
}
}
return matrix;
}
任何帮助,将不胜感激!!
该矩阵表示的图形: