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

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

There is a newer version: 2.1.1
Show newest version
/*
 * Created on Mar 26, 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.HashSet;
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 DirectedGraph suitable for sparse graphs.
 */
@SuppressWarnings("serial")
public class DirectedSparseGraph extends AbstractTypedGraph implements
        DirectedGraph
{
    /**
     * @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 final  Supplier> getFactory() {
        return new Supplier> () {
            public DirectedGraph get() {
                return new DirectedSparseGraph();
            }
        };
    }

    protected Map>> vertices;  // Map of vertices to Pair of adjacency maps {incoming, outgoing} 
                                                // of neighboring vertices to incident edges
    protected Map> edges;            // Map of edges to incident vertex pairs

    /**
     * Creates an instance.
     */
    public DirectedSparseGraph() 
    {
    	super(EdgeType.DIRECTED);
        vertices = new HashMap>>();
        edges = new HashMap>();
    }
    
    @Override
    public boolean addEdge(E edge, Pair endpoints, EdgeType edgeType)
    {
    	this.validateEdgeType(edgeType);
        Pair new_endpoints = getValidatedEndpoints(edge, endpoints);
        if (new_endpoints == null)
            return false;
        
        V source = new_endpoints.getFirst();
        V dest = new_endpoints.getSecond();
        
        if (findEdge(source, dest) != null)
            return false;
        
        edges.put(edge, new_endpoints);

        if (!vertices.containsKey(source))
            this.addVertex(source);
        
        if (!vertices.containsKey(dest))
            this.addVertex(dest);
        
        // map source of this edge to  and vice versa
        vertices.get(source).getSecond().put(dest, edge);
        vertices.get(dest).getFirst().put(source, edge);

        return true;
    }

    @Override
    public E findEdge(V v1, V v2)
    {
        if (!containsVertex(v1) || !containsVertex(v2))
            return null;
        return vertices.get(v1).getSecond().get(v2);
    }

    @Override
    public Collection findEdgeSet(V v1, V v2)
    {
        if (!containsVertex(v1) || !containsVertex(v2))
            return null;
        ArrayList edge_collection = new ArrayList(1);
        E e = findEdge(v1, v2);
        if (e == null)
            return edge_collection;
        edge_collection.add(e);
        return edge_collection;
    }
    
    protected Collection getIncoming_internal(V vertex)
    {
        return vertices.get(vertex).getFirst().values();
    }
    
    protected Collection getOutgoing_internal(V vertex)
    {
        return vertices.get(vertex).getSecond().values();
    }
    
    protected Collection getPreds_internal(V vertex)
    {
        return vertices.get(vertex).getFirst().keySet();
    }
    
    protected Collection getSuccs_internal(V vertex)
    {
        return vertices.get(vertex).getSecond().keySet();
    }
    
    public Collection getInEdges(V vertex)
    {
        if (!containsVertex(vertex))
            return null;
        return Collections.unmodifiableCollection(getIncoming_internal(vertex));
    }

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

    public Collection getPredecessors(V vertex)
    {
        if (!containsVertex(vertex))
            return null;
        return Collections.unmodifiableCollection(getPreds_internal(vertex));
    }

    public Collection getSuccessors(V vertex)
    {
        if (!containsVertex(vertex))
            return null;
        return Collections.unmodifiableCollection(getSuccs_internal(vertex));
    }

    public Pair getEndpoints(E edge)
    {
        if (!containsEdge(edge))
            return null;
        return edges.get(edge);
    }

    public V getSource(E directed_edge)
    {
        if (!containsEdge(directed_edge))
            return null;
        return edges.get(directed_edge).getFirst();
    }

    public V getDest(E directed_edge)
    {
        if (!containsEdge(directed_edge))
            return null;
        return edges.get(directed_edge).getSecond();
    }

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

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

    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;
        
        Collection neighbors = new HashSet();
        neighbors.addAll(getPreds_internal(vertex));
        neighbors.addAll(getSuccs_internal(vertex));
        return Collections.unmodifiableCollection(neighbors);
    }

    public Collection getIncidentEdges(V vertex)
    {
        if (!containsVertex(vertex))
            return null;
        
        Collection incident_edges = new HashSet();
        incident_edges.addAll(getIncoming_internal(vertex));
        incident_edges.addAll(getOutgoing_internal(vertex));
        return Collections.unmodifiableCollection(incident_edges);
    }

    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 HashMap(), new HashMap()));
            return true;
        } else {
            return false;
        }
    }

    public boolean removeVertex(V vertex) {
        if (!containsVertex(vertex))
            return false;
        
        // copy to avoid concurrent modification in removeEdge
        ArrayList incident = new ArrayList(getIncoming_internal(vertex));
        incident.addAll(getOutgoing_internal(vertex));
        
        for (E edge : incident)
            removeEdge(edge);
        
        vertices.remove(vertex);
        
        return true;
    }
    
    public boolean removeEdge(E edge) {
        if (!containsEdge(edge))
            return false;
        
        Pair endpoints = this.getEndpoints(edge);
        V source = endpoints.getFirst();
        V dest = endpoints.getSecond();
        
        // remove vertices from each others' adjacency maps
        vertices.get(source).getSecond().remove(dest);
        vertices.get(dest).getFirst().remove(source);
        
        edges.remove(edge);
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy