Skip to content

Commit c39994f

Browse files
committed
Merge branch 'feature/graph-prim' into develop
2 parents cd18c84 + 2b46430 commit c39994f

File tree

2 files changed

+53
-7
lines changed
  • java-algorithm/src

2 files changed

+53
-7
lines changed

java-algorithm/src/main/java/com/example/algorithm/graph/Prim.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.algorithm.graph;
22

3+
import com.google.common.base.Objects;
4+
35
import java.util.HashSet;
46
import java.util.LinkedHashSet;
57
import java.util.PriorityQueue;
@@ -126,13 +128,32 @@ public void addEdge(Edge edge) {
126128
}
127129
}
128130

129-
public static class Vertex {
131+
public static class Vertex implements Comparable<Vertex> {
130132
char key;
131133
Set<Edge> edges = new HashSet<>();
132134

133135
public Vertex(char key) {
134136
this.key = key;
135137
}
138+
139+
@Override
140+
public int compareTo(Vertex other) {
141+
return Character.compare(this.key, other.key);
142+
}
143+
144+
@Override
145+
public boolean equals(Object o) {
146+
if (this == o) return true;
147+
if (o == null || getClass() != o.getClass()) return false;
148+
Vertex vertex = (Vertex) o;
149+
return key == vertex.key
150+
&& Objects.equal(edges, vertex.edges);
151+
}
152+
153+
@Override
154+
public int hashCode() {
155+
return Objects.hashCode(key);
156+
}
136157
}
137158

138159
public static class Edge implements Comparable<Edge> {
@@ -148,7 +169,22 @@ public Edge(Vertex source, Vertex sink, int weight) {
148169

149170
@Override
150171
public int compareTo(Edge other) {
151-
return Integer.compare(weight, other.weight);
172+
return Integer.compare(this.weight, other.weight);
173+
}
174+
175+
@Override
176+
public boolean equals(Object o) {
177+
if (this == o) return true;
178+
if (o == null || getClass() != o.getClass()) return false;
179+
Edge edge = (Edge) o;
180+
return weight == edge.weight
181+
&& Objects.equal(source, edge.source)
182+
&& Objects.equal(sink, edge.sink);
183+
}
184+
185+
@Override
186+
public int hashCode() {
187+
return Objects.hashCode(source, sink, weight);
152188
}
153189
}
154190
}

java-algorithm/src/test/java/com/example/algorithm/graph/PrimTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
810

911
class PrimTest {
1012
Prim.Graph graph;
@@ -69,14 +71,22 @@ void tearDown() {
6971
@Test
7072
void primAlgorithmVertex() {
7173
var expected = new Prim.Vertex[]{vertexA, vertexB, vertexC, vertexI, vertexF, vertexG, vertexH, vertexD, vertexE};
72-
var actual = Prim.primAlgorithmVertex(graph, vertexA);
73-
assertArrayEquals(expected, actual);
74+
var vertices = Prim.primAlgorithmVertex(graph, vertexA);
75+
Arrays.sort(expected);
76+
Arrays.sort(vertices);
77+
for (int i = 0; i < expected.length; i++) {
78+
assertEquals(expected[i].key, vertices[i].key);
79+
}
7480
}
7581

7682
@Test
7783
void primAlgorithmEdge() {
7884
var expected = new Prim.Edge[]{edgeAB, edgeBC, edgeCI, edgeCF, edgeFG, edgeGH, edgeCD, edgeDE};
79-
var actual = Prim.primAlgorithmEdge(graph, vertexA);
80-
assertArrayEquals(expected, actual);
85+
var edges = Prim.primAlgorithmEdge(graph, vertexA);
86+
Arrays.sort(expected);
87+
Arrays.sort(edges);
88+
for (int i = 0; i < expected.length; i++) {
89+
assertEquals(expected[i].weight, edges[i].weight);
90+
}
8191
}
8292
}

0 commit comments

Comments
 (0)