【问题标题】:I am implementing a Minimum Spanning Forrest algorithm in Java. But stuck on how to write a loop我正在用 Java 实现最小跨度 Forrest 算法。但坚持如何编写循环
【发布时间】:2025-12-03 19:10:01
【问题描述】:

算法:

输入:图G

输出: MST 集 T

开始

T=空; E=G.边;

对于 G 中的所有顶点, 创建具有单个顶点 b 的树 t

将 t 添加到 T

结束

 repeat
    Find an edge e ∈ E having minimum weight
    such that one end belongs to t ∈ T and the other
    end does not belongs to any of the trees in T
    Add e to t
  until e = NULL

我卡在突出显示块的逻辑上。 我对顶点、边和树使用了简单的对象。对于他们的集合,使用了对象数组。

我有代码:

      Tree[] findMSF(){
   T=new Tree[numofMST];
   E=new Edge[C.v.length];
   for(int i=0;i<E.length;i++){
       E[i]=C.e[i];//E ← C.Edges
   }
   for(int i=0;i<B.length;i++){

       t=new Tree(B[i].v);//Create a tree t having single vertex b
       T[i]=t;// T ← T U t

   }

   do{
     e=find_e(E);

   }while(e!=null);

    return T;
    }

我需要 find_e(E) 的实现;

【问题讨论】:

    标签: java algorithm graph-algorithm


    【解决方案1】:

    使用 for 循环。

    for (int i = 1; i < 10; i++) { Your code } 要更改重复次数,请将 10 更改为另一个数字。

    【讨论】:

    • 我知道循环是如何实现的。但我需要块的比较逻辑