Advertisement
Guest User

graph

a guest
Aug 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. package hw4;
  2.  
  3. import java.util.*;
  4.  
  5.  
  6.  
  7. /**
  8. * Abstraction function: Graph stored as an adjacency list
  9. * Node are stored as keys in the hash map
  10. * The edges from that node are stored as the value of hash map
  11. * The value of the hash map is an array list
  12. */
  13.  
  14. public class Graph {
  15.  
  16. private HashMap<Node, ArrayList<Edge>> adjList;
  17. private int numEdges;
  18. private int numNodes;
  19. private int numDegrees;
  20.  
  21.  
  22. public Graph() {
  23. adjList = new HashMap<Node, ArrayList<Edge>>();
  24. numEdges = 0;
  25. numNodes = 0;
  26. numDegrees = 0;
  27. checkRep();
  28. }
  29. /**
  30. * gets the number of nodes in the graph
  31. * @requires the graph can't be null
  32. * @return number of nodes int the graph
  33. */
  34. public int getNumNodes(){
  35. return numNodes;
  36. }
  37. /**
  38. * gets the number of edges in the graph
  39. * @requires the graph can't be null
  40. * @return number of edges int the graph
  41. */
  42. public int getNumEdges() {
  43. return numEdges;
  44. }
  45. /**
  46. * gets the number of degrees in the graph
  47. * @requires the graph can't be null
  48. * @return number of degrees int the graph
  49. */
  50.  
  51. public int getNumDegrees() {
  52. return numDegrees;
  53. }
  54.  
  55. /**
  56. * Figures out if the graph is empty
  57. * @return true only if the graph is empty
  58. */
  59.  
  60. public boolean isEmpty() {
  61. return adjList.isEmpty();
  62. }
  63.  
  64.  
  65. /**
  66. * Adds a node into the graph
  67. * @requires the graph can't be null
  68. * @param tempNode the name of the node to be added to the graph
  69. * @effects Attempts to put the node in the graph, if node is null or the node is already in the graph then it does nothing
  70. * @modifies the graph
  71. * @return true only in when the node has been added to the graph
  72. */
  73.  
  74. public boolean addNode(String nodeString){
  75. if(nodeString == null) {
  76. return false;
  77. }
  78. return addNode(new Node(nodeString));
  79. }
  80.  
  81. /**
  82. * Adds a node into the graph
  83. * @requires the graph can't be null
  84. * @param tempNode the name of the node to be added to the graph
  85. * @effects Attempts to put the node in the graph, if node is null or the node is already in the graph then it does nothing
  86. * @modifies the graph
  87. * @return true only in when the node has been added to the graph
  88. */
  89.  
  90. public boolean addNode(Node tempNode) {
  91. if (tempNode == null) {
  92. return false;
  93. }
  94. if(!adjList.containsKey(tempNode)){
  95. this.adjList.put(tempNode,new ArrayList<Edge>());
  96. this.numNodes++;
  97. return true;
  98. }
  99. checkRep();
  100. return false;
  101. }
  102.  
  103. /**
  104. * Adds a new edge to the graph
  105. * @requires the graph can't be null
  106. * @param tempEdge edge to be added to the graph
  107. * @effects Attempts to put the edge in the graph, if node is null or the node is already in the graph then it does nothing
  108. * @modifies the graph
  109. * @return true only if the edge is added to the graph
  110. */
  111.  
  112. public boolean addEdge(Edge tempEdge) {
  113. if(tempEdge == null) {
  114. return false;
  115. }
  116.  
  117. Node start = tempEdge.getStartPoint();
  118. Node end = tempEdge.getEndPoint();
  119. if(adjList.containsKey(start)) {
  120. if(!adjList.get(start).contains(tempEdge)) {
  121. adjList.get(start).add(tempEdge);
  122. numEdges++;
  123. numDegrees++;
  124. addNode(end);
  125. return true;
  126. }
  127. return false;
  128. }
  129. addNode(start);
  130. addNode(end);
  131. numEdges++;
  132. numDegrees++;
  133. adjList.get(start).add(tempEdge);
  134. checkRep();
  135. return true;
  136.  
  137. }
  138.  
  139. /**
  140. * Adds a new edge to the graph
  141. * @requires the graph can't be null
  142. * @param parent String of parent name
  143. * @param child String of child name
  144. * @param label String of label
  145. * @effects Attempts to put the edge in the graph, if edge is null or the edge is already in the graph then it does nothing
  146. * @modifies the graph
  147. * @return true only if the edge is added to the graph
  148. */
  149.  
  150. public boolean addEdge(String parent, String child, String label){
  151. if(parent == null || child == null || label == null){
  152. return false;
  153. }
  154. return addEdge(new Edge(new Node(parent),new Node(child),label));
  155. }
  156.  
  157. /**
  158. * Removes an edge from the graph
  159. * @requires the graph can't be null
  160. * @param tempEdge the edge that's going to be removed from the graph
  161. * @effects Attempts to remove the edge in the graph, if edge is null or the edge is already not in the graph then it does nothing
  162. * @modifies the graph
  163. * @return true only if the edge is removed from the graph
  164. */
  165.  
  166. public boolean removeEdge(Edge tempEdge){
  167. if (tempEdge == null) return false;
  168. if(!adjList.containsKey(tempEdge.getStartPoint())) {
  169. return false;
  170. }
  171. ArrayList<Edge> edgeList = adjList.get(tempEdge.getStartPoint());
  172. for (int i = 0; i < edgeList.size(); i++) {
  173. if (tempEdge.equals(edgeList.get(i))) {
  174. numDegrees --;
  175. numEdges --;
  176. edgeList.remove(i);
  177. break;
  178. }
  179. }
  180. checkRep();
  181. return true;
  182. }
  183.  
  184. /**
  185. * checks to see if the graph has the respective edge
  186. * @requires graph can't be null
  187. * @param edge the edge we're looking to see whether it is in the graph or nah
  188. * @return true only if the edge exists in the graph
  189. */
  190.  
  191. public boolean hasEdge(Edge edge){
  192. if(edge == null) {
  193. return false;
  194. }
  195. if(!adjList.containsKey(edge.getStartPoint())) {
  196. return false;
  197. }
  198. ArrayList<Edge> edgeList = adjList.get(edge.getStartPoint());
  199. for(int i = 0; i < edgeList.size(); i++) {
  200. if(edge.equals(edgeList.get(i))) {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206.  
  207. /**
  208. * checks to see if the node has the respective edge
  209. * @requires graph can't be null
  210. * @param node the node we're looking to see whether it is in the graph or nah
  211. * @return true node if the edge exists in the graph
  212. */
  213.  
  214. public boolean hasNode(Node node) {
  215. if(node == null) {
  216. return false;
  217. }
  218. return adjList.containsKey(node);
  219. }
  220.  
  221. /**
  222. * checks to see if the node has the respective edge
  223. * @requires graph can't be null
  224. * @param node the node we're looking to see whether it is in the graph or nah
  225. * @return true node if the edge exists in the graph
  226. */
  227.  
  228. public boolean hasNode(String node){
  229. if(node == null) {
  230. return false;
  231. }
  232. return hasNode(new Node(node));
  233. }
  234. /**
  235. * Removes an edge from the graph
  236. * @requires the graph can't be null
  237. * @param tempEdge the edge that's going to be removed from the graph
  238. * @effects Attempts to remove the edge in the graph, if edge is null or the edge is already not in the graph then it does nothing
  239. * @modifies the graph
  240. * @return true only if the edge is removed from the graph
  241. */
  242.  
  243. public boolean removeNode(Node tempNode) {
  244. if(tempNode == null) {
  245. return false;
  246. }
  247. if(!hasNode(tempNode)) {
  248. return false;
  249. }
  250.  
  251. int temp = adjList.get(tempNode).size();
  252. adjList.remove(tempNode);
  253. numNodes--;
  254. numDegrees-= temp;
  255. numEdges-= temp;
  256.  
  257. Iterator itr = adjList.entrySet().iterator();
  258. ArrayList<Edge> remove = new ArrayList<Edge>();
  259. while(itr.hasNext()) {
  260. Map.Entry pair = (Map.Entry) itr.next();
  261. ArrayList<Edge> currentEdge = (ArrayList<Edge>) pair.getValue();
  262.  
  263. for(int i = 0; i<currentEdge.size(); i++) {
  264. if(currentEdge.get(i).getEndPoint().equals(tempNode)) {
  265. numDegrees --;
  266. numEdges --;
  267. remove.add(currentEdge.get(i));
  268. }
  269. }
  270. }
  271. checkRep();
  272. remove.clear();
  273. return true;
  274. }
  275. /**
  276. * List the nodes in the graph
  277. * @requires graph can't be null
  278. * @return list of nodes
  279. */
  280.  
  281. public ArrayList<String> listNodes(){
  282. ArrayList<String> temp = new ArrayList<String>();
  283. for (Node x : adjList.keySet()) {
  284. temp.add(x.getName());
  285. }
  286. temp.sort(Comparator.naturalOrder());
  287. return temp;
  288. }
  289.  
  290. /**
  291. * List the child nodes in the graph
  292. * @requires graph can't be null
  293. * @param parentNode the parent node that will have its children listed
  294. * @return list of child nodes or an empty list if the parent isn't in the graph or is null
  295. */
  296.  
  297. public ArrayList<Node> listChildren(String parentNode){
  298. if (parentNode == null || !hasNode(new Node(parentNode))) {
  299. return new ArrayList<Node>();
  300. }
  301. ArrayList<Node> childNodes = new ArrayList<Node>();
  302. ArrayList<Edge> nodeSort = new ArrayList<Edge>(adjList.get(new Node(parentNode)));
  303. nodeSort.sort((temp1, temp2) -> {
  304. if(temp1.getEndPoint().getName().equals(temp2.getEndPoint().getName())){
  305. return temp1.getLabel().compareTo(temp2.getLabel());
  306. }
  307. return temp1.getEndPoint().getName().compareTo(temp2.getEndPoint().getName());
  308. });
  309.  
  310. Iterator<Edge> child = nodeSort.iterator();
  311. while(child.hasNext()){
  312. childNodes.add(child.next().getEndPoint());
  313. }
  314. return childNodes;
  315. }
  316.  
  317. /**
  318. * checks to see if the rep invariant holds true
  319. * @throws RuntimeException if the graph doesn't pass the checks
  320. */
  321.  
  322. public void checkRep(){
  323. if(isEmpty()){
  324. return;
  325. }
  326.  
  327. if(numEdges < 0 || numDegrees < 0 || numNodes< 0) {
  328. throw new RuntimeException("Error: negative degree, # of edges or # of nodes");
  329. }
  330.  
  331. Iterator itr = adjList.entrySet().iterator();
  332. while(itr.hasNext()) {
  333. Map.Entry pair = (Map.Entry) itr.next();
  334. Node currentNode = (Node) pair.getKey();
  335. ArrayList<Edge> currentEdge = (ArrayList<Edge>) pair.getValue();
  336. for (int i = 0; i < currentEdge.size(); i++) {
  337. if (!currentNode.equals(currentEdge.get(i).getStartPoint())){
  338. throw new RuntimeException ("first node in the Edge of each <Node, List<Edge>> is not the node itself");
  339. }
  340. }
  341. }
  342. }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement