Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Name: Idess Lee
- Part 1: Choosing a representation
- 1.a. One advantage to representing a graph as a collection of edges is that
- it is very easy to implement, since the only type of object associated
- with it is an edge which requires very minimum bookkeeping. However,
- one disadvantage to this representation is that the runtime complexity
- for any operation on a node is much higher. For example, removing a node
- would first require traversing through every edge and finding the node,
- then recompute all the edge that has that node. Or, assume the collection
- of edges to be in sorted order, but the question of how to sort edges
- defeats the ease of implementation.
- One advantage to representing a graph as an adjacency list is that the
- runtime complexity for finding a child node and the existence of an edge
- is relatively fast, possibly in O(log n) time given that the list of child
- nodes in sorted. One disadvantage to this method is that it is harder to
- implement, given that every node must be linked exactly to the correct
- list or else the graph fails. There are many more invariants I could think
- of for an adjacency list than for a collection of edges or an adjacency
- matrix.
- One advantage to representing a graph as an adjacency matrix is that find-
- ing the existence of an edge is super time-efficient at O(1), since a matrix
- only requires looking up the corresponding entry of the two nodes. However,
- one disadvantage to representing a graph this way is that it is very space
- costly. The number of edges between every two nodes must be kept track
- of, regardless of whether or not the edges even exist, which corresponds to
- a N^2 space usage for N number of nodes.
- 1.b. I choose to represent my graph as an adjacency list because I think it is
- the most intuitive representation, seeing as I expect a graph to ultimately
- be a collection of nodes with supporting edges. In addition, its runtime
- complexity and space complexity are relatively efficient compared to other
- methods. The only downside to this method is that I have to enforce more
- invariants in my implementation.
- 4. Changes made:
- - I have deleted several methods in Graph and their respective tests in my test
- suite such as size(), because I realized these methods are not as useful in
- a graph as I originally thought.
- - I have changed my test suite to not test for reference equality on a graph,
- but instead test the adjacency list.
- - I have updated my script file tests to set up for the tests properly, seeing
- as my original tests lead to NullPointerException.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement