I am preparing interviews and viewing graph implementations. The big ones that I see are a list of adjacency and adjacency matrix. When we look at the runtime of basic operations, why do I never see data structures using hashing?
In Java, for example, an adjacency list is usually ArrayList<LinkedList<Node>> , but why don't people use HashMap<Node, HashSet<Node>> ?
Let n = the number of nodes and m = the number of edges.
In both implementations, deleting node v involves searching all collections and deleting v. In the adjacency list, O (n ^ 2), but in the "adjacency set" it is O (n). Similarly, deleting an edge involves removing node u from list v and node v from list u. The adjacency list is O (n), and the adjacency set is O (1). Other operations, such as searching for successors of nodes, searching if there is a path between two nodes, etc., are the same with both implementations. Cosmic difficulties are also O (n + m).
The only drawback of the adjacency set that I can think of is that adding nodes / edges is depreciated by O (1), while in the adjacency list it is really O (1).
Perhaps I don’t see anything or forgot to take things into account when calculating battery life, so please let me know.
algorithm graph runtime adjacency-list
Matt sttern
source share