Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18 lesson #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/main/java/io/sidorovgreg/otus/App.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<KruskalAlgorithm.Edge> 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);
}
}
78 changes: 78 additions & 0 deletions src/main/java/io/sidorovgreg/otus/KruskalAlgorithm.java
Original file line number Diff line number Diff line change
@@ -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<Edge> 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<Edge> comp = Comparator.comparingInt(o -> o.weight);
}
static class Subset {
int parent;
int rank;
}
}