edu.uci.ics.jung.graph.UndirectedSparseGraph Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jung-graph-impl Show documentation
Show all versions of jung-graph-impl Show documentation
Graph implementations for the JUNG project
/*
* Created on Apr 1, 2007
*
* Copyright (c) 2007, The JUNG Authors
*
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* https://github.com/jrtom/jung/blob/master/LICENSE for a description.
*/
package edu.uci.ics.jung.graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Supplier;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.graph.util.Pair;
/**
* An implementation of UndirectedGraph
that is suitable
* for sparse graphs.
*/
@SuppressWarnings("serial")
public class UndirectedSparseGraph extends AbstractTypedGraph
implements UndirectedGraph
{
/**
* @param the vertex type for the graph Supplier
* @param the edge type for the graph Supplier
* @return a {@code Supplier} that creates an instance of this graph type.
*/
public static Supplier> getFactory() {
return new Supplier> () {
public UndirectedGraph get() {
return new UndirectedSparseGraph();
}
};
}
protected Map> vertices; // Map of vertices to adjacency maps of vertices to incident edges
protected Map> edges; // Map of edges to incident vertex sets
/**
* Creates an instance.
*/
public UndirectedSparseGraph() {
super(EdgeType.UNDIRECTED);
vertices = new HashMap>();
edges = new HashMap>();
}
@Override
public boolean addEdge(E edge, Pair extends V> endpoints, EdgeType edgeType)
{
this.validateEdgeType(edgeType);
Pair new_endpoints = getValidatedEndpoints(edge, endpoints);
if (new_endpoints == null)
return false;
V v1 = new_endpoints.getFirst();
V v2 = new_endpoints.getSecond();
if (findEdge(v1, v2) != null)
return false;
edges.put(edge, new_endpoints);
if (!vertices.containsKey(v1))
this.addVertex(v1);
if (!vertices.containsKey(v2))
this.addVertex(v2);
// map v1 to and vice versa
vertices.get(v1).put(v2, edge);
vertices.get(v2).put(v1, edge);
return true;
}
public Collection getInEdges(V vertex)
{
return this.getIncidentEdges(vertex);
}
public Collection getOutEdges(V vertex)
{
return this.getIncidentEdges(vertex);
}
public Collection getPredecessors(V vertex)
{
return this.getNeighbors(vertex);
}
public Collection getSuccessors(V vertex)
{
return this.getNeighbors(vertex);
}
@Override
public E findEdge(V v1, V v2)
{
if (!containsVertex(v1) || !containsVertex(v2))
return null;
return vertices.get(v1).get(v2);
}
@Override
public Collection findEdgeSet(V v1, V v2)
{
if (!containsVertex(v1) || !containsVertex(v2))
return null;
ArrayList edge_collection = new ArrayList(1);
// if (!containsVertex(v1) || !containsVertex(v2))
// return edge_collection;
E e = findEdge(v1, v2);
if (e == null)
return edge_collection;
edge_collection.add(e);
return edge_collection;
}
public Pair getEndpoints(E edge)
{
return edges.get(edge);
}
public V getSource(E directed_edge)
{
return null;
}
public V getDest(E directed_edge)
{
return null;
}
public boolean isSource(V vertex, E edge)
{
return false;
}
public boolean isDest(V vertex, E edge)
{
return false;
}
public Collection getEdges()
{
return Collections.unmodifiableCollection(edges.keySet());
}
public Collection getVertices()
{
return Collections.unmodifiableCollection(vertices.keySet());
}
public boolean containsVertex(V vertex)
{
return vertices.containsKey(vertex);
}
public boolean containsEdge(E edge)
{
return edges.containsKey(edge);
}
public int getEdgeCount()
{
return edges.size();
}
public int getVertexCount()
{
return vertices.size();
}
public Collection getNeighbors(V vertex)
{
if (!containsVertex(vertex))
return null;
return Collections.unmodifiableCollection(vertices.get(vertex).keySet());
}
public Collection getIncidentEdges(V vertex)
{
if (!containsVertex(vertex))
return null;
return Collections.unmodifiableCollection(vertices.get(vertex).values());
}
public boolean addVertex(V vertex)
{
if(vertex == null) {
throw new IllegalArgumentException("vertex may not be null");
}
if (!containsVertex(vertex)) {
vertices.put(vertex, new HashMap());
return true;
} else {
return false;
}
}
public boolean removeVertex(V vertex)
{
if (!containsVertex(vertex))
return false;
// iterate over copy of incident edge collection
for (E edge : new ArrayList(vertices.get(vertex).values()))
removeEdge(edge);
vertices.remove(vertex);
return true;
}
public boolean removeEdge(E edge)
{
if (!containsEdge(edge))
return false;
Pair endpoints = getEndpoints(edge);
V v1 = endpoints.getFirst();
V v2 = endpoints.getSecond();
// remove incident vertices from each others' adjacency maps
vertices.get(v1).remove(v2);
vertices.get(v2).remove(v1);
edges.remove(edge);
return true;
}
}