Interface WeightFunctionInt<K>

  • Type Parameters:
    K - the elements (vertices/edges) type
    All Superinterfaces:
    Comparator<K>, WeightFunction<K>
    All Known Subinterfaces:
    IWeightFunctionInt, IWeightsByte, IWeightsInt, IWeightsShort, WeightsByte<K>, WeightsInt<K>, WeightsShort<K>
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface WeightFunctionInt<K>
    extends WeightFunction<K>
    Weight function that maps graph edges or vertices to integer weights.

    Some algorithms implementations support only integers weights, or run faster in such a case. This interface is the API for these algorithms for the edges (or vertices) integer weights.

     
     // Create an undirected graph with three vertices and edges between them
     Graph<String, Integer> g = Graph.newUndirected();
     g.addVertex("Berlin");
     g.addVertex("Leipzig");
     g.addVertex("Dresden");
     g.addEdge("Berlin", "Leipzig", 9);
     g.addEdge("Berlin", "Dresden", 13);
     g.addEdge("Dresden", "Leipzig", 14);
    
     // Assign some weights to the edges
     WeightsInt<Integer> w = g.addEdgesWeights("distance-km", int.class);
     w.set(9, 191);
     w.set(13, 193);
     w.set(14, 121);
    
     // Calculate the shortest paths from Berlin to all other cities
     ShortestPathSingleSource ssspAlgo = ShortestPathSingleSource.newInstance();
     ShortestPathSingleSource.Result<String, Integer> ssspRes = ssspAlgo.computeShortestPaths(g, w, "Berlin");
    
     // Print the shortest path from Berlin to Leipzig
     System.out.println("Distance from Berlin to Leipzig is: " + ssspRes.distance("Leipzig"));
     System.out.println("The shortest path from Berlin to Leipzig is:");
     for (Integer e : ssspRes.getPath("Leipzig").edges()) {
     	String u = g.edgeSource(e), v = g.edgeTarget(e);
     	System.out.println(" " + e + "(" + u + ", " + v + ")");
     }
     
    Author:
    Barak Ugav
    • Method Detail

      • weightInt

        int weightInt​(K element)
        Get the integer weight of an element.
        Parameters:
        element - an element identifier
        Returns:
        the weight of the element
        Throws:
        NoSuchVertexException - if this weight function maps vertices and element is not a valid vertex identifier in the graph
        NoSuchEdgeException - if this weight function maps edges and element is not a valid edge identifier in the graph
      • weight

        @Deprecated
        default double weight​(K element)
        Deprecated.
        Description copied from interface: WeightFunction
        Get the weight of an element.
        Specified by:
        weight in interface WeightFunction<K>
        Parameters:
        element - an element identifier
        Returns:
        the weight of the element
      • weightSum

        default double weightSum​(Iterable<K> elements)
        Description copied from interface: WeightFunction
        Get the sum of the weights of multiple elements.
        Specified by:
        weightSum in interface WeightFunction<K>
        Parameters:
        elements - a collection of elements
        Returns:
        the sum of the weights of the elements