All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgrapht.graph.guava.MutableDoubleValueGraphAdapter Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
/*
 * (C) Copyright 2018-2018, by Dimitrios Michail and Contributors.
 *
 * JGraphT : a free Java graph-theory library
 *
 * This program and the accompanying materials are dual-licensed under
 * either
 *
 * (a) the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation, or (at your option) any
 * later version.
 *
 * or (per the licensee's choosing)
 *
 * (b) the terms of the Eclipse Public License v1.0 as published by
 * the Eclipse Foundation.
 */
package org.jgrapht.graph.guava;

import com.google.common.graph.*;
import org.jgrapht.Graph;

import java.io.*;
import java.util.function.*;

/**
 * A graph adapter class using Guava's {@link MutableValueGraph} specialized with double values.
 * 
 * 

* The adapter uses class {@link EndpointPair} to represent edges. Changes in the adapter such as * adding or removing vertices and edges are reflected in the underlying value graph. * *

* Each edge in {@link MutableValueGraph} is associated with a double value which is mapped to the * edge weight in the resulting {@link Graph}. Thus, the graph is weighted and calling methods * {@link #getEdgeWeight(Object)} and {@link #setEdgeWeight(EndpointPair, double)} will get and set * the value of an edge. * *

* See the example below on how to create such an adapter:

* *
 * MutableValueGraph<String, Double> mutableValueGraph =
 *     ValueGraphBuilder.directed().allowsSelfLoops(true).build();
 * 
 * mutableValueGraph.addNode("v1");
 * mutableValueGraph.addNode("v2");
 * mutableValueGraph.putEdgeValue("v1", "v2", 3.0);
 * 
 * Graph<String, EndpointPair<String>> graph = new MutableDoubleValueGraphAdapter<>(mutableValueGraph);
 * 
 * System.out.println(graph.getEdgeWeight(EndpointPair.ordered("v1", "v2")); // outputs 3.0
 * 
 * graph.setEdgeWeight(EndpointPair.ordered("v1", "v2"), 7.0);
 * 
 * System.out.println(graph.getEdgeWeight(EndpointPair.ordered("v1", "v2")); // outputs 7.0
 * 
* *
* * @author Dimitrios Michail * * @param the graph vertex type */ public class MutableDoubleValueGraphAdapter extends MutableValueGraphAdapter { private static final long serialVersionUID = -6335845255406679994L; /** * Create a new adapter. * * @param valueGraph the value graph */ public MutableDoubleValueGraphAdapter(MutableValueGraph valueGraph) { this(valueGraph, null, null); } /** * Create a new adapter. * * @param valueGraph the value graph * @param vertexSupplier the vertex supplier * @param edgeSupplier the edge supplier */ public MutableDoubleValueGraphAdapter( MutableValueGraph valueGraph, Supplier vertexSupplier, Supplier> edgeSupplier) { super( valueGraph, Graph.DEFAULT_EDGE_WEIGHT, (ToDoubleFunction & Serializable) x -> x, vertexSupplier, edgeSupplier); } @Override public void setEdgeWeight(EndpointPair e, double weight) { if (e == null) { throw new NullPointerException(); } if (!containsEdge(e)) { throw new IllegalArgumentException("no such edge in graph: " + e.toString()); } super.valueGraph.putEdgeValue(e.nodeU(), e.nodeV(), weight); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy