AlgoPrecision

Graph Visualizer

Graph Traversal

Trace BFS, DFS, and Dijkstra across a weighted directed graph with frontier, visited, settled, and distance state.

O(E log E)
4251731ABCDEF

Step Note

Sort edges by weight for Kruskal MST.

Details
function kruskal(graph: Graph) {
  const edges = graph.edges.sort((a, b) => a.weight - b.weight)
  const parent = makeSet()
  const mst = []
  for (const edge of edges) {
    if (find(edge.u) !== find(edge.v)) {
      union(edge.u, edge.v)
      mst.push(edge)
    }
  }
  return mst
}