本文整理汇总了Java中org.jgrapht.WeightedGraph类的典型用法代码示例。如果您正苦于以下问题:Java WeightedGraph类的具体用法?Java WeightedGraph怎么用?Java WeightedGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WeightedGraph类属于org.jgrapht包,在下文中一共展示了WeightedGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWeightedGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* Convert FNSS Topology to JGraphT weighted graph.
*
* The weights assigned to the edges of the returned graph are the link
* weights of the original FNSS topology. If the topology object does not
* have weights assigned, all nodes are assigned a weight equal to 1.
*
* Because of the specific limitations of JGraphT, the edges of the returned
* graph must be instances of JGraphT <code>DefaultWeightedEdge</code>, and
* do not hold capacity, buffer sizes and delay information possibly present
* in the original FNSS Topology. The returned graph maintains however
* link weight information.
*
* See examples for further information.
*
* @param topology FNSS Topology object
* @return A JGraphT weighted graph
*/
public static WeightedGraph<String, DefaultWeightedEdge> getWeightedGraph(Topology topology) {
WeightedGraph<String, DefaultWeightedEdge> graph = null;
if (topology.isDirected()) {
graph = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
} else {
graph = new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
}
for(String node : topology.getAllNodes()) {
graph.addVertex(node);
}
for(Pair<String, String> endpoints : topology.getAllEdges()) {
float weight = topology.getEdge(endpoints).getWeight();
DefaultWeightedEdge edge = new DefaultWeightedEdge();
graph.addEdge(endpoints.getU(), endpoints.getV(), edge);
graph.setEdgeWeight(edge, weight);
}
return graph;
}
示例2: generateZoneGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
public static WeightedGraph<NdPoint, DefaultWeightedEdge> generateZoneGraph(Collection<Zone> allZones) {
WeightedGraph<NdPoint, DefaultWeightedEdge> graph = new SimpleWeightedGraph<NdPoint, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
for (Zone p : allZones) {
graph.addVertex(p.getLocation());
}
for (Zone p1 : allZones) {
for (Zone p2 : p1.getNeighbours()) {
DefaultWeightedEdge edge = graph.addEdge(p1.getLocation(), p2.getLocation());
/**
* If we get edge==null, the edge already has been added.
*/
if (edge != null) {
graph.setEdgeWeight(edge, p1.distanceTo(p2));
}
}
}
return graph;
}
示例3: findPath
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* Find the list of point to be traversed from the start to the end using the DijkstraShortestPath algorithm.
*
* @param graph
* the graph upon which we need to find the path
* @param start
* the start node from which to navigate
* @param end
* the end node to which to navigate to
* @return the succession of node to traverse from start to end, empty if no path was found
*/
public static List<NdPoint> findPath(WeightedGraph<NdPoint, DefaultWeightedEdge> graph, NdPoint start,
NdPoint end) {
List<DefaultWeightedEdge> edgeList = DijkstraShortestPath.findPathBetween(graph, start, end);
if (edgeList == null) {
return new ArrayList<>(0);
}
List<NdPoint> path = new LinkedList<>();
NdPoint current = start;
path.add(current);
// Add each path node, but also check for the order of the edges so that
// the correct point (source or target) is added.
for (DefaultWeightedEdge edge : edgeList) {
current = GraphHelper.getOpposite(graph, edge, current);
if (current != null) {
path.add(current);
}
}
return path;
}
示例4: match
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
static KuhnMunkresMinimalWeightBipartitePerfectMatching<V, WeightedEdge>
match(final double[][] costMatrix, final int partitionCardinality) {
List<? extends V> first = firstPartition.subList(0, partitionCardinality);
List<? extends V> second = secondPartition.subList(0, partitionCardinality);
WeightedGraph<V, WeightedEdge> target =
new SimpleWeightedGraph<V, WeightedEdge>(new EdgeFactory<V, WeightedEdge>() {
@Override
public WeightedEdge createEdge(V sourceVertex, V targetVertex) {
return WeightedEdge.make(sourceVertex, targetVertex);
}
});
WeightedGraphGeneratorAdapter<V, WeightedEdge, V> generator =
new SimpleWeightedBipartiteGraphMatrixGenerator<V, WeightedEdge>()
.first (first)
.second (second)
.weights(costMatrix);
generator.generateGraph(target, null, null);
return new KuhnMunkresMinimalWeightBipartitePerfectMatching<V, WeightedEdge>(target, first, second);
}
开发者ID:j123b567,项目名称:stack-usage,代码行数:26,代码来源:KuhnMunkresMinimalWeightBipartitePerfectMatchingTest.java
示例5: weightedDirectedGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* Create a simple directed graph.
*/
@SuppressWarnings("boxing")
public static WeightedGraph<Integer, DefaultWeightedEdge> weightedDirectedGraph()
{
final WeightedGraph<Integer, DefaultWeightedEdge> g = new SimpleWeightedGraph<>(
DefaultWeightedEdge.class);
final Set<Integer> vertices = CollectionUtil.toSet(7, 5, 3, 11, 8, 2, 9, 10);
for (final Integer v : vertices)
{
g.addVertex(v);
}
final DefaultWeightedEdge e = g.addEdge(7, 8);
g.setEdgeWeight(e, 1.0d);
return g;
}
示例6: createWeightedDirectedGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* Create a weighted directed graph.
*/
@Test
public void createWeightedDirectedGraph()
{
final WeightedGraph<Integer, DefaultWeightedEdge> g = weightedDirectedGraph();
if (log.isDebugEnabled())
{
log.debug("Graph " + g);
}
assertEquals(8, g.vertexSet().size());
assertEquals(1, g.edgeSet().size());
}
示例7: exampleWeightedGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* Shows how to create an FNSS Topology with weighted edges, convert it to
* a JGraphT WeightedGraph and then compute shortest paths
*/
public static void exampleWeightedGraph() {
// create unidrected topology
Topology topology = new Topology();
// Create edge with high weight and edge with low weight
Edge edgeHighWeight = new Edge();
edgeHighWeight.setWeight(1000);
Edge edgeLowWeight = new Edge();
edgeLowWeight.setWeight(1);
// Assign edges to topology
topology.addEdge("1", "4", edgeHighWeight);
topology.addEdge("2", "3", edgeLowWeight);
topology.addEdge("1", "2", edgeLowWeight);
topology.addEdge("4", "3", edgeLowWeight);
// convert to JGraphT
WeightedGraph<String, DefaultWeightedEdge> graph =
JGraphTConverter.getWeightedGraph(topology);
// Find shortest paths
String source = "1";
String destination = "3";
List<DefaultWeightedEdge> sp =
DijkstraShortestPath.findPathBetween(graph, source, destination);
// Print results
System.out.println("Shortest path from " + source + " to " + destination + ":");
for (DefaultWeightedEdge e : sp) {
System.out.println(graph.getEdgeSource(e) + " -> " + graph.getEdgeTarget(e));
}
}
示例8: testGetWeightedGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
@Test
public void testGetWeightedGraph() {
Topology topology = new Topology();
topology.addEdge("1", "2", new Edge());
topology.addEdge("2", "3", new Edge());
WeightedGraph<String, DefaultWeightedEdge> graph =
JGraphTConverter.getWeightedGraph(topology);
assertNotNull(graph);
assertTrue(graph.containsEdge("1", "2"));
assertTrue(graph.containsEdge("2", "3"));
assertTrue(graph.containsEdge("2", "1"));
assertTrue(graph.containsEdge("3", "2"));
}
示例9: testGetWeightedGraphDirected
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
@Test
public void testGetWeightedGraphDirected() {
Topology topology = new Topology(true);
topology.addEdge("1", "2", new Edge());
topology.addEdge("2", "3", new Edge());
WeightedGraph<String, DefaultWeightedEdge> graph =
JGraphTConverter.getWeightedGraph(topology);
assertNotNull(graph);
assertTrue(graph.containsEdge("1", "2"));
assertTrue(graph.containsEdge("2", "3"));
assertFalse(graph.containsEdge("2", "1"));
assertFalse(graph.containsEdge("3", "2"));
}
示例10: setEdgeWeightIfRequired
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
private void setEdgeWeightIfRequired(final E e, final Map<String, Attribute> attributes) {
// special handling for weighted graphs
if (g instanceof WeightedGraph<?, ?> && attributes.containsKey(EDGE_WEIGHT_ATTRIBUTE_NAME)) {
try {
((WeightedGraph<V, E>) g).setEdgeWeight(e, Float.valueOf(attributes.get(EDGE_WEIGHT_ATTRIBUTE_NAME).getValue()));
} catch (final NumberFormatException nfe) {
((WeightedGraph<V, E>) g).setEdgeWeight(e, getEdgeWeight());
}
}
}
示例11: getEdgeWeight
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
private double getEdgeWeight() {
for (final Key k : edgeValidKeys.values()) {
if (k.attributeName.equals(EDGE_WEIGHT_ATTRIBUTE_NAME)) {
try {
if (k.defaultValue != null) {
return Double.parseDouble(k.defaultValue);
}
} catch (final NumberFormatException e) {
return WeightedGraph.DEFAULT_EDGE_WEIGHT;
}
}
}
return WeightedGraph.DEFAULT_EDGE_WEIGHT;
}
示例12: extractNetwork
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
protected WeightedGraph<Figure, DefaultWeightedEdge> extractNetwork(JCas jcas, Annotation range) {
SimpleWeightedGraph<Figure, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Figure, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
for (Figure figure : JCasUtil.select(jcas, Figure.class)) {
if (!graph.containsVertex(figure)) {
graph.addVertex(figure);
}
}
for (Scene scene : JCasUtil.select(jcas, Scene.class)) {
List<Speaker> speakers = JCasUtil.selectCovered(Speaker.class, scene);
for (Speaker s1 : speakers) {
Figure gf1 = s1.getFigure();
if (gf1 != null)
for (Speaker s2 : speakers) {
Figure gf2 = s2.getFigure();
if (gf2 != null)
if (graph.containsEdge(gf1, gf2)) {
DefaultWeightedEdge edge = graph.getEdge(gf1, gf2);
double w = graph.getEdgeWeight(edge);
graph.setEdgeWeight(edge, w + 1.0);
} else {
if (gf1 != gf2)
graph.addEdge(gf1, gf2);
}
}
}
;
}
return graph;
}
示例13: export
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
public void export(JCas jcas, Graph<Figure, ?> graph) throws IOException {
StringWriter sw = new StringWriter();
sw.write(" \n");
for (Figure figure1 : graph.vertexSet()) {
for (Figure figure2 : graph.vertexSet()) {
if (graph.containsEdge(figure1, figure2)) {
sw.write(figure1.getBegin() + " " + figure2.getBegin());
Object edge = graph.getEdge(figure1, figure2);
try {
@SuppressWarnings("unchecked")
double w = ((WeightedGraph<Figure, Object>) graph).getEdgeWeight(edge);
sw.write(" " + w);
} catch (ClassCastException e) {
// we try to cast, but ignore it if impossible
}
sw.write("\n");
}
}
}
sw.flush();
sw.close();
jcas.setDocumentText(sw.toString());
jcas.setDocumentLanguage("");
GraphMetaData graphAnnotation = AnnotationFactory.createAnnotation(jcas, 0, 1, GraphMetaData.class);
graphAnnotation.setGraphClassName(graph.getClass().getCanonicalName());
if (!graph.edgeSet().isEmpty())
graphAnnotation.setEdgeClassName(graph.edgeSet().iterator().next().getClass().getCanonicalName());
}
示例14: testGraphIO
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
@Test
public void testGraphIO()
throws IOException, CASException, ClassNotFoundException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
WeightedGraph<Figure, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Figure, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
for (int i = 0; i < figures.length; i++) {
graph.addVertex(figures[i]);
}
for (int i = 0; i < figures.length - 1; i++) {
DefaultWeightedEdge edge = graph.addEdge(figures[i], figures[i + 1]);
graph.setEdgeWeight(edge, random.nextDouble());
}
GraphExporter ge = new GraphExporter();
ge.export(jcas.createView("Testview"), graph);
Graph<Figure, DefaultWeightedEdge> g2 = GraphImporter.getGraph(jcas, "Testview");
assertNotNull(g2);
assertFalse(g2.vertexSet().isEmpty());
for (int i = 0; i < figures.length; i++) {
for (int j = 0; j < figures.length; j++) {
assertEquals(graph.containsEdge(figures[i], figures[j]), g2.containsEdge(figures[i], figures[j]));
if (graph.containsEdge(figures[i], figures[j])) {
assertEquals(graph.getEdgeWeight(graph.getEdge(figures[i], figures[j])),
g2.getEdgeWeight(g2.getEdge(figures[i], figures[j])), 0.1);
}
}
}
}
示例15: generateGraph
import org.jgrapht.WeightedGraph; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void generateGraph(
Graph<V, E> target,
VertexFactory<V> vertexFactory,
Map<String, V> resultMap)
{
final int size = readNodeCount();
if (resultMap == null) {
resultMap = new HashMap<>();
}
for (int i = 0; i < size; i++) {
V newVertex = vertexFactory.createVertex();
target.addVertex(newVertex);
resultMap.put(Integer.toString(i + 1), newVertex);
}
String[] cols = skipComments();
while (cols != null) {
if (cols[0].equals("e")) {
E edge = target
.addEdge(resultMap.get(cols[1]), resultMap.get(cols[2]));
if (target instanceof WeightedGraph && (edge != null)) {
double weight = defaultWeight;
if (cols.length > 3) {
weight = Double.parseDouble(cols[3]);
}
((WeightedGraph<V, E>) target).setEdgeWeight(edge, weight);
}
}
cols = skipComments();
}
}