Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.01 KB | None | 0 0
  1. /**
  2. * Homework 13 - Graph
  3. *
  4. * Problem: Graph
  5. *
  6. * Prompt: Create a directed graph class using adjacency list edge
  7. * representation.
  8. *
  9. * The Graph will have the following properties:
  10. *
  11. * storage: {HashMap} - the keys represent unique vertex ids {Integer}
  12. * and the values are Lists representing the
  13. * vertex ids of its neighors.
  14. *
  15. * The Graph will also have the following methods:
  16. *
  17. * addVertex: Method that accepts a vertex id {Integer} and adds the
  18. * vertex to the graph. Return true if success and false
  19. * if failed.
  20. *
  21. * Input: id {Integer}
  22. * Output: {Boolean}
  23. *
  24. * removeVertex: Method that accepts a vertex id and removes the vertex
  25. * with the matching id. Return true if success and false
  26. * if failed.
  27. *
  28. * Input: id {Integer}
  29. * Output: {Boolean}
  30. *
  31. * addEdge: Method that accepts two vertex ids and creates a
  32. * directed edge from the first vertex to the second.
  33. * Returns true if success and false if failed.
  34. *
  35. * Input: id1 {Integer}
  36. * Input: id2 {Integer}
  37. * Output: {Boolean}
  38. *
  39. * removeEdge: Method that accepts two vertex id's and removes the
  40. * directed edge from the first vertex to the second.
  41. * Returns true if success and false if failed.
  42. *
  43. * Input: id1 {Integer}
  44. * Input: id2 {Integer}
  45. * Output: {Boolean}
  46. *
  47. * isVertex: Method that accepts an id, and returns whether the vertex
  48. * exists in the graph.
  49. *
  50. * Input: id {Integer}
  51. * Output: {Boolean}
  52. *
  53. * neighbors: Method that accepts a vertex id, and returns all of the
  54. * edges of that vertex.
  55. *
  56. * Input: id {Integer}
  57. * Output: {List<Integer>}
  58. *
  59. *
  60. * Input: {Void}
  61. * Output: {Graph}
  62. *
  63. * Notes: The notation for Time/Auxiliary Space Complexity for graphs
  64. * is slightly different since certain functions depend on
  65. * either the number of vertices, edges or even both
  66. *
  67. * O(V) = Linear w/ respect to the number of vertices
  68. * O(E) = Linear w/ respect to the number of edges
  69. * O(V+E) = Linear w/ respect to the number of vertices * number of edges
  70. */
  71.  
  72. import java.util.*;
  73.  
  74. class Graph {
  75.  
  76. public Map<Integer, List<Integer>> storage = new HashMap<>();
  77.  
  78. // Time Complexity: O(1)
  79. // Auxiliary Space Complexity: O(1)
  80. public boolean addVertex(Integer id) {
  81. if (storage.containsKey(id)) {
  82. return false;
  83. }
  84. storage.put(id, new ArrayList<Integer>());
  85. return true;
  86. }
  87.  
  88. public boolean removeVertex(Integer id) {
  89. if (!storage.containsKey(id)) {
  90. return false;
  91. }
  92. for (List<Integer> edges : storage.values()) {
  93. int index = edges.indexOf(id);
  94. if (index > -1) {
  95. edges.remove(index);
  96. }
  97. }
  98. storage.remove(id);
  99. return true;
  100. }
  101.  
  102. // Time Complexity: O(E)
  103. // Auxiliary Space Complexity: O(1)
  104. public boolean addEdge(Integer id1, Integer id2) {
  105. if(!storage.containsKey(id1) || !storage.containsKey(id2)) {
  106. return false;
  107. }
  108. if(storage.get(id1).indexOf(id2) >= 0) {
  109. return false;
  110. }
  111. storage.get(id1).add(id2);
  112. return true;
  113. }
  114.  
  115. // Time Complexity: O(1)
  116. // Auxiliary Space Complexity: O(1)
  117. public boolean removeEdge(Integer id1, Integer id2) {
  118. if (storage.containsKey(id1) && storage.containsKey(id2)) {
  119. int index = storage.get(id1).indexOf(id2);
  120. if (index >= 0) {
  121. storage.get(id1).remove(index);
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127.  
  128. // Time Complexity: O(1)
  129. // Auxiliary Space Complexity: O(1)
  130. public boolean isVertex(Integer id) {
  131. return storage.containsKey(id);
  132. }
  133.  
  134. // Time Complexity: O(1)
  135. // Auxiliary Space Complexity: O(E)
  136. public List<Integer> neighbors(Integer id) {
  137. return storage.containsKey(id) ? new ArrayList<Integer>(storage.get(id)) : null;
  138. }
  139.  
  140. }
  141.  
  142.  
  143. ////////////////////////////////////////////////////////////
  144. /////////////// DO NOT TOUCH TEST BELOW!!! ///////////////
  145. ////////////////////////////////////////////////////////////
  146.  
  147. // use the Main class to run the test cases
  148. class Main {
  149. private int[] testCount;
  150.  
  151. // an interface to perform tests
  152. public interface Test {
  153. public boolean execute();
  154. }
  155.  
  156. public static void main(String[] args) {
  157.  
  158. int[] testCount = {0, 0};
  159. System.out.println("Graph Class");
  160.  
  161. assertTest(testCount, "able to create an instance", new Test() {
  162. public boolean execute() {
  163. Graph graph = new Graph();
  164. return graph instanceof Graph;
  165. }
  166. });
  167.  
  168. assertTest(testCount, "has storage field", new Test() {
  169. public boolean execute() {
  170. try {
  171. Graph graph = new Graph();
  172. graph.getClass().getDeclaredField("storage");
  173. return true;
  174. } catch (Exception e) {
  175. e.printStackTrace();
  176. return false;
  177. }
  178. }
  179. });
  180.  
  181. assertTest(testCount, "storage property initialized to a hash map", new Test() {
  182. public boolean execute() {
  183. try {
  184. Graph graph = new Graph();
  185. return graph.getClass().getDeclaredField("storage").getType().isAssignableFrom(Map.class) ||
  186. graph.getClass().getDeclaredField("storage").getType().isAssignableFrom(HashMap.class);
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. return false;
  190. }
  191. }
  192. });
  193.  
  194. assertTest(testCount, "storage property initialized as empty", new Test() {
  195. public boolean execute() {
  196. try {
  197. Graph graph = new Graph();
  198. return graph.storage.size() == 0;
  199. } catch (Exception e) {
  200. e.printStackTrace();
  201. return false;
  202. }
  203. }
  204. });
  205.  
  206. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  207.  
  208.  
  209. testCount[0] = 0;
  210. testCount[1] = 0;
  211. System.out.println("Graph addVertex method");
  212.  
  213. assertTest(testCount, "has addVertex method", new Test() {
  214. public boolean execute() {
  215. try {
  216. Graph graph = new Graph();
  217. graph.getClass().getMethod("addVertex", new Class[] { Integer.class });
  218. return true;
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. return false;
  222. }
  223. }
  224. });
  225.  
  226. assertTest(testCount, "is able to add a vertex", new Test() {
  227. public boolean execute() {
  228. try {
  229. Graph graph = new Graph();
  230. graph.addVertex(5);
  231. return graph.storage.size() == 1;
  232. } catch (Exception e) {
  233. e.printStackTrace();
  234. return false;
  235. }
  236. }
  237. });
  238.  
  239. assertTest(testCount, "vertices store an list or set of connections", new Test() {
  240. public boolean execute() {
  241. try {
  242. Graph graph = new Graph();
  243. graph.addVertex(5);
  244. return graph.storage.get(5) instanceof List ||
  245. graph.storage.get(5) instanceof ArrayList;
  246. } catch (Exception e) {
  247. e.printStackTrace();
  248. return false;
  249. }
  250. }
  251. });
  252.  
  253. assertTest(testCount, "is able to add two vertices", new Test() {
  254. public boolean execute() {
  255. try {
  256. Graph graph = new Graph();
  257. graph.addVertex(5);
  258. graph.addVertex(10);
  259. return graph.storage.size() == 2 &&
  260. graph.storage.get(10) instanceof List ||
  261. graph.storage.get(10) instanceof ArrayList;
  262. } catch (Exception e) {
  263. e.printStackTrace();
  264. return false;
  265. }
  266. }
  267. });
  268.  
  269. assertTest(testCount, "does not add in duplicate vertex", new Test() {
  270. public boolean execute() {
  271. try {
  272. Graph graph = new Graph();
  273. graph.addVertex(5);
  274. graph.addVertex(5);
  275. return graph.storage.size() == 1 && graph.storage.get(5).size() == 0;
  276. } catch (Exception e) {
  277. e.printStackTrace();
  278. return false;
  279. }
  280. }
  281. });
  282.  
  283. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  284.  
  285.  
  286. testCount[0] = 0;
  287. testCount[1] = 0;
  288. System.out.println("Graph removeVertex method");
  289.  
  290. assertTest(testCount, "has removeVertex method", new Test() {
  291. public boolean execute() {
  292. try {
  293. Graph graph = new Graph();
  294. graph.getClass().getMethod("removeVertex", new Class[] { Integer.class });
  295. return true;
  296. } catch (Exception e) {
  297. e.printStackTrace();
  298. return false;
  299. }
  300. }
  301. });
  302.  
  303. assertTest(testCount, "able to remove a vertex within graph", new Test() {
  304. public boolean execute() {
  305. Graph graph = new Graph();
  306. graph.addVertex(5);
  307. graph.removeVertex(5);
  308. return graph.storage.size() == 0 && graph.storage.get(5) == null;
  309. }
  310. });
  311.  
  312. assertTest(testCount, "does not remove vertex that does not exist", new Test() {
  313. public boolean execute() {
  314. Graph graph = new Graph();
  315. graph.addVertex(5);
  316. boolean result = graph.removeVertex(10);
  317. return graph.storage.size() == 1 && graph.storage.get(5) != null;
  318. }
  319. });
  320.  
  321. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  322.  
  323.  
  324. testCount[0] = 0;
  325. testCount[1] = 0;
  326. System.out.println("Graph addEdge method");
  327.  
  328. assertTest(testCount, "has addEdge method", new Test() {
  329. public boolean execute() {
  330. try {
  331. Graph graph = new Graph();
  332. graph.getClass().getMethod("addEdge", new Class[] { Integer.class, Integer.class });
  333. return true;
  334. } catch (Exception e) {
  335. e.printStackTrace();
  336. return false;
  337. }
  338. }
  339. });
  340.  
  341. assertTest(testCount, "able to create an edge between two vertices that exist", new Test() {
  342. public boolean execute() {
  343. try {
  344. Graph graph = new Graph();
  345. graph.addVertex(5);
  346. graph.addVertex(10);
  347. graph.addEdge(5, 10);
  348. return graph.storage.get(5).size() == 1 &&
  349. graph.storage.get(10).size() == 0;
  350. } catch (Exception e) {
  351. e.printStackTrace();
  352. return false;
  353. }
  354. }
  355. });
  356.  
  357. assertTest(testCount, "addVertex returns true if vertices are added", new Test() {
  358. public boolean execute() {
  359. try {
  360. Graph graph = new Graph();
  361. graph.addVertex(5);
  362. graph.addVertex(10);
  363. return graph.addEdge(5, 10) == true;
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. return false;
  367. }
  368. }
  369. });
  370.  
  371. assertTest(testCount, "does not create an edge when one of the vertices does not exist", new Test() {
  372. public boolean execute() {
  373. Graph graph = new Graph();
  374. graph.addVertex(5);
  375. boolean result = graph.addEdge(5, 10);
  376. return graph.storage.get(5).size() == 0 && result == false;
  377. }
  378. });
  379.  
  380. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  381.  
  382.  
  383. testCount[0] = 0;
  384. testCount[1] = 0;
  385. System.out.println("Graph removeEdge method");
  386.  
  387. assertTest(testCount, "has removeEdge method", new Test() {
  388. public boolean execute() {
  389. try {
  390. Graph graph = new Graph();
  391. graph.getClass().getMethod("removeEdge", new Class[] { Integer.class, Integer.class });
  392. return true;
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. return false;
  396. }
  397. }
  398. });
  399.  
  400. assertTest(testCount, "able to remove an edge between two vertices", new Test() {
  401. public boolean execute() {
  402. try {
  403. Graph graph = new Graph();
  404. graph.addVertex(5);
  405. graph.addVertex(10);
  406. graph.addEdge(5, 10);
  407. graph.removeEdge(5, 10);
  408. return graph.storage.get(5).size() == 0 && graph.storage.size() == 2;
  409. } catch (Exception e) {
  410. e.printStackTrace();
  411. return false;
  412. }
  413. }
  414. });
  415.  
  416. assertTest(testCount, "returns true if able to remove an edge", new Test() {
  417. public boolean execute() {
  418. try {
  419. Graph graph = new Graph();
  420. graph.addVertex(5);
  421. graph.addVertex(10);
  422. graph.addEdge(5, 10);
  423. return graph.removeEdge(5, 10) == true;
  424. } catch (Exception e) {
  425. e.printStackTrace();
  426. return false;
  427. }
  428. }
  429. });
  430.  
  431. assertTest(testCount, "does not remove edge when edge does not exist", new Test() {
  432. public boolean execute() {
  433. try{
  434. Graph graph = new Graph();
  435. graph.addVertex(5);
  436. graph.addVertex(10);
  437. graph.addEdge(5, 10);
  438. graph.removeEdge(6, 10);
  439. return graph.storage.get(5) != null && graph.storage.get(5).size() == 1;
  440. } catch (Exception e) {
  441. e.printStackTrace();
  442. return false;
  443. }
  444. }
  445. });
  446.  
  447. assertTest(testCount, "returns false if edge does not exist", new Test() {
  448. public boolean execute() {
  449. try{
  450. Graph graph = new Graph();
  451. graph.addVertex(5);
  452. graph.addVertex(10);
  453. graph.addEdge(5, 10);
  454. return graph.removeEdge(6, 10) == false;
  455. } catch (Exception e) {
  456. e.printStackTrace();
  457. return false;
  458. }
  459. }
  460. });
  461.  
  462. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  463.  
  464.  
  465. testCount[0] = 0;
  466. testCount[1] = 0;
  467. System.out.println("Graph isVertex method");
  468.  
  469. assertTest(testCount, "has isVertex method", new Test() {
  470. public boolean execute() {
  471. try {
  472. Graph graph = new Graph();
  473. graph.getClass().getMethod("isVertex", new Class[] { Integer.class });
  474. return true;
  475. } catch (Exception e) {
  476. e.printStackTrace();
  477. return false;
  478. }
  479. }
  480. });
  481.  
  482. assertTest(testCount, "returns true when a vertex exists", new Test() {
  483. public boolean execute() {
  484. try {
  485. Graph graph = new Graph();
  486. graph.addVertex(5);
  487. graph.isVertex(5);
  488. return graph.isVertex(5) == true;
  489. } catch (Exception e) {
  490. e.printStackTrace();
  491. return false;
  492. }
  493. }
  494. });
  495.  
  496. assertTest(testCount, "returns false when a vertex does not exist'", new Test() {
  497. public boolean execute() {
  498. try {
  499. Graph graph = new Graph();
  500. graph.addVertex(5);
  501. return graph.isVertex(10) == false;
  502. } catch (Exception e) {
  503. e.printStackTrace();
  504. return false;
  505. }
  506. }
  507. });
  508.  
  509. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  510.  
  511.  
  512. testCount[0] = 0;
  513. testCount[1] = 0;
  514. System.out.println("Graph neighbors method");
  515.  
  516. assertTest(testCount, "has neighbors method", new Test() {
  517. public boolean execute() {
  518. try {
  519. Graph graph = new Graph();
  520. graph.getClass().getMethod("neighbors", new Class[] { Integer.class });
  521. return true;
  522. } catch (Exception e) {
  523. e.printStackTrace();
  524. return false;
  525. }
  526. }
  527. });
  528.  
  529. assertTest(testCount, "returns null if the vertex does not exist", new Test() {
  530. public boolean execute() {
  531. try {
  532. Graph graph = new Graph();
  533. return graph.neighbors(5) == null;
  534. } catch (Exception e) {
  535. e.printStackTrace();
  536. return false;
  537. }
  538. }
  539. });
  540.  
  541. assertTest(testCount, "returns an empty array if vertex has no edges", new Test() {
  542. public boolean execute() {
  543. try {
  544. Graph graph = new Graph();
  545. graph.addVertex(5);
  546. return graph.neighbors(5).size() == 0;
  547. } catch (Exception e) {
  548. e.printStackTrace();
  549. return false;
  550. }
  551. }
  552. });
  553.  
  554. assertTest(testCount, "returns neighbors if edges exist for a vertex", new Test() {
  555. public boolean execute() {
  556. try {
  557. Graph graph = new Graph();
  558. graph.addVertex(5);
  559. graph.addVertex(10);
  560. graph.addVertex(15);
  561. graph.addVertex(20);
  562. graph.addEdge(5, 10);
  563. graph.addEdge(5, 15);
  564. graph.addEdge(5, 20);
  565. return graph.neighbors(5).size() == 3;
  566. } catch (Exception e) {
  567. e.printStackTrace();
  568. return false;
  569. }
  570. }
  571. });
  572.  
  573. System.out.println("PASSED: " + testCount[0] + " / " + testCount[1] + "\n\n");
  574. }
  575.  
  576.  
  577. // function for checking if arrays contain same elements
  578. // (do not need to be in the same order)
  579. private static boolean arraysMatching(Integer[] arr1, Integer[] arr2) {
  580. if (arr1.length != arr2.length) {
  581. return false;
  582. } else {
  583. Arrays.sort(arr1);
  584. Arrays.sort(arr2);
  585.  
  586. for (int i = 0 ; i < arr1.length ; i++) {
  587. if (arr1[i] != arr2[i]) {
  588. return false;
  589. }
  590. }
  591.  
  592. return true;
  593. }
  594. }
  595.  
  596. // do not edit below, this is to wrap the test and check for exceptions
  597. private static void assertTest(int[] count, String name, Test test) {
  598. String pass = "false";
  599. count[1]++;
  600.  
  601. try {
  602. if (test.execute()) {
  603. pass = " true";
  604. count[0]++;
  605. }
  606. } catch(Exception e) {}
  607. String result = " " + (count[1] + ") ").substring(0, 5) + pass + " : " + name;
  608. System.out.println(result);
  609. }
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement