Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. Name: Idess Lee
  2.  
  3. Part 1: Choosing a representation
  4.  
  5. 1.a. One advantage to representing a graph as a collection of edges is that
  6. it is very easy to implement, since the only type of object associated
  7. with it is an edge which requires very minimum bookkeeping. However,
  8. one disadvantage to this representation is that the runtime complexity
  9. for any operation on a node is much higher. For example, removing a node
  10. would first require traversing through every edge and finding the node,
  11. then recompute all the edge that has that node. Or, assume the collection
  12. of edges to be in sorted order, but the question of how to sort edges
  13. defeats the ease of implementation.
  14.  
  15. One advantage to representing a graph as an adjacency list is that the
  16. runtime complexity for finding a child node and the existence of an edge
  17. is relatively fast, possibly in O(log n) time given that the list of child
  18. nodes in sorted. One disadvantage to this method is that it is harder to
  19. implement, given that every node must be linked exactly to the correct
  20. list or else the graph fails. There are many more invariants I could think
  21. of for an adjacency list than for a collection of edges or an adjacency
  22. matrix.
  23.  
  24. One advantage to representing a graph as an adjacency matrix is that find-
  25. ing the existence of an edge is super time-efficient at O(1), since a matrix
  26. only requires looking up the corresponding entry of the two nodes. However,
  27. one disadvantage to representing a graph this way is that it is very space
  28. costly. The number of edges between every two nodes must be kept track
  29. of, regardless of whether or not the edges even exist, which corresponds to
  30. a N^2 space usage for N number of nodes.
  31.  
  32. 1.b. I choose to represent my graph as an adjacency list because I think it is
  33. the most intuitive representation, seeing as I expect a graph to ultimately
  34. be a collection of nodes with supporting edges. In addition, its runtime
  35. complexity and space complexity are relatively efficient compared to other
  36. methods. The only downside to this method is that I have to enforce more
  37. invariants in my implementation.
  38.  
  39. 4. Changes made:
  40.  
  41. - I have deleted several methods in Graph and their respective tests in my test
  42. suite such as size(), because I realized these methods are not as useful in
  43. a graph as I originally thought.
  44. - I have changed my test suite to not test for reference equality on a graph,
  45. but instead test the adjacency list.
  46. - I have updated my script file tests to set up for the tests properly, seeing
  47. as my original tests lead to NullPointerException.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement