Graph Basics
So you want to learn about graphs? Trust me, this is where things get really interesting. A graph is basically a collection of points connected by lines — like a spider web or a social network where people are friends with each other.
In computer science terms, those points are called vertices (or nodes), and the lines connecting them are called edges. Think of it like a map where cities are vertices and roads between them are edges.
Directed vs Undirected
Here's the thing — not all graphs are created equal. An undirected graph is like a two-way street. If Alice is friends with Bob, then Bob is friends with Alice. The relationship goes both ways.
A directed graph (or digraph) is more like Twitter. If you follow someone, they don't automatically follow you back. The edges have a direction — they point from one vertex to another.
const graph = {
Alice: ["Bob", "Charlie"],
Bob: ["Alice"],
Charlie: ["Bob"]
};
Weighted vs Unweighted
Another flavor of graphs involves weights. An unweighted graph just tells you whether a connection exists — like "these two people are friends or they're not."
A weighted graph adds extra information to each edge. Think of Google Maps — the edge between two cities isn't just "there's a road," it's "this road takes 45 minutes and costs $12 in gas." That's a weighted edge!
const weightedGraph = {
NewYork: [{ city: "Boston", distance: 215 }],
Boston: [{ city: "NewYork", distance: 215 }]
};
Try it Yourself →
Real-World Examples
Graphs are everywhere once you know what to look for. Social networks are graphs — people are vertices and friendships are edges. The internet itself is a giant graph of websites connected by hyperlinks.
Maps and navigation apps use weighted graphs to find the shortest route between two places. Even your family tree is technically a graph! Once you start seeing them, you can't unsee them.
Try it Yourself →