Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.cmu.cs211.bacon.util;
- import java.util.Collection;
- import java.util.Map;
- import java.util.Set;
- import java.util.HashSet;
- /**
- * A base to help implement a directed graph
- */
- public class GeneralGraph<V,E extends Edge<V>> implements Graph<V,E> {
- private Set<V> vertexSet;
- private Set<Edge<V>> edgeSet;
- /**
- * Creates an empty graph.
- */
- public GeneralGraph()
- {
- vertexSet=new HashSet<V>();
- edgeSet=new HashSet<Edge<V>>();
- }
- /**
- * Creates a graph with pre-defined vertices.
- *
- * @param vertices
- * A list of the vertices in the graph.
- * @throws NullPointerException
- * If vertices is null, or contains null items
- */
- public GeneralGraph (Collection<? extends V> vertices)
- {
- if(vertices==null)
- throw new NullPointerException();
- vertexSet=new HashSet<V>();
- edgeSet=new HashSet<Edge<V>>();
- addVertices(vertices);
- }
- /**
- * Creates a graph with pre-defined edges and vertices.
- *
- * @param vertices
- * A list of the vertices in the graph.
- * @param edges
- * A list of edges for the graph.
- * @throws IllegalArgumentException if edges contains invalid edges.
- * @throws NullPointerException
- * If vertices or edges is null, or either contain null items
- */
- public GeneralGraph (Collection<? extends V> vertices, Collection<? extends E> edges)
- {
- if(vertices==null || edges==null)
- throw new NullPointerException();
- vertexSet=new HashSet<V>();
- edgeSet=new HashSet<Edge<V>>();
- addVertices(vertices);
- addEdges(edges);
- }
- /**
- * Copy Constructor
- *
- * @param g
- * A graph to copy
- * @throws IllegalArgumentException if g violates Graph invariants by
- * returning illegal edges in its outgoingEdge methods
- * @throws NullPointerException
- * If g is null, or g's methods violates Graph invariants
- * by returning null items in verticies or outgoingEdges
- */
- public GeneralGraph (Graph <V, E> g)
- {
- if(g==null)
- throw new NullPointerException();
- vertexSet=g.vertices();
- edgeSet=new HashSet<Edge<V>>();
- for(V v:g.vertices())
- addEdges(g.outgoingEdges(v));
- }
- public boolean addVertex(V vertex)
- {
- if(vertex==null)
- throw new NullPointerException();
- if(vertexSet.contains(vertex))
- return false;
- else
- {
- vertexSet.add(vertex);
- return true;
- }
- }
- public boolean addVertices(Collection<? extends V> vertices)
- {
- boolean contains=true;
- for(V v: vertices)
- {
- if(v==null)
- throw new NullPointerException();
- if(!vertexSet.contains(v)){
- vertexSet.add(v);
- contains=false;
- }
- }
- if(contains==false)
- return true;
- else
- return false;
- }
- public boolean addEdge (E e)
- {
- if(e==null)
- throw new NullPointerException();
- //Edge edge=(Edge)e;
- if(!vertexSet.contains(e.src()) || !vertexSet.contains(e.dest()) || e.src()==e.dest())
- throw new IllegalArgumentException();
- if(edgeSet.contains(e))
- return false;
- else
- edgeSet.add(e);
- return true;
- }
- public boolean addEdges (Collection<? extends E> edges)
- {
- if(edges==null)
- throw new NullPointerException();
- boolean bool=false;
- for(E e: edges){
- //Edge edge=(Edge)e;
- if(!vertexSet.contains(e.src()) || !vertexSet.contains(e.dest()))
- throw new IllegalArgumentException();
- if(!edgeSet.contains(e)){
- edgeSet.add(e);
- bool=true;
- }
- }
- if(bool)
- return true;
- else
- return false;
- }
- public boolean removeEdge (V src, V dest)
- {
- if(src==null || dest==null)
- throw new NullPointerException();
- else if(!vertexSet.contains(src) || !vertexSet.contains(dest))
- throw new IllegalArgumentException();
- else
- {
- for(Edge e: edgeSet)
- if(e.src()==src && e.dest() == dest){
- edgeSet.remove(e);
- return true;
- }
- return false;
- }
- }
- public void clearEdges ()
- {
- edgeSet=new HashSet<Edge<V>>();
- }
- public Set<V> vertices ()
- {
- return vertexSet;
- }
- public E connected (V i, V j)
- {
- if(i==null || j==null)
- throw new NullPointerException();
- if(!vertexSet.contains(i) || !vertexSet.contains(j))
- throw new IllegalArgumentException();
- for(Edge e:edgeSet)
- if((e.src()==i && e.dest()==j) || (e.src()==j && e.dest()==i))
- return (E)e;
- return null;
- }
- public Set<V> neighbors (V vertex)
- {
- if(vertex==null)
- throw new NullPointerException();
- if(!vertexSet.contains(vertex))
- throw new IllegalArgumentException();
- Set<V> neighbors=new HashSet<V>();
- for(Edge e: edgeSet)
- {
- if(e.src()==vertex)
- neighbors.add((V)e.dest());
- /*else if(e.dest()==vertex)
- neighbors.add((V)e.src());*/
- }
- return neighbors;
- }
- public Collection<E> outgoingEdges (V vertex)
- {
- if(vertex==null)
- throw new NullPointerException();
- if(!vertexSet.contains(vertex))
- throw new IllegalArgumentException();
- Collection<E> outgoingEdges=new HashSet<E>();
- for(Edge e: edgeSet)
- if(e.src()==vertex)
- outgoingEdges.add((E)e);
- return outgoingEdges;
- }
- }
Add Comment
Please, Sign In to add comment