Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const dfs = (graph, visited = [], n, v, x, cnt, path = "") => {
- path += `${v}-`;
- if (v == x) {
- cnt++;
- // console.log(path);
- console.log(path.slice(0, path.length - 1));
- path = "";
- return;
- }
- visited[v] = true;
- for (let i = 0; i < n; i++) {
- if (graph[v][i] && !visited[i]) {
- dfs(graph, visited, n, i, x, cnt, path);
- }
- }
- visited[v] = false;
- };
- const graph = [
- [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
- [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0],
- [0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0],
- [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
- [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0],
- [0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0],
- [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1],
- [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]
- ];
- dfs(graph, [], graph.length, 0, graph.length - 1, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement