【问题标题】:Prim's Algorithem, please explainPrim的算法,请解释一下
【发布时间】: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


【解决方案1】:

查看 cmets 是否清除:

      while (countIncluded != vertices.length) { 

            //u represents a previous i value 
            int u = -1; //value serves as flag for "first time use"  

            //the purpose of this loop is to iterate over included array, which presumably 
            //if of the same length as vertices.
            for (int i = 0; i < vertices.length; i++) { 
                if (!included[i]) { //execute if the value of included[i] is false
                    /* execute if one of these apply:
                       u value is -1 : meaning that it is the first time 
                       included[i] is false (and there is not previous value to compare to).
                       OR C[u] > C[i] : previous vertex value  > current vertex value
                     */
                    if (u == -1 || C[u] > C[i])    
                                      u = i;     //keep i value, the index of the lowest vertex
                                                 //value so far 
                }
            }

            //at the end of the for loop u is the index of the lowest C[u]
            included[u] = true;
            countIncluded++;
     }

【讨论】:

    【解决方案2】:

    所以基本上这个算法正在做的是遍历一个顶点列表,并根据从一个顶点到另一个顶点的成本创建一条路径。成本只是解释从一个顶点到另一个顶点的难度的一个术语,通常只是距离。让我们进入代码。

    while (countIncluded != vertices.length) {
    

    我知道你说过你理解这意味着什么,但我还是会复习一遍。这个 while 循环将确保您遍历数组中的每个顶点,以便每个顶点都至少相互连接。

    int u = -1;
    for (int i = 0; i < vertices.length; i++) {
    

    我将这两行结合起来,因为第一行没有太多作用。变量u 是当前讨论顶点的索引。它最初设置为 -1,因为这不是数组中的有效位置。下一行,for 循环,只是遍历给定数组中的每个顶点。

    if (!included[i]) {
        if (u == -1 || C[u] > C[i])
            u = i;
    

    第一行只是检查i 的当前值,或者当前顶点是否已经包含在树中。如果是,我们就不需要再检查了,继续下一个。下一行首先检查u 是否等于-1。如上所述,-1 只是一个临时占位符值,此检查确保它始终指向一个有效的顶点。第二个检查是检查u 的成本是否大于i 的成本。这实际上是在做算法。它基本上做的是获取u 的成本,或临时顶点。然后根据i 的成本进行检查。如果i 的成本小于u 的成本,则将u 设置为i。在这样做的过程中,它会找到成本最低的顶点,因为它会在整个过程中记住u 的值。

    included[u] = true;
    countIncluded++;
    

    第一行将数组中u 的索引设置为true。这将确保不会在您的算法中再次检查它,以防止每次迭代都检查相同顶点的无限循环。之后,countIncluded 递增以跟踪当前添加的顶点数。

    我希望这会有所帮助!不要犹豫,要求我澄清任何事情!

    【讨论】:

    • 太棒了 :) 谢谢!帮了大忙。
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多