Interface WeightFunction<K>
- Type Parameters:
K- the elements (vertices/edges) type
- All Superinterfaces:
Comparator<K>
- All Known Subinterfaces:
IWeightFunction,IWeightFunctionInt,IWeightsByte,IWeightsDouble,IWeightsFloat,IWeightsInt,IWeightsLong,IWeightsShort,WeightFunctionInt<K>,WeightsByte<K>,WeightsDouble<K>,WeightsFloat<K>,WeightsInt<K>,WeightsLong<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.
This interface is usually used as weight function of edges, for example in algorithms such as
ShortestPathSingleSource, MinimumSpanningTree and MatchingAlgo, in which the algorithm try to
find a set of edges satisfying some constraint while minimizing/maximizing some objective function based on the
weights of the edges. But it can represent weights assigned to vertices, in algorithms such as VertexCover.
An instance of this interface represent weights of edges only or vertices only, and never both. As this function represent weights for either edges or vertex, the documentation refer to these edges/vertices as elements.
// 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
WeightsDouble<Integer> w = g.addEdgesWeights("distance-km", double.class);
w.set(9, 191.1);
w.set(13, 193.3);
w.set(14, 121.3);
// 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
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final WeightFunctionInt<?> A weight function that assign a weight of1to any element. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K> WeightFunctionInt<K> Get the cardinality weight function.default intCompare two elements by their weights.static booleanisCardinality(WeightFunction<?> weightFunc) Check if the given weight function is the cardinality weight function.static booleanisInteger(WeightFunction<?> weightFunc) Check if the given weight function is an integer weight function.static <K> WeightFunction<K> replaceNullWeightFunc(WeightFunction<K> weightFunc) Replacenullweight function withCardinalityWeightFunction.static <K> WeightFunctionInt<K> replaceNullWeightFunc(WeightFunctionInt<K> weightFunc) Replacenullweight function withCardinalityWeightFunction.doubleGet the weight of an element.static <K> doubleweightSum(WeightFunction<K> weightFunc, Iterable<K> elements) Get the sum of the weights of multiple elements.default doubleGet the sum of the weights of multiple elements.Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
CardinalityWeightFunction
A weight function that assign a weight of1to any element.
-
-
Method Details
-
weight
Get the weight of an element.- Parameters:
element- an element identifier- Returns:
- the weight of the element
- Throws:
NoSuchVertexException- if this weight function maps vertices andelementis not a valid vertex identifier in the graphNoSuchEdgeException- if this weight function maps edges andelementis not a valid edge identifier in the graph
-
compare
Compare two elements by their weights.- Specified by:
comparein interfaceComparator<K>
-
weightSum
-
weightSum
Get the sum of the weights of multiple elements.This method is equivalent to
weightSum(Iterable), but it also supportnullweight function, which is treated is cardinality weight function.- Type Parameters:
K- the elements (vertices/edges) type- Parameters:
weightFunc- the weight function to use, ornullto use cardinality weight functionelements- a collection of elements- Returns:
- the sum of the weights of the elements
-
cardinalityWeightFunction
Get the cardinality weight function.The cardinality weight function assign a weight of
1to any element. The function always return the same object, which can be accessed directed viaCardinalityWeightFunction. This is method is exposed only to avoid unchecked casts with generics.- Type Parameters:
K- the type of the elements- Returns:
- the cardinality weight function
-
replaceNullWeightFunc
Replacenullweight function withCardinalityWeightFunction.- Type Parameters:
K- the elements (vertices/edges) type- Parameters:
weightFunc- the weight function to replace- Returns:
CardinalityWeightFunctionifweightFuncisnull, otherwiseweightFunc
-
replaceNullWeightFunc
Replacenullweight function withCardinalityWeightFunction.- Type Parameters:
K- the elements (vertices/edges) type- Parameters:
weightFunc- the weight function to replace- Returns:
CardinalityWeightFunctionifweightFuncisnull, otherwiseweightFunc
-
isCardinality
Check if the given weight function is the cardinality weight function.This function does not checks that the weight function maps every element to
1, but only compares it to the static weight functionsCardinalityWeightFunctionandIWeightFunction.CardinalityWeightFunction, ornull.- Parameters:
weightFunc- the weight function to check- Returns:
trueif the weight function is the cardinality weight function,false
-
isInteger
Check if the given weight function is an integer weight function.This function does not checks that the weight function maps every element to an integer, but only checks that the weight function is an instance of
WeightFunctionIntor it isnull.- Parameters:
weightFunc- the weight function to check- Returns:
trueif the weight function is an integer weight function,false
-