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

edu.uci.ics.jung.graph.OrderedSparseMultigraph 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.LinkedHashMap;
import java.util.LinkedHashSet;
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 orders its vertex and edge collections
 * according to insertion time, is suitable for sparse graphs, and 
 * permits directed, undirected, and parallel edges.
 */
@SuppressWarnings("serial")
public class OrderedSparseMultigraph 
    extends SparseMultigraph
    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 OrderedSparseMultigraph();
			}
		};
	}

    /**
     * Creates a new instance.
     */
    public OrderedSparseMultigraph()
    {
        vertices = new LinkedHashMap>>();
        edges = new LinkedHashMap>();
        directedEdges = new LinkedHashSet();
    }

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


    @Override
    public Collection getPredecessors(V vertex)
    {
        if (!containsVertex(vertex))
            return null;
        
        Set preds = new LinkedHashSet();
        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);
    }

    @Override
    public Collection getSuccessors(V vertex)
    {
        if (!containsVertex(vertex))
            return null;

        Set succs = new LinkedHashSet();
        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);
    }

    @Override
    public Collection getNeighbors(V vertex)
    {
        if (!containsVertex(vertex))
            return null;

        Collection out = new LinkedHashSet();
        out.addAll(this.getPredecessors(vertex));
        out.addAll(this.getSuccessors(vertex));
        return out;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy