Graph Introduction
A graph is a non-linear data structure made of vertices (nodes) and edges that connect pairs of vertices. It is written G = (V, E), where V is the set of vertices and E the set of edges.

The graph above has 5 vertices and 6 edges: V = {A, B, C, D, E} and E = {(A,B), (A,C), (B,D), (B,E), (C,D), (D,E)}.
Graphs can describe almost any network of relationships, which is their great strength; the same generality is also the source of their costs. The two lists below weigh them against each other.
Advantages of Graph Data Structure
- Models any relationship: many-to-many links, cycles, one-way (directed) or two-way (undirected) connections, and weighted edges can all be represented, which trees and lists cannot do.
- Rich set of proven algorithms: BFS and DFS, shortest paths (Dijkstra, Bellman-Ford), minimum spanning trees (Prim, Kruskal), topological sorting and cycle detection solve many practical problems efficiently, often in O(V + E) or O((V + E) log V) time.
- Natural fit for real systems: road and flight networks, social networks, the web's link structure, computer networks, task dependencies and recommendation systems are all graphs.
- Easy to visualise: drawing vertices and edges makes complex structures, clusters and bottlenecks easy to see and explain.
- Flexible storage: the representation can be chosen to suit the graph, an adjacency list for sparse graphs or a matrix for dense ones.
These strengths come with some costs.
Disadvantages of Graph Data Structure
- More complex algorithms: graph code must handle cycles, disconnected parts and visited sets, which makes it harder to write and debug than code for lists or trees.
- Memory use: an adjacency matrix needs O(V2) space even for few edges, and even an adjacency list needs O(V + E), which is large for graphs with billions of edges.
- Some problems are very hard: finding the longest simple path, a Hamiltonian cycle or the best travelling-salesman tour has no known efficient (polynomial-time) algorithm.
- No natural order or root: unlike a tree or a list, a graph has no built-in starting point or ordering, so even "visit everything once" needs a traversal algorithm.