【发布时间】:2019-08-27 08:57:28
【问题描述】:
有人可以向我解释一下这个循环中目前正在发生什么吗? 这是我在 Prim 算法上的作业代码的一部分。
我得到了:虽然 countIcluded 不是 vertices.length, 部分。
我需要了解下面发生的事情。
值得一提的是,included 是一个布尔数组。
希望我是新手,因为我是新手,请尽可能简单地解释(如果可能的话),以便我能够理解基础知识。
while (countIncluded != vertices.length) {
// find the not included vertex with minimum cost
int u = -1;
for (int i = 0; i < vertices.length; i++) {
if (!included[i]) {
if (u == -1 || C[u] > C[i])
u = i;
}
}
// include in MST
included[u] = true;
countIncluded++;
}
【问题讨论】:
-
请Edit添加循环结束(
}) -
你有什么不明白的?
-
你到底有什么不明白的? for循环?如果条件?它基本上是“遍历所有顶点,如果不包括索引 i 处的顶点并获取第一个顶点 (u == -1) 或具有最大 C (C[u] > C[i]) 的顶点 - 然后标记包含索引 u 处的顶点”。
-
你为什么不问问给你任务的人?
-
这可能有助于显示
C[]在问题中声明的内容,这样人们就不必猜测了。我没有练习过这个算法,所以它可能很明显。
标签: java arrays algorithm boolean prims-algorithm