Skip to content

Commit 5a10f94

Browse files
committed
Fixed formatting issues.
1 parent f615f9a commit 5a10f94

10 files changed

Lines changed: 569 additions & 510 deletions

File tree

algorithms-miscellaneous-3/pom.xml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<artifactId>algorithms-miscellaneous-3</artifactId>
55
<version>0.0.1-SNAPSHOT</version>
@@ -18,29 +18,29 @@
1818
<version>${org.assertj.core.version}</version>
1919
<scope>test</scope>
2020
</dependency>
21-
21+
2222
<dependency>
23-
<groupId>org.apache.commons</groupId>
24-
<artifactId>commons-collections4</artifactId>
25-
<version>${commons-collections4.version}</version>
23+
<groupId>org.apache.commons</groupId>
24+
<artifactId>commons-collections4</artifactId>
25+
<version>${commons-collections4.version}</version>
2626
</dependency>
27-
27+
2828
<dependency>
29-
<groupId>com.google.guava</groupId>
30-
<artifactId>guava</artifactId>
31-
<version>${guava.version}</version>
29+
<groupId>com.google.guava</groupId>
30+
<artifactId>guava</artifactId>
31+
<version>${guava.version}</version>
3232
</dependency>
3333

34-
<dependency>
35-
<groupId>com.squareup.retrofit2</groupId>
36-
<artifactId>retrofit</artifactId>
37-
<version>${retrofit.version}</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>com.squareup.retrofit2</groupId>
41-
<artifactId>converter-jackson</artifactId>
42-
<version>${retrofit.version}</version>
43-
</dependency>
34+
<dependency>
35+
<groupId>com.squareup.retrofit2</groupId>
36+
<artifactId>retrofit</artifactId>
37+
<version>${retrofit.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.squareup.retrofit2</groupId>
41+
<artifactId>converter-jackson</artifactId>
42+
<version>${retrofit.version}</version>
43+
</dependency>
4444
</dependencies>
4545

4646
<build>
@@ -59,6 +59,6 @@
5959
<org.assertj.core.version>3.9.0</org.assertj.core.version>
6060
<commons-collections4.version>4.3</commons-collections4.version>
6161
<guava.version>28.0-jre</guava.version>
62-
<retrofit.version>2.6.0</retrofit.version>
62+
<retrofit.version>2.6.0</retrofit.version>
6363
</properties>
6464
</project>

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Centroid.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,34 @@
88
*/
99
public class Centroid {
1010

11-
/**
12-
* The centroid coordinates.
13-
*/
14-
private final Map<String, Double> coordinates;
15-
16-
public Centroid(Map<String, Double> coordinates) {
17-
this.coordinates = coordinates;
18-
}
19-
20-
public Map<String, Double> getCoordinates() {
21-
return coordinates;
22-
}
23-
24-
@Override
25-
public boolean equals(Object o) {
26-
if (this == o) return true;
27-
if (o == null || getClass() != o.getClass()) return false;
28-
Centroid centroid = (Centroid) o;
29-
return Objects.equals(getCoordinates(), centroid.getCoordinates());
30-
}
31-
32-
@Override
33-
public int hashCode() {
34-
return Objects.hash(getCoordinates());
35-
}
36-
37-
@Override
38-
public String toString() {
39-
return "Centroid " + coordinates;
40-
}
11+
/**
12+
* The centroid coordinates.
13+
*/
14+
private final Map<String, Double> coordinates;
15+
16+
public Centroid(Map<String, Double> coordinates) {
17+
this.coordinates = coordinates;
18+
}
19+
20+
public Map<String, Double> getCoordinates() {
21+
return coordinates;
22+
}
23+
24+
@Override
25+
public boolean equals(Object o) {
26+
if (this == o) return true;
27+
if (o == null || getClass() != o.getClass()) return false;
28+
Centroid centroid = (Centroid) o;
29+
return Objects.equals(getCoordinates(), centroid.getCoordinates());
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return Objects.hash(getCoordinates());
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Centroid " + coordinates;
40+
}
4141
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Distance.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
*/
99
public interface Distance {
1010

11-
/**
12-
* Calculates the distance between two feature vectors.
13-
*
14-
* @param f1 The first set of features.
15-
* @param f2 The second set of features.
16-
* @return Calculated distance.
17-
* @throws IllegalArgumentException If the given feature vectors are invalid.
18-
*/
19-
double calculate(Map<String, Double> f1, Map<String, Double> f2);
11+
/**
12+
* Calculates the distance between two feature vectors.
13+
*
14+
* @param f1 The first set of features.
15+
* @param f2 The second set of features.
16+
* @return Calculated distance.
17+
* @throws IllegalArgumentException If the given feature vectors are invalid.
18+
*/
19+
double calculate(Map<String, Double> f1, Map<String, Double> f2);
2020
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/Errors.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
*/
99
public class Errors {
1010

11-
public static double sse(Map<Centroid, List<Record>> clustered, Distance distance) {
12-
double sum = 0;
13-
for (Map.Entry<Centroid, List<Record>> entry : clustered.entrySet()) {
14-
Centroid centroid = entry.getKey();
15-
for (Record record : entry.getValue()) {
16-
double d = distance.calculate(centroid.getCoordinates(), record.getFeatures());
17-
sum += Math.pow(d, 2);
18-
}
19-
}
11+
public static double sse(Map<Centroid, List<Record>> clustered, Distance distance) {
12+
double sum = 0;
13+
for (Map.Entry<Centroid, List<Record>> entry : clustered.entrySet()) {
14+
Centroid centroid = entry.getKey();
15+
for (Record record : entry.getValue()) {
16+
double d = distance.calculate(centroid.getCoordinates(), record.getFeatures());
17+
sum += Math.pow(d, 2);
18+
}
19+
}
2020

21-
return sum;
22-
}
21+
return sum;
22+
}
2323
}

algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/kmeans/EuclideanDistance.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
*/
88
public class EuclideanDistance implements Distance {
99

10-
@Override
11-
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
12-
if (f1 == null || f2 == null)
13-
throw new IllegalArgumentException("Feature vectors can't be null");
10+
@Override
11+
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
12+
if (f1 == null || f2 == null) throw new IllegalArgumentException("Feature vectors can't be null");
1413

15-
double sum = 0;
16-
for (String key : f1.keySet()) {
17-
Double v1 = f1.get(key);
18-
Double v2 = f2.get(key);
14+
double sum = 0;
15+
for (String key : f1.keySet()) {
16+
Double v1 = f1.get(key);
17+
Double v2 = f2.get(key);
1918

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

23-
return Math.sqrt(sum);
24-
}
22+
return Math.sqrt(sum);
23+
}
2524
}

0 commit comments

Comments
 (0)