Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Graph Search in Class
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
SOFT Core
SOFT 260
Graph Search in Class
Commits
6f17c38c
You need to sign in or sign up before continuing.
Commit
6f17c38c
authored
1 year ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Recorded work from Thursday.
parent
293d2182
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
graph-search/notes.md
+33
-10
33 additions, 10 deletions
graph-search/notes.md
graph-search/src/features/search/search.js
+53
-4
53 additions, 4 deletions
graph-search/src/features/search/search.js
graph-search/src/features/search/solution.js
+1
-1
1 addition, 1 deletion
graph-search/src/features/search/solution.js
with
87 additions
and
15 deletions
graph-search/notes.md
+
33
−
10
View file @
6f17c38c
...
@@ -103,31 +103,54 @@ How many moves does it take to reverse the string `123`?
...
@@ -103,31 +103,54 @@ How many moves does it take to reverse the string `123`?
## Best-First Search
## Best-First Search
*
Worklist:
…
*
Worklist:
Priority Queue
*
Priority:
…
*
Priority:
Distance traveled from the source plus the estimated distance to the destination
*
Guaranteed to find a path if one exists
*
Guaranteed to find a path if one exists
*
Not guaranteed to find a good path (but often does anyway)
*
Not guaranteed to find a good path (but often does anyway)
*
Can have good best- and average-case time efficiency
*
Can have good best- and average-case time efficiency
Worklist Backpointers
Worklist Backpointers
------------ --------------
------------------ --------------
(⊥, a, 0)
(⊥, a, 0) → 2 ✓ a → (⊥, a, 0)
(a, c, 7) → 9 ✓ c → (a, c, 7)
(c, e, 12) → 13 ✓ b → (c, b, 8)
(c, b, 8) → 9 ✓ e → (c, e, 12)
(b, e, 17) → 18 d → (e, d, 15)
(b, a, 12) → 14 ✓
(e, d, 15) → 15 ✓
(e, f, 14) → 16
Reversed Path
Reversed Path
----
----
…
d ← e ← c ← a
## A*
## A*
*
Worklist:
…
*
Worklist:
Priority Queue
*
Priority:
…
*
Priority:
Distance traveled from the source plus the estimated distance to the destination (estimated by an
*admissible*
heuristic)
*
Guaranteed to find a path with least weight
*
Guaranteed to find a path with least weight
*
Can have good best- and average-case time efficiency
*
Can have good best- and average-case time efficiency
## Recursive Depth-First Search (DFS)
## Recursive Depth-First Search (DFS)
*
Worklist:
…
*
Worklist:
Program Stack
Activation Frames Backpointers Returned
Activation Frames Backpointers Returned
----------------------------------- ------------ ------------------
----------------------------------- ------------ ------------------
edge = (⊥, a), incidence = …
edge = (⊥, a), incidence = (a, c) a → (⊥, a) [a, c, b, e, f, d]
edge = (a, c), incidence = (c, b) c → (a, c) [c, b, e, f, d]
edge = (c, b), incidence = (b, e) b → (c, b) [b, e, f, d]
edge = (b, a) ⊥
edge = (b, e), incidence = (e, f) e → (b, e) [e, f, d]
edge = (e, f), incidence = (f, d) f → (e, f) [f, d]
edge = (f, d) d → (f, d) [d]
Activation Frames Visited Returned
----------------------------------- ------------ ------------------
source = a, incidence = (a, c) a [a, c, b, e, f, d]
source = c, incidence = (c, b) c [c, b, e, f, d]
source = b, incidence = (b, e) b [b, e, f, d]
source = a ⊥
source = e, incidence = (e, f) e [e, f, d]
source = f, incidence = (f, d) f [f, d]
source = d d [d]
This diff is collapsed.
Click to expand it.
graph-search/src/features/search/search.js
+
53
−
4
View file @
6f17c38c
...
@@ -92,13 +92,62 @@ export function dijkstras(graph, source, destination) {
...
@@ -92,13 +92,62 @@ export function dijkstras(graph, source, destination) {
}
}
function
heuristic
(
vertex
,
destination
)
{
function
heuristic
(
vertex
,
destination
)
{
return
0
;
// TODO: stub
if
(
'
acf
'
.
includes
(
vertex
))
{
return
2
;
}
if
(
'
be
'
.
includes
(
vertex
))
{
return
1
;
}
return
0
;
}
}
export
function
bestFirst
(
graph
,
source
,
destination
)
{
export
function
bestFirst
(
graph
,
source
,
destination
)
{
return
undefined
;
// TODO: stub
const
backpointers
=
new
Map
();
const
worklist
=
new
PriorityQueue
();
worklist
.
insert
(
new
Edge
(
undefined
,
source
,
0
),
0
+
heuristic
(
source
,
destination
),
);
while
(
worklist
.
size
>
0
)
{
const
workitem
=
worklist
.
remove
();
if
(
backpointers
.
has
(
workitem
.
to
)
&&
backpointers
.
get
(
workitem
.
to
).
distance
<=
workitem
.
distance
)
{
continue
;
}
backpointers
.
set
(
workitem
.
to
,
workitem
);
if
(
workitem
.
to
===
destination
)
{
const
reversedPath
=
[];
for
(
let
current
=
destination
;
current
!==
undefined
;
current
=
backpointers
.
get
(
current
).
from
)
{
reversedPath
.
push
(
current
);
}
return
reversedPath
.
reverse
();
}
for
(
const
incidence
of
graph
.
getIncidences
(
workitem
.
to
))
{
worklist
.
insert
(
new
Edge
(
workitem
.
to
,
incidence
.
destination
,
workitem
.
distance
+
incidence
.
weight
),
workitem
.
distance
+
incidence
.
weight
+
heuristic
(
incidence
.
destination
,
destination
),
);
}
}
return
undefined
;
}
}
export
function
recursiveDFS
(
graph
,
source
,
destination
)
{
export
function
recursiveDFS
(
graph
,
source
,
destination
,
visited
=
new
Set
())
{
return
undefined
;
// TODO: stub
if
(
visited
.
has
(
source
))
{
return
undefined
;
}
visited
.
add
(
source
);
if
(
source
===
destination
)
{
return
[
source
];
}
for
(
const
incidence
of
[...
graph
.
getIncidences
(
source
)].
reverse
())
{
const
suffix
=
recursiveDFS
(
graph
,
incidence
.
destination
,
destination
,
visited
);
if
(
suffix
!==
undefined
)
{
return
[
source
,
...
suffix
];
}
}
return
undefined
;
}
}
This diff is collapsed.
Click to expand it.
graph-search/src/features/search/solution.js
+
1
−
1
View file @
6f17c38c
...
@@ -29,7 +29,7 @@ function formatSolution(solution) {
...
@@ -29,7 +29,7 @@ function formatSolution(solution) {
}
}
export
function
Solution
(
props
)
{
export
function
Solution
(
props
)
{
const
search
=
dijkstras
;
const
search
=
recursiveDFS
;
const
firstSolution
=
search
(
firstExample
,
'
a
'
,
'
d
'
);
const
firstSolution
=
search
(
firstExample
,
'
a
'
,
'
d
'
);
const
secondSolution
=
search
(
secondExample
,
'
123
'
,
'
321
'
);
const
secondSolution
=
search
(
secondExample
,
'
123
'
,
'
321
'
);
return
(
return
(
...
...
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