1
- import { PriorityQueue } from '../DataStructures/PriorityQueue' ;
2
- export function aSearch ( grid , start , finish ) {
1
+ import { PriorityQueue } from '../DataStructures/PriorityQueue' ;
2
+ import { getAllNodes , getUnvisitedNeighbours } from './Dijkstra' ;
3
3
4
- let op = [ ] ;
5
- let cl = [ ] ;
4
+ export function aSearch ( grid , start , finish ) {
5
+
6
+ let open = [ ] ;
7
+ let close = [ ] ;
6
8
const allnodes = getAllNodes ( grid ) ;
7
9
8
- let open = new PriorityQueue ( ) ;
9
- start . aEndDis = calDis ( finish , start )
10
+ let pqueue = new PriorityQueue ( ) ;
11
+ start . aEndDis = calculateDistance ( finish , start )
10
12
11
- open . enqueue ( start , start . aEndDis + start . cost ) ;
12
- op . push ( start ) ;
13
+ pqueue . enqueue ( start , start . aEndDis + start . cost ) ;
14
+ open . push ( start ) ;
13
15
14
16
let closed = new PriorityQueue ( ) ;
15
17
let visitedNodesInOrder = [ ]
16
18
19
+ while ( pqueue . size ( ) > 0 ) {
17
20
21
+ pqueue . print ( )
18
22
19
- while ( open . size ( ) > 0 ) {
20
-
21
- open . print ( )
22
-
23
- const curObject = open . dequeue ( ) ;
23
+ const curObject = pqueue . dequeue ( ) ;
24
24
const current = curObject . element ;
25
- op = removeElement ( op , current ) ;
25
+ open = removeElement ( open , current ) ;
26
26
27
- current . isVisited = true
27
+ current . isVisited = true
28
28
29
- closed . enqueue ( current , current . aEndDis + current . cost ) ;
30
- cl . push ( current )
29
+ closed . enqueue ( current , current . aEndDis + current . cost ) ;
30
+ close . push ( current )
31
31
closed . print ( )
32
- const neigh = getUnvisitedNeighboursBfs ( current , grid ) ;
32
+ const neigh = getUnvisitedNeighbours ( current , grid ) ;
33
33
34
- let short = null
35
- let index = 0
34
+ let short = null
35
+ let index = 0
36
36
37
- for ( let i = 0 ; i < neigh . length ; i ++ ) {
38
- let s = 0 , d = 0
37
+ for ( let i = 0 ; i < neigh . length ; i ++ ) {
38
+ let s = 0 ,
39
+ d = 0
40
+
41
+ s = calculateDistance ( start , neigh [ i ] )
42
+ d = calculateDistance ( finish , neigh [ i ] )
39
43
40
- s = calDis ( start , neigh [ i ] )
41
- d = calDis ( finish , neigh [ i ] )
42
-
43
44
44
45
neigh [ i ] . distance = s
45
- neigh [ i ] . aDis = neigh [ i ] . distance + d ;
46
- neigh [ i ] . aEndDis = d ;
46
+ neigh [ i ] . aDis = neigh [ i ] . distance + d ;
47
+ neigh [ i ] . aEndDis = d ;
47
48
48
- if ( neigh [ i ] in cl ) {
49
+ if ( neigh [ i ] in close ) {
49
50
continue ;
50
51
}
51
52
52
- if ( ! ( neigh [ i ] in op ) ) {
53
- open . enqueue ( neigh [ i ] , neigh [ i ] . aEndDis + neigh [ i ] . cost ) ;
54
- op . push ( neigh [ i ] )
53
+ if ( ! ( neigh [ i ] in open ) ) {
54
+ pqueue . enqueue ( neigh [ i ] , neigh [ i ] . aEndDis + neigh [ i ] . cost ) ;
55
+ open . push ( neigh [ i ] )
55
56
neigh [ i ] . previousNode = current
56
57
57
58
}
58
-
59
-
60
59
}
61
60
61
+ current . isVisited = true ;
62
62
63
- current . isVisited = true ;
64
-
65
- visitedNodesInOrder . push ( short ) ;
66
-
67
- if ( current === finish ) {
68
-
69
- return cl ;
70
- }
63
+ visitedNodesInOrder . push ( short ) ;
71
64
65
+ if ( current === finish ) {
66
+ return close ;
67
+ }
72
68
}
73
69
74
-
75
70
}
76
71
77
- function calDis ( start , node ) {
78
- let dis = Math . abs ( start . row - node . row ) + Math . abs ( start . col - node . col )
72
+ function calculateDistance ( start , node ) {
73
+ let dis = Math . abs ( start . row - node . row ) + Math . abs ( start . col - node . col )
79
74
return dis
80
75
}
81
76
82
- function removeElement ( ar , ele ) {
83
- return ar . filter ( function ( val ) {
84
- return val != ele ;
77
+ function removeElement ( ar , ele ) {
78
+ return ar . filter ( function ( val ) {
79
+ return val != ele ;
85
80
} )
86
- }
87
-
88
- function updateUnvisitedNeighbors ( node , grid ) {
89
- /** Updates the distances of unvisited nodes */
90
- const unvisitedNeighbors = getUnvisitedNeighboursBfs ( node , grid ) ;
91
- for ( const neighbor of unvisitedNeighbors ) {
92
- neighbor . previousNode = node ;
93
- }
94
- }
95
-
96
- function getUnvisitedNeighboursBfs ( node , grid ) {
97
- /** Returns all univisted neighbours of given node*/
98
- const neighbors = [ ] ;
99
- const col = node . col ;
100
- const row = node . row ;
101
- if ( row > 0 ) neighbors . push ( grid [ row - 1 ] [ col ] ) ;
102
- if ( row < grid . length - 1 ) neighbors . push ( grid [ row + 1 ] [ col ] ) ;
103
- if ( col > 0 ) neighbors . push ( grid [ row ] [ col - 1 ] ) ;
104
- if ( col < grid [ 0 ] . length - 1 ) neighbors . push ( grid [ row ] [ col + 1 ] ) ;
105
- return neighbors . filter ( neighbor => ! neighbor . isVisited ) ;
106
- }
107
-
108
- function getAllNodes ( grid ) {
109
- /** Returns all nodes in grid*/
110
- const nodes = [ ] ;
111
- for ( const row of grid ) {
112
- for ( const node of row ) {
113
- node . distance = 0 ;
114
- nodes . push ( node ) ;
115
- }
116
- }
117
- return nodes ;
118
- }
119
-
81
+ }
0 commit comments