Skip to content
Snippets Groups Projects
Commit 20bf45fb authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Recorded work from Wednesday.

parent 6d326d07
Branches
No related tags found
No related merge requests found
...@@ -60,30 +60,46 @@ How many moves does it take to reverse the string `123`? ...@@ -60,30 +60,46 @@ How many moves does it take to reverse the string `123`?
## Breadth-First Search (BFS) ## Breadth-First Search (BFS)
* Worklist: * Worklist: Queue
* Guaranteed to find a path with the fewest edges * Guaranteed to find a path with the fewest edges
Worklist Backpointers Worklist Backpointers
-------- ------------ -------- ------------
(⊥, a) (⊥, a) ✓ a → (⊥, a)
(a, c) ✓ c → (a, c)
(c, e) ✓ e → (c, e)
(c, b) ✓ b → (c, b)
(e, d) ✓ d → (e, d)
(e, f)
(b, e)
(b, a)
Reversed Path Reversed Path
---- ----
d ← e ← c ← a
## Dijkstra's Algorithm ## Dijkstra's Algorithm
* Worklist: * Worklist: Priority Queue
* Priority: * Priority: Distance traveled from the source
* Guaranteed to find a path with least weight * Guaranteed to find a path with least weight
Worklist Backpointers Worklist Backpointers
------------ -------------- ------------ --------------
(⊥, a, 0) (⊥, a, 0) ✓ a → (⊥, a, 0)
(a, c, 7) ✓ c → (a, c, 7)
(c, e, 12) ✓ b → (c, b, 8)
(c, b, 8) ✓ e → (c, e, 12)
(b, e, 17) f → (e, f, 14)
(b, a, 12) ✓ d → (f, d, 14)
(e, d, 15)
(e, f, 14) ✓
(f, c, 22)
(f, d, 14) ✓
Reversed Path Reversed Path
---- ----
d ← f ← e ← c ← a
## Best-First Search ## Best-First Search
......
...@@ -9,15 +9,91 @@ class Edge { ...@@ -9,15 +9,91 @@ class Edge {
} }
export function dfs(graph, source, destination) { export function dfs(graph, source, destination) {
return undefined; // TODO: stub const backpointers = new Map();
const worklist = [];
worklist.push(new Edge(undefined, source));
while (worklist.length > 0) {
const workitem = worklist.pop();
if (backpointers.has(workitem.to)) {
continue;
}
backpointers.set(workitem.to, workitem);
if (workitem.to === destination) {
const reversedPath = [];
for (let current = workitem.to;
current !== undefined;
current = backpointers.get(current).from) {
reversedPath.push(current);
}
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) {
return undefined; // TODO: stub const backpointers = new Map();
const worklist = new Queue();
worklist.insert(new Edge(undefined, source));
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to)) {
continue;
}
backpointers.set(workitem.to, workitem);
if (workitem.to === destination) {
const reversedPath = [];
for (let current = workitem.to;
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));
}
}
return undefined;
} }
export function dijkstras(graph, source, destination) { export function dijkstras(graph, source, destination) {
return undefined; // TODO: stub const backpointers = new Map();
const worklist = new PriorityQueue();
worklist.insert(
new Edge(undefined, source, 0),
0,
);
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to)) {
continue;
}
backpointers.set(workitem.to, workitem);
if (workitem.to === destination) {
const reversedPath = [];
for (let current = workitem.to;
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,
);
}
}
return undefined;
} }
function heuristic(vertex, destination) { function heuristic(vertex, destination) {
......
...@@ -29,7 +29,7 @@ function formatSolution(solution) { ...@@ -29,7 +29,7 @@ function formatSolution(solution) {
} }
export function Solution(props) { export function Solution(props) {
const search = dfs; const search = dijkstras;
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 (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment