Site Search:

Model for edge weighted digraph

EwDigraph.java


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 EwDigraph {
    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() {
        Set<Integer> points = neighbors.keySet().stream().collect(Collectors.toCollection(HashSet::new));
        neighbors.values().stream().flatMap(j -> j.stream()).forEach(i -> {points.add(i.a); points.add(i.b); });
        return points;
    }
    public int size() {
        return getPoints().size();
    }
    public long edgeSize() {
        return neighbors.values().stream().collect(Collectors.summingLong(Set::size));
    }
    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(EwDigraph g) {
        System.out.println("strict digraph {");
        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 EwDigraph reverse(EwDigraph g) {
        EwDigraph rg = new EwDigraph();
        g.neighbors.keySet().forEach(i -> {
            g.neighbors.get(i).forEach(j -> rg.addEdge(j.b, i, j.weight));
            });
        return rg;
    }
    
    public static void main(String...args) {
        EwDigraph g = new EwDigraph();
        for(int i = 0; i < 4; i++) {
            g.addEdge(i, i + 1, Math.random());
            g.addEdge(i, i + 2, Math.random());
        }
        EwDigraph.printDotScript(g);
        System.out.println();
        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);
        
        System.out.println();
        System.out.println("========reverse digraph========");
        EwDigraph rg = EwDigraph.reverse(g);
        EwDigraph.printDotScript(rg);
        System.out.println();
        System.out.println("number of points: " + rg.size());
        System.out.println("number of edges: " + rg.edgeSize());
        System.out.printf("longest edge: %.2f \n", 
                rg.neighbors.values().stream().flatMap(j -> j.stream()).max(Edge::compareTo).get().weight);
    }

}