Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Graph Search Lab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sfarahmand2
Graph Search Lab
Commits
81fa6cd9
Commit
81fa6cd9
authored
8 months ago
by
sfarahmand2
Browse files
Options
Downloads
Patches
Plain Diff
Update solvers.js
parent
805b8bce
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
graph-search/src/features/maze/solvers.js
+88
-3
88 additions, 3 deletions
graph-search/src/features/maze/solvers.js
with
88 additions
and
3 deletions
graph-search/src/features/maze/solvers.js
+
88
−
3
View file @
81fa6cd9
...
@@ -11,11 +11,64 @@ class Edge {
...
@@ -11,11 +11,64 @@ class Edge {
}
}
export
function
solveByBFS
(
maze
)
{
export
function
solveByBFS
(
maze
)
{
// TODO: stub
const
backpointers
=
new
Map
();
const
worklist
=
new
Queue
();
worklist
.
insert
(
new
Edge
(
undefined
,
maze
.
entrance
));
while
(
worklist
.
size
>
0
)
{
const
workitem
=
worklist
.
remove
();
if
(
backpointers
.
has
(
workitem
.
to
))
{
continue
;
}
workitem
.
to
.
type
=
CellType
.
CONSIDERED
;
backpointers
.
set
(
workitem
.
to
,
workitem
);
if
(
workitem
.
to
===
maze
.
exit
)
{
const
reversedPath
=
[];
for
(
let
current
=
workitem
.
to
;
// cells to CellType.SOLUTION
// current.type = CellType.SOLUTION;
current
!==
undefined
;
current
=
backpointers
.
get
(
current
).
from
)
{
reversedPath
.
push
(
current
);
current
.
type
=
CellType
.
SOLUTION
;
}
return
reversedPath
.
reverse
();
}
for
(
const
incidence
of
workitem
.
to
.
neighbors
)
{
worklist
.
insert
(
new
Edge
(
workitem
.
to
,
incidence
));
}
}
return
undefined
;
}
}
export
function
solveByDijkstras
(
maze
)
{
export
function
solveByDijkstras
(
maze
)
{
// TODO: stub
const
backpointers
=
new
Map
();
const
worklist
=
new
PriorityQueue
();
worklist
.
insert
(
new
Edge
(
undefined
,
maze
.
entrance
,
0
),
0
);
while
(
worklist
.
size
>
0
)
{
const
workitem
=
worklist
.
remove
();
if
(
backpointers
.
has
(
workitem
.
to
))
{
continue
;
}
workitem
.
to
.
type
=
CellType
.
CONSIDERED
;
backpointers
.
set
(
workitem
.
to
,
workitem
);
if
(
workitem
.
to
===
maze
.
exit
)
{
const
reversedPath
=
[];
for
(
let
current
=
maze
.
exit
;
current
!==
undefined
;
current
=
backpointers
.
get
(
current
).
from
)
{
reversedPath
.
push
(
current
);
current
.
type
=
CellType
.
SOLUTION
;
}
return
reversedPath
.
reverse
();
}
for
(
const
incidence
of
workitem
.
to
.
neighbors
)
{
worklist
.
insert
(
new
Edge
(
workitem
.
to
,
incidence
,
workitem
.
distance
+
1
),
workitem
.
distance
,
);
}
}
return
undefined
;
}
}
function
heuristic
(
maze
,
cell
)
{
function
heuristic
(
maze
,
cell
)
{
...
@@ -23,5 +76,37 @@ function heuristic(maze, cell) {
...
@@ -23,5 +76,37 @@ function heuristic(maze, cell) {
}
}
export
function
solveByAStar
(
maze
)
{
export
function
solveByAStar
(
maze
)
{
// TODO: stub
const
backpointers
=
new
Map
();
const
worklist
=
new
PriorityQueue
();
worklist
.
insert
(
new
Edge
(
undefined
,
maze
.
entrance
,
0
),
0
+
heuristic
(
maze
.
entrance
,
maze
.
exit
),
);
while
(
worklist
.
size
>
0
)
{
const
workitem
=
worklist
.
remove
();
if
(
backpointers
.
has
(
workitem
.
to
)
&&
backpointers
.
get
(
workitem
.
to
).
distance
<=
workitem
.
distance
)
{
continue
;
}
workitem
.
to
.
type
=
CellType
.
CONSIDERED
;
backpointers
.
set
(
workitem
.
to
,
workitem
);
if
(
workitem
.
to
===
maze
.
exit
)
{
const
reversedPath
=
[];
for
(
let
current
=
maze
.
exit
;
current
!==
undefined
;
current
=
backpointers
.
get
(
current
).
from
)
{
reversedPath
.
push
(
current
);
current
.
type
=
CellType
.
SOLUTION
;
}
return
reversedPath
.
reverse
();
}
for
(
const
incidence
of
workitem
.
to
.
neighbors
)
{
worklist
.
insert
(
new
Edge
(
workitem
.
to
,
incidence
.
destination
,
workitem
.
distance
),
workitem
.
distance
+
heuristic
(
incidence
.
destination
,
maze
.
exit
),
);
}
}
return
undefined
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment