Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5807F Assignment #193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="lib" path="lib/junit-4_4.3.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="lib" path="lib/junit-4_4.3.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/bin/
/build/
/dist/
/bin/
/build/
/dist/
34 changes: 17 additions & 17 deletions .project
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaAlgorithmsImplementation</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaAlgorithmsImplementation</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
68 changes: 34 additions & 34 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
before_script:
- sudo apt-get install ant-optional
- export OPTS="-server -Xmx3072M"
- export JAVA_OPTS="${JAVA_OPTS} ${OPTS}"
- export ANT_OPTS="${ANT_OPTS} ${OPTS}"
- echo $JAVA_OPTS
- echo $ANT_OPTS

dist: trusty

language: java

jdk:
- oraclejdk9
- oraclejdk8
# - oraclejdk7
# - oraclejdk6

# - openjdk9
- openjdk8
- openjdk7
# - openjdk6

env:
- TEST_SUITE=run_tests
# - TEST_SUITE=mathematics
# - TEST_SUITE=numbers
# - TEST_SUITE=search
# - TEST_SUITE=sequences
# - TEST_SUITE=strings
# - TEST_SUITE=data_structures
# - TEST_SUITE=sorts

script: "ant $TEST_SUITE"
before_script:
- sudo apt-get install ant-optional
- export OPTS="-server -Xmx3072M"
- export JAVA_OPTS="${JAVA_OPTS} ${OPTS}"
- export ANT_OPTS="${ANT_OPTS} ${OPTS}"
- echo $JAVA_OPTS
- echo $ANT_OPTS
dist: trusty
language: java
jdk:
- oraclejdk9
- oraclejdk8
# - oraclejdk7
# - oraclejdk6
# - openjdk9
- openjdk8
- openjdk7
# - openjdk6
env:
- TEST_SUITE=run_tests
# - TEST_SUITE=mathematics
# - TEST_SUITE=numbers
# - TEST_SUITE=search
# - TEST_SUITE=sequences
# - TEST_SUITE=strings
# - TEST_SUITE=data_structures
# - TEST_SUITE=sorts
script: "ant $TEST_SUITE"
184 changes: 184 additions & 0 deletions DisjointSetTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package com.jwetherell.algorithms.data_structures.test;

import org.junit.Assert;
import org.junit.Test;

import com.jwetherell.algorithms.data_structures.DisjointSet;

@SuppressWarnings("unchecked")
public class DisjointSetTests {

private static boolean DEBUG = false;

@Test
public void testDisjointSet1() {
final int maximum = 10;
final int[] array = new int[maximum];
for (int i = 0; i < array.length; i++)
array[i] = i + 1;

final DisjointSet.Item<Integer>[] items = new DisjointSet.Item[array.length];
for (int i = 0; i < items.length; i++) {
final int v = array[i];
final DisjointSet.Item<Integer> s = DisjointSet.makeSet(v);
items[i] = s;
final DisjointSet.Item<Integer> f = DisjointSet.find(s);
Assert.assertTrue(f != null);
Assert.assertTrue(f.getValue() == v);
}
if (DEBUG)
System.out.println(toString(items));

final int half = items.length / 2;

// first half set
DisjointSet.Item<Integer> first = items[0];
for (int i = 1; i < half; i++) {
final DisjointSet.Item<Integer> item = items[i];
first = DisjointSet.union(first, item);
}
if (DEBUG)
System.out.println(toString(items));
// second half set
DisjointSet.Item<Integer> second = items[half];
for (int i = half + 1; i < items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
second = DisjointSet.union(second, item);
}
if (DEBUG)
System.out.println(toString(items));

// Make sure all items in first set are actually in first set and vice versa
for (int i = 0; i < half; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(first));
}
for (int i = half; i < items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(second));
}

// merge second set and the first set and make sure the superset is created
DisjointSet.Item<Integer> superSet = first;
for (int i = 0; i < items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
superSet = DisjointSet.union(superSet, item);
}
if (DEBUG)
System.out.println(toString(items));
for (int i = 0; i < items.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
final DisjointSet.Item<Integer> result = DisjointSet.find(item);
Assert.assertTrue(result.equals(superSet));
}
}

@Test
public void testDisjointSet2() {
final int maximum = 10;
final int[] array = new int[maximum];
for (int i = 0; i < array.length; i++)
array[i] = i + 1;

final DisjointSet.Item<Integer>[] items = new DisjointSet.Item[array.length];
for (int i = 0; i < items.length; i++) {
final int v = array[i];
final DisjointSet.Item<Integer> s = DisjointSet.makeSet(v);
items[i] = s;
final DisjointSet.Item<Integer> f = DisjointSet.find(s);
Assert.assertTrue(f != null);
Assert.assertTrue(f.getValue() == v);
}
if (DEBUG)
System.out.println(toString(items));

final int quarter = items.length / 4;

DisjointSet.Item<Integer> i1 = items[0];
for (int i = 1; i < quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i1 = DisjointSet.union(i1, item);
}
DisjointSet.Item<Integer> i2 = items[quarter];
for (int i = quarter + 1; i < 2 * quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i2 = DisjointSet.union(i2, item);
}
DisjointSet.Item<Integer> i3 = items[2 * quarter];
for (int i = 2 * quarter + 1; i < 3 * quarter; i++) {
final DisjointSet.Item<Integer> item = items[i];
i3 = DisjointSet.union(i3, item);
}
DisjointSet.Item<Integer> i4 = items[3 * quarter];
for (int i = 3 * quarter + 1; i < array.length; i++) {
final DisjointSet.Item<Integer> item = items[i];
i4 = DisjointSet.union(i4, item);
}
if (DEBUG)
System.out.println(toString(items));

DisjointSet.Item<Integer> s1 = DisjointSet.union(i1, i2);
DisjointSet.Item<Integer> s2 = DisjointSet.union(i3, i4);
DisjointSet.Item<Integer> s3 = DisjointSet.union(s1, s2);
Assert.assertTrue(s3.getValue() == 1 && s3.getRank() == 3);
if (DEBUG)
System.out.println(toString(items));
}

private static final String toString(DisjointSet.Item<Integer>[] items) {
final StringBuilder builder = new StringBuilder();
builder.append("{\n");
for (DisjointSet.Item<Integer> item : items) {
builder.append('\t').append(item.toString()).append('\n');
}
builder.append("}\n");
return builder.toString();
}

@Test
public void testDisjointSet3() {
final int maximum = 10;
final int[] arr = new int[maximum];
for (int i = 0; i < arr.length; i++)
arr[i] = i + 1;
final DisjointSet.Item<Integer>[] items = new DisjointSet.Item[arr.length];
for (int i = 0; i < items.length; i++) {
final int v = arr[i];
final DisjointSet.Item<Integer> s = DisjointSet.makeSet(v);
items[i] = s;
}
DisjointSet.Item<Integer> i1 = items[0];
DisjointSet.Item<Integer> i2 = items[1];
DisjointSet.Item<Integer> i3 = items[2];
DisjointSet.Item<Integer> i4 = items[3];

final DisjointSet.Item<Integer> xRoot = DisjointSet.union(null, i1);
Assert.assertTrue(xRoot.getValue() == i1.getValue());

final DisjointSet.Item<Integer> yRoot = DisjointSet.union(i2, null);
Assert.assertTrue(yRoot.getValue() == i2.getValue());

final DisjointSet.Item<Integer> equalRoots = DisjointSet.union(i2, i2);
Assert.assertTrue(equalRoots.getValue() == i2.getValue());

Assert.assertNull(DisjointSet.find(null));

Assert.assertNull(DisjointSet.union(null, null));

DisjointSet.Item<Integer> unequalIt1 = DisjointSet.union(i1, i2);
Assert.assertNotNull(unequalIt1);
DisjointSet.Item<Integer> unequalIt2 = DisjointSet.union(i1, i3);
Assert.assertNotNull(unequalIt2);
DisjointSet.Item<Integer> unequalIt3 = DisjointSet.union(i4, i1);
Assert.assertNotNull(unequalIt3);

Assert.assertFalse(unequalIt1.equals("False"));

String iString = i2.toString();
String expected = "parent=1 value=2";
Assert.assertTrue(iString.equals(expected));

}
}
Loading