import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class EwGraph {
private class Edge implements Comparable<Edge>{
final int a, b;
final double weight;
public Edge(int a, int b, double weight) {
this.a = a;
this.b = b;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
if(this.weight > o.weight) return 1;
else if(this.weight < o.weight) return -1;
else return 0;
}
}
private Map<Integer, Set<Edge>> neighbors = new HashMap<>();
public Set<Integer> getPoints() {
return neighbors.keySet();
}
public int size() {
return getPoints().size();
}
public long edgeSize() {
return neighbors.values().stream().collect(Collectors.summingLong(Set::size))/2;
}
public void addEdge(int a, int b, double weight) {
Edge e = new Edge(a, b, weight);
neighbors.computeIfAbsent(a, k -> new HashSet<Edge>()).add(e);
neighbors.computeIfAbsent(b, k -> new HashSet<Edge>()).add(e);
}
public static void printDotScript(EwGraph g) {
System.out.println("strict graph {");
g.neighbors.values().stream().forEach(i ->
{
Stream.of(i).flatMap(j -> j.stream())
.forEach(k -> System.out.printf(k.a + "--" + k.b
+ "[label=\"%.2f\",weight=\"%.2f\"]\n", k.weight, k.weight ));
});
System.out.println("}");
}
public static void main(String...args) {
EwGraph g = new EwGraph();
for(int i = 0; i < 4; i++) {
g.addEdge(i, i + 1, Math.random());
g.addEdge(i, i + 2, Math.random());
}
System.out.println("strict graph {");
EwGraph.printDotScript(g);
System.out.println("number of points: " + g.size());
System.out.println("number of edges: " + g.edgeSize());
System.out.printf("longest edge: %.2f \n",
g.neighbors.values().stream().flatMap(j -> j.stream()).max(Edge::compareTo).get().weight);
}
}