TRT
Sign in

Advanced · Module 13 · Lesson 4 of 5

Cycles and connected components

One walk over a graph can split it into connected pieces, tell whether it is a tree, and find cycles. Directed graphs need one extra idea: remembering which vertices are on the current path.

Pro — members onlyAbout 14 minutes

By the end you can

  • Count and label the connected components of an undirected graph
  • Detect a cycle in an undirected graph, and decide whether a graph is a tree
  • Detect a cycle in a directed graph with three colors, and explain why a visited set isn't enough

Builds on Depth-first search.

01 · A question first

A package manager must refuse to install packages whose dependencies loop back on themselves. A network tool must report how many separate networks are plugged together. Both come down to one walk over a graph. What does that walk need to remember?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Connected components
  • Cycles in undirected graphs
  • Cycles in directed graphs
  • Strongly connected components
  • Where this shows up
  • Pseudocode & Multi-Language Implementations (C++, Java, Python)
  • Complexity Derivation, Edge Cases & Common Mistakes

03 · Practice

Something wrong with this lesson? Report a problem