Algorithm Visualizations
Hash table separate chaining: Insert keys, delete a key, insert another key, then search. Resolve collisions with bucket chains. Explore a worked example,…
javascript
function algorithm(a, n, deleteKey, insertKey, target, output) {
let present = Array(256).fill(0);
let head = Array(256).fill(0); let key = Array(256).fill(0); let next = Array(256).fill(0); let used = 0; let u = 0; let previous = 0; let bucket = 0;
let value = 0; let deleting = 0; let found = 0; let length = 0; let comparisons = 0;
for (let operation = 0; operation < n + 2; operation++) {
deleting = 0; value = insertKey;
if (operation < n) { value = a[operation]; } else if (operation == n) { value = deleteKey; deleting = 1; }
bucket = value % 17; u = head[bucket]; previous = 0;
while (u != 0 && key[u] != value) { previous = u; u = next[u]; }
if (deleting != 0 && u != 0) {
if (previous == 0) { head[bucket] = next[u]; } else { next[previous] = next[u]; } present[value] = 0;
Nov 16, 2026
javascript
Algorithm Visualizations
Held-Karp traveling salesperson: Store the cheapest path for each visited subset and final vertex, then close the tour back to vertex 0. Reconstruct an exact…
javascript
function algorithm(costs, n, output) {
let dist = Array(256).fill(0); let parent = Array(256).fill(0); let states = 1 << n; let next = 0; let candidate = 0; let best = 1000000; let end = -1; let mask = 0; let u = 0; let previous = 0;
for (let i = 0; i < states * n; i++) { dist[i] = 1000000; parent[i] = -1; }
dist[n] = 0;
for (mask = 1; mask < states; mask++) {
if ((mask & 1) == 0) { continue; }
for (u = 0; u < n; u++) {
if (dist[mask * n + u] == 1000000) { continue; }
for (let v = 1; v < n; v++) {
if ((mask & (1 << v)) == 0 && costs[u * n + v] >= 0) {
next = (mask | (1 << v)) * n + v; candidate = dist[mask * n + u] + costs[u * n + v];
if (candidate < dist[next]) { dist[next] = candidate; parent[next] = u; }
Nov 16, 2026
javascript
Algorithm Visualizations
Optimal binary search tree: Minimize weighted search cost over ordered keys, including frequencies for unsuccessful searches in every gap. Try each root for…
javascript
function algorithm(p, q, n, output) {
let cost = Array(256).fill(0); let weight = Array(256).fill(0); let s = n + 1; let j = 0; let candidate = 0;
for (let i = 0; i < s * s; i++) { output[i] = -1; }
for (let i = 0; i <= n; i++) { cost[i * s + i] = q[i]; weight[i * s + i] = q[i]; }
for (let length = 1; length <= n; length++) {
for (let i = 0; i + length <= n; i++) {
j = i + length; weight[i * s + j] = weight[i * s + j - 1] + p[j - 1] + q[j]; cost[i * s + j] = 1000000;
for (let r = i; r < j; r++) {
candidate = cost[i * s + r] + cost[(r + 1) * s + j] + weight[i * s + j];
if (candidate < cost[i * s + j]) { cost[i * s + j] = candidate; output[i * s + j] = r; }
}
}
Nov 16, 2026
javascript
Algorithm Visualizations
Hierholzer directed Euler trail: Consume each directed edge once, then splice the trail by reversing the backtracking order. Reject invalid degrees or…
javascript
function algorithm(from, to, weight, edges, vertices, output) {
let incoming = Array(256).fill(0); let outgoing = Array(256).fill(0); let cursor = Array(256).fill(0); let stack = Array(256).fill(0); let reversed = Array(256).fill(0);
let u = 0; let index = 0; let delta = 0; let plus = 0; let minus = 0; let start = 0; let top = 0; let length = 0;
for (index = 0; index < edges; index++) { outgoing[from[index]]++; incoming[to[index]]++; }
for (u = 0; u < vertices; u++) {
delta = outgoing[u] - incoming[u];
if (delta == 1) { plus++; start = u; } else if (delta == -1) { minus++; } else if (delta != 0) { return -1; }
}
if (!((plus == 0 && minus == 0) || (plus == 1 && minus == 1))) { return -1; }
if (plus == 0) { for (u = 0; u < vertices; u++) { if (outgoing[u] > 0) { start = u; break; } } }
stack[top++] = start;
while (top > 0) {
Nov 16, 2026
javascript
Algorithm Visualizations
Articulation points (low-link DFS): Treat each input edge as undirected. Skip only the parent edge, preserving parallel edges; compare discovery times and low…
javascript
function algorithm(from, to, weight, edges, vertices, output) {
let disc = Array(256).fill(0); let low = Array(256).fill(0); let parent = Array(256).fill(0); let parentEdge = Array(256).fill(0); let cursor = Array(256).fill(0); let children = Array(256).fill(0); let dfs = Array(256).fill(0); let cut = Array(256).fill(0); let bridge = Array(256).fill(0);
let root = 0; let u = 0; let v = 0; let index = 0; let top = 0; let clock = 0; let count = 0;
for (u = 0; u < vertices; u++) { disc[u] = -1; parent[u] = -1; parentEdge[u] = -1; }
for (root = 0; root < vertices; root++) {
if (disc[root] >= 0) { continue; }
disc[root] = clock; low[root] = clock++; dfs[top++] = root;
while (top > 0) {
u = dfs[top - 1]; index = cursor[u];
while (index < edges && (index == parentEdge[u] || (from[index] != u && to[index] != u))) { index++; }
cursor[u] = index + 1;
if (index < edges) {
Nov 16, 2026
javascript