Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | // Javascript program to print DFS
// traversal from a given
// graph
// This class represents a
// directed graph using adjacency
// list representation
export class Graph {
// Constructor
//v=number of vertices
constructor() {
this.visited = []
this.dict = []
this.adj = []
}
// Function to add an edge into the graph
addEdge(uuid1, uuid2) {
let v = this.dict.indexOf(uuid1)
let length
if (v == -1) {
length = this.dict.length
this.dict[length] = uuid1
this.adj[length] = []
v = length
}
let w = this.dict.indexOf(uuid2)
if (w == -1) {
length = this.dict.length
this.dict[length] = uuid2
this.adj[length] = []
w = length
}
// Add w to v's list.
if (!this.adj[v]) {
this.adj[v] = []
}
this.adj[v].push(w)
if (!this.adj[w]) {
this.adj[w] = []
}
this.adj[w].push(v)
}
// A function used by DFS
DFSUtil(v, visited) {
// Mark the current node as visited and print it
visited[v] = true
this.visited.push(v)
// Recur for all the vertices adjacent to this
// vertex
for (let i in this.adj[v]) {
let n = this.adj[v][i]
if (!visited[n]) this.DFSUtil(n, visited)
}
}
// The function to do DFS traversal.
// It uses recursive
// DFSUtil()
DFS(uuidStart) {
let v = this.dict.indexOf(uuidStart)
// Mark all the vertices as
// not visited(set as
// false by default in java)
let visited = []
// Call the recursive helper
// function to print DFS
// traversal
this.DFSUtil(v, visited)
return [
...new Set(
this.visited.map((i) => {
return this.dict[i]
})
)
]
}
}
|