Graph
ELI5 — The Vibe Check
A graph is like a network of dots connected by lines. Each dot is a node and each line is an edge. A city map is a graph: intersections are nodes, roads are edges. Social networks are graphs: people are nodes, friendships are edges. Unlike trees, graphs can have cycles and connections in any direction.
Real Talk
A graph is a data structure consisting of vertices (nodes) and edges (connections). Graphs can be directed (edges have direction) or undirected, weighted (edges have values) or unweighted, and cyclic or acyclic. Graph algorithms include BFS (breadth-first search), DFS (depth-first search), Dijkstra's (shortest path), and topological sort. Used in routing, social networks, and dependency resolution.
Show Me The Code
// Adjacency list representation:
const graph = {
A: ['B', 'C'],
B: ['A', 'D', 'E'],
C: ['A', 'F'],
D: ['B'],
E: ['B', 'F'],
F: ['C', 'E']
};
// BFS traversal:
function bfs(start) {
const visited = new Set([start]);
const queue = [start];
while (queue.length) {
const node = queue.shift();
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
}
When You'll Hear This
"Model the dependencies as a graph and detect cycles." / "BFS on a graph is how you find the shortest unweighted path."
Related Terms
Algorithm
An algorithm is just a step-by-step recipe for solving a problem. Sort a list? There is an algorithm. Find the shortest path? Algorithm. Make a sandwich?
Data Structure
A data structure is a way of organizing information in your code so it is easy to use and fast to access.
Tree
A tree is a data structure shaped like an upside-down tree — one root at the top, branches going down, and leaves at the very bottom.