From dd64d54b87a4e9d306ef276c839ac895c9ae4aaf Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 15:33:47 +0530 Subject: [PATCH 1/6] FibonacciSearch --- Search/FibonacciSearch.js | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Search/FibonacciSearch.js diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js new file mode 100644 index 0000000000..b44e7a4ffa --- /dev/null +++ b/Search/FibonacciSearch.js @@ -0,0 +1,55 @@ +/* In a sorted array, it uses the Fibonacci series to determine the index +* position to be searched in the array. The idea is it find the Fibonacci number(Fn) +* that is greater or equal to the size of the array. Then compare the item with the element +* at (Fn-1) position in the array. If they are equal, the search is successful, and the element is at (Fn-1 + 1). +* If the item is smaller, it is searched in the sublist left to (Fn-1). +* If the item is greater, it is searched in the sublist right to (Fn-1). If the item is not found, then again the +* Fibonacci number >= size of the sublist to be searched is taken, and the whole process is repeated +* until the desired element is found, or the sublist is reduced to a single element that is not equal to item. +*/ + +function minVal (x, y) { + return (x <= y) ? x : y +} + +function fiboNacciSearch (arr, x, size) { + let fibTwo = 0 + let fibOne = 1 + let fibN = fibTwo + fibOne + + while (fibN < size) { + fibTwo = fibOne + fibOne = fibN + fibN = fibTwo + fibOne + } + + let offset = -1 + + while (fibN > 1) { + const i = minVal(offset + fibTwo, size - 1) + + if (arr[i] < x) { + fibN = fibOne + fibOne = fibTwo + fibTwo = fibN - fibOne + offset = i + } else if (arr[i] > x) { + fibN = fibTwo + fibOne = fibOne - fibTwo + fibTwo = fibN - fibOne + } else { + return i + } + + if ((fibOne && arr[offset + 1]) === x) { + return offset + 1 + } + return -1 + } +} + +const arr = [10, 22, 35, 40, 45, 50, 80, 82] +const size = arr.length +fiboNacciSearch(arr, 85, size) +fiboNacciSearch(arr, 50, size) +fiboNacciSearch(arr, 35, size) From 8c438c317ccb42b904d54c8d6ce3bd4575fa9555 Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 15:54:57 +0530 Subject: [PATCH 2/6] FibonacciSearch --- Search/FibonacciSearch.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js index b44e7a4ffa..f41fb0c731 100644 --- a/Search/FibonacciSearch.js +++ b/Search/FibonacciSearch.js @@ -1,5 +1,5 @@ /* In a sorted array, it uses the Fibonacci series to determine the index -* position to be searched in the array. The idea is it find the Fibonacci number(Fn) +* position to be searched in the array. The idea is to find the smallest Fibonacci number(Fn) * that is greater or equal to the size of the array. Then compare the item with the element * at (Fn-1) position in the array. If they are equal, the search is successful, and the element is at (Fn-1 + 1). * If the item is smaller, it is searched in the sublist left to (Fn-1). @@ -8,10 +8,6 @@ * until the desired element is found, or the sublist is reduced to a single element that is not equal to item. */ -function minVal (x, y) { - return (x <= y) ? x : y -} - function fiboNacciSearch (arr, x, size) { let fibTwo = 0 let fibOne = 1 @@ -26,7 +22,7 @@ function fiboNacciSearch (arr, x, size) { let offset = -1 while (fibN > 1) { - const i = minVal(offset + fibTwo, size - 1) + const i = Math.min(offset + fibTwo, size - 1) if (arr[i] < x) { fibN = fibOne From 01172a2856164d0a20010429d23dbc91f5f3c7cc Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 19:44:42 +0530 Subject: [PATCH 3/6] Depth First Search Algorithms --- Search/FibonacciSearch.js | 51 --------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 Search/FibonacciSearch.js diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js deleted file mode 100644 index f41fb0c731..0000000000 --- a/Search/FibonacciSearch.js +++ /dev/null @@ -1,51 +0,0 @@ -/* In a sorted array, it uses the Fibonacci series to determine the index -* position to be searched in the array. The idea is to find the smallest Fibonacci number(Fn) -* that is greater or equal to the size of the array. Then compare the item with the element -* at (Fn-1) position in the array. If they are equal, the search is successful, and the element is at (Fn-1 + 1). -* If the item is smaller, it is searched in the sublist left to (Fn-1). -* If the item is greater, it is searched in the sublist right to (Fn-1). If the item is not found, then again the -* Fibonacci number >= size of the sublist to be searched is taken, and the whole process is repeated -* until the desired element is found, or the sublist is reduced to a single element that is not equal to item. -*/ - -function fiboNacciSearch (arr, x, size) { - let fibTwo = 0 - let fibOne = 1 - let fibN = fibTwo + fibOne - - while (fibN < size) { - fibTwo = fibOne - fibOne = fibN - fibN = fibTwo + fibOne - } - - let offset = -1 - - while (fibN > 1) { - const i = Math.min(offset + fibTwo, size - 1) - - if (arr[i] < x) { - fibN = fibOne - fibOne = fibTwo - fibTwo = fibN - fibOne - offset = i - } else if (arr[i] > x) { - fibN = fibTwo - fibOne = fibOne - fibTwo - fibTwo = fibN - fibOne - } else { - return i - } - - if ((fibOne && arr[offset + 1]) === x) { - return offset + 1 - } - return -1 - } -} - -const arr = [10, 22, 35, 40, 45, 50, 80, 82] -const size = arr.length -fiboNacciSearch(arr, 85, size) -fiboNacciSearch(arr, 50, size) -fiboNacciSearch(arr, 35, size) From 8a028c5157b1cac5ba85a0e73b8107ffe2e7af86 Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 19:53:33 +0530 Subject: [PATCH 4/6] Depth First Search Algorithms --- Graphs/DFS.js | 71 ++++++++++++++++++++++++++ Graphs/Graph.js | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 Graphs/DFS.js create mode 100644 Graphs/Graph.js diff --git a/Graphs/DFS.js b/Graphs/DFS.js new file mode 100644 index 0000000000..20b16fe0a0 --- /dev/null +++ b/Graphs/DFS.js @@ -0,0 +1,71 @@ +/* + * Author: Surendra Kumar + * DFS Algorithm implementation in JavaScript + * DFS Algorithm for traversing or searching graph data structures. +*/ + +function traverseDFS (root) { + const stack = [root] + const res = [] + + while (stack.length) { + const curr = stack.pop() + res.push(curr.key) + + if (curr.right) { + stack.push(curr.right) + } + + if (curr.left) { + stack.push(curr.left) + } + } + + return res.reverse() +} + +function searchDFS (tree, value) { + var stack = [] + + stack.push(tree[0]) + + while (stack.length !== 0) { + for (let i = 0; i < stack.length; i++) { + var node = stack.pop() + + if (node.value === value) { + return node + } + if (node.right) { + stack.push(tree[node.right]) + } + if (node.left) { + stack.push(tree[node.left]) + } + } + } + return null +} + +var tree = [ + { value: 6, left: 1, right: 2 }, + { value: 5, left: 3, right: 4 }, + { value: 7, left: null, right: 5 }, + { value: 3, left: 6, right: null }, + { value: 4, left: null, right: null }, + { value: 9, left: 7, right: 8 }, + { value: 2, left: 9, right: null }, + { value: 8, left: null, right: null }, + { value: 10, left: null, right: null }, + { value: 1, left: null, right: null } +] + +// 6 +// / \ +// 5 7 +// / \ \ +// 3 4 9 +// / / \ +// 2 8 10 +// / +// 1 diff --git a/Graphs/Graph.js b/Graphs/Graph.js new file mode 100644 index 0000000000..3b2b50bbed --- /dev/null +++ b/Graphs/Graph.js @@ -0,0 +1,133 @@ +class Graph { + /** + * Graph constructor. + * @param {boolean} isDirected + */ + constructor (isDirected = false) { + this.vertices = {} + this.edges = {} + this.isDirected = isDirected + } + + /** + * Add new vertex to the graph. + * @param {GraphVertex} newVertex + * @returns {Graph} + */ + addVertex (newVertex) { + this.vertices[newVertex.getKey()] = newVertex + + return this + } + + /** + * Get vertex by its key. + * @param {string} vertexKey + * @returns GraphVertex + */ + getVertexByKey (vertexKey) { + return this.vertices[vertexKey] + } + + /** + * Get the list of all graph vertices. + * @return {GraphVertex[]} + */ + getAllVertices () { + return Object.values(this.vertices) + } + + /** + * Get the list of all graph edges. + * @return {GraphEdge[]} + */ + getAllEdges () { + return Object.values(this.edges) + } + + /** + * Add new edge to the graph. + * @param {GraphEdge} edge + * @returns {Graph} + */ + addEdge (edge) { + // Try to find and end start vertices. + let startVertex = this.getVertexByKey(edge.startVertex.getKey()) + let endVertex = this.getVertexByKey(edge.endVertex.getKey()) + + // Insert start vertex if it wasn't inserted. + if (!startVertex) { + this.addVertex(edge.startVertex) + startVertex = this.getVertexByKey(edge.startVertex.getKey()) + } + + // Insert end vertex if it wasn't inserted. + if (!endVertex) { + this.addVertex(edge.endVertex) + endVertex = this.getVertexByKey(edge.endVertex.getKey()) + } + + // Check if edge has been already added. + if (this.edges[edge.getKey()]) { + throw new Error('Edge has already been added before') + } else { + this.edges[edge.getKey()] = edge + } + + // Add edge to the vertices. + if (this.isDirected) { + // If graph IS directed then add the edge only to start vertex. + startVertex.addEdge(edge) + } else { + // If graph ISN'T directed then add the edge to both vertices. + startVertex.addEdge(edge) + endVertex.addEdge(edge) + } + + return this + } + + /** + * Delete specific edge from the graph. + * @param {GraphEdge} edge + */ + deleteEdge (edge) { + // Delete edge from the list of edges. + if (this.edges[edge.getKey()]) { + delete this.edges[edge.getKey()] + } else { + throw new Error('Edge not found in graph') + } + + // Try to find and end start vertices and delete edge from them. + const startVertex = this.getVertexByKey(edge.startVertex.getKey()) + const endVertex = this.getVertexByKey(edge.endVertex.getKey()) + + startVertex.deleteEdge(edge) + endVertex.deleteEdge(edge) + } + + /** + * Find the edge by start and end vertices. + * @param {GraphVertex} startVertex + * @param {GraphVertex} endVertex + * @return {(GraphEdge|null)} + */ + findEdge (startVertex, endVertex) { + const vertex = this.getVertexByKey(startVertex.getKey()) + + if (!vertex) { + return null + } + + return vertex.findEdge(endVertex) + } + + /** + * Convert graph to string. + * @return {string} + */ + toString () { + return Object.keys(this.vertices).toString() + } +} From 64c49f7656d13451ffe34c06f2983f32fd1a8c07 Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 20:01:24 +0530 Subject: [PATCH 5/6] Depth First Search Algorithms --- Graphs/Graph.js | 133 ------------------------------------------------ 1 file changed, 133 deletions(-) delete mode 100644 Graphs/Graph.js diff --git a/Graphs/Graph.js b/Graphs/Graph.js deleted file mode 100644 index 3b2b50bbed..0000000000 --- a/Graphs/Graph.js +++ /dev/null @@ -1,133 +0,0 @@ -class Graph { - /** - * Graph constructor. - * @param {boolean} isDirected - */ - constructor (isDirected = false) { - this.vertices = {} - this.edges = {} - this.isDirected = isDirected - } - - /** - * Add new vertex to the graph. - * @param {GraphVertex} newVertex - * @returns {Graph} - */ - addVertex (newVertex) { - this.vertices[newVertex.getKey()] = newVertex - - return this - } - - /** - * Get vertex by its key. - * @param {string} vertexKey - * @returns GraphVertex - */ - getVertexByKey (vertexKey) { - return this.vertices[vertexKey] - } - - /** - * Get the list of all graph vertices. - * @return {GraphVertex[]} - */ - getAllVertices () { - return Object.values(this.vertices) - } - - /** - * Get the list of all graph edges. - * @return {GraphEdge[]} - */ - getAllEdges () { - return Object.values(this.edges) - } - - /** - * Add new edge to the graph. - * @param {GraphEdge} edge - * @returns {Graph} - */ - addEdge (edge) { - // Try to find and end start vertices. - let startVertex = this.getVertexByKey(edge.startVertex.getKey()) - let endVertex = this.getVertexByKey(edge.endVertex.getKey()) - - // Insert start vertex if it wasn't inserted. - if (!startVertex) { - this.addVertex(edge.startVertex) - startVertex = this.getVertexByKey(edge.startVertex.getKey()) - } - - // Insert end vertex if it wasn't inserted. - if (!endVertex) { - this.addVertex(edge.endVertex) - endVertex = this.getVertexByKey(edge.endVertex.getKey()) - } - - // Check if edge has been already added. - if (this.edges[edge.getKey()]) { - throw new Error('Edge has already been added before') - } else { - this.edges[edge.getKey()] = edge - } - - // Add edge to the vertices. - if (this.isDirected) { - // If graph IS directed then add the edge only to start vertex. - startVertex.addEdge(edge) - } else { - // If graph ISN'T directed then add the edge to both vertices. - startVertex.addEdge(edge) - endVertex.addEdge(edge) - } - - return this - } - - /** - * Delete specific edge from the graph. - * @param {GraphEdge} edge - */ - deleteEdge (edge) { - // Delete edge from the list of edges. - if (this.edges[edge.getKey()]) { - delete this.edges[edge.getKey()] - } else { - throw new Error('Edge not found in graph') - } - - // Try to find and end start vertices and delete edge from them. - const startVertex = this.getVertexByKey(edge.startVertex.getKey()) - const endVertex = this.getVertexByKey(edge.endVertex.getKey()) - - startVertex.deleteEdge(edge) - endVertex.deleteEdge(edge) - } - - /** - * Find the edge by start and end vertices. - * @param {GraphVertex} startVertex - * @param {GraphVertex} endVertex - * @return {(GraphEdge|null)} - */ - findEdge (startVertex, endVertex) { - const vertex = this.getVertexByKey(startVertex.getKey()) - - if (!vertex) { - return null - } - - return vertex.findEdge(endVertex) - } - - /** - * Convert graph to string. - * @return {string} - */ - toString () { - return Object.keys(this.vertices).toString() - } -} From 2f93034bfbddefbca5d188ed089ade7a5403a1cf Mon Sep 17 00:00:00 2001 From: comradesurendra Date: Sun, 21 Jun 2020 20:13:57 +0530 Subject: [PATCH 6/6] Depth First Search Algorithms --- Graphs/DFS.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Graphs/DFS.js b/Graphs/DFS.js index 20b16fe0a0..ec8ca7e669 100644 --- a/Graphs/DFS.js +++ b/Graphs/DFS.js @@ -60,6 +60,11 @@ var tree = [ { value: 1, left: null, right: null } ] +searchDFS(tree, 9) +searchDFS(tree, 10) + +traverseDFS(6) + // 6 // / \ // 5 7