Advertisement
Guest User

willsabich

a guest
Nov 14th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Set;
  4. import java.util.stream.Collectors;
  5.  
  6. /**
  7. * Filename: Graph.java
  8. * Project: p4
  9. * Authors: Alex Restifo (restifo@wisc.edu)
  10. * <p>
  11. * Directed and unweighted graph implementation
  12. */
  13.  
  14. public class Graph implements GraphADT {
  15. List<Package> packages;
  16.  
  17. /*
  18. * Default no-argument constructor
  19. */
  20. public Graph() {
  21. this.packages = new ArrayList<>();
  22. }
  23.  
  24. /**
  25. * Add new vertex to the graph.
  26. * <p>
  27. * If vertex is null or already exists,
  28. * method ends without adding a vertex or
  29. * throwing an exception.
  30. * <p>
  31. * Valid argument conditions:
  32. * 1. vertex is non-null
  33. * 2. vertex is not already in the graph
  34. */
  35. public void addVertex(String vertex) {
  36. // Validate input
  37. if (vertex == null || vertex.isEmpty() || containsPackage(vertex)) return;
  38.  
  39. // Create package
  40. Package newVertex = new Package();
  41. newVertex.setDependencies(new String[10]);
  42. newVertex.setName(vertex);
  43.  
  44. packages.add(newVertex);
  45. }
  46.  
  47. /**
  48. * Remove a vertex and all associated
  49. * edges from the graph.
  50. * <p>
  51. * If vertex is null or does not exist,
  52. * method ends without removing a vertex, edges,
  53. * or throwing an exception.
  54. * <p>
  55. * Valid argument conditions:
  56. * 1. vertex is non-null
  57. * 2. vertex is not already in the graph
  58. */
  59. public void removeVertex(String vertex) {
  60. if (vertex == null || !containsPackage(vertex)) return;
  61.  
  62. // Remove all edges associated with the given vertex
  63. for (Package p : packages) {
  64. String[] pEdges = p.getDependencies();
  65. for (int i = 0; i < pEdges.length; i++) {
  66. if (pEdges[i] != null) {
  67. // If any edge points to the given vertex, remove it
  68. if (pEdges[i].equals(vertex)) {
  69. pEdges[i] = null;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. // After removing all edges referring to the package, remove the package itself
  76. Package toBeRemoved = getPackageFromName(vertex);
  77. packages.remove(toBeRemoved);
  78. }
  79.  
  80. /**
  81. * Add the edge from vertex1 to vertex2
  82. * to this graph. (edge is directed and unweighted)
  83. * If either vertex does not exist,
  84. * add vertex, and add edge, no exception is thrown.
  85. * If the edge exists in the graph,
  86. * no edge is added and no exception is thrown.
  87. * <p>
  88. * Valid argument conditions:
  89. * 1. neither vertex is null
  90. * 2. both packages are in the graph
  91. * 3. the edge is not in the graph
  92. */
  93. public void addEdge(String vertex1, String vertex2) {
  94. if (vertex1 == null || vertex1.isEmpty() || vertex2 == null || vertex2.isEmpty()) return;
  95.  
  96. if (!containsPackage(vertex1)) addVertex(vertex1);
  97. if (!containsPackage(vertex2)) addVertex(vertex2);
  98.  
  99. if (!packageHasDependency(vertex1, vertex2)) addEdgeHelper(vertex1, vertex2);
  100. }
  101.  
  102. /**
  103. * Remove the edge from vertex1 to vertex2
  104. * from this graph. (edge is directed and unweighted)
  105. * If either vertex does not exist,
  106. * or if an edge from vertex1 to vertex2 does not exist,
  107. * no edge is removed and no exception is thrown.
  108. * <p>
  109. * Valid argument conditions:
  110. * 1. neither vertex is null
  111. * 2. both packages are in the graph
  112. * 3. the edge from vertex1 to vertex2 is in the graph
  113. */
  114. public void removeEdge(String vertex1, String vertex2) {
  115. // Input validation
  116. if (vertex1 == null || vertex1.isEmpty() || vertex2 == null || vertex2.isEmpty()) return;
  117. if (!containsPackage(vertex1) || !containsPackage(vertex2)) return;
  118. if (!packageHasDependency(vertex1, vertex2)) return;
  119.  
  120. Package pack = getPackageFromName(vertex1);
  121. String[] depends = pack.getDependencies();
  122. // Iterate through each edge and if it matches, delete it
  123. for (int i = 0; i < depends.length; i++) {
  124. if (depends[i] != null) {
  125. if (depends[i].equals(vertex2)) {
  126. depends[i] = null;
  127. }
  128. }
  129. }
  130. }
  131.  
  132. /**
  133. * Returns a Set that contains all the packages
  134. */
  135. public Set<String> getAllVertices() {
  136. // Turns our List of Packages into a Set of Strings.
  137. return packages.stream() // Get stream of packages
  138. .map(Package::getName) // Apply Package.getName() to each member of the stream
  139. .collect(Collectors.toSet()); // Collect all the package names into a Set
  140. }
  141.  
  142. /**
  143. * Get all the neighbor (adjacent) packages of a vertex
  144. */
  145. public List<String> getAdjacentVerticesOf(String vertex) {
  146. List<String> adjVert = new ArrayList<>();
  147.  
  148. Package pack = getPackageFromName(vertex);
  149. String[] depends = pack.getDependencies();
  150. // Iterate through all dependencies of the given vertex. These are our adjacent vertices.
  151. for (int i = 0; i < depends.length; i++) {
  152. if (depends[i] != null) {
  153. adjVert.add(depends[i]);
  154. }
  155. }
  156. return adjVert;
  157. }
  158.  
  159. /**
  160. * Returns the number of edges in this graph.
  161. */
  162. public int size() {
  163. int edges = 0;
  164. // Look at each vertex to count the number of edges
  165. for (Package p : packages) {
  166. String[] depends = p.getDependencies();
  167. for (int i = 0; i < depends.length; i++) {
  168. if (depends[i] != null) {
  169. edges++;
  170. }
  171. }
  172. }
  173. return edges;
  174. }
  175.  
  176. /**
  177. * Returns the number of packages in this graph.
  178. */
  179. public int order() {
  180. return packages.size();
  181. }
  182.  
  183.  
  184. // All the following methods are private helper methods
  185. private boolean containsPackage(String packageName) {
  186. for (Package p : packages) {
  187. if (p.getName().equals(packageName)) {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193.  
  194. private Package getPackageFromName(String packageName) {
  195. for (Package p : packages) {
  196. if (p.getName().equals(packageName)) {
  197. return p;
  198. }
  199. }
  200. return null;
  201. }
  202.  
  203. private void addEdgeHelper(String vertex1, String vertex2) {
  204. // Need to use this annoying work around because Package requires us to use an array instead
  205. // of ArrayList...
  206. Package p = getPackageFromName(vertex1);
  207. String[] packEdges = p.getDependencies();
  208.  
  209. // This loop finds the next available index for a dependency
  210. int index;
  211. for (index = 0; index < packEdges.length && packEdges[index] != null; index++);
  212.  
  213. packEdges[index] = vertex2;
  214. p.setDependencies(packEdges);
  215. }
  216.  
  217. private boolean packageHasDependency(String p, String dependency) {
  218. Package pack = getPackageFromName(p);
  219. String[] packDepends = pack.getDependencies();
  220. for (String depend : packDepends) {
  221. if (depend != null) {
  222. if (depend.equals(dependency)) {
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229.  
  230. private void printPackageList(List<Package> packages) {
  231. for (Package p : packages) {
  232. System.out.print(p.getName() + ": ");
  233. for (String depend : p.getDependencies()) {
  234. if (depend != null) {
  235. System.out.print(depend + ", ");
  236. }
  237. }
  238. System.out.println();
  239. }
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement