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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sfarahmand2
Graph Search Lab
Compare revisions
main to main
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
sfarahmand2/graph-search-lab
Select target project
No results found
main
Select Git revision
Branches
main
1 result
Swap
Target
soft-core/soft-260/graph-search-lab
Select target project
sfarahmand2/graph-search-lab
jguzman12/graph-search-lab
oaddison2/graph-search-lab
soft-core/soft-260/graph-search-lab
4 results
main
Select Git revision
Branches
main
1 result
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Update solvers.js
· 81fa6cd9
sfarahmand2
authored
9 months ago
81fa6cd9
implemented solveByAStar and heurisitic functions
· 411af3e6
skhourshed2
authored
8 months ago
411af3e6
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
graph-search/src/features/maze/solvers.js
+84
-4
84 additions, 4 deletions
graph-search/src/features/maze/solvers.js
with
84 additions
and
4 deletions
graph-search/src/features/maze/solvers.js
View file @
411af3e6
...
...
@@ -11,17 +11,97 @@ class Edge {
}
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
)
{
// 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
)
{
return
0
;
// TODO: stub
return
Math
.
abs
(
maze
.
exit
.
x
-
cell
.
x
)
+
Math
.
abs
(
maze
.
exit
.
y
-
cell
.
y
);
}
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
,
maze
.
exit
),
);
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
)
{
for
(
let
current
=
maze
.
exit
;
current
!==
undefined
;
current
=
backpointers
.
get
(
current
).
from
)
{
current
.
type
=
CellType
.
SOLUTION
;
}
return
;
}
for
(
const
neighbor
of
workitem
.
to
.
neighbors
)
{
worklist
.
insert
(
new
Edge
(
workitem
.
to
,
neighbor
,
workitem
.
distance
+
1
),
workitem
.
distance
+
1
+
heuristic
(
maze
,
neighbor
),
);
}
}
}
This diff is collapsed.
Click to expand it.