1 回答
TA贡献1909条经验 获得超7个赞
您应该引入一个布尔数组,以指示某个顶点是否已经被访问。此外,应检查数组是否遵循边并在更新后进行更新。这可以如下进行。
public int[] graph1VertexList = new int[] { 1, 2, 3, 4, 5, 6, 7 };
public int[,] graph1 = new int[7, 7];
public bool[] visited = new bool[7]; // new array, initialized to false
public void bfs()
{
Console.Write(graph1VertexList[0]);//print the first value
for (int i = 0; i < graph1VertexList.Length; i++)
{
for (int z = 0; z < graph1VertexList.Length; z++)
{
if (graph1[i, z] == 1 && !visited[z]) // check for visit
{
visited[z] = true; // mark as visited
Console.Write(graph1VertexList[z]);
}
}
}
}
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报