- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个程序来找到最小生成树。但是我在使用该算法时遇到的一个问题是测试电路。在 Java 中执行此操作的最佳方法是什么。
好的,这是我的代码
import java.io.*;
import java.util.*;
public class JungleRoads
{
public static int FindMinimumCost(ArrayList graph,int size)
{
int total = 0;
int [] marked = new int[size]; //keeps track over integer in the mst
//convert an arraylist to an array
List<String> wrapper = graph;
String[] arrayGraph = wrapper.toArray(new String[wrapper.size()]);
String[] temp = new String[size];
HashMap visited = new HashMap();
for(int i = 0; i < size; i++)
{
// System.out.println(arrayGraph[i]);
temp = arrayGraph[i].split(" ");
//loop over connections of a current node
for(int j = 2; j < Integer.parseInt(temp[1])*2+2; j++)
{
if(temp[j].matches("[0-9]+"))
{
System.out.println(temp[j]);
}
}
}
graph.clear();
return total;
}
public static void main(String[] args) throws IOException
{
FileReader fin = new FileReader("jungle.in");
BufferedReader infile = new BufferedReader(fin);
FileWriter fout = new FileWriter("jungle.out");
BufferedWriter outfile = new BufferedWriter(fout);
String line;
line = infile.readLine();
ArrayList graph = new ArrayList();
do
{
int num = Integer.parseInt(line);
if(num!= 0)
{
int size = Integer.parseInt(line)-1;
for(int i=0; i < size; i++)
{
line = infile.readLine();
graph.add(line);
}
outfile.write(FindMinimumCost(graph, size));
}
line = infile.readLine();
}while(!line.equals("0"));
}
}
最佳答案
Kruskall 算法不搜索循环,因为它的性能效率不高;而是创建不相交的树,然后将它们连接起来。由于用一条边连接两个不同的子树会创建一棵新树,因此无需检查循环。
如果你看wiki page算法如下:
1. create a forest **F** (a set of trees), where each vertex in the graph is a separate tree
2. create a set S containing all the edges in the graph
3. while S is nonempty and F is not yet spanning
a. remove an edge with minimum weight from S
b. if that edge connects two different trees, then add it to the forest, combining
two trees into a single tree
c. otherwise discard that edge.
你应该使用 Disjoint Set Data Structure为了这。再次来自维基:
first sort the edges by weight using a comparison sort in O(E log E)time; this allows the step "remove an edge with minimum weight from S"to operate in constant time. Next, we use a disjoint-set datastructure (Union&Find) to keep track of which vertices are in whichcomponents. We need to perform O(E) operations, two 'find' operationsand possibly one union for each edge. Even a simple disjoint-set datastructure such as disjoint-set forests with union by rank can performO(E) operations in O(E log V) time. Thus the total time is O(E log E)= O(E log V).
#创建不相交的森林现在你可以看看Boost Graph Library-Incremental Components部分。你应该实现一些方法:MakeSet,Find,Union,然后你就可以实现Kruskall的算法。您所做的只是处理集合,最简单的方法是使用链表。
每个集合都有一个名为代表元素的元素,它是集合中的第一个元素。
1-首先通过链表实现MakeSet:
This prepares the disjoint-sets data structure for the incrementalconnected components algorithm by making each vertex in the graph amember of its own component (or set).
只需将每个顶点(元素)初始化为新集合的代表元素,我们可以通过将它们设置为自己的父节点来实现:
function MakeSet(x)
x.parent := x
2- 实现查找方法:
查找包含顶点 x
的集合的代表性元素:
function Find(x)
if x.parent == x
return x
else
return Find(x.parent)
if
部分检查元素是否为代表元素。我们将集合的所有代表性元素设置为它们的第一个元素,方法是将它们设置为自己的父元素。
3- 最后,当所有前面的步骤都完成后,简单的部分就是实现 Union 方法:
function Union(x, y)
xRoot := Find(x) // find representative element of first element(set)
yRoot := Find(y) // find representative element of second element(set)
xRoot.parent := yRoot // set representative element of first set
// as same as representative element of second set
现在您应该如何运行 Kruskall?
首先通过MakeSet 方法将所有节点放入n
个不相交的集合中。在每次迭代中找到想要的边(未标记的和最小的边)后,通过Find方法找到其端点顶点的相关集合(它们的代表元素),如果它们相同,则丢弃这条边,因为这edge 会导致循环,但如果它们在不同的集合中,则使用 Union 方法合并这些集合。由于每个集合都是一棵树,因此它们的联合也是一棵树。
您可以通过为不相交的集合选择更好的数据结构来优化它,但现在我认为这已经足够了。如果你对更高级的数据结构感兴趣,你可以实现 rank 基本方法,在 wiki 中有一个很好的关于它的文档,很简单但我没有提到它以防止混淆。
关于java - 实现 Kruskal 算法时测试电路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9459938/
我正在尝试用 Python (scipy) 替换 Python 脚本中的一些 rpy2 代码。在这种情况下,我需要用 (Python: kruskal.test() ) 替换 Kruskal-Wall
一 构建后的图 二 代码 package graph.kruskal; import java.util.ArrayList; import java.util.Collections; impor
一 点睛 构造最小生成树还有一种算法,即 Kruskal 算法:设图 G=(V,E)是无向连通带权图,V={1,2,...n};设最小生成树 T=(V,TE),该树的初始状态只有 n 个节点而无边的非
来这里之前我已经努力尝试并做了我的研究。下面的代码崩溃了,我怀疑滥用了 cout。 (这不应该是最好的实现,但暂时不是 pb) 有经验的人能看出问题出在哪里吗? 提前致谢 #include #inc
据说Kruskal构造MST的算法是贪心的,但是该算法选择的是全局最小值而不是局部最小值,这与Prim的算法不同。谁能解释一下 Kruskal 算法为何被视为贪婪算法? 最佳答案 我们在克鲁斯卡尔做什
假设G是一个有n个顶点的无向图,每对顶点之间有加权边。你能按以下结构构造一棵树吗: v_1-v_2-v_3-...-v_n 使得树中的每个节点对应于 G 中的一个顶点,并且每个节点除叶子外只有一个子节
我正在学习 Kruskal 的算法,我遇到了几个不同的实现,并且想知道它们之间的权衡是什么。两种实现方式如下: 实现一- 将图中的所有边放入优先队列 PQ- 从 PQ 中移除最小边 e - 如果 e
我对什么是 Kruskal 算法有了基本的了解,这就是我的发现: 该算法基本上是通过合并多棵树来构造一棵最小生成树,它首先根据边的权重对边进行排序。该算法从一个空子图开始,扫描边列表,如果不创建循环,
我正在研究来自 this geeksforgeeks article 的 Kruskal 的 MST .给出的步骤是: 按权重的非降序对所有边进行排序。 选择最小的边。检查它是否与目前形成的生成树形成
我有一个任务,我需要从单元格网格创建一个迷宫。 我使用 Randomized Kruskal's algorithm 成功地做到了如 Wiki 页面所述并使用 Disjoint-set data st
我找到了一个创建 makes 和 finds 方法的教程 public void makeSet(long data) { Node node = new Node();
我发现最小生成树 (MST) 的某些边使用联合查找方法重叠,详见 here ,经过修改 - 使用 float 而不是 integer 权重,使用 integer 值而不是 string ID。下图中的
我打算用 C++ 实现 Kriskal 的算法,但是... Unhandled exception at 0x0127160d in DAA.exe: 0xC0000005: Access viola
我正在计算这样的 kruskal 算法的时间复杂度(请参阅附件中的算法) T(n) = O(1) + O(V) + O(E log E) + O(V log V) = O(E log E)
我正在尝试尽可能高效地实现 Kruskal。 对于运行时效率,使用堆或排序算法对边进行排序有区别吗? 还有哪些其他技术可以使 Kruskal 算法更有效地工作? 最佳答案 这取决于您要解决的确切问题。
我如何使用 Kruskal 算法计算 im R(3.0.0 - Linux x32) 最小生成树? 我使用 igraph (0.6.5) 库创建一个加权全图,如下所示: set.seed(123456
当图有多个连通分量时,我不确定如何实现 Kruskal 算法 根据我对 Kruskal 算法的理解,它反复将最小边添加到集合中。然后,当检查完所有边时,它返回最多的边集。 但是,如果我的图表断开连接怎
虽然我在工作an assignment在 Stanford CS106B C++ 类(class)上,但我大量坚持实现 Kruskal 算法以找到最小生成树。 更具体地说,我无法弄清楚确定是否向树添加
由于 Kruskal 算法是贪心算法,这意味着我们有时可能得不到真正的最小路径。 但是谁能举出一个例子来说明这个算法没有得到最小值? 我不是在寻找复杂性,只是需要一个确实没有得到最佳解决方案的案例。
Here is a Graph我需要使用 Prim 的 和 Kruskal 的 算法找到 G 的最小生成树。 我使用 Prim 算法找到了最小生成树。 Here is my attempt . 我在使
我是一名优秀的程序员,十分优秀!