Module 01 · DSA · The toolbox · about 16 min to read
Continue where you left off ×
Graphs (BFS, DFS, Dijkstra, topological sort)
Graphs · Open board
Dots and the lines between them, the shape this whole lesson is built on. The same picture backs routing in Networking (How a parcel finds any house on earth ), the graph data model in Data Models and Query Languages , and the dataflow DAGs in Batch Processing .
Before you start. You need only arrays: a numbered list where you can jump straight to slot i. Nothing about graphs is assumed here, every new word below is shown doing something before it gets named.
The idea in 30 seconds
Breadth-first search visits ring by ring and finds the fewest hops. Depth-first search dives down one road and finds a path, not necessarily the shortest, the gap can be a hundred-times-longer path, in about the same time.
Dijkstra’s algorithm always expands whichever unvisited stop is cheapest, and needs every cost non-negative.
A topological sort orders tasks so every dependency comes first. A loop among the dependencies means no order exists.
Answering “are these already joined” fast, and building cheapest-first while skipping loops, connects everything for the least possible cost, both ideas get their names next.
Read the story
Lesson 7 of 96 in DSA
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 1735: Königsberg, the walk nobody could finish One-way streets and toll roads Which runner finds it first The cheapest path, not the fewest hops An order that respects what comes first Connecting every landmass as cheaply as possible Terms it explains: vertices, edges, graph, breadth-first search, depth-first search, Dijkstra's algorithm, topological sort, directed, undirected, weighted, queue, stack, priority queue, directed acyclic graph, cycle, sparse, dense, {A-B, B-C, C-D}, A*, adjacency list, adjacency matrix, Bellman-Ford, BFS, DAG, DFS, Dijkstra, directed vs undirected, in-degree, MST, relax an edge, sparse vs dense, union-find, vertex/edge.
Review DSA
Mark as read Saved in this browser, and to your account when you sign in.