Advertisement
gasaichan

Modified Dijkstra

Oct 27th, 2019
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const dfs = (graph, visited = [], n, v, x, cnt, path = "") => {
  2.   path += `${v}-`;
  3.  
  4.   if (v == x) {
  5.     cnt++;
  6.     // console.log(path);
  7.  
  8.     console.log(path.slice(0, path.length - 1));
  9.     path = "";
  10.     return;
  11.   }
  12.  
  13.   visited[v] = true;
  14.  
  15.   for (let i = 0; i < n; i++) {
  16.     if (graph[v][i] && !visited[i]) {
  17.       dfs(graph, visited, n, i, x, cnt, path);
  18.     }
  19.   }
  20.  
  21.   visited[v] = false;
  22. };
  23.  
  24. const graph = [
  25.   [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  26.   [1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
  27.   [1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
  28.   [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  29.   [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0],
  30.   [0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0],
  31.   [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
  32.   [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0],
  33.   [0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0],
  34.   [0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1],
  35.   [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
  36.   [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0]
  37. ];
  38.  
  39. dfs(graph, [], graph.length, 0, graph.length - 1, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement