Created
January 9, 2019 02:24
-
-
Save kevinahn7/7e5b2fa281ac7dff92c1f7016fda96cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| input will be a graph | |
| { | |
| v : ['A', 'B', 'C', 'D', E', 'F'], | |
| e: { | |
| 'A': ['B'], | |
| 'B': ['A', 'C', 'D'], | |
| 'C': ['B', 'D'], | |
| 'D': ['B', 'C'], | |
| 'E': ['F'], | |
| 'F': ['E'] | |
| } | |
| } | |
| function DFS(g) { | |
| let v = g.v; | |
| let e = g.e; | |
| let visited = {}; | |
| for (let i = 0; i < v.length; i++) { | |
| if (!visited[v[i]]) { | |
| visit(v[i]); | |
| } | |
| } | |
| } | |
| function visit(vertex) { | |
| visited[vertex] = true; | |
| console.log(vertex); | |
| let edges = e[vertex]; | |
| for (let i = 0; i < edges.length; i++) { | |
| if (!visited[edges[i]]) { | |
| visit(edges[i]) | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment