diff --git a/src/main/java/io/sidorovgreg/otus/App.java b/src/main/java/io/sidorovgreg/otus/App.java index 33b6ea0..67ef6cd 100644 --- a/src/main/java/io/sidorovgreg/otus/App.java +++ b/src/main/java/io/sidorovgreg/otus/App.java @@ -1,33 +1,29 @@ package io.sidorovgreg.otus; -import java.lang.reflect.Constructor; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Paths; -import java.util.List; -import java.util.Objects; +import java.util.ArrayList; public class App { public static void main(String[] args) throws Exception { - Class clazz = Class.forName("io.sidorovgreg.otus.tasks." + args[0]); - Constructor ctor = clazz.getConstructor(); - Task task = (Task) ctor.newInstance(); - String path = args[1]; - for (int i = 0; true; i++) { - try { - List in = Files.readAllLines(Paths.get(String.format("%s/test.%s.in", path, i))); - String out = Files.readAllLines(Paths.get(String.format("%s/test.%s.out", path, i))).stream() - .findFirst() - .orElseThrow(() -> new RuntimeException("expected result isn't exist")); - String result = task.execute(in); - if (Objects.equals(result, out)) { - System.out.printf("[%s] success%n", in); - } else { - System.out.printf("[%s] fail: got %s, expected %s %n", in, result, out); - } - } catch (NoSuchFileException e) { - break; - } - } + ArrayList graph[] = new ArrayList[4]; + KruskalAlgorithm.Edge edges[] = new KruskalAlgorithm.Edge[5]; + for (int i = 0; i < 4; i++) + graph[i] = new ArrayList<>(); + graph[0].add(new KruskalAlgorithm.Edge(0, 1, 5)); + graph[0].add(new KruskalAlgorithm.Edge(0, 2, 8)); + graph[1].add(new KruskalAlgorithm.Edge(1, 0, 5)); + graph[1].add(new KruskalAlgorithm.Edge(1, 2, 10)); + graph[1].add(new KruskalAlgorithm.Edge(1, 3, 15)); + graph[2].add(new KruskalAlgorithm.Edge(2, 0, 8)); + graph[2].add(new KruskalAlgorithm.Edge(2, 1, 10)); + graph[2].add(new KruskalAlgorithm.Edge(2, 3, 20)); + graph[3].add(new KruskalAlgorithm.Edge(3, 1, 15)); + graph[3].add(new KruskalAlgorithm.Edge(3, 2, 20)); + edges[0] = new KruskalAlgorithm.Edge(0, 1, 5); + edges[1] = new KruskalAlgorithm.Edge(0, 2, 8); + edges[2] = new KruskalAlgorithm.Edge(1, 2, 10); + edges[3] = new KruskalAlgorithm.Edge(1, 3, 15); + edges[4] = new KruskalAlgorithm.Edge(2, 3, 20); + + KruskalAlgorithm.findPrintMST(graph, edges); } } diff --git a/src/main/java/io/sidorovgreg/otus/KruskalAlgorithm.java b/src/main/java/io/sidorovgreg/otus/KruskalAlgorithm.java new file mode 100644 index 0000000..a14f153 --- /dev/null +++ b/src/main/java/io/sidorovgreg/otus/KruskalAlgorithm.java @@ -0,0 +1,78 @@ +package io.sidorovgreg.otus; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; + +public class KruskalAlgorithm { + private static int find(Subset subsets[], int i) { + if (subsets[i].parent != i) + subsets[i].parent = find(subsets, subsets[i].parent); + return subsets[i].parent; + } + private static void Union(Subset subsets[], int x, int y) { + int xRoot = find(subsets, x); + int yRoot = find(subsets, y); + if (subsets[xRoot].rank < subsets[yRoot].rank) { + subsets[xRoot].parent = yRoot; + } else if (subsets[xRoot].rank > subsets[yRoot].rank) { + subsets[yRoot].parent = xRoot; + } else { + subsets[yRoot].parent = xRoot; + subsets[xRoot].rank++; + } + } + public static void findPrintMST(ArrayList graph[], Edge edges[]) { + Arrays.sort(edges, Edge.comp); + Edge mst[] = new Edge[graph.length - 1]; + for (int i = 0; i < graph.length - 1; i++) { + mst[i] = new Edge(-1, -1, -1); + } + int e = 0; + + Subset subsets[] = new Subset[graph.length]; + for (int i = 0; i < graph.length; i++) { + subsets[i] = new Subset(); + } + for (int i = 0; i < graph.length; i++) { + subsets[i].parent = i; + subsets[i].rank = 0; + } + for (int i = 0; i < edges.length; i++) { + int x = find(subsets, edges[i].from); + int y = find(subsets, edges[i].to); + if (x != y) { + mst[e].from = edges[i].from; + mst[e].to = edges[i].to; + mst[e].weight = edges[i].weight; + Union(subsets, x, y); + e++; + } else { + } + if (e == graph.length - 1) { + break; + } + } + + // Print the MST + for (int i = 0; i < graph.length - 1; i++) { + System.out.println("From " + mst[i].from + " to " + mst[i].to + " weight " + mst[i].weight); + } + } + + static class Edge { + int from; + int to; + int weight; + public Edge(int from, int to, int weight) { + this.from = from; + this.to = to; + this.weight = weight; + } + public static final Comparator comp = Comparator.comparingInt(o -> o.weight); + } + static class Subset { + int parent; + int rank; + } +} \ No newline at end of file