Skip to content

Commit

Permalink
Merge branch 'master' of github.com:eugenp/tutorials into feature/BAE…
Browse files Browse the repository at this point in the history
…L-2774-Run-Java-main-method-using-Gradle
  • Loading branch information
Sorin Zamfir committed Aug 19, 2019
2 parents 1e59e56 + 2c7c520 commit d8b0c63
Show file tree
Hide file tree
Showing 199 changed files with 6,138 additions and 553 deletions.
30 changes: 21 additions & 9 deletions algorithms-miscellaneous-3/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-miscellaneous-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
Expand All @@ -18,17 +18,28 @@
<version>${org.assertj.core.version}</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>${retrofit.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>${retrofit.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -61,5 +72,6 @@
<org.assertj.core.version>3.9.0</org.assertj.core.version>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>28.0-jre</guava.version>
<retrofit.version>2.6.0</retrofit.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baeldung.algorithms.interpolationsearch;

public class InterpolationSearch {

public static int interpolationSearch(int[] data, int item) {

int highEnd = (data.length - 1);
int lowEnd = 0;

while (item >= data[lowEnd] && item <= data[highEnd] && lowEnd <= highEnd) {

int probe = lowEnd + (highEnd - lowEnd) * (item - data[lowEnd]) / (data[highEnd] - data[lowEnd]);

if (highEnd == lowEnd) {
if (data[lowEnd] == item) {
return lowEnd;
} else {
return -1;
}
}

if (data[probe] == item) {
return probe;
}

if (data[probe] < item) {
lowEnd = probe + 1;
} else {
highEnd = probe - 1;
}
}
return -1;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.baeldung.algorithms.kmeans;

import java.util.Map;
import java.util.Objects;

/**
* Encapsulates all coordinates for a particular cluster centroid.
*/
public class Centroid {

/**
* The centroid coordinates.
*/
private final Map<String, Double> coordinates;

public Centroid(Map<String, Double> coordinates) {
this.coordinates = coordinates;
}

public Map<String, Double> getCoordinates() {
return coordinates;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Centroid centroid = (Centroid) o;
return Objects.equals(getCoordinates(), centroid.getCoordinates());
}

@Override
public int hashCode() {
return Objects.hash(getCoordinates());
}

@Override
public String toString() {
return "Centroid " + coordinates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.algorithms.kmeans;

import java.util.Map;

/**
* Defines a contract to calculate distance between two feature vectors. The less the
* calculated distance, the more two items are similar to each other.
*/
public interface Distance {

/**
* Calculates the distance between two feature vectors.
*
* @param f1 The first set of features.
* @param f2 The second set of features.
* @return Calculated distance.
* @throws IllegalArgumentException If the given feature vectors are invalid.
*/
double calculate(Map<String, Double> f1, Map<String, Double> f2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.algorithms.kmeans;

import java.util.List;
import java.util.Map;

/**
* Encapsulates methods to calculates errors between centroid and the cluster members.
*/
public class Errors {

public static double sse(Map<Centroid, List<Record>> clustered, Distance distance) {
double sum = 0;
for (Map.Entry<Centroid, List<Record>> entry : clustered.entrySet()) {
Centroid centroid = entry.getKey();
for (Record record : entry.getValue()) {
double d = distance.calculate(centroid.getCoordinates(), record.getFeatures());
sum += Math.pow(d, 2);
}
}

return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.algorithms.kmeans;

import java.util.Map;

/**
* Calculates the distance between two items using the Euclidean formula.
*/
public class EuclideanDistance implements Distance {

@Override
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
if (f1 == null || f2 == null) {
throw new IllegalArgumentException("Feature vectors can't be null");
}

double sum = 0;
for (String key : f1.keySet()) {
Double v1 = f1.get(key);
Double v2 = f2.get(key);

if (v1 != null && v2 != null) sum += Math.pow(v1 - v2, 2);
}

return Math.sqrt(sum);
}
}
Loading

0 comments on commit d8b0c63

Please sign in to comment.