Advertisement
Washy

Untitled

Apr 1st, 2021
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node {
  2.     constructor(name, g, h) {
  3.         this.name = name;
  4.         this.g = g;
  5.         this.h = h;
  6.         this.f = 0;
  7.     }
  8.  
  9. }
  10.  
  11. class Edge {
  12.     constructor(n1, n2, w) {
  13.         this.node_1 = n1;
  14.         this.node_2 = n2;
  15.         this.weight = w;
  16.     }
  17. }
  18.  
  19. class Graph {
  20.     constructor(start, end) {
  21.         this.start = start;
  22.         this.end = end;
  23.         this.edges = new Array();
  24.         this.nodes = new Array(start, end);
  25.     }
  26.     add_node(node) {
  27.         if (this.nodes.includes(node)) {
  28.             console.log('This node is already in the graph');
  29.         } else {
  30.             this.nodes.push(node);
  31.         }
  32.     }
  33.     add_edge(n1, n2, w) {
  34.         if (!(this.nodes.includes(n1))) {
  35.             this.add_node(n1);
  36.         }
  37.         if (!(this.nodes.includes(n2))) {
  38.             this.add_node(n2);
  39.         }
  40.         this.add_edge(n1, n2, w);
  41.     }
  42.     get_edges() {
  43.         return this.edges;
  44.     }
  45.     get_nodes() {
  46.         return this.nodes;
  47.     }
  48. }
  49. function start_up(){
  50.     console.log('running');
  51.     var sn = new Node('a', 4, 4);
  52.     var mn = new Node('b', 2, 2);
  53.     var en = new Node('z', 0, 0);
  54.  
  55.     var e1 = new Edge(sn, mn, 2);
  56.     var e2 = new Edge(mn, en, 2);
  57.  
  58.     var g = new Graph(sn, en);
  59.  
  60.     g.add_edge(sn, mn);
  61.     g.add_edge(mn, en);
  62.  
  63.     a_star(g, new Array(), new Array(), sn);
  64. }
  65.  
  66. function a_star(graph, visited, path, current) {
  67.     var edges = graph.get_edges();
  68.     var nodes = graph.get_nodes();
  69.  
  70.     if (current != graph.end) {
  71.         visited.push(current);
  72.         path.push(current.name);
  73.  
  74.         console.log("Current Node: " + current);
  75.         console.log('Visited Nodes: ' + visited);
  76.  
  77.         c_from_c = new Array();
  78.         for (i = 0; i < edges.length; i++) {
  79.             if (current.name in edges[i]){
  80.                 tmp = edges[i].replace(current.name, '');
  81.                 c_from_c.push(tmp);
  82.             }
  83.         }
  84.         var available_nodes = new Array();
  85.         for (i = 0; i < c_from_c.length; i++){
  86.             var f = parseInt(c_from_c[i][0]);
  87.             for (j = 0; j < nodes.length; j++){
  88.                 if (nodes[j].name == c_from_c[i][0]){
  89.                     f += nodes[j].h;
  90.                     nodes[j].f = f;
  91.                     available_nodes.push(nodes[j]);
  92.                 }
  93.             }
  94.         }
  95.         var f_val = new Array();
  96.         for (j = 0; j < available_nodes.length; j++){
  97.             f_val.push(available_nodes[j].f)
  98.         }
  99.         var index = available_nodes.findIndex(Math.min(f_val));
  100.  
  101.         var current = available_nodes[index];
  102.  
  103.         a_star(graph, visited, path, current);
  104.     }
  105.     else {
  106.         visited.push(current);
  107.         var shortest = new Array();
  108.         for (i = 0; i < visited.length; i++){
  109.             shortest.push(visited[i])
  110.         }
  111.         console.log(shortest);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement