7
7
*/
8
8
9
9
function createGraph ( V , E ) {
10
- // V - Number of vertices in graph
11
- // E - Number of edges in graph (u,v,w)
12
- const adjList = [ ] // Adjacency list
13
- for ( let i = 0 ; i < V ; i ++ ) {
14
- adjList . push ( [ ] )
15
- }
16
- for ( let i = 0 ; i < E . length ; i ++ ) {
17
- adjList [ E [ i ] [ 0 ] ] . push ( [ E [ i ] [ 1 ] , E [ i ] [ 2 ] ] )
18
- adjList [ E [ i ] [ 1 ] ] . push ( [ E [ i ] [ 0 ] , E [ i ] [ 2 ] ] )
19
- }
20
- return adjList
10
+ // V - Number of vertices in graph
11
+ // E - Number of edges in graph (u,v,w)
12
+ const adjList = [ ] // Adjacency list
13
+ for ( let i = 0 ; i < V ; i ++ ) {
14
+ adjList . push ( [ ] )
21
15
}
22
-
23
- // Heuristic function to estimate the cost to reach the goal
24
- // You can modify this based on your specific problem, for now, we're using Manhattan distance
25
- function heuristic ( a , b ) {
26
- return Math . abs ( a - b )
16
+ for ( let i = 0 ; i < E . length ; i ++ ) {
17
+ adjList [ E [ i ] [ 0 ] ] . push ( [ E [ i ] [ 1 ] , E [ i ] [ 2 ] ] )
18
+ adjList [ E [ i ] [ 1 ] ] . push ( [ E [ i ] [ 0 ] , E [ i ] [ 2 ] ] )
27
19
}
28
-
29
- function aStar ( graph , V , src , target ) {
30
- const openSet = new Set ( [ src ] ) // Nodes to explore
31
- const cameFrom = Array ( V ) . fill ( - 1 ) // Keep track of path
32
- const gScore = Array ( V ) . fill ( Infinity ) // Actual cost from start to a node
33
- gScore [ src ] = 0
34
-
35
- const fScore = Array ( V ) . fill ( Infinity ) // Estimated cost from start to goal (g + h)
36
- fScore [ src ] = heuristic ( src , target )
37
-
38
- while ( openSet . size > 0 ) {
39
- // Get the node in openSet with the lowest fScore
40
- let current = - 1
41
- openSet . forEach ( ( node ) => {
42
- if ( current === - 1 || fScore [ node ] < fScore [ current ] ) {
43
- current = node
44
- }
45
- } )
46
-
47
- // If the current node is the target, reconstruct the path and return
48
- if ( current === target ) {
49
- const path = [ ]
50
- while ( cameFrom [ current ] !== - 1 ) {
51
- path . push ( current )
52
- current = cameFrom [ current ]
53
- }
54
- path . push ( src )
55
- return path . reverse ( )
20
+ return adjList
21
+ }
22
+
23
+ // Heuristic function to estimate the cost to reach the goal
24
+ // You can modify this based on your specific problem, for now, we're using Manhattan distance
25
+ function heuristic ( a , b ) {
26
+ return Math . abs ( a - b )
27
+ }
28
+
29
+ function aStar ( graph , V , src , target ) {
30
+ const openSet = new Set ( [ src ] ) // Nodes to explore
31
+ const cameFrom = Array ( V ) . fill ( - 1 ) // Keep track of path
32
+ const gScore = Array ( V ) . fill ( Infinity ) // Actual cost from start to a node
33
+ gScore [ src ] = 0
34
+
35
+ const fScore = Array ( V ) . fill ( Infinity ) // Estimated cost from start to goal (g + h)
36
+ fScore [ src ] = heuristic ( src , target )
37
+
38
+ while ( openSet . size > 0 ) {
39
+ // Get the node in openSet with the lowest fScore
40
+ let current = - 1
41
+ openSet . forEach ( ( node ) => {
42
+ if ( current === - 1 || fScore [ node ] < fScore [ current ] ) {
43
+ current = node
56
44
}
57
-
58
- openSet . delete ( current )
59
-
60
- // Explore neighbors
61
- for ( let i = 0 ; i < graph [ current ] . length ; i ++ ) {
62
- const neighbor = graph [ current ] [ i ] [ 0 ]
63
- const tentative_gScore = gScore [ current ] + graph [ current ] [ i ] [ 1 ]
64
-
65
- if ( tentative_gScore < gScore [ neighbor ] ) {
66
- cameFrom [ neighbor ] = current
67
- gScore [ neighbor ] = tentative_gScore
68
- fScore [ neighbor ] = gScore [ neighbor ] + heuristic ( neighbor , target )
69
-
70
- if ( ! openSet . has ( neighbor ) ) {
71
- openSet . add ( neighbor )
72
- }
45
+ } )
46
+
47
+ // If the current node is the target, reconstruct the path and return
48
+ if ( current === target ) {
49
+ const path = [ ]
50
+ while ( cameFrom [ current ] !== - 1 ) {
51
+ path . push ( current )
52
+ current = cameFrom [ current ]
53
+ }
54
+ path . push ( src )
55
+ return path . reverse ( )
56
+ }
57
+
58
+ openSet . delete ( current )
59
+
60
+ // Explore neighbors
61
+ for ( let i = 0 ; i < graph [ current ] . length ; i ++ ) {
62
+ const neighbor = graph [ current ] [ i ] [ 0 ]
63
+ const tentative_gScore = gScore [ current ] + graph [ current ] [ i ] [ 1 ]
64
+
65
+ if ( tentative_gScore < gScore [ neighbor ] ) {
66
+ cameFrom [ neighbor ] = current
67
+ gScore [ neighbor ] = tentative_gScore
68
+ fScore [ neighbor ] = gScore [ neighbor ] + heuristic ( neighbor , target )
69
+
70
+ if ( ! openSet . has ( neighbor ) ) {
71
+ openSet . add ( neighbor )
73
72
}
74
73
}
75
74
}
76
-
77
- return [ ] // Return empty path if there's no path to the target
78
75
}
79
-
80
- module . exports = { createGraph, aStar }
81
76
82
- // const V = 9
83
- // const E = [
84
- // [0, 1, 4],
85
- // [0, 7, 8],
86
- // [1, 7, 11],
87
- // [1, 2, 8],
88
- // [7, 8, 7],
89
- // [6, 7, 1],
90
- // [2, 8, 2],
91
- // [6, 8, 6],
92
- // [5, 6, 2],
93
- // [2, 5, 4],
94
- // [2, 3, 7],
95
- // [3, 5, 14],
96
- // [3, 4, 9],
97
- // [4, 5, 10]
98
- // ]
99
-
100
- // const graph = createGraph(V, E)
101
- // const path = aStar(graph, V, 0, 4) // Find path from node 0 to node 4
102
- // console.log(path)
103
-
104
- /**
105
- * The function returns the optimal path from the source to the target node.
106
- * The heuristic used is Manhattan distance but it can be modified.
107
- */
108
-
77
+ return [ ] // Return empty path if there's no path to the target
78
+ }
79
+
80
+ module . exports = { createGraph, aStar }
81
+
82
+ // const V = 9
83
+ // const E = [
84
+ // [0, 1, 4],
85
+ // [0, 7, 8],
86
+ // [1, 7, 11],
87
+ // [1, 2, 8],
88
+ // [7, 8, 7],
89
+ // [6, 7, 1],
90
+ // [2, 8, 2],
91
+ // [6, 8, 6],
92
+ // [5, 6, 2],
93
+ // [2, 5, 4],
94
+ // [2, 3, 7],
95
+ // [3, 5, 14],
96
+ // [3, 4, 9],
97
+ // [4, 5, 10]
98
+ // ]
99
+
100
+ // const graph = createGraph(V, E)
101
+ // const path = aStar(graph, V, 0, 4) // Find path from node 0 to node 4
102
+ // console.log(path)
103
+
104
+ /**
105
+ * The function returns the optimal path from the source to the target node.
106
+ * The heuristic used is Manhattan distance but it can be modified.
107
+ */
0 commit comments