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

一 安装环境

直接下载algs4.exe

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

原文:

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工作环境下

3 在eclipse中设置cmd环境

选择 External tools configrations

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

Location 是cmd的目录

working director 默认的工作目录

 

三 编写程序并运行

 1 import edu.princeton.cs.algs4.StdIn;
 2 import edu.princeton.cs.algs4.StdOut;
 3 
 4 public class WeightedQuickUnionUF {
 5     private int[] id;    // id[i] = parent of i
 6     private int[] sz;    // sz[i] = number of objects in subtree rooted at i
 7     private int count;   // number of components
 8 
 9     // Create an empty union find data structure with N isolated sets.
10     public WeightedQuickUnionUF(int N) {
11         count = N;
12         id = new int[N];
13         sz = new int[N];
14         for (int i = 0; i < N; i++) {
15             id[i] = i;
16             sz[i] = 1;
17         }
18     }
19 
20     // Return the number of disjoint sets.
21     public int count() {
22         return count;
23     }
24 
25     // Return component identifier for component containing p
26     public int find(int p) {
27         while (p != id[p])
28             p = id[p];
29         return p;
30     }
31 
32    // Are objects p and q in the same set?
33     public boolean connected(int p, int q) {
34         return find(p) == find(q);
35     }
36 
37   
38    // Replace sets containing p and q with their union.
39     public void union(int p, int q) {
40         int i = find(p);
41         int j = find(q);
42         if (i == j) return;
43 
44         // make smaller root point to larger one
45         if   (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
46         else                 { id[j] = i; sz[i] += sz[j]; }
47         count--;
48     }
49 
50 
51     public static void main(String[] args) {
52         int N = StdIn.readInt();
53         WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);
54 
55         // read in a sequence of pairs of integers (each in the range 0 to N-1),
56          // calling find() for each pair: If the members of the pair are not already
57         // call union() and print the pair.
58         while (!StdIn.isEmpty()) {
59             int p = StdIn.readInt();
60             int q = StdIn.readInt();
61             if (uf.connected(p, q)) continue;
62             uf.union(p, q);
63             StdOut.println(p + " " + q);
64         }
65         StdOut.println("# components: " + uf.count());
66     }
67 
68 }

点击刚刚创建的UF1.5

在console可以看到cmd

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

 

 在命令框内输入

折磨了好久终于搞好啦!

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

posted @ 2016-11-20 22:23  乐乐章  阅读(825)  评论(0编辑  收藏  举报