Data Structures

Graphs

What a graph data structure is: nodes, edges and cycles, why a tree can't hold data with two parents, how to store a graph in code and where graphs are used.

  • 6 min read
  • 5 questions
  • +35 XP
Transcript

Graphs, explained by cats. Kitty plans a party. Needs to invite friends. Fluffy knows Mittens. Mittens knows Whiskers. Whiskers knows Shadow. Shadow knows Fluffy. Wait, that’s a loop. Lines everywhere. Connections in all directions. Kitty tries to draw a tree.

One parent. One path. Nice and tidy. But Fluffy knows both Mittens and Shadow. Two parents. Tree breaks. Kitty’s brain melts. Old Cat says, ‘You need a graph.’ Not a bar chart. A graph data structure. Each cat is a node.

Each friendship, an edge. Connections can go anywhere. Loops allowed. Now it makes sense. Facebook is a graph cats and friendships. Google Maps is a graph places and roads. LinkedIn? A graph of who knows who. Recommendations? Graphs of shared tuna tastes. Graphs are everywhere. Messy. Connected. Real. Trees are neat. Graphs are life.