edu.uci.ics.jung.graph.SortedSparseMultigraph 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 Oct 18, 2005
*
* Copyright (c) 2005, 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.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.google.common.base.Supplier;
import com.google.common.collect.Ordering;
import edu.uci.ics.jung.graph.util.Pair;
/**
* An implementation of Graph
that is suitable for sparse graphs,
* orders its vertex and edge collections according to either specified Comparator
* instances or the natural ordering of their elements, and permits directed, undirected,
* and parallel edges.
*
* @author Joshua O'Madadhain
*/
@SuppressWarnings("serial")
public class SortedSparseMultigraph
extends OrderedSparseMultigraph
implements MultiGraph
{
/**
* @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 Graph get()
{
return new SortedSparseMultigraph();
}
};
}
/**
* Comparator
used in ordering vertices. Defaults to util.ComparableComparator
* if no comparators are specified in the constructor.
*/
protected Comparator vertex_comparator;
/**
* Comparator
used in ordering edges. Defaults to util.ComparableComparator
* if no comparators are specified in the constructor.
*/
protected Comparator edge_comparator;
/**
* Creates a new instance which sorts its vertices and edges according to the
* specified {@code Comparator}s.
* @param vertex_comparator specifies how the vertices are to be compared
* @param edge_comparator specifies how the edges are to be compared
*/
public SortedSparseMultigraph(Comparator vertex_comparator, Comparator edge_comparator)
{
this.vertex_comparator = vertex_comparator;
this.edge_comparator = edge_comparator;
vertices = new TreeMap>>(vertex_comparator);
edges = new TreeMap>(edge_comparator);
directedEdges = new TreeSet(edge_comparator);
}
/**
* Creates a new instance which sorts its vertices and edges according to
* their natural ordering.
*/
public SortedSparseMultigraph()
{
this(new Ordering(){
@SuppressWarnings("unchecked")
public int compare(V v1, V v2) {
return ((Comparable) v1).compareTo(v2);
}},
new Ordering(){
@SuppressWarnings("unchecked")
public int compare(E e1, E e2) {
return ((Comparable) e1).compareTo(e2);
}});
}
/**
* Provides a new {@code Comparator} to be used in sorting the vertices.
* @param vertex_comparator the comparator that defines the new ordering
*/
public void setVertexComparator(Comparator vertex_comparator)
{
this.vertex_comparator = vertex_comparator;
Map>> tmp_vertices = new TreeMap>>(vertex_comparator);
for (Map.Entry>> entry : vertices.entrySet())
tmp_vertices.put(entry.getKey(), entry.getValue());
this.vertices = tmp_vertices;
}
@Override
public boolean addVertex(V vertex) {
if(vertex == null) {
throw new IllegalArgumentException("vertex may not be null");
}
if (!containsVertex(vertex))
{
vertices.put(vertex, new Pair>(new TreeSet(edge_comparator),
new TreeSet(edge_comparator)));
return true;
}
else
{
return false;
}
}
}