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
3f4515c6
Commit
3f4515c6
authored
2 years ago
by
Brady James Garvin
Browse files
Options
Downloads
Patches
Plain Diff
Designed, traced, and coded DFS.
parent
6500808f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
graph-search/notes.md
+20
-5
20 additions, 5 deletions
graph-search/notes.md
graph-search/src/features/search/search.js
+25
-19
25 additions, 19 deletions
graph-search/src/features/search/search.js
with
45 additions
and
24 deletions
graph-search/notes.md
+
20
−
5
View file @
3f4515c6
...
@@ -18,10 +18,16 @@ but it cannot move left, because trying would move it past the end of the string
...
@@ -18,10 +18,16 @@ but it cannot move left, because trying would move it past the end of the string
How many moves does it take to reverse the string
`123`
?
How many moves does it take to reverse the string
`123`
?
> Four, in one of two ways:
> `123` → `213` → `132` → `312` → `321`
> `123` → `213` → `231` → `312` → `321`
# Problem 2:
# Problem 2:
In the weighted directed graph on the whiteboard, what is the shortest path from
`a`
to
`d`
?
In the weighted directed graph on the whiteboard, what is the shortest path from
`a`
to
`d`
?
> a → c → e → f → d (total weight of 14)
---------------------------------------------------------------------
---------------------------------------------------------------------
# Worklist Algorithms for Graph Search
# Worklist Algorithms for Graph Search
...
@@ -31,18 +37,27 @@ In the weighted directed graph on the whiteboard, what is the shortest path from
...
@@ -31,18 +37,27 @@ In the weighted directed graph on the whiteboard, what is the shortest path from
## Depth-First Search (DFS)
## Depth-First Search (DFS)
*
Worklist:
…
*
Worklist:
Stack
*
Guaranteed to find a path if one exists
*
Guaranteed to find a path if one exists
*
Not guaranteed to find a good path
*
Not guaranteed to find a good path
*
Can be made very memory efficient (topic for Thursday)
*
Can be made very memory efficient (topic for Thursday)
Worklist Backpointers
Worklist Backpointers
-------- ------------
-------- ------------
(⊥, a)
(⊥, a) ✓ a → (⊥, a)
(a, c) ✓ c → (a, c)
(c, e) b → (c, b)
(c, b) ✓ e → (b, e)
(b, e) ✓ f → (e, f)
(b, a) ✓ d → (f, d)
(e, d)
(e, f) ✓
(f, c)
(f, d) ✓
Reversed Path
Reversed Path
----
----
…
d ← f ← e ← b ← c ← a
## Breadth-First Search (BFS)
## Breadth-First Search (BFS)
...
...
This diff is collapsed.
Click to expand it.
graph-search/src/features/search/search.js
+
25
−
19
View file @
3f4515c6
...
@@ -9,25 +9,31 @@ class Edge {
...
@@ -9,25 +9,31 @@ class Edge {
}
}
export
function
dfs
(
graph
,
source
,
destination
)
{
export
function
dfs
(
graph
,
source
,
destination
)
{
// // SEARCH:
// SEARCH:
// // base case
const
backpointers
=
new
Map
();
// // initiation
const
worklist
=
[];
// while (…) { // termination condition
worklist
.
push
(
new
Edge
(
undefined
,
source
));
// // consecution (part i)
while
(
worklist
.
length
>
0
)
{
// // action
const
workitem
=
worklist
.
pop
();
// if (…) { // early termination conditional
if
(
backpointers
.
has
(
workitem
.
to
))
{
// // POSTPROCESSING:
continue
;
// // base case
}
// for(…; // initiation
backpointers
.
set
(
workitem
.
to
,
workitem
);
// …; // termination condition
if
(
workitem
.
to
===
destination
)
{
// …) { // consecution
// POSTPROCESSING:
// // action
const
reversedPath
=
[];
// }
for
(
let
current
=
destination
;
// return …;
current
!==
undefined
;
// }
current
=
backpointers
.
get
(
current
).
from
)
{
// // consecution (part ii)
reversedPath
.
push
(
current
);
// }
}
return
undefined
;
// TODO: stub
return
reversedPath
.
reverse
();
}
for
(
const
incidence
of
graph
.
getIncidences
(
workitem
.
to
))
{
worklist
.
push
(
new
Edge
(
workitem
.
to
,
incidence
.
destination
));
}
}
return
undefined
;
}
}
export
function
bfs
(
graph
,
source
,
destination
)
{
export
function
bfs
(
graph
,
source
,
destination
)
{
...
...
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