Skip to content

Traversing a graph

Original Graph

digraph G {
  rankdir=TB; // top-to-bottom layout

  // --- Level 1 ---
  subgraph cluster_Level1 {
    label="Level 1";
    S [label="S"];
  }

  // --- Level 2 ---
  subgraph cluster_Level2 {
    label="Level 2";
    C [label="C"];
    A [label="A"];
  }

  // --- Level 3 ---
  subgraph cluster_Level3 {
    label="Level 3";
    B [label="B"];
    D [label="D"];
  }

  // --- Level 4 ---
  subgraph cluster_Level4 {
    label="Level 4";
    E [label="E"];
    F [label="F"];
  }

  // --- Level 5 ---
  subgraph cluster_Level5 {
    label="Level 5";
    G [label="G"];
  }

  // Force nodes in each level to share the same rank
  { rank=same; S; }
  { rank=same; C; A; }
  { rank=same; B; D; }
  { rank=same; E; F; }
  { rank=same; G; }

  // Directed edges (matching the graph structure)
  S -> C;
  S -> A;
  C -> B;
  C -> D;
  C -> A;
  A -> B;
  B -> E;
  B -> F;
  D -> F;
  E -> G;
  F -> G;
}

DFS Path

digraph G {
  rankdir=TB; // top-to-bottom layout

  // --- Level 1 ---
  subgraph cluster_Level1 {
    label="Level 1";
    S [label="S"];
  }

  // --- Level 2 ---
  subgraph cluster_Level2 {
    label="Level 2";
    C [label="C"];
    A [label="A"];
  }

  // --- Level 3 ---
  subgraph cluster_Level3 {
    label="Level 3";
    B [label="B"];
    D [label="D"];
  }

  // --- Level 4 ---
  subgraph cluster_Level4 {
    label="Level 4";
    E [label="E"];
    F [label="F"];
  }

  // --- Level 5 ---
  subgraph cluster_Level5 {
    label="Level 5";
    G [label="G"];
  }

  // Force nodes in each level to share the same rank
  { rank=same; S; }
  { rank=same; C; A; }
  { rank=same; B; D; }
  { rank=same; E; F; }
  { rank=same; G; }

  // Directed edges (matching the graph structure)

  S -> C [color="red", penwidth=2, style="bold"];
  S -> A;
  C -> B;
  C -> D [color="red", penwidth=2, style="bold"];
  C -> A;
  A -> B;
  B -> E;
  B -> F;
  D -> F [color="red", penwidth=2, style="bold"];
  E -> G;
  F -> G [color="red", penwidth=2, style="bold"];
}

BFS Path

digraph G {
  rankdir=TB; // top-to-bottom layout

  // --- Level 1 ---
  subgraph cluster_Level1 {
    label="Level 1";
    S [label="S"];
  }

  // --- Level 2 ---
  subgraph cluster_Level2 {
    label="Level 2";
    C [label="C"];
    A [label="A"];
  }

  // --- Level 3 ---
  subgraph cluster_Level3 {
    label="Level 3";
    B [label="B"];
    D [label="D"];
  }

  // --- Level 4 ---
  subgraph cluster_Level4 {
    label="Level 4";
    E [label="E"];
    F [label="F"];
  }

  // --- Level 5 ---
  subgraph cluster_Level5 {
    label="Level 5";
    G [label="G"];
  }

  // Force nodes in each level to share the same rank
  { rank=same; S; }
  { rank=same; C; A; }
  { rank=same; B; D; }
  { rank=same; E; F; }
  { rank=same; G; }

  // Directed edges (matching the graph structure)
  S -> C [color="red", penwidth=2, style="bold"];
  S -> A;
  C -> B [color="red", penwidth=2, style="bold"];
  C -> D;
  C -> A;
  A -> B;
  B -> E [color="red", penwidth=2, style="bold"];
  B -> F;
  D -> F;
  E -> G [color="red", penwidth=2, style="bold"];
  F -> G;
}