Skip to content
BENIBIX Learn

Graph (Data Structure)

Development🧮 Algorithms & Data StructuresData Structures

Also known as Graph, Network Graph, Nodes and Edges

Nodes joined by edges, where any node can link to any other.

Think of it like

Think of it like a map of towns joined by roads. Any town can link to any other.

Example

Emma's social app stores who follows whom as a graph. Finding friends of friends means walking two edges out from her.

Why it matters

Road trips, friend lists and web links are all graph problems underneath.

Definition

A graph is a set of nodes joined by lines called edges. Unlike a tree, any node can link to any other, and the links can form loops. Edges can have a direction, like a one-way street, and a weight, like a distance. Road maps, friend networks and links between web pages are all graphs. Many hard problems become easier once you see them as one.

Common mistake

Walking a graph without marking the nodes you have seen. Because of loops, the walk can go round forever.

Key takeaways

  1. 1
    Nodes and edges. Things and links.
  2. 2
    Loops allowed. Unlike a tree.
  3. 3
    Direction and weight. One-way, distance, cost.

Compare