Interface WeightFunction.Int

  • All Superinterfaces:
    Comparator<Integer>, IntComparator, WeightFunction
    All Known Subinterfaces:
    Weights.Byte, Weights.Int, Weights.Short
    Enclosing interface:
    WeightFunction
    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 static interface WeightFunction.Int
    extends WeightFunction
    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 a directed graph with three vertices and edges between them
     Graph g = GraphFactory.newDirected().newGraph();
     int v1 = g.addVertex();
     int v2 = g.addVertex();
     int v3 = g.addVertex();
     int e1 = g.addEdge(v1, v2);
     int e2 = g.addEdge(v2, v3);
     int e3 = g.addEdge(v1, v3);
    
     // Assign some weights to the edges
     Weights.Int weights = g.addEdgesWeights("weightsKey", int.class);
     weights.set(e1, 1);
     weights.set(e2, 3);
     weights.set(e3, 15);
     EdgeWeightFunc.Int weightFunc = weights;
    
     // Calculate the shortest paths from v1 to all other vertices
     ShortestPathSingleSource ssspAlgo = ShortestPathSingleSource.newBuilder().setIntWeights(true).build();
     ShortestPathSingleSource.Result ssspRes = ssspAlgo.computeShortestPaths(g, weightFunc, v1);
    
     // Print the shortest path from v1 to v3
     assert ssspRes.distance(v3) == 4;
     assert ssspRes.getPath(v3).equals(IntList.of(e1, e2));
     System.out.println("Distance from v1 to v3 is: " + ssspRes.distance(v3));
     System.out.println("The shortest path from v1 to v3 is:");
     for (int e : ssspRes.getPath(v3)) {
     	int u = g.edgeSource(e), v = g.edgeTarget(e);
     	System.out.println(" " + e + "(" + u + ", " + v + ")");
     }
     
    Author:
    Barak Ugav
    • Method Detail

      • weight

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

        int weightInt​(int element)
        Get the integer weight of an element.
        Parameters:
        element - an element identifier
        Returns:
        the integer weight of the element
        Throws:
        IndexOutOfBoundsException - if element is not a valid element identifier