forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:eugenp/tutorials into feature/BAE…
…L-2774-Run-Java-main-method-using-Gradle
- Loading branch information
Showing
199 changed files
with
6,138 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...eous-3/src/main/java/com/baeldung/algorithms/interpolationsearch/InterpolationSearch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Distance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
23 changes: 23 additions & 0 deletions
23
algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Errors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.