Skip to content

Commit 43c2555

Browse files
committed
Merge branch 'feature/type' into develop
2 parents 40ee2b7 + 538d0b2 commit 43c2555

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+232
-225
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public class BellmanFord1 {
1616
* @return the predecessor array of the shortest path
1717
*/
1818
public static int[] bellmanFordAlgorithm(int[][] edges, int[] weight, int source) {
19-
var vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
20-
var distance = new int[vertices.length];
21-
var predecessor = new int[vertices.length];
19+
int[] vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
20+
int[] distance = new int[vertices.length];
21+
int[] predecessor = new int[vertices.length];
2222
Arrays.fill(distance, Integer.MAX_VALUE);
2323
Arrays.fill(predecessor, -1);
2424
distance[source] = 0;
25-
for (var i = 0; i < vertices.length - 1; i++) {
26-
for (var j = 0; j < edges.length; j++) {
27-
var u = edges[j][0];
28-
var v = edges[j][1];
29-
var newDistance = distance[u] + weight[j];
25+
for (int i = 0; i < vertices.length - 1; i++) {
26+
for (int j = 0; j < edges.length; j++) {
27+
int u = edges[j][0];
28+
int v = edges[j][1];
29+
int newDistance = distance[u] + weight[j];
3030
if (distance[u] != Integer.MAX_VALUE && newDistance < distance[v]) {
3131
distance[v] = newDistance;
3232
predecessor[v] = u;
@@ -38,7 +38,7 @@ public static int[] bellmanFordAlgorithm(int[][] edges, int[] weight, int source
3838

3939
public static int[] shortestPath(int[] predecessor, int source, int dest) {
4040
var path = new LinkedList<Integer>();
41-
var current = dest;
41+
int current = dest;
4242
while (current != source) {
4343
path.addFirst(current);
4444
current = predecessor[current];
@@ -48,8 +48,8 @@ public static int[] shortestPath(int[] predecessor, int source, int dest) {
4848
}
4949

5050
public static int shortestDistance(int[] predecessor, int source, int dest) {
51-
var distance = 0;
52-
var current = dest;
51+
int distance = 0;
52+
int current = dest;
5353
while (current != source) {
5454
distance++;
5555
current = predecessor[current];

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
public class BellmanFord2 {
99
public static void bellmanFordAlgorithm(Graph graph, Vertex source) {
1010
source.distance = 0;
11-
for (var i = 0; i < graph.vertices.size() - 1; i++) {
11+
for (int i = 0; i < graph.vertices.size() - 1; i++) {
1212
for (var edge : graph.edges.keySet()) {
13-
var u = edge.keySet().iterator().next();
14-
var v = edge.values().iterator().next();
15-
var newDistance = u.distance + graph.edges.get(edge);
13+
Vertex u = edge.keySet().iterator().next();
14+
Vertex v = edge.values().iterator().next();
15+
int newDistance = u.distance + graph.edges.get(edge);
1616
if (u.distance != Integer.MAX_VALUE && newDistance < v.distance) {
1717
v.distance = newDistance;
1818
v.previous = u;
@@ -23,7 +23,7 @@ public static void bellmanFordAlgorithm(Graph graph, Vertex source) {
2323

2424
public static Vertex[] shortestPath(Vertex dest) {
2525
var path = new ArrayList<Vertex>();
26-
for (var vertex = dest; vertex != null; vertex = vertex.previous) {
26+
for (Vertex vertex = dest; vertex != null; vertex = vertex.previous) {
2727
path.add(vertex);
2828
}
2929
Collections.reverse(path);
@@ -43,13 +43,12 @@ public static void printPath(Vertex dest) {
4343
}
4444

4545
public static void printAllPaths(Graph graph) {
46-
for (var v : graph.vertices) {
46+
for (Vertex v : graph.vertices) {
4747
printPath(v);
4848
System.out.println();
4949
}
5050
}
5151

52-
5352
public static class Graph {
5453
Set<Vertex> vertices = new HashSet<>();
5554
Map<Map<Vertex, Vertex>, Integer> edges = new HashMap<>();

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
*/
1111
public class BreadthFirstSearch1 {
1212
public static int breadthFirstSearch(int[][] edges, int source, int dest) {
13-
var vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
14-
var distance = new int[vertices.length];
13+
int[] vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
14+
int[] distance = new int[vertices.length];
1515
var queue = new LinkedList<Integer>();
1616
Arrays.fill(distance, -1);
1717
distance[source] = 0;
1818
queue.add(source);
1919
while (!queue.isEmpty()) {
20-
var u = queue.poll();
21-
for (var edge : edges) {
22-
var v = edge[1];
20+
Integer u = queue.poll();
21+
for (int[] edge : edges) {
22+
int v = edge[1];
2323
if (edge[0] == u && distance[v] == -1) {
2424
distance[v] = distance[u] + 1;
2525
queue.add(v);
@@ -30,14 +30,14 @@ public static int breadthFirstSearch(int[][] edges, int source, int dest) {
3030
}
3131

3232
public static int breadthFirstSearchPreProcessingVersion(int[][] edges, int source, int dest) {
33-
var vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
34-
var distance = new int[vertices.length];
35-
var visited = new boolean[vertices.length];
33+
int[] vertices = Arrays.stream(edges).flatMapToInt(Arrays::stream).distinct().toArray();
34+
int[] distance = new int[vertices.length];
35+
boolean[] visited = new boolean[vertices.length];
3636
var queue = new LinkedList<Integer>();
3737
var neighbors = new HashMap<Integer, List<Integer>>();
38-
for (var edge : edges) {
39-
var sourceVertex = edge[0];
40-
var destVertex = edge[1];
38+
for (int[] edge : edges) {
39+
int sourceVertex = edge[0];
40+
int destVertex = edge[1];
4141
neighbors.putIfAbsent(sourceVertex, new LinkedList<>());
4242
neighbors.get(sourceVertex).add(destVertex);
4343
}
@@ -46,9 +46,9 @@ public static int breadthFirstSearchPreProcessingVersion(int[][] edges, int sour
4646
queue.add(source);
4747
visited[source] = true;
4848
while (!queue.isEmpty()) {
49-
var u = queue.poll();
49+
Integer u = queue.poll();
5050
if (neighbors.containsKey(u)) {
51-
for (var v : neighbors.get(u)) {
51+
for (Integer v : neighbors.get(u)) {
5252
if (!visited[v]) {
5353
visited[v] = true;
5454
distance[v] = distance[u] + 1;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public static Vertex breadthFirstSearch(Vertex source, Vertex dest) {
1212
var queue = new ArrayDeque<Vertex>();
1313
queue.add(source);
1414
while (!queue.isEmpty()) {
15-
var u = queue.poll();
15+
Vertex u = queue.poll();
1616
if (u == dest) {
1717
return u;
1818
}
19-
for (var v : u.neighbors) {
19+
for (Vertex v : u.neighbors) {
2020
if (!v.visited) {
2121
v.visited = true;
2222
v.distance = u.distance + 1;
@@ -30,7 +30,7 @@ public static Vertex breadthFirstSearch(Vertex source, Vertex dest) {
3030

3131
public static Vertex[] getShortestPath(Vertex dest) {
3232
var path = new ArrayList<Vertex>();
33-
for (var vertex = dest; vertex != null; vertex = vertex.previous) {
33+
for (Vertex vertex = dest; vertex != null; vertex = vertex.previous) {
3434
path.add(vertex);
3535
}
3636
Collections.reverse(path);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ private static String[] depthFirstSearch(String[][] edges, String source, String
3333
if (source.equals(dest)) {
3434
return visited.toArray(String[]::new);
3535
}
36-
for (var edge : edges) {
37-
var u = edge[0];
38-
var v = edge[1];
36+
for (String[] edge : edges) {
37+
String u = edge[0];
38+
String v = edge[1];
3939
if (u.equals(source) && !visited.contains(v)) {
40-
var path = depthFirstSearch(edges, v, dest, visited);
40+
String[] path = depthFirstSearch(edges, v, dest, visited);
4141
if (path != null) {
4242
return path;
4343
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class DepthFirstSearch2 {
1313
* @return the distance from source to dest
1414
*/
1515
public static int performDepthFirstSearch(int[][] matrix, int source, int dest) {
16-
var visited = new boolean[matrix.length];
16+
boolean[] visited = new boolean[matrix.length];
1717
return depthFirstSearch(matrix, source, dest, visited) - 1;
1818
}
1919

@@ -31,9 +31,9 @@ private static int depthFirstSearch(int[][] matrix, int source, int dest, boolea
3131
return 0;
3232
}
3333
visited[source] = true;
34-
for (var i = 0; i < matrix.length; i++) {
34+
for (int i = 0; i < matrix.length; i++) {
3535
if (matrix[source][i] == 1 && !visited[i]) {
36-
var result = depthFirstSearch(matrix, i, dest, visited);
36+
int result = depthFirstSearch(matrix, i, dest, visited);
3737
if (result != -1) {
3838
return result + 1;
3939
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ private static Vertex depthFirstSearch(Vertex source, Vertex dest) {
1919
if (source == dest) {
2020
return source;
2121
}
22-
for (var v : source.neighbors) {
22+
for (Vertex v : source.neighbors) {
2323
if (!v.visited) {
2424
v.previous = source;
2525
v.distance = source.distance + 1;
26-
var u = depthFirstSearch(v, dest);
26+
Vertex u = depthFirstSearch(v, dest);
2727
if (u != null) {
2828
return u;
2929
}
@@ -34,7 +34,7 @@ private static Vertex depthFirstSearch(Vertex source, Vertex dest) {
3434

3535
public static Vertex[] getShortestPath(Vertex dest) {
3636
var path = new ArrayList<Vertex>();
37-
for (var vertex = dest; vertex != null; vertex = vertex.previous) {
37+
for (Vertex vertex = dest; vertex != null; vertex = vertex.previous) {
3838
path.add(vertex);
3939
}
4040
Collections.reverse(path);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public static void dijkstraAlgorithm(Graph graph, Vertex source) {
1111
NavigableSet<Vertex> queue = new TreeSet<>();
1212
queue.add(source);
1313
while (!queue.isEmpty()) {
14-
var u = queue.pollFirst();
14+
Vertex u = queue.pollFirst();
1515
assert u != null;
16-
for (var v : u.neighbors) {
17-
var newDistance = u.distance + graph.edges.get(Map.of(u, v));
16+
for (Vertex v : u.neighbors) {
17+
int newDistance = u.distance + graph.edges.get(Map.of(u, v));
1818
if (newDistance < v.distance) {
1919
queue.remove(v);
2020
v.distance = newDistance;
@@ -27,7 +27,7 @@ public static void dijkstraAlgorithm(Graph graph, Vertex source) {
2727

2828
public static Vertex[] getShortestPath(Vertex dest) {
2929
var path = new ArrayList<Vertex>();
30-
for (var vertex = dest; vertex != null; vertex = vertex.previous) {
30+
for (Vertex vertex = dest; vertex != null; vertex = vertex.previous) {
3131
path.add(vertex);
3232
}
3333
Collections.reverse(path);
@@ -47,7 +47,7 @@ public static void printPath(Vertex dest) {
4747
}
4848

4949
public static void printAllPaths(Graph graph) {
50-
for (var v : graph.vertices) {
50+
for (Vertex v : graph.vertices) {
5151
printPath(v);
5252
System.out.println();
5353
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ public class FloydWarshall {
1515
* @return shortest path matrix
1616
*/
1717
public static int[][] floydWarshall(int[][] matrix) {
18-
var length = matrix.length;
18+
int length = matrix.length;
1919

2020
// initialize the shortest path matrix
21-
var dist = new int[length][length];
22-
for (var i = 0; i < length; i++) {
21+
int[][] dist = new int[length][length];
22+
for (int i = 0; i < length; i++) {
2323
// for (var j = 0; j < length; j++) {
2424
// dist[i][j] = matrix[i][j];
2525
// }
2626
System.arraycopy(matrix[i], 0, dist[i], 0, length);
2727
}
2828

2929
// compute the shortest path weight
30-
for (var k = 0; k < length; k++) {
31-
for (var i = 0; i < length; i++) {
32-
for (var j = 0; j < length; j++) {
30+
for (int k = 0; k < length; k++) {
31+
for (int i = 0; i < length; i++) {
32+
for (int j = 0; j < length; j++) {
3333
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
3434
}
3535
}
@@ -48,21 +48,21 @@ public static int[][] floydWarshall(int[][] matrix) {
4848
* @return shortest path matrix
4949
*/
5050
public static int[][] floydWarshallPositiveWeight(int[][] matrix) {
51-
var length = matrix.length;
51+
int length = matrix.length;
5252

5353
// initialize the shortest path matrix
54-
var dist = new int[length][length];
55-
for (var i = 0; i < length; i++) {
54+
int[][] dist = new int[length][length];
55+
for (int i = 0; i < length; i++) {
5656
// for (var j = 0; j < length; j++) {
5757
// dist[i][j] = matrix[i][j];
5858
// }
5959
System.arraycopy(matrix[i], 0, dist[i], 0, length);
6060
}
6161

6262
// compute the shortest path weight
63-
for (var k = 0; k < length; k++) {
64-
for (var i = 0; i < length; i++) {
65-
for (var j = 0; j < length; j++) {
63+
for (int k = 0; k < length; k++) {
64+
for (int i = 0; i < length; i++) {
65+
for (int j = 0; j < length; j++) {
6666
if (dist[i][k] + dist[k][j] < dist[i][j])
6767
dist[i][j] = dist[i][k] + dist[k][j];
6868
}
@@ -78,10 +78,10 @@ public static int[][] floydWarshallPositiveWeight(int[][] matrix) {
7878
* @return initialized adjacency matrix
7979
*/
8080
public static int[][] initializeMatrix(int[][] matrix) {
81-
var length = matrix.length;
82-
var newMatrix = new int[length][length];
83-
for (var i = 0; i < length; i++) {
84-
for (var j = 0; j < length; j++) {
81+
int length = matrix.length;
82+
int[][] newMatrix = new int[length][length];
83+
for (int i = 0; i < length; i++) {
84+
for (int j = 0; j < length; j++) {
8585
if (i == j) {
8686
newMatrix[i][j] = 0;
8787
} else if (matrix[i][j] != 0) {
@@ -100,9 +100,9 @@ public static int[][] initializeMatrix(int[][] matrix) {
100100
* @param matrix adjacency matrix
101101
*/
102102
private static void printMatrix(int[][] matrix) {
103-
var length = matrix.length;
104-
for (var i = 0; i < length; i++) {
105-
for (var j = 0; j < length; j++) {
103+
int length = matrix.length;
104+
for (int i = 0; i < length; i++) {
105+
for (int j = 0; j < length; j++) {
106106
if (matrix[i][j] == INF)
107107
System.out.print("INF ");
108108
else

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
public class GraphUtil {
44
public static int[][] edgeToAdjacencyMatrix(int[][] edges, int n) {
5-
var matrix = new int[n][n];
6-
for (var edge : edges) {
5+
int[][] matrix = new int[n][n];
6+
for (int[] edge : edges) {
77
matrix[edge[0]][edge[1]] = 1;
88
}
99
return matrix;
1010
}
1111

1212
public static void printAdjacencyMatrix(int[][] matrix) {
13-
for (var i = 0; i < matrix.length; i++) {
14-
for (var j = 0; j < matrix[i].length; j++) {
13+
for (int i = 0; i < matrix.length; i++) {
14+
for (int j = 0; j < matrix[i].length; j++) {
1515
System.out.print(matrix[i][j] + " ");
1616
}
1717
System.out.println();

0 commit comments

Comments
 (0)