In simple terms, a graph is a mathematical structure used to model relationships between objects. It consists of two basic components:
Formally, a graph $G$ is defined as an ordered pair $G = (V, E)$, where:
Analogy: Think of a map of airline routes.
The cities are the vertices.
The flights between cities are the edges.
Let's imagine a small social network.
This graph shows that Alice is friends with Bob and Charles, Bob is friends with Alice and Charles, and so on.
Here is a visual representation of that graph:
The primary difference lies in whether the relationship represented by the edges has a direction.
In an undirected graph, the edges represent a mutual or symmetric relationship. If there is an edge between vertex A and vertex B, the relationship goes both ways. It's a two-way street.
{A, B}. This means the edge from A to B is the same as the edge from B to A.Example of an Undirected Graph:
The social network graph above is a perfect example of an undirected graph. The edge {Alice, Bob} implies a mutual friendship.
In a directed graph, the edges represent a one-way relationship. An edge from vertex A to vertex B does not necessarily mean there is a relationship from B to A. It's a one-way street.
(A, B). This means the edge goes from A to B. The edge (A, B) is completely different from the edge (B, A).Example of a Directed Graph:
Let's model a "follows" relationship.
This means:
Alice follows Bob, and Bob follows Alice (a mutual relationship represented by two directed edges).
Charles follows Alice (but Alice does not follow Charles).
* Charles and Diana follow each other.
Here is a visual representation:
| Feature | Undirected Graph | Directed Graph (Digraph) |
| :--- | :--- | :--- |
| Relationship | Symmetric / Two-way | Asymmetric / One-way |
| Edge Notation | Unordered set: {u, v} | Ordered pair: (u, v) |
| Edge Meaning | {u, v} is the same as {v, u} | (u, v) is different from (v, u) |
| Visual | Edges are lines | Edges are arrows (arcs) |
| Classic Example | Facebook Friends | Twitter Followers |
| Use Case | Modeling computer networks, maps of two-way roads | Modeling web links, task dependencies, flowcharts |