Skip to content

Commit 46f8ff4

Browse files
committed
Convert Vertex and Edge to records
1 parent 864cffb commit 46f8ff4

15 files changed

Lines changed: 70 additions & 130 deletions

src/main/java/com/github/refhumbold/algolib/graphs/DirectedSimpleGraph.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ public int getInputDegree(Vertex<VertexId> vertex)
4242
{
4343
return representation.getEdgesSet()
4444
.flatMap(edges -> edges.stream()
45-
.filter(edge -> edge.destination.equals(
46-
vertex)))
45+
.filter(edge -> edge.destination()
46+
.equals(vertex)))
4747
.mapToInt(edge -> 1)
4848
.sum();
4949
}
5050

5151
@Override
5252
public Edge<VertexId> addEdge(Edge<VertexId> edge, EdgeProperty property)
5353
{
54-
if(getEdge(edge.source, edge.destination) != null)
54+
if(getEdge(edge.source(), edge.destination()) != null)
5555
throw new IllegalArgumentException("Edge %s already exists".formatted(edge));
5656

5757
representation.addEdgeToSource(edge);
@@ -64,7 +64,7 @@ public void reverse()
6464
{
6565
GraphRepresentation<VertexId, VertexProperty, EdgeProperty> newRepresentation =
6666
new GraphRepresentation<>(
67-
getVertices().stream().map(v -> v.id).collect(Collectors.toList()));
67+
getVertices().stream().map(v -> v.id()).collect(Collectors.toList()));
6868

6969
representation.getVertices().forEach(vertex -> {
7070
newRepresentation.setProperty(vertex, representation.getProperty(vertex));
@@ -83,7 +83,7 @@ public DirectedGraph<VertexId, VertexProperty, EdgeProperty> reversedCopy()
8383
{
8484
DirectedSimpleGraph<VertexId, VertexProperty, EdgeProperty> reversedGraph =
8585
new DirectedSimpleGraph<>(
86-
getVertices().stream().map(v -> v.id).collect(Collectors.toList()));
86+
getVertices().stream().map(Vertex::id).collect(Collectors.toList()));
8787

8888
getVertices().forEach(
8989
vertex -> reversedGraph.getProperties().set(vertex, getProperties().get(vertex)));

src/main/java/com/github/refhumbold/algolib/graphs/Edge.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
package com.github.refhumbold.algolib.graphs;
22

3-
import java.util.Objects;
4-
53
/** Structure of graph edge. */
6-
public class Edge<VertexId>
4+
public record Edge<VertexId>(Vertex<VertexId> source, Vertex<VertexId> destination)
75
{
8-
public final Vertex<VertexId> source;
9-
public final Vertex<VertexId> destination;
10-
11-
public Edge(Vertex<VertexId> source, Vertex<VertexId> destination)
12-
{
13-
this.source = source;
14-
this.destination = destination;
15-
}
16-
176
/**
187
* Gets the neighbour of given adjacent vertex.
198
* @param vertex the vertex adjacent to this edge
@@ -41,24 +30,6 @@ public Edge<VertexId> reversed()
4130
return new Edge<>(destination, source);
4231
}
4332

44-
@Override
45-
public boolean equals(Object obj)
46-
{
47-
if(this == obj)
48-
return true;
49-
50-
if(!(obj instanceof Edge<?> other))
51-
return false;
52-
53-
return source.equals(other.source) && destination.equals(other.destination);
54-
}
55-
56-
@Override
57-
public int hashCode()
58-
{
59-
return Objects.hash(source, destination);
60-
}
61-
6233
@Override
6334
public String toString()
6435
{

src/main/java/com/github/refhumbold/algolib/graphs/Graph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface Graph<VertexId, VertexProperty, EdgeProperty>
5454
*/
5555
default Edge<VertexId> getEdge(Vertex<VertexId> source, Vertex<VertexId> destination)
5656
{
57-
return getEdge(source.id, destination.id);
57+
return getEdge(source.id(), destination.id());
5858
}
5959

6060
/**

src/main/java/com/github/refhumbold/algolib/graphs/GraphRepresentation.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Vertex<VertexId> getVertex(VertexId vertexId)
4646
{
4747
return graphMap.keySet()
4848
.stream()
49-
.filter(v -> v.id.equals(vertexId))
49+
.filter(v -> v.id().equals(vertexId))
5050
.findFirst()
5151
.orElse(null);
5252
}
@@ -55,12 +55,13 @@ Edge<VertexId> getEdge(VertexId sourceId, VertexId destinationId)
5555
{
5656
return graphMap.entrySet()
5757
.stream()
58-
.filter(entry -> entry.getKey().id.equals(sourceId))
58+
.filter(entry -> entry.getKey().id().equals(sourceId))
5959
.findFirst()
6060
.flatMap(entry -> entry.getValue()
6161
.stream()
62-
.filter(edge -> edge.getNeighbour(
63-
entry.getKey()).id.equals(destinationId))
62+
.filter(edge -> edge.getNeighbour(entry.getKey())
63+
.id()
64+
.equals(destinationId))
6465
.findFirst())
6566
.orElse(null);
6667
}
@@ -105,13 +106,13 @@ boolean addVertex(Vertex<VertexId> vertex)
105106
void addEdgeToSource(Edge<VertexId> edge)
106107
{
107108
validateEdgeVertices(edge);
108-
graphMap.get(edge.source).add(edge);
109+
graphMap.get(edge.source()).add(edge);
109110
}
110111

111112
void addEdgeToDestination(Edge<VertexId> edge)
112113
{
113114
validateEdgeVertices(edge);
114-
graphMap.get(edge.destination).add(edge);
115+
graphMap.get(edge.destination()).add(edge);
115116
}
116117

117118
private void validateVertex(Vertex<VertexId> vertex)
@@ -123,22 +124,22 @@ private void validateVertex(Vertex<VertexId> vertex)
123124

124125
private void validateEdgeVertices(Edge<VertexId> edge)
125126
{
126-
if(!graphMap.containsKey(edge.source))
127+
if(!graphMap.containsKey(edge.source()))
127128
throw new IllegalArgumentException(
128-
"Edge source %s does not belong to this graph".formatted(edge.source));
129+
"Edge source %s does not belong to this graph".formatted(edge.source()));
129130

130-
if(!graphMap.containsKey(edge.destination))
131+
if(!graphMap.containsKey(edge.destination()))
131132
throw new IllegalArgumentException(
132133
"Edge destination %s does not belong to this graph".formatted(
133-
edge.destination));
134+
edge.destination()));
134135
}
135136

136137
private void validateEdge(Edge<VertexId> edge)
137138
{
138139
validateEdgeVertices(edge);
139140

140-
if(!graphMap.get(edge.source).contains(edge) && !graphMap.get(edge.destination)
141-
.contains(edge))
141+
if(!graphMap.get(edge.source()).contains(edge) && !graphMap.get(edge.destination())
142+
.contains(edge))
142143
throw new IllegalArgumentException(
143144
"Edge %s does not belong to this graph".formatted(edge));
144145
}

src/main/java/com/github/refhumbold/algolib/graphs/MultipartiteGraph.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public MultipartiteGraph(int groupsCount, Collection<Collection<VertexId>> verte
2929
if(vertexIds.size() > this.groupsCount)
3030
throw new IllegalArgumentException(
3131
String.format("Cannot add vertices to group %d, graph contains only %d groups",
32-
vertexIds.size(), this.groupsCount));
32+
vertexIds.size(), this.groupsCount));
3333

3434
int i = 0;
3535

@@ -171,8 +171,9 @@ public Vertex<VertexId> addVertex(int groupNumber, Vertex<VertexId> vertex)
171171
* @return the created vertex
172172
* @throws IllegalArgumentException if vertex already exists
173173
*/
174-
public Vertex<VertexId> addVertex(
175-
int groupNumber, Vertex<VertexId> vertex, VertexProperty property)
174+
public Vertex<VertexId> addVertex(int groupNumber,
175+
Vertex<VertexId> vertex,
176+
VertexProperty property)
176177
{
177178
validateGroup(groupNumber);
178179

@@ -204,8 +205,9 @@ public Edge<VertexId> addEdgeBetween(Vertex<VertexId> source, Vertex<VertexId> d
204205
* @throws IllegalArgumentException if edge already exists
205206
* @throws GraphPartitionException if the vertices belong to the same group
206207
*/
207-
public Edge<VertexId> addEdgeBetween(
208-
Vertex<VertexId> source, Vertex<VertexId> destination, EdgeProperty property)
208+
public Edge<VertexId> addEdgeBetween(Vertex<VertexId> source,
209+
Vertex<VertexId> destination,
210+
EdgeProperty property)
209211
{
210212
return addEdge(new Edge<>(source, destination), property);
211213
}
@@ -232,7 +234,7 @@ public Edge<VertexId> addEdge(Edge<VertexId> edge)
232234
*/
233235
public Edge<VertexId> addEdge(Edge<VertexId> edge, EdgeProperty property)
234236
{
235-
if(areInSameGroup(edge.source, edge.destination))
237+
if(areInSameGroup(edge.source(), edge.destination()))
236238
throw new GraphPartitionException(
237239
"Cannot create an edge between vertices in the same group");
238240

@@ -252,6 +254,6 @@ private void validateGroup(int groupNumber)
252254
if(groupNumber < 0 || groupNumber >= groupsCount)
253255
throw new IndexOutOfBoundsException(
254256
String.format("Invalid group number %d, graph contains only %d groups",
255-
groupNumber, groupsCount));
257+
groupNumber, groupsCount));
256258
}
257259
}

src/main/java/com/github/refhumbold/algolib/graphs/UndirectedSimpleGraph.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public int getInputDegree(Vertex<VertexId> vertex)
4545
@Override
4646
public Edge<VertexId> addEdge(Edge<VertexId> edge, EdgeProperty property)
4747
{
48-
if(getEdge(edge.source, edge.destination) != null)
48+
if(getEdge(edge.source(), edge.destination()) != null)
4949
throw new IllegalArgumentException("Edge %s already exists".formatted(edge));
5050

5151
representation.addEdgeToSource(edge);
@@ -59,15 +59,15 @@ public DirectedSimpleGraph<VertexId, VertexProperty, EdgeProperty> asDirected()
5959
{
6060
DirectedSimpleGraph<VertexId, VertexProperty, EdgeProperty> directedSimpleGraph =
6161
new DirectedSimpleGraph<>(
62-
getVertices().stream().map(v -> v.id).collect(Collectors.toList()));
62+
getVertices().stream().map(Vertex::id).collect(Collectors.toList()));
6363

6464
getVertices().forEach(vertex -> directedSimpleGraph.getProperties()
6565
.set(vertex,
66-
getProperties().get(vertex)));
66+
getProperties().get(vertex)));
6767
getEdges().forEach(edge -> {
6868
directedSimpleGraph.addEdge(edge, getProperties().get(edge));
6969

70-
if(!edge.source.equals(edge.destination))
70+
if(!edge.source().equals(edge.destination()))
7171
directedSimpleGraph.addEdge(edge.reversed(), getProperties().get(edge));
7272
});
7373

src/main/java/com/github/refhumbold/algolib/graphs/Vertex.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,8 @@
11
package com.github.refhumbold.algolib.graphs;
22

3-
import java.util.Objects;
4-
53
/** Structure of graph vertex. */
6-
public class Vertex<VertexId>
4+
public record Vertex<VertexId>(VertexId id)
75
{
8-
public final VertexId id;
9-
10-
public Vertex(VertexId id)
11-
{
12-
this.id = id;
13-
}
14-
15-
@Override
16-
public boolean equals(Object obj)
17-
{
18-
if(this == obj)
19-
return true;
20-
21-
if(!(obj instanceof Vertex<?> other))
22-
return false;
23-
24-
return Objects.equals(id, other.id);
25-
}
26-
27-
@Override
28-
public int hashCode()
29-
{
30-
return Objects.hash(id);
31-
}
32-
336
@Override
347
public String toString()
358
{

src/main/java/com/github/refhumbold/algolib/graphs/algorithms/Matching.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,8 @@ private boolean dfs(Vertex<VertexId> vertex,
101101
{
102102
Vertex<VertexId> matched = matching.get(neighbour);
103103

104-
if(matched == null)
105-
{
106-
matching.put(vertex, neighbour);
107-
matching.put(neighbour, vertex);
108-
return true;
109-
}
110-
111-
if(!visited.contains(matched) && distances.get(matched) == distances.get(vertex) + 1
112-
&& dfs(matched, visited, distances))
104+
if(matched == null || (!visited.contains(matched) && (distances.get(matched) == (
105+
distances.get(vertex) + 1)) && dfs(matched, visited, distances)))
113106
{
114107
matching.put(vertex, neighbour);
115108
matching.put(neighbour, vertex);

src/main/java/com/github/refhumbold/algolib/graphs/algorithms/MinimalSpanningTree.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static <VertexId, VertexProperty, EdgeProperty extends Weighted> Undirect
2727
{
2828
UndirectedSimpleGraph<VertexId, VertexProperty, EdgeProperty> mst =
2929
new UndirectedSimpleGraph<>(
30-
graph.getVertices().stream().map(v -> v.id).collect(Collectors.toList()));
30+
graph.getVertices().stream().map(Vertex::id).collect(Collectors.toList()));
3131
DisjointSets<Vertex<VertexId>> vertexSets =
3232
new DisjointSets<>(graph.getVertices().stream().map(List::of).toList());
3333
PriorityQueue<Edge<VertexId>> edgeQueue = new PriorityQueue<>(
@@ -39,10 +39,10 @@ public static <VertexId, VertexProperty, EdgeProperty extends Weighted> Undirect
3939
{
4040
Edge<VertexId> edge = edgeQueue.remove();
4141

42-
if(!vertexSets.isSameSet(edge.source, edge.destination))
42+
if(!vertexSets.isSameSet(edge.source(), edge.destination()))
4343
mst.addEdge(edge, graph.getProperties().get(edge));
4444

45-
vertexSets.unionSet(edge.source, edge.destination);
45+
vertexSets.unionSet(edge.source(), edge.destination());
4646
}
4747

4848
return mst;
@@ -60,7 +60,7 @@ public static <VertexId, VertexProperty, EdgeProperty extends Weighted> Undirect
6060
{
6161
UndirectedSimpleGraph<VertexId, VertexProperty, EdgeProperty> mst =
6262
new UndirectedSimpleGraph<>(
63-
graph.getVertices().stream().map(v -> v.id).collect(Collectors.toList()));
63+
graph.getVertices().stream().map(v -> v.id()).collect(Collectors.toList()));
6464
Set<Vertex<VertexId>> visited = new HashSet<>();
6565
PriorityQueue<Pair<Edge<VertexId>, Vertex<VertexId>>> queue = new PriorityQueue<>(
6666
(pair1, pair2) -> Double.compare(graph.getProperties().get(pair1.first).getWeight(),

src/main/java/com/github/refhumbold/algolib/graphs/algorithms/ShortestPaths.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public static <VertexId, VertexProperty, EdgeProperty extends Weighted> Map<Vert
4141
for(int i = 0; i < graph.getVerticesCount() - 1; ++i)
4242
for(Vertex<VertexId> vertex : graph.getVertices())
4343
for(Edge<VertexId> edge : graph.getAdjacentEdges(vertex))
44-
distances.put(edge.destination, Math.min(distances.get(edge.destination),
44+
distances.put(edge.destination(), Math.min(distances.get(edge.destination()),
4545
distances.get(vertex) + graph.getProperties().get(edge).getWeight()));
4646

4747
for(Vertex<VertexId> vertex : graph.getVertices())
4848
for(Edge<VertexId> edge : graph.getAdjacentEdges(vertex))
4949
if(distances.get(vertex) < Weighted.INFINITY
5050
&& distances.get(vertex) + graph.getProperties().get(edge).getWeight()
51-
< distances.get(edge.destination))
51+
< distances.get(edge.destination()))
5252
throw new IllegalStateException("Graph contains a negative cycle.");
5353

5454
return distances;
@@ -123,7 +123,7 @@ public static <VertexId, VertexProperty, EdgeProperty extends Weighted> Map<Pair
123123
distances.put(Pair.of(v, u), v.equals(u) ? 0.0 : Weighted.INFINITY);
124124

125125
for(Edge<VertexId> e : graph.getEdges())
126-
distances.put(Pair.of(e.source, e.destination),
126+
distances.put(Pair.of(e.source(), e.destination()),
127127
graph.getProperties().get(e).getWeight());
128128

129129
for(Vertex<VertexId> w : graph.getVertices())

0 commit comments

Comments
 (0)