Graphs & BFS/DFS

Explore graph data structures and traverse them with Breadth-First and Depth-First Search animations.

Category: Algorithms · v1.0.0
Step 1 of 6

Step 1: What is a Graph?

A Graph is a data structure made up of vertices (also called nodes) connected by edges (also called links). Graphs model relationships — friendships in social networks, roads between cities, links between web pages, and much more.

Types of Graphs:

  • Directed vs Undirected — In a directed graph, edges have a direction (A → B is different from B → A). In an undirected graph, edges go both ways.
  • Weighted vs Unweighted — Weighted graphs assign a cost or distance to each edge. Unweighted graphs treat all edges equally.
  • Cyclic vs Acyclic — A cyclic graph contains at least one cycle (a path that starts and ends at the same vertex). A DAG (Directed Acyclic Graph) has no cycles.

Key Terms:

  • Vertex (Node) — A point in the graph
  • Edge — A connection between two vertices
  • Adjacent — Two vertices connected by an edge
  • Degree — The number of edges connected to a vertex
  • Path — A sequence of vertices connected by edges
  • Cycle — A path that starts and ends at the same vertex

Below is an example undirected graph with 6 nodes (A–F):

About This Lab

Graphs model relationships between objects — social networks, maps, web pages, and more. In this lab, you'll learn about vertices and edges, directed vs undirected graphs, build graphs interactively, visualize BFS and DFS traversals step by step, and understand when to use each algorithm.

How It Works

  1. Read each step's explanation of graph concepts
  2. Build graphs by adding nodes and edges
  3. Watch BFS and DFS traverse the graph
  4. Compare the two traversal strategies
  5. Complete the quiz to test your understanding