Guest User

Untitled

a guest
Jan 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. package edu.cmu.cs211.bacon.util;
  2.  
  3. import java.util.Collection;
  4. import java.util.Map;
  5. import java.util.Set;
  6. import java.util.HashSet;
  7. /**
  8. * A base to help implement a directed graph
  9. */
  10. public class GeneralGraph<V,E extends Edge<V>> implements Graph<V,E> {
  11.  
  12. private Set<V> vertexSet;
  13. private Set<Edge<V>> edgeSet;
  14. /**
  15. * Creates an empty graph.
  16. */
  17. public GeneralGraph()
  18. {
  19. vertexSet=new HashSet<V>();
  20. edgeSet=new HashSet<Edge<V>>();
  21. }
  22.  
  23. /**
  24. * Creates a graph with pre-defined vertices.
  25. *
  26. * @param vertices
  27. * A list of the vertices in the graph.
  28. * @throws NullPointerException
  29. * If vertices is null, or contains null items
  30. */
  31. public GeneralGraph (Collection<? extends V> vertices)
  32. {
  33. if(vertices==null)
  34. throw new NullPointerException();
  35. vertexSet=new HashSet<V>();
  36. edgeSet=new HashSet<Edge<V>>();
  37. addVertices(vertices);
  38. }
  39.  
  40. /**
  41. * Creates a graph with pre-defined edges and vertices.
  42. *
  43. * @param vertices
  44. * A list of the vertices in the graph.
  45. * @param edges
  46. * A list of edges for the graph.
  47. * @throws IllegalArgumentException if edges contains invalid edges.
  48. * @throws NullPointerException
  49. * If vertices or edges is null, or either contain null items
  50. */
  51. public GeneralGraph (Collection<? extends V> vertices, Collection<? extends E> edges)
  52. {
  53. if(vertices==null || edges==null)
  54. throw new NullPointerException();
  55. vertexSet=new HashSet<V>();
  56. edgeSet=new HashSet<Edge<V>>();
  57. addVertices(vertices);
  58. addEdges(edges);
  59. }
  60.  
  61. /**
  62. * Copy Constructor
  63. *
  64. * @param g
  65. * A graph to copy
  66. * @throws IllegalArgumentException if g violates Graph invariants by
  67. * returning illegal edges in its outgoingEdge methods
  68. * @throws NullPointerException
  69. * If g is null, or g's methods violates Graph invariants
  70. * by returning null items in verticies or outgoingEdges
  71. */
  72. public GeneralGraph (Graph <V, E> g)
  73. {
  74. if(g==null)
  75. throw new NullPointerException();
  76. vertexSet=g.vertices();
  77. edgeSet=new HashSet<Edge<V>>();
  78. for(V v:g.vertices())
  79. addEdges(g.outgoingEdges(v));
  80.  
  81. }
  82.  
  83.  
  84. public boolean addVertex(V vertex)
  85. {
  86. if(vertex==null)
  87. throw new NullPointerException();
  88. if(vertexSet.contains(vertex))
  89. return false;
  90. else
  91. {
  92. vertexSet.add(vertex);
  93. return true;
  94. }
  95. }
  96.  
  97. public boolean addVertices(Collection<? extends V> vertices)
  98. {
  99. boolean contains=true;
  100. for(V v: vertices)
  101. {
  102. if(v==null)
  103. throw new NullPointerException();
  104. if(!vertexSet.contains(v)){
  105. vertexSet.add(v);
  106. contains=false;
  107. }
  108. }
  109. if(contains==false)
  110. return true;
  111. else
  112. return false;
  113. }
  114.  
  115. public boolean addEdge (E e)
  116. {
  117. if(e==null)
  118. throw new NullPointerException();
  119. //Edge edge=(Edge)e;
  120. if(!vertexSet.contains(e.src()) || !vertexSet.contains(e.dest()) || e.src()==e.dest())
  121. throw new IllegalArgumentException();
  122. if(edgeSet.contains(e))
  123. return false;
  124. else
  125. edgeSet.add(e);
  126. return true;
  127. }
  128.  
  129. public boolean addEdges (Collection<? extends E> edges)
  130. {
  131. if(edges==null)
  132. throw new NullPointerException();
  133. boolean bool=false;
  134. for(E e: edges){
  135. //Edge edge=(Edge)e;
  136. if(!vertexSet.contains(e.src()) || !vertexSet.contains(e.dest()))
  137. throw new IllegalArgumentException();
  138. if(!edgeSet.contains(e)){
  139. edgeSet.add(e);
  140. bool=true;
  141. }
  142. }
  143. if(bool)
  144. return true;
  145. else
  146. return false;
  147. }
  148.  
  149. public boolean removeEdge (V src, V dest)
  150. {
  151. if(src==null || dest==null)
  152. throw new NullPointerException();
  153. else if(!vertexSet.contains(src) || !vertexSet.contains(dest))
  154. throw new IllegalArgumentException();
  155. else
  156. {
  157. for(Edge e: edgeSet)
  158. if(e.src()==src && e.dest() == dest){
  159. edgeSet.remove(e);
  160. return true;
  161. }
  162. return false;
  163. }
  164.  
  165. }
  166.  
  167.  
  168. public void clearEdges ()
  169. {
  170. edgeSet=new HashSet<Edge<V>>();
  171. }
  172.  
  173. public Set<V> vertices ()
  174. {
  175. return vertexSet;
  176. }
  177.  
  178. public E connected (V i, V j)
  179. {
  180. if(i==null || j==null)
  181. throw new NullPointerException();
  182. if(!vertexSet.contains(i) || !vertexSet.contains(j))
  183. throw new IllegalArgumentException();
  184. for(Edge e:edgeSet)
  185. if((e.src()==i && e.dest()==j) || (e.src()==j && e.dest()==i))
  186. return (E)e;
  187. return null;
  188. }
  189.  
  190. public Set<V> neighbors (V vertex)
  191. {
  192. if(vertex==null)
  193. throw new NullPointerException();
  194. if(!vertexSet.contains(vertex))
  195. throw new IllegalArgumentException();
  196. Set<V> neighbors=new HashSet<V>();
  197. for(Edge e: edgeSet)
  198. {
  199. if(e.src()==vertex)
  200. neighbors.add((V)e.dest());
  201. /*else if(e.dest()==vertex)
  202. neighbors.add((V)e.src());*/
  203. }
  204. return neighbors;
  205. }
  206.  
  207. public Collection<E> outgoingEdges (V vertex)
  208. {
  209. if(vertex==null)
  210. throw new NullPointerException();
  211. if(!vertexSet.contains(vertex))
  212. throw new IllegalArgumentException();
  213. Collection<E> outgoingEdges=new HashSet<E>();
  214. for(Edge e: edgeSet)
  215. if(e.src()==vertex)
  216. outgoingEdges.add((E)e);
  217. return outgoingEdges;
  218. }
  219. }
Add Comment
Please, Sign In to add comment