算法第四版 用eclipse实现书中UnionFind例子

时间:2023-03-09 22:39:33
算法第四版 用eclipse实现书中UnionFind例子

一 安装环境

直接下载algs4.exe

下载完成后C:\Users\zle 下面会有algs4 文件夹

算法第四版 用eclipse实现书中UnionFind例子

原文:

Our installer downloads, installs, and configures the Java programming environment you will be using, including Java SE 7, DrJava, the textbook libraries, and the Command Prompt.

  • Log in to the user account in which you will be programming. Your account must have Administrator privileges and you must be connected to the Internet.
  • Download algs4.exe and double-click it to perform the installation. If you receive a User Account Control alert before the installation, click Yes or Allow; if you receive a Program Compatibility Assistant alert after the installation, click This program installed correctly.
  • If the installation succeeds, you will see the following:
    • A Command Prompt windows containing approximately this execution log
    • A Standard Drawing window containing a blue bullseye and textbook graphic.

    Note that the installation can take several minutes or longer if you have a slow internet connection.

  • Delete algs4.exe.

 二 将这个文件夹复制到eclipse工作环境下

算法第四版 用eclipse实现书中UnionFind例子

3 在eclipse中设置cmd环境

算法第四版 用eclipse实现书中UnionFind例子

选择 External tools configrations

算法第四版 用eclipse实现书中UnionFind例子

Name 名字随便取我的是UF1.5

Location 是cmd的目录

working director 默认的工作目录

三 编写程序并运行

 import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class WeightedQuickUnionUF {
private int[] id; // id[i] = parent of i
private int[] sz; // sz[i] = number of objects in subtree rooted at i
private int count; // number of components // Create an empty union find data structure with N isolated sets.
public WeightedQuickUnionUF(int N) {
count = N;
id = new int[N];
sz = new int[N];
for (int i = 0; i < N; i++) {
id[i] = i;
sz[i] = 1;
}
} // Return the number of disjoint sets.
public int count() {
return count;
} // Return component identifier for component containing p
public int find(int p) {
while (p != id[p])
p = id[p];
return p;
} // Are objects p and q in the same set?
public boolean connected(int p, int q) {
return find(p) == find(q);
} // Replace sets containing p and q with their union.
public void union(int p, int q) {
int i = find(p);
int j = find(q);
if (i == j) return; // make smaller root point to larger one
if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
else { id[j] = i; sz[i] += sz[j]; }
count--;
} public static void main(String[] args) {
int N = StdIn.readInt();
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N); // read in a sequence of pairs of integers (each in the range 0 to N-1),
// calling find() for each pair: If the members of the pair are not already
// call union() and print the pair.
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.connected(p, q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
StdOut.println("# components: " + uf.count());
} }

点击刚刚创建的UF1.5

算法第四版 用eclipse实现书中UnionFind例子

在console可以看到cmd

算法第四版 用eclipse实现书中UnionFind例子

进入bin 目录下 因为.class文件在bin目录下

算法第四版 用eclipse实现书中UnionFind例子

在命令框内输入

算法第四版 用eclipse实现书中UnionFind例子

折磨了好久终于搞好啦!

参考:http://algs4.cs.princeton.edu/windows/