@@ -31,6 +31,9 @@ class Graph {
3131 /**
3232 * Add a node to the graph.
3333 * Returns the new node or the existing one if it already exits.
34+ *
35+ * Runtime: O(1)
36+ *
3437 * @param {any } value node's value
3538 */
3639 addVertex ( value ) {
@@ -45,6 +48,9 @@ class Graph {
4548
4649 /**
4750 * Removes node from graph
51+ *
52+ * Runtime: O(|V| + |E|)
53+ *
4854 * @param {any } value node's value
4955 */
5056 removeVertex ( value ) {
@@ -58,44 +64,66 @@ class Graph {
5864 }
5965
6066 /**
61- * Create a connection between source node and target node.
62- * If the graph is undirected it will also create the conneciton from target to destination.
67+ * Create a connection between source node and destination node.
68+ * If the graph is undirected it will also create the conneciton from destination to destination.
6369 * If the nodes doesn't exist then it will create them on the fly
70+ *
71+ * Runtime: O(1)
72+ *
6473 * @param {any } source
65- * @param {any } target
74+ * @param {any } destination
6675 */
67- addEdge ( source , target ) {
76+ addEdge ( source , destination ) {
6877 const sourceNode = this . addVertex ( source ) ;
69- const targetNode = this . addVertex ( target ) ;
78+ const destinationNode = this . addVertex ( destination ) ;
7079
71- sourceNode . addAdjacent ( targetNode ) ;
80+ sourceNode . addAdjacent ( destinationNode ) ;
7281
7382 if ( this . edgeDirection === Graph . UNDIRECTED ) {
74- targetNode . addAdjacent ( sourceNode ) ;
83+ destinationNode . addAdjacent ( sourceNode ) ;
7584 }
7685
77- return [ sourceNode , targetNode ] ;
86+ return [ sourceNode , destinationNode ] ;
7887 }
7988
8089 /**
81- * Remove connection between source node and target.
82- * If the graph is undirected it will also remove the conneciton from target to destination.
90+ * Remove connection between source node and destination.
91+ * If the graph is undirected it will also remove the conneciton from destination to destination.
92+ *
93+ * Runtime: O(|E|)
94+ *
8395 * @param {any } source
84- * @param {any } target
96+ * @param {any } destination
8597 */
86- removeEdge ( source , target ) {
98+ removeEdge ( source , destination ) {
8799 const sourceNode = this . nodes . get ( source ) ;
88- const targetNode = this . nodes . get ( target ) ;
100+ const destinationNode = this . nodes . get ( destination ) ;
89101
90- if ( sourceNode && targetNode ) {
91- sourceNode . removeAdjacent ( targetNode ) ;
102+ if ( sourceNode && destinationNode ) {
103+ sourceNode . removeAdjacent ( destinationNode ) ;
92104
93105 if ( this . edgeDirection === Graph . UNDIRECTED ) {
94- targetNode . removeAdjacent ( sourceNode ) ;
106+ destinationNode . removeAdjacent ( sourceNode ) ;
95107 }
96108 }
97109
98- return [ sourceNode , targetNode ] ;
110+ return [ sourceNode , destinationNode ] ;
111+ }
112+
113+ /**
114+ * True if two nodes are adjacent to each other
115+ * @param {any } source node's value
116+ * @param {any } destination node's value
117+ */
118+ areAdjacents ( source , destination ) {
119+ const sourceNode = this . nodes . get ( source ) ;
120+ const destinationNode = this . nodes . get ( destination ) ;
121+
122+ if ( sourceNode && destinationNode ) {
123+ return sourceNode . isAdjacent ( destinationNode ) ;
124+ }
125+
126+ return false ;
99127 }
100128
101129 /**
0 commit comments