CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 0 · Foundations

Lesson 0.2.5 · Item 6 of 91

Directed graphs

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. How many objects guarantee two in the same box with 6 boxes?
    Stuck? Peek at 0.2.4 Pigeonhole principle
    • n + 1 objects in n boxes ⇒ some box has ≥ 2.
    • N objects in k boxes ⇒ some box has ≥ ⌈N/k⌉. To force m in a box: (m−1)k + 1 objects.
    • Machine with n modes, input length ≥ n ⇒ some mode repeats ⇒ a loop in the input.

    Read lesson 0.2.4

  2. Why is “If Q then P” not equivalent to “If P then Q”?
    Stuck? Peek at 0.2.3 Proof techniques
    • P ⇒ Q false only when P true, Q false. Contrapositive ¬Q ⇒ ¬P ≡ original. Converse/inverse are NOT.
    • Contradiction: assume ¬S, derive the impossible. Used for “no machine can…” proofs.
    • Induction: base S(n₀) + step S(k) ⇒ S(k+1). Strong induction assumes all S(≤k).
    • One counterexample kills a “for all”. Examples never prove it.

    Read lesson 0.2.3

  3. If |A| = 3, |B| = 4, what is |A × B|?
    Stuck? Peek at 0.2.1 Sets
    • x ∈ A (element) vs X ⊆ A (subset). ∅ ⊆ every set. A = B ⇔ A ⊆ B and B ⊆ A.
    • A ∪ B, A ∩ B, A − B = A ∩ B̅, A̅ = U − A. De Morgan: (A∪B)̅ = A̅∩B̅, (A∩B)̅ = A̅∪B̅.
    • |A ∪ B| = |A| + |B| − |A ∩ B|. |A × B| = |A|·|B|. |P(A)| = 2^|A|.
    • |∅| = 0, |{∅}| = 1, P(∅) = {∅}.

    Read lesson 0.2.1

The Hook

A city map with one-way streets. Can you drive from the station to the stadium? Is there a route that brings you back to where you started? A machine diagram is exactly such a map: dots are modes, one-way arrows are moves labelled by the symbol read. Asking “is there any input this machine says yes to?” becomes “can you drive from the start to a yes-dot?”.

Predict First

In a map with 5 junctions, if a route visits 6 junctions (counting repeats), must it pass through some junction twice?

Core Concept

Prediction check

Answer: yes — pigeonhole: 6 visits, 5 junctions. A route that repeats a junction contains a cycle.

J1J3J4J2J3J5cycle: J3 → J4 → J2 → J3
6 visits, 5 junctions: some junction repeats, and the route between repeats is a cycle.

Directed graph

Directed graph G = (V, E): a set V of nodes (vertices) and a set E ⊆ V × V of edges. Edge (u, v) is an arrow from u to v.
  • Self-loop: an edge (u, u).
  • Out-degree of u: number of edges leaving u. In-degree: number entering.
  • Labelled graph: each edge carries a label (like a symbol a). Machine diagrams are labelled directed graphs.
Directed graph with labelled edges

States: 0, 1, 2, 3, 4

Transitions: 0>1: a; 1>2: b; 2>2: c; 2>3: b; 3>1: d; 0>4: e

Nodes 0–4 with labelled one-way edges; node 2 has a self-loop c. Machine diagrams will look exactly like this.

Paths and cycles

  • A path is a sequence of nodes v₀, v₁, …, vₖ with an edge from each to the next; its length is k (number of edges).
  • A cycle is a path of length ≥ 1 that starts and ends at the same node. A self-loop is a cycle of length 1.
  • In a labelled graph, a path spells a word: follow arrows a, b, b → the path spells “abb”.
flowplay illustration
A path spells a word.

Reachability

Node v is reachable from u if there is a path from u to v (u is always reachable from itself by the length-0 path).

To find everything reachable from u: start with {u}; repeatedly add any node that has an edge from a node already found; stop when nothing new is added. (This is breadth-first search.) It always finishes because there are finitely many nodes.

Fact: if v is reachable from u in a graph with n nodes, there is a path of length ≤ n − 1 (drop any cycle from a longer path).

Reachability by growing the found set (breadth-first search).
Reachability by growing the found set (breadth-first search).

Cycles and “infinitely many routes”

If a cycle is reachable from u and the cycle can reach v, then there are routes from u to v of unboundedly many lengths (go around the cycle as often as you like). If no cycle lies on any u→v route, there are only finitely many routes. Later, “does this machine accept infinitely many inputs?” will become exactly this question.

0 → 3
a cycle (2 → 2, or 1 → 2 → 3 → 1) lies on the way: infinitely many paths
0 → 4
no cycle on the way: exactly one path
Infinitely many routes ⇔ a usable cycle on the way.

Putting it together

  • A directed graph is nodes plus one-way edges; labelled paths spell words.
  • Reachable set: grow from the start until nothing new appears.
  • Infinitely many paths from u to v exactly when a cycle lies on some route between them.

Visual

        a           b
   ┌──► 1 ──────► 2 ─────┐        edges: (0,1) (1,2) (2,3) (3,1) (2,2) (0,4)
   │              │ ▲    ▼        2 has a self-loop labelled c
   0              └─┘c   3        cycle: 1 → 2 → 3 → 1
   │                     │        reachable from 0: {0,1,2,3,4}
   └──► 4         1 ◄────┘ d      reachable from 4: {4}  (no edges out)
      e
 path 0→1→2→2→3 spells "a b c b"

Worked Examples

Easy

In the graph above, list the out-degree of node 2 and the in-degree of node 1.

Show the full solution

Out of 2: edges (2,3) and (2,2) ⇒ 2. Into 1: (0,1) and (3,1) ⇒ 2.

Medium

Is node 0 reachable from node 3?

Show the full solution

From 3 you go to 1, from 1 to 2, from 2 to 3 or 2. Nothing ever leads to 0. No. Reachability is one-way: 3 is reachable from 0 but not vice versa.

GATE-level

In the same graph, are there infinitely many different paths from 0 to 3? From 0 to 4?

Show the full solution

0 → 3: yes — routes can loop 2→2 or 1→2→3→1 any number of times before ending at 3, so infinitely many. 0 → 4: exactly one path (0,4), because no cycle lies on a route to 4. The test is “does some cycle lie on a route between them?”.

GATE Traps

  • Directed edges are one-way: reachable(u→v) does not imply reachable(v→u).
  • Path length counts edges, not nodes.
  • A node with no outgoing edges is a dead end — keep this picture for “dead” modes in machines.

Memory Hook

One-way streets: you can reach the stadium from the station without being able to drive back.

Connect the Dots

Math toolkit done: sets, functions, equivalence classes, proofs, pigeonhole, graphs. Next we build the raw material machines read: symbols and strings.

Self-Test

  1. MCQ

    In a directed graph with 6 nodes, if v is reachable from u, there is always a path from u to v of length at most:

    1. 3
    2. 5
    3. 6
    4. 36
  2. MSQ

    Graph: edges (1,2), (2,3), (3,2), (3,4). Which are true?

    1. 4 is reachable from 1
    2. 1 is reachable from 4
    3. There is a cycle
    4. There are infinitely many paths from 1 to 4
  3. NAT

    A directed graph on 3 nodes may have any subset of the 9 possible edges (self-loops allowed). How many such graphs are there?

    Write your answer before opening the key.

  4. MCQ

    A path in a labelled graph follows edges labelled 0, 1, 1, 0. What word does it spell and what is its length?

    1. 0110, length 4
    2. 0110, length 5
    3. 01, length 2
    4. 0110, length 3

Rule Card

  • Directed graph (V, E ⊆ V×V); self-loop; in/out-degree; labelled edges spell words along paths.
  • Path length = #edges. Cycle = path returning to start. Self-loop = cycle of length 1.
  • Reachable set: grow from u until nothing new (BFS). Shortest path in n-node graph ≤ n−1 edges.
  • Infinitely many u→v paths ⇔ some cycle lies on a u→v route.

Concept Ledger

  • directed graph, node, edge
  • self-loop
  • in-degree, out-degree
  • labelled edge
  • path, path length
  • cycle
  • reachability (BFS)
  • dead end (no outgoing edges)

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. 7.
  2. It is the converse; it can be false while the original is true.
  3. 12.

Self-test answers

  1. B — A shortest path never repeats a node, so it visits at most 6 nodes = 5 edges.
    • A: Too small; a chain of 6 nodes needs 5 edges.
    • C: 6 edges would repeat a node.
    • D: Far too large.
  2. ACD — 1→2→3→4; cycle 2→3→2 lies on that route, so infinitely many paths.
    • B: Nothing leaves 4.
  3. 512 — Edges ⊆ V × V with |V × V| = 9 ⇒ 2⁹ = 512.
  4. A — One label per edge; 4 edges.
    • B: Length counts edges (4), not nodes (5).
    • C: All labels are spelled.
    • D: There are 4 edges.

Flashcards

  1. When are there infinitely many paths from u to v?
    Reveal answerWhen some cycle lies on a route from u to v.
  2. Longest shortest path in an n-node graph?
    Reveal answern − 1 edges.
  3. Path length counts…?
    Reveal answerEdges.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.