No history yet

Graph Basics

What Is a Graph?

When you hear the word "graph," you might picture a bar chart or a line graph from a math class. In computer science, a graph is something different. It's a way to represent connections between things.

Think of it as a network of points and the lines that link them. Every graph is made of two basic components: vertices and edges.

Vertex

noun

A point or node in a graph.

Vertices are often just called "nodes."

Edge

noun

A connection or link between two vertices.

Imagine a map of cities. Each city is a vertex. A road connecting two cities is an edge. A graph is simply the collection of all the cities and all the roads that connect them. This structure is incredibly powerful for modeling all sorts of real-world relationships.

Giving Edges Direction

Edges can be simple lines or they can have a direction, like a one-way street. This distinction creates two main types of graphs: undirected and directed.

An undirected graph has edges that go both ways. If vertex A is connected to vertex B, then B is also connected to A. The relationship is mutual.

A directed graph has edges with a specific direction, shown with an arrow. A connection from A to B doesn't automatically mean there's a connection from B to A.

A great analogy is social media. A Facebook friendship is an undirected edge. If you are friends with someone, they are also friends with you. The connection is two-way.

Following someone on Instagram or Twitter is a directed edge. You can follow a celebrity, but that doesn't mean they follow you back. The connection is one-way.

Now let's see what this network would look like if it represented who follows whom. Maybe Alex follows Ben, but Ben doesn't follow Alex back. The arrows show the direction of the relationship.

Understanding these basic building blocks—vertices, edges, and direction—is the first step. With this foundation, we can start exploring how to navigate these networks and find our way through them.