Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7c7947

Browse files
fix(book/graph): add comments for runtimes using hashset implementations
1 parent c137930 commit c7c7947

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

‎book/content/part03/graph.asc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ include::{codedir}/data-structures/graphs/node.js[tag=removeAdjacent, indent=0]
298298
^|_Add_ ^|_Remove_ ^|_Add_ ^|_Remove_
299299
| Graph (adj. matrix) ^| O(\|V\|^2^) ^| O(\|V\|^2^) ^|O(1) ^|O(1) ^|O(\|V\|^2^)
300300
| Graph (adj. list w/array) ^| O(1) ^| O(\|V\| + \|E\|)) ^|O(1) ^|O(\|V\| + \|E\|) ^|O(\|V\| + \|E\|)
301-
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(\|V\|) ^|O(\|V\| + \|E\|)
301+
| Graph (adj. list w/HashSet) ^| O(1) ^| O(\|V\|)) ^|O(1) ^|O(1) ^|O(\|V\| + \|E\|)
302302
|===
303303
// end::table[]
304304

‎src/data-structures/graphs/graph.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class Graph {
4141
* Removes node from graph
4242
* It also removes the reference of the deleted node from
4343
* anywhere it was adjacent to.
44-
* Runtime: O(|V| + |E|)
44+
* Runtime: O(|V|) because adjacency list is implemented with a HashSet.
45+
* It were implemented with an array then it would be O(|V| + |E|).
4546
* @param {any} value node's value
4647
*/
4748
removeVertex(value) {
@@ -55,9 +56,9 @@ class Graph {
5556

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

8081
// tag::removeEdge[]
8182
/**
82-
* Remove connection between source node and destination.
83-
* If the graph is undirected it will also remove the conneciton from destination to destination.
83+
* Remove the connection between source node and destination.
84+
* If the graph is undirected, it will also create the link from destination to source.
8485
*
85-
* Runtime: O(|E|)
86+
* Runtime: O(1): implemented with HashSet.
87+
* If implemented with array, would be O(|E|).
8688
*
8789
* @param {any} source
8890
* @param {any} destination
@@ -105,7 +107,7 @@ class Graph {
105107

106108
// tag::areAdjacents[]
107109
/**
108-
* True if two nodes are adjacent to each other
110+
* True if two nodes are adjacent.
109111
* @param {any} source node's value
110112
* @param {any} destination node's value
111113
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /