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

edu.uci.ics.jung.graph.SparseMultigraph Maven / Gradle / Ivy

The newest version!
/*
 * 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.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

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 Graph that is suitable for sparse graphs
 * and permits directed, undirected, and parallel edges.
 */
@SuppressWarnings("serial")
public class SparseMultigraph 
    extends AbstractGraph
    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 SparseMultigraph();
			}
		};
	}
    
    // TODO: refactor internal representation: right now directed edges each have two references (in vertices and directedEdges)
    // and undirected also have two (incoming and outgoing).  
    protected Map>> vertices; // Map of vertices to Pair of adjacency sets {incoming, outgoing}
    protected Map> edges;            // Map of edges to incident vertex pairs
    protected Set directedEdges;

    /**
     * Creates a new instance.
     */
    public SparseMultigraph()
    {
        vertices = new HashMap>>();
        edges = new HashMap>();
        directedEdges = new HashSet();
    }

    public Collection getEdges()
    {
        return Collections.unmodifiableCollection(edges.keySet());
    }

    public Collection getVertices()
    {
        return Collections.unmodifiableCollection(vertices.keySet());
    }
    
    public boolean containsVertex(V vertex) {
    	return vertices.keySet().contains(vertex);
    }
    
    public boolean containsEdge(E edge) {
    	return edges.keySet().contains(edge);
    }

    protected Collection getIncoming_internal(V vertex)
    {
        return vertices.get(vertex).getFirst();
    }
    
    protected Collection getOutgoing_internal(V vertex)
    {
        return vertices.get(vertex).getSecond();
    }
    
    public boolean addVertex(V vertex) {
        if(vertex == null) {
            throw new IllegalArgumentException("vertex may not be null");
        }
        if (!vertices.containsKey(vertex)) {
            vertices.put(vertex, new Pair>(new HashSet(), new HashSet()));
            return true;
        } else {
        	return false;
        }
    }

    public boolean removeVertex(V vertex) {
        if (!containsVertex(vertex))
            return false;
        
        // copy to avoid concurrent modification in removeEdge
        Set incident = new HashSet(getIncoming_internal(vertex));
        incident.addAll(getOutgoing_internal(vertex));
        
        for (E edge : incident)
            removeEdge(edge);
        
        vertices.remove(vertex);
        
        return true;
    }
    
    @Override
    public boolean addEdge(E edge, Pair endpoints, EdgeType edgeType) {

        Pair new_endpoints = getValidatedEndpoints(edge, endpoints);
        if (new_endpoints == null)
            return false;
        
        V v1 = new_endpoints.getFirst();
        V v2 = new_endpoints.getSecond();
        
        if (!vertices.containsKey(v1))
            this.addVertex(v1);
        
        if (!vertices.containsKey(v2))
            this.addVertex(v2);
        

        vertices.get(v1).getSecond().add(edge);        
        vertices.get(v2).getFirst().add(edge);        
        edges.put(edge, new_endpoints);
        if(edgeType == EdgeType.DIRECTED) {
        	directedEdges.add(edge);
        } else {
          vertices.get(v1).getFirst().add(edge);        
          vertices.get(v2).getSecond().add(edge);        
        }
        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 edge from incident vertices' adjacency sets
        vertices.get(v1).getSecond().remove(edge);
        vertices.get(v2).getFirst().remove(edge);

        if(directedEdges.remove(edge) == false) {
        	
        	// its an undirected edge, remove the other ends
            vertices.get(v2).getSecond().remove(edge);
            vertices.get(v1).getFirst().remove(edge);
        }
        edges.remove(edge);
        return true;
    }
    
    public Collection getInEdges(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;
        return Collections.unmodifiableCollection(vertices.get(vertex).getFirst());
    }

    public Collection getOutEdges(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;
        return Collections.unmodifiableCollection(vertices.get(vertex).getSecond());
    }

    // TODO: this will need to get changed if we modify the internal representation
    public Collection getPredecessors(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;

        Set preds = new HashSet();
        for (E edge : getIncoming_internal(vertex)) {
        	if(getEdgeType(edge) == EdgeType.DIRECTED) {
        		preds.add(this.getSource(edge));
        	} else {
        		preds.add(getOpposite(vertex, edge));
        	}
        }
        return Collections.unmodifiableCollection(preds);
    }

    // TODO: this will need to get changed if we modify the internal representation
    public Collection getSuccessors(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;
        Set succs = new HashSet();
        for (E edge : getOutgoing_internal(vertex)) {
        	if(getEdgeType(edge) == EdgeType.DIRECTED) {
        		succs.add(this.getDest(edge));
        	} else {
        		succs.add(getOpposite(vertex, edge));
        	}
        }
        return Collections.unmodifiableCollection(succs);
    }

    public Collection getNeighbors(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;
        Collection out = new HashSet();
        out.addAll(this.getPredecessors(vertex));
        out.addAll(this.getSuccessors(vertex));
        return out;
    }

    public Collection getIncidentEdges(V vertex)
    {
    	if (!containsVertex(vertex))
    		return null;
        Collection out = new HashSet();
        out.addAll(this.getInEdges(vertex));
        out.addAll(this.getOutEdges(vertex));
        return out;
    }

    @Override
    public E findEdge(V v1, V v2)
    {
    	if (!containsVertex(v1) || !containsVertex(v2))
    		return null;
        for (E edge : getOutgoing_internal(v1))
            if (this.getOpposite(v1, edge).equals(v2))
                return edge;
        
        return null;
    }

    public Pair getEndpoints(E edge)
    {
        return edges.get(edge);
    }

    public V getSource(E edge) {
    	if(directedEdges.contains(edge)) {
    		return this.getEndpoints(edge).getFirst();
    	}
    	return null;
    }

    public V getDest(E edge) {
    	if(directedEdges.contains(edge)) {
    		return this.getEndpoints(edge).getSecond();
    	}
    	return null;
    }

    public boolean isSource(V vertex, E edge) {
    	if (!containsEdge(edge) || !containsVertex(vertex))
    		return false;
        return getSource(edge).equals(vertex);
    }

    public boolean isDest(V vertex, E edge) {
    	if (!containsEdge(edge) || !containsVertex(vertex))
    		return false;
        return getDest(edge).equals(vertex);
    }

    public EdgeType getEdgeType(E edge) {
    	return directedEdges.contains(edge) ?
    		EdgeType.DIRECTED :
    			EdgeType.UNDIRECTED;
    }

	@SuppressWarnings("unchecked")
	public Collection getEdges(EdgeType edgeType) {
		if(edgeType == EdgeType.DIRECTED) {
			return Collections.unmodifiableSet(this.directedEdges);
		} else if(edgeType == EdgeType.UNDIRECTED) {
			Collection edges = new HashSet(getEdges());
			edges.removeAll(directedEdges);
			return edges;
		} else {
			return Collections.EMPTY_SET;
		}
		
	}

	public int getEdgeCount() {
		return edges.keySet().size();
	}

	public int getVertexCount() {
		return vertices.keySet().size();
	}

    public int getEdgeCount(EdgeType edge_type)
    {
        return getEdges(edge_type).size();
    }

	public EdgeType getDefaultEdgeType() 
	{
		return EdgeType.UNDIRECTED;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy