Skip to content

Commit

Permalink
fix(book/graph): add comments for runtimes using hashset implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
amejiarosario committed Dec 22, 2020
1 parent c137930 commit c7c7947
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion book/content/part03/graph.asc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ include::{codedir}/data-structures/graphs/node.js[tag=removeAdjacent, indent=0]
^|_Add_ ^|_Remove_ ^|_Add_ ^|_Remove_
| Graph (adj. matrix) ^| O(\|V\|^2^) ^| O(\|V\|^2^) ^|O(1) ^|O(1) ^|O(\|V\|^2^)
| Graph (adj. list w/array) ^| O(1) ^| O(\|V\| + \|E\|)) ^|O(1) ^|O(\|V\| + \|E\|) ^|O(\|V\| + \|E\|)
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(\|V\|) ^|O(\|V\| + \|E\|)
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(1) ^|O(\|V\| + \|E\|)
|===
// end::table[]

Expand Down
18 changes: 10 additions & 8 deletions src/data-structures/graphs/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Graph {
* Removes node from graph
* It also removes the reference of the deleted node from
* anywhere it was adjacent to.
* Runtime: O(|V| + |E|)
* Runtime: O(|V|) because adjacency list is implemented with a HashSet.
* It were implemented with an array then it would be O(|V| + |E|).
* @param {any} value node's value
*/
removeVertex(value) {
Expand All @@ -55,9 +56,9 @@ class Graph {

// tag::addEdge[]
/**
* Create a connection between source node and destination node.
* If the graph is undirected it will also create the conneciton from destination to destination.
* If the nodes doesn't exist then it will create them on the fly
* Create a connection between the source node and the destination node.
* If the graph is undirected, it will also create the link from destination to source.
* If the nodes don't exist, then it will make them on the fly.
* Runtime: O(1)
* @param {any} source
* @param {any} destination
Expand All @@ -79,10 +80,11 @@ class Graph {

// tag::removeEdge[]
/**
* Remove connection between source node and destination.
* If the graph is undirected it will also remove the conneciton from destination to destination.
* Remove the connection between source node and destination.
* If the graph is undirected, it will also create the link from destination to source.
*
* Runtime: O(|E|)
* Runtime: O(1): implemented with HashSet.
* If implemented with array, would be O(|E|).
*
* @param {any} source
* @param {any} destination
Expand All @@ -105,7 +107,7 @@ class Graph {

// tag::areAdjacents[]
/**
* True if two nodes are adjacent to each other
* True if two nodes are adjacent.
* @param {any} source node's value
* @param {any} destination node's value
*/
Expand Down

0 comments on commit c7c7947

Please sign in to comment.