Class Name: 
kiasan.examples.disjsets.DisjSets

Report Rendered: Mon May 04 11:28:33 CDT 2009, by Sireum/Kiasan for Java v0.1.20090504

Methods Covered:
PercentRatio
Class / Method T E Instruction Coverage Branch Coverage Time
55 0
  18/18
  100%
  2/2
  100%
0.010s
20 0
  6/6
  100%
  0/0
  100%
0.000s
Total
75 0
  24/24
  100%
  2/2
  100%
0.010s
Source Code:
1   package kiasan.examples.disjsets;
2   
3   // DisjSets class
4   //
5   // CONSTRUCTION: with int representing initial number of sets
6   //
7   // ******************PUBLIC OPERATIONS*********************
8   // void union( root1, root2 ) --> Merge two sets
9   // int find( x )              --> Return set containing x
10  // ******************ERRORS********************************
11  // No error checking is performed
12  
13  /**
14   * Disjoint set class. (Package friendly so not used accidentally) Does not use
15   * union heuristics or path compression. Elements in the set are numbered
16   * starting at 0.
17   * 
18   * @author Mark Allen Weiss
19   * @see DisjSetsFast
20   */
21  public class DisjSets {
22    // Test main; all finds on same output line should be identical
23    public static void main(final String[] args) {
24      final int numElements = 128;
25      final int numInSameSet = 16;
26  
27      final DisjSets ds = new DisjSets(numElements);
28      int set1, set2;
29  
30      for (int k = 1; k < numInSameSet; k *= 2) {
31        for (int j = 0; j + k < numElements; j += 2 * k) {
32          set1 = ds.find(j);
33          set2 = ds.find(+ k);
34          ds.union(set1, set2);
35        }
36      }
37      System.out.println(ds.acyclic());
38  
39      for (int i = 0; i < numElements; i++) {
40        System.out.print(ds.find(i) + "*");
41        if (% numInSameSet == numInSameSet - 1) {
42          System.out.println();
43        }
44      }
45      System.out.println();
46    }
47  
48    private int[] s;
49  
50    public DisjSets() {
51    }
52  
53    /**
54     * Construct the disjoint sets object.
55     * 
56     * @param numElements
57     *          the initial number of disjoint sets.
58     */
59    public DisjSets(final int numElements) {
60      this.= new int[numElements];
61      for (int i = 0; i < this.s.length; i++) {
62        this.s[i] = -1;
63      }
64    }
65  
66    private boolean acyclic() {
67      final boolean[] visited = new boolean[this.s.length];
68      for (int i = 0; i < visited.length; i++) {
69        visited[i] = false;
70      }
71      final int[] parents = new int[this.s.length];
72  
73      for (int i = 0; i < this.s.length; i++) {
74        if (!visited[i]) {
75          int numParents = 0;
76          int currentIndex = i;
77          while (this.s[currentIndex] != -1) {
78            for (int j = 0; j < numParents - 1; j++) {
79              if (parents[j] == this.s[currentIndex]) {
80                return false;
81              }
82            }
83            parents[numParents] = this.s[currentIndex];
84            numParents++;
85            visited[currentIndex] = true;
86            currentIndex = this.s[currentIndex];
87          }
88        }
89      }
90      return true;
91    }
92  
93    public int find(final int x) {
94      if (this.s[x] < 0) {
95        return x;
96      } else {
97        return find(this.s[x]);
98      }
99    }
100 
101   /**
102    * Perform a find. Error checks omitted again for simplicity.
103    * 
104    * @param x
105    *          the element being searched for.
106    * @return the set containing x.
107    */
108   //@ requires (this.s != null) && goodValues() && acyclic() && (x >= 0) && (x < this.s.length);
109   //@ ensures this.s[\result] < 0;
110   public int Find(final int x) {
111     return find(x);
112   }
113 
114   boolean goodValues() {
115     boolean hasRoot = false;
116     for (int i = 0; i < this.s.length; i++) {
117       if (this.s[i] == -1) {
118         hasRoot = true;
119       } else if (!(((this.s[i] >= 0) && (this.s[i] < this.s.length)))) {
120         return false;
121       }
122     }
123     return hasRoot;
124   }
125 
126   /**
127    * Union two disjoint sets. For simplicity, we assume root1 and root2 are
128    * distinct and represent set names.
129    * 
130    * @param root1
131    *          the root of set 1.
132    * @param root2
133    *          the root of set 2.
134    */
135   //@ requires (this.s != null) && goodValues() && acyclic() && (root1 >= 0) && (root1 < this.s.length) && (root2 >= 0) && (root2 < this.s.length) && (root1 != root2) && (this.s[root1] == -1) && (this.s[root2] == -1);
136   //@ ensures find(root1) == find(root2);
137   public void union(final int root1, final int root2) {
138     this.s[root2] = root1;
139   }
140 }