Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@types/node": "^24.7.1",
"@types/react": "^19.1.16",
"@types/react-dom": "^19.1.9",
"@vitejs/plugin-react": "^5.0.4",
"@vitejs/plugin-react": "^5.1.0",
"autoprefixer": "^10.4.21",
"eslint": "^9.36.0",
"eslint-plugin-react-hooks": "^5.2.0",
Expand Down
87 changes: 87 additions & 0 deletions src/algorithms/graph/topoSortDFS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
export default function runTopologicalSortDFS(nodes, edges) {
const adjList = {};
const visited = {};
const topoOrder = [];
const steps = [];
let hasCycle = false;

// Initialize adjacency list
nodes.forEach(node => {
adjList[node] = [];
visited[node] = 0; // 0 = unvisited, 1 = visiting, 2 = visited
});

// Build graph
edges.forEach(({ from, to }) => {
adjList[from].push(to);
});

function dfs(node) {
if (hasCycle) return; // stop early if cycle found
visited[node] = 1;
steps.push({
type: "visit",
node,
status: "visiting",
message: `Visiting ${node}`
});

for (const neighbor of adjList[node]) {
if (visited[neighbor] === 0) {
steps.push({
type: "exploreEdge",
from: node,
to: neighbor,
message: `Exploring edge ${node} → ${neighbor}`
});
dfs(neighbor);
} else if (visited[neighbor] === 1) {
// Back edge → cycle detected
hasCycle = true;
steps.push({
type: "cycleDetected",
from: node,
to: neighbor,
message: `Cycle detected via ${node} → ${neighbor}`
});
}
}

visited[node] = 2;
topoOrder.push(node);
steps.push({
type: "addToTopoOrder",
node,
topoOrder: [...topoOrder],
message: `Added ${node} to topo order`
});
}

// Run DFS for all unvisited nodes
for (const node of nodes) {
if (visited[node] === 0) {
steps.push({
type: "startDFS",
node,
message: `Starting DFS from ${node}`
});
dfs(node);
}
}

if (!hasCycle) {
topoOrder.reverse(); // reverse for correct topological order
steps.push({
type: "finalOrder",
topoOrder: [...topoOrder],
message: `Topological sort completed successfully`
});
} else {
steps.push({
type: "finalCycle",
message: `Topological sort failed — cycle exists in graph`
});
}

return steps;
}
73 changes: 73 additions & 0 deletions src/algorithms/graph/topoSortKahn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
export default function runTopologicalSort(nodes, edges) {
const inDegree = {};
const adjList = {};

// Initialize in-degree and adjacency list
nodes.forEach(node => {
inDegree[node] = 0;
adjList[node] = [];
});

// Build graph
edges.forEach(({ from, to }) => {
adjList[from].push(to);
inDegree[to]++;
});

const steps = [];
const queue = [];
const topoOrder = [];

// Enqueue nodes with 0 in-degree
for (const node of nodes) {
if (inDegree[node] === 0) {
queue.push(node);
steps.push({
type: "enqueue",
node,
reason: "in-degree 0"
});
}
}

// Process nodes
while (queue.length > 0) {
const current = queue.shift();
topoOrder.push(current);

steps.push({
type: "visit",
node: current,
topoOrder: [...topoOrder]
});

for (const neighbor of adjList[current]) {
inDegree[neighbor]--;
steps.push({
type: "decrementInDegree",
from: current,
to: neighbor,
newInDegree: inDegree[neighbor]
});

if (inDegree[neighbor] === 0) {
queue.push(neighbor);
steps.push({
type: "enqueue",
node: neighbor,
reason: "in-degree became 0"
});
}
}
}

// Check for cycles (if topoOrder doesn't include all nodes)
if (topoOrder.length !== nodes.length) {
steps.push({
type: "cycleDetected",
remainingNodes: nodes.filter(n => !topoOrder.includes(n))
});
}

return steps;
}
Loading
Loading