org.jgrapht.graph.AsUndirectedGraph Maven / Gradle / Ivy
/*
* (C) Copyright 2003-2016, by John V Sichi 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.*;
import org.jgrapht.util.*;
/**
* An undirected view of the backing directed graph specified in the constructor. This graph allows
* modules to apply algorithms designed for undirected graphs to a directed graph by simply ignoring
* edge direction. If the backing directed graph is an
* oriented graph, then the view will
* be a simple graph; otherwise, it will be a multigraph. Query operations on this graph "read
* through" to the backing graph. Attempts to add edges will result in an
* UnsupportedOperationException
, but vertex addition/removal and edge removal are all
* supported (and immediately reflected in the backing graph).
*
*
* Note that edges returned by this graph's accessors are really just the edges of the underlying
* directed graph. Since there is no interface distinction between directed and undirected edges,
* this detail should be irrelevant to algorithms.
*
*
*
* 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 John V. Sichi
* @since Aug 14, 2003
*/
public class AsUndirectedGraph
extends GraphDelegator
implements Serializable, UndirectedGraph
{
private static final long serialVersionUID = 3257845485078065462L; // @todo renew
private static final String NO_EDGE_ADD = "this graph does not support edge addition";
private static final String UNDIRECTED = "this graph only supports undirected operations";
/**
* Constructor for AsUndirectedGraph.
*
* @param g the backing directed graph over which an undirected view is to be created.
*/
public AsUndirectedGraph(DirectedGraph g)
{
super(g);
}
/**
* @see Graph#getAllEdges(Object, Object)
*/
@Override
public Set getAllEdges(V sourceVertex, V targetVertex)
{
Set forwardList = super.getAllEdges(sourceVertex, targetVertex);
if (sourceVertex.equals(targetVertex)) {
// avoid duplicating loops
return forwardList;
}
Set reverseList = super.getAllEdges(targetVertex, sourceVertex);
Set list = new ArrayUnenforcedSet<>(forwardList.size() + reverseList.size());
list.addAll(forwardList);
list.addAll(reverseList);
return list;
}
/**
* @see Graph#getEdge(Object, Object)
*/
@Override
public E getEdge(V sourceVertex, V targetVertex)
{
E edge = super.getEdge(sourceVertex, targetVertex);
if (edge != null) {
return edge;
}
// try the other direction
return super.getEdge(targetVertex, sourceVertex);
}
/**
* @see Graph#addEdge(Object, Object)
*/
@Override
public E addEdge(V sourceVertex, V targetVertex)
{
throw new UnsupportedOperationException(NO_EDGE_ADD);
}
/**
* @see Graph#addEdge(Object, Object, Object)
*/
@Override
public boolean addEdge(V sourceVertex, V targetVertex, E e)
{
throw new UnsupportedOperationException(NO_EDGE_ADD);
}
/**
* @see UndirectedGraph#degreeOf(Object)
*/
@Override
public int degreeOf(V vertex)
{
// this counts loops twice, which is consistent with AbstractBaseGraph
return super.inDegreeOf(vertex) + super.outDegreeOf(vertex);
}
/**
* @see DirectedGraph#inDegreeOf(Object)
*/
@Override
public int inDegreeOf(V vertex)
{
throw new UnsupportedOperationException(UNDIRECTED);
}
/**
* @see DirectedGraph#incomingEdgesOf(Object)
*/
@Override
public Set incomingEdgesOf(V vertex)
{
throw new UnsupportedOperationException(UNDIRECTED);
}
/**
* @see DirectedGraph#outDegreeOf(Object)
*/
@Override
public int outDegreeOf(V vertex)
{
throw new UnsupportedOperationException(UNDIRECTED);
}
/**
* @see DirectedGraph#outgoingEdgesOf(Object)
*/
@Override
public Set outgoingEdgesOf(V vertex)
{
throw new UnsupportedOperationException(UNDIRECTED);
}
/**
* @see AbstractBaseGraph#toString()
*/
@Override
public String toString()
{
return super.toStringFromSets(vertexSet(), edgeSet(), false);
}
}
// End AsUndirectedGraph.java
© 2015 - 2025 Weber Informatics LLC | Privacy Policy