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

org.jgrapht.graph.AsWeightedGraph Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
/*
 * (C) Copyright 2007-2017, by Lucas J Scharenbroich 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;

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

import org.jgrapht.*;

/**
 * 

* A weighted view of the backing graph specified in the constructor. This graph allows modules to * apply algorithms designed for weighted graphs to an unweighted graph by providing an explicit * edge weight mapping. The implementation also allows for "masking" weights for a subset of the * edges in an existing weighted graph. *

* *

* Query operations on this graph "read through" to the backing graph. Vertex addition/removal and * edge addition/removal are all supported (and immediately reflected in the backing graph). Setting * an edge weight will pass the operation to the backing graph as well if the backing graph * implements the WeightedGraph interface. Setting an edge weight will modify the weight map in * order to maintain a consistent graph. *

* *

* Note that edges returned by this graph's accessors are really just the edges of the underlying * directed graph. *

* *

* This graph does not pass the hashCode and equals operations through to the backing graph, * but relies on Object's equals and hashCode methods. This graph will be * serializable if the backing graph is serializable. *

* * @param the graph vertex type * @param the graph edge type * * @author Lucas J. Scharenbroich * @since Sep 10, 2007 */ public class AsWeightedGraph extends GraphDelegator implements Serializable, WeightedGraph { /** */ private static final long serialVersionUID = -716810639338971372L; protected final Map weightMap; private final boolean isWeightedGraph; /** * Constructor for AsWeightedGraph. * * @param g the backing graph over which a weighted view is to be created. * @param weightMap A mapping of edges to weights. If an edge is not present in the weight map, * the edge weight for the underlying graph is returned. Note that a live reference to * this map is retained, so if the caller changes the map after construction, the changes * will affect the AsWeightedGraph instance as well. */ public AsWeightedGraph(Graph g, Map weightMap) { super(g); assert (weightMap != null); this.weightMap = weightMap; // Remember whether the backing graph implements the WeightedGraph // interface this.isWeightedGraph = (g instanceof WeightedGraph); } /** * @see WeightedGraph#setEdgeWeight */ @Override public void setEdgeWeight(E e, double weight) { if (isWeightedGraph) { super.setEdgeWeight(e, weight); } // Always modify the weight map. It would be a terrible violation // of the use contract to silently ignore changes to the weights. weightMap.put(e, weight); } /** * @see Graph#getEdgeWeight */ @Override public double getEdgeWeight(E e) { double weight; // Always return the value from the weight map first and // only pass the call through as a backup if (weightMap.containsKey(e)) { weight = weightMap.get(e); } else { weight = super.getEdgeWeight(e); } return weight; } } // End AsWeightedGraph.java




© 2015 - 2024 Weber Informatics LLC | Privacy Policy