Repository: pbiswas101/Mathball (js)

Issue:
Kaprekar Number
### Do the checklist before filing the issue:

- [ ] Is this something you can **debug and fix**?
- [x] Do you have Node.js and all the relevant dev-dependencies installed?
- [ ] Is this a **feature request** that you can add?

###### NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it.

- *Give your answer below:*

------------

[Function: **M.isKaprekar()**] needs to be added!
For more info:

https://www.geeksforgeeks.org/kaprekar-number/

*M.isKaprekar()* needs to accept a positive integer and all other arguments passed should throw a type error which needs to be handled gracefully! For error handling, you should refer to how other features like M.isArmstrong() is implemented.

Pull request description:
Added DFS function to Graph. Issue #170
### Do the checklist before submitting the PR:

- [x] Have you read the guidelines mentioned in **CONTRIBUTING.md**?
- [x] Have you squashed your commits?

**Q**: What version of *Node.js* you've used in the project? (`e.g. Node v10.11.0`)
**A**:v11.6.0

**Q**: Mention the *Issue Number*! (`e.g. Fixed #8`)
**A**:#170

- *Give additional information regarding the PR below:*

------------


Patch:
diff --git a/docs/Graph/index.md b/docs/Graph/index.md
index de3f926..72723a4 100644
--- a/docs/Graph/index.md
+++ b/docs/Graph/index.md
@@ -26,6 +26,7 @@ console.log(g);   // Graph { noOfVertices: 6, Adjlist: Map {} }
 - **removeVertex** - This method removes the vertex passed as argument. If list is empty, returns `Empty Graph`.
 - **removeEdge** - This method removes the edge between the source and destination. If list is empty, returns `Empty Graph`. Returns `Vertex does not exist in the Graph` if vertex name is not found. Returns `Edge not found!` if edge does not exist between source and destination.
 - **bfs** -  This method performs Breadth First Search from the given starting node.
+- **dfs** -  This method performs recursive Depth First Search from the given starting node.
 
 ------
 
@@ -211,7 +212,36 @@ console.log(g.bfs('B'));   //  [ 'B', 'C', 'D', 'A' ]
 console.log(g.bfs('D'));   //  [ 'D', 'A', 'B', 'C' ]
 console.log(g.bfs('E'));   //  Node doesn't exist in the Graph
 ```
+###### Info
+Implemented using JavaScript's built-in `from`, `keys`, `includes`, `push`,  and `shift` function.
+
+
+###### Syntax
+> g.dfs(startNode)
+
+###### Arguments
+> Number/String
+
+###### Return
+> Object
 
+###### Examples
+```js
+const g=new M.Graph(4);
+const vertices = [ 'A', 'B', 'C', 'D' ];
+for (var i = 0; i < vertices.length; i++) { 
+    g.addVertex(vertices[i]); 
+} 
+g.addEdge('A', 'B'); 
+g.addEdge('B', 'C'); 
+g.addEdge('C', 'D'); 
+g.addEdge('D', 'A'); 
+console.log(g.printGraph());   //  Map { 'A' => [ 'B' ], 'B' => [ 'C' ], 'C' => [ 'D' ], 'D' => [ 'A' ] }
+console.log(g.dfs('A'));   //  [ 'A', 'B', 'C', 'D' ]
+console.log(g.dfs('B'));   //  [ 'B', 'C', 'D', 'A' ]
+console.log(g.dfs('D'));   //  [ 'D', 'A', 'B', 'C' ]
+console.log(g.dfs('E'));   //  Node doesn't exist in the Graph
+```
 ###### Info
 Implemented using JavaScript's built-in `from`, `keys`, `includes`, `push`,  and `shift` function.
 
diff --git a/src/graph/index.js b/src/graph/index.js
index f7b4b14..3a79399 100644
--- a/src/graph/index.js
+++ b/src/graph/index.js
@@ -84,6 +84,33 @@ class Graph{
         }
         return output;
     }
+
+    dfs(startNode){
+
+        if(!Array.from(this.Adjlist.keys()).includes(startNode)){
+            return "Node doesn't exist in the Graph";
+        }
+        let visited = [];
+        let output = new Array();
+
+        for(let i = 0; i < this.noOfVertices; i++){
+            visited.push(false);
+        }
+        this.dfsUtil(startNode, visited, output);
+        return output;
+    }
+
+    dfsUtil(node, visited, output){
+        visited[Array.from(this.Adjlist.keys()).indexOf(node)] = true;
+        output.push(node);
+
+        let neighbours = this.Adjlist.get(node);
+        for(let i = 0; i < neighbours.length; i++){
+            if(visited[Array.from(this.Adjlist.keys()).indexOf(neighbours[i])] === false){
+                this.dfsUtil(neighbours[i], visited, output);
+            }
+        }
+    }
 }
 
 module.exports = Graph;
\ No newline at end of file


Test patch:
diff --git a/test/graph-spec.js b/test/graph-spec.js
index 0e13ba6..c0c3f80 100644
--- a/test/graph-spec.js
+++ b/test/graph-spec.js
@@ -87,7 +87,19 @@ const assert = require('assert'),
         it(`should return 'Node doesn't exist in the Graph' when 'bfs('f')' is called`, () => {
             assert.deepEqual(sample.bfs('f'), 'Node doesn\'t exist in the Graph');
         });
+
+        it(`should return 'Node doesn't exist in the Graph' when 'dfs('f')' is called`, () => {
+            assert.deepEqual(sample.dfs('f'), 'Node doesn\'t exist in the Graph');
+        });
         
+        it(`should return 'object' type when 'dfs('A')' is called`, () => {
+            assert.strictEqual(typeof sample.dfs('A'), 'object');
+        });
+
+        it(`should return '[ 'A', 'B', 'D', 'C', 'E' ]' type when 'dfs('A')' is called`, () => {
+            assert.deepEqual(sample.dfs('A'), ["A", "B", "D", "E", "C"]);
+        });
+
         it(`should return 'D' when 'removeEdge('A', 'D')' is called`, () => {
             assert.strictEqual(sample.removeEdge('A', 'D'), 'D');
         });