15
Lesson 26 of 44

Advanced graphs and trees

Many real problems are networks: maps, dependencies, friendships, hierarchies. In this lesson you will go from walking graphs to solving problems with them: tree traversals, common ancestor, shortest weighted paths (Dijkstra), topological order, components with union-find, minimum spanning trees, bipartite graphs and tries. You will learn to choose the right structure and to reason about its cost.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

How to traverse a binary tree

A binary tree can be traversed in four usual ways. The first three are recursive and differ only in when the node is visited with…

Theory
Read more

Weighted graphs and Dijkstra

When edges have a cost (kilometers, minutes, euros), the path with the fewest edges is no longer necessarily the best. For…

Theory
Read more

Topological order and cycles

A directed acyclic graph (DAG) represents dependencies: tasks that must be done before others, courses with prerequisites…

Theory
Read more

Union-find and minimum spanning tree

The union-find structure (or disjoint sets) answers two questions very fast: “are these two elements in the same group?” and…

Theory
Read more

Bipartite graphs and tries

A graph is bipartite if its nodes can be divided into two groups with no edges inside each group (for example, people and jobs)…

Theory
Read more

Practice exercises 10

Binary tree traversals

There are three classic ways to traverse a binary tree recursively: preorder (node, left, right), inorder (left, node, right) and…

Exercise Medium
Solve it

Level-order traversal

In the level-order traversal the root is visited first, then its children, then the grandchildren, and so on. It is done with a…

Exercise Medium
Solve it

Lowest common ancestor

The lowest common ancestor of two nodes is the deepest node that has both as descendants (a node counts as a descendant of…

Exercise Hard
Solve it

Weighted shortest path (Dijkstra)

In a graph with weights the path with fewer edges is not necessarily the cheapest. Dijkstra ’s algorithm uses a priority queue to…

Exercise Hard
Solve it

Topological order

If some tasks depend on others, a topological order places them so that each task appears after the ones it needs. It only exists…

Exercise Hard
Solve it

Merging groups

The union-find structure keeps disjoint groups: each element points to a “parent” and the root identifies the group. Merging two…

Exercise Hard
Solve it

Minimum spanning tree

A minimum spanning tree connects all the nodes at the lowest possible total cost and without cycles. Kruskal ’s algorithm sorts…

Exercise Hard
Solve it

Is it bipartite?

A graph is bipartite if its nodes can be painted with two colours so that no edge joins two nodes of the same colour (equivalent…

Exercise Hard
Solve it

Autocomplete with a trie

A trie (prefix tree) stores words letter by letter in nested dictionaries: words sharing a prefix share a path. It is the typical…

Exercise Hard
Solve it

Validate a search tree

A binary tree is a BST if, for each node, all the values of its left subtree are smaller and all those of the right one are…

Exercise Hard
Solve it