Many real problems are about connections: which cities are linked by roads, who follows whom, which web pages link to which. A graph is the data structure for exactly this kind of information. This tutorial explains what a graph is, the basic vocabulary, how graphs are stored, and how they are traversed.
What is Graph Data Structure?
A graph is a non-linear data structure made of a set of vertices (also called nodes) and a set of edges that connect pairs of vertices. A vertex stands for an object, and an edge stands for a relationship between two objects.
Unlike a tree, a graph has no root, any vertex may be connected to any other, and the edges may form cycles. Graphs model road and flight networks, social networks, computer networks, circuits, dependencies between tasks, and much more.
Formally, a graph is written as G = (V, E), where V is the set of vertices and E is the set of edges.
For example, consider this graph:

It has 5 vertices and 6 edges, so G = (V, E) with
V = {A, B, C, D, E}
E = {(A,B), (A,C), (B,D), (B,E), (C,D), (D,E)}
Terminology of Graph Data Structure
Vertex: a point in the graph that represents an object, such as a city or a person. Vertices are usually drawn as labelled circles.
Edge: a connection between two vertices, drawn as a line (or an arrow in a directed graph). Two vertices joined by an edge are called adjacent.
Weight: a number attached to an edge, such as the distance between two cities or the cost of a link. A graph with weights is a weighted graph.
Path: a sequence of vertices in which each consecutive pair is joined by an edge, for example A → B → D → E in the graph above.
Degree: the number of edges touching a vertex (in a directed graph, split into in-degree and out-degree).
Cycle: a path that starts and ends at the same vertex without reusing an edge, such as A → B → D → C → A.
Representations of Graph Data Structure
To work with a graph in a program it must be stored in memory. The two standard representations are:
Adjacency Matrix
A V × V table in which the entry in row i and column j is 1 (or the edge weight) if there is an edge from vertex i to vertex j, and 0 otherwise. Checking whether an edge exists takes O(1), but the table always needs O(V2) space. Read more...
Adjacency List
Each vertex keeps a list of the vertices it is connected to. The space is O(V + E), and visiting all neighbours of a vertex is fast, which makes this the usual choice for sparse graphs. Read more...
Choose a matrix for small or dense graphs where fast edge checks matter, and a list for large, sparse graphs, which covers most real-world networks.
Traversing a Graph
Traversing a graph means visiting every vertex reachable from a start vertex. Because graphs can contain cycles, a traversal must remember which vertices it has already visited. There are two standard methods:
Breadth First Search (BFS): starts at a source vertex, visits all of its neighbours first, then their neighbours, and so on, moving outwards one layer at a time. It uses a queue and finds shortest paths (by number of edges) in unweighted graphs.

Depth First Search (DFS): starts at a source vertex and follows one path as deep as possible before backtracking to try the next branch. It uses a stack or recursion and is used for cycle detection, topological sorting and finding connected components.
Both traversals take O(V + E) time with an adjacency list.