Package com.jgalgo.graph
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.
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
-
Field Summary
Fields inherited from interface com.jgalgo.graph.WeightFunction
CardinalityWeightFunction
-
Method Summary
Modifier and TypeMethodDescriptiondefault int
Compare two elements by their weights.default double
Deprecated.int
Get the integer weight of an element.default double
Get the sum of the weights of multiple elements.Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Method Details
-
weightInt
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 andelement
is not a valid vertex identifier in the graphNoSuchEdgeException
- if this weight function maps edges andelement
is not a valid edge identifier in the graph
-
weight
Deprecated.Please useweightInt(Object)
instead to avoid un/boxing.Get the weight of an element.- Specified by:
weight
in interfaceWeightFunction<K>
- Parameters:
element
- an element identifier- Returns:
- the weight of the element
-
compare
Description copied from interface:WeightFunction
Compare two elements by their weights.- Specified by:
compare
in interfaceComparator<K>
- Specified by:
compare
in interfaceWeightFunction<K>
-
weightSum
Description copied from interface:WeightFunction
Get the sum of the weights of multiple elements.- Specified by:
weightSum
in interfaceWeightFunction<K>
- Parameters:
elements
- a collection of elements- Returns:
- the sum of the weights of the elements
-
weightInt(Object)
instead to avoid un/boxing.