Graph (abstract data type)
Abstract data type implementing graph theory concepts.
In computer science, a graph is an abstract data type that implements the undirected graph and directed graph concepts from graph theory. It consists of a finite set of vertices (nodes) and a set of edges (links), which may be unordered pairs for undirected graphs or ordered pairs for directed graphs. Graphs are fundamental for modeling relationships and networks, with operations such as testing adjacency, listing neighbors, and adding or removing vertices and edges.
- field
- Computer science
- known_for
- Abstract data type implementing graph theory concepts
- operations
- adjacent, neighbors, add_vertex, remove_vertex, add_edge, remove_edge, get_vertex_value, set_vertex_value, get_edge_value, set_edge_value
- common_representations
- Adjacency list, adjacency matrix, incidence matrix
- parallel_representations
- Shared memory, distributed memory (1D and 2D partitioning)
Lore & Background
A graph data structure is an abstract data type used to implement the mathematical concepts of undirected and directed graphs from graph theory. It consists of a finite, and possibly mutable, set of vertices (also called nodes or points) and a set of edges. In an undirected graph, an edge is an unordered pair of vertices; in a directed graph, an edge is an ordered pair, often called an arrow or arc. Vertices can be part of the graph structure itself or can be external entities represented by integer indices or references. Each edge may also carry a value, such as a symbolic label or a numeric attribute like cost, capacity, or length. The defining characteristics of a graph include its operations, such as testing for the existence of an edge between two vertices, listing all adjacent vertices, adding or removing vertices and edges, and retrieving or setting values associated with vertices or edges. Common representations include adjacency lists, where each vertex stores a list of adjacent vertices; adjacency matrices, a two-dimensional matrix with rows for source vertices and columns for destinations; and incidence matrices, where rows represent vertices and columns represent edges. Adjacency lists are preferred for sparse graphs, while adjacency matrices are favored for dense graphs or when quick edge lookup is needed. For parallel computing, graph representation depends on the memory model: shared memory uses sequential representations, while distributed memory partitions the vertex set among processing elements, requiring careful heuristics to balance communication cost and partition size.
Reader's Guide
The graph abstract data type is significant because it provides a formal, implementable structure for representing pairwise relationships, enabling algorithms for networking, routing, and dependency analysis. Its operations—such as testing adjacency, listing neighbors, and adding or removing vertices and edges—form the basis for graph algorithms. Common representations include adjacency lists, which are preferred for sparse graphs, and adjacency matrices, which are preferred for dense graphs or when quick edge lookup is needed. The time complexity of operations varies by representation; for example, using hash tables for adjacency sets yields amortized O(1) adjacency testing and edge removal. In parallel computing, graph representation choices critically affect communication cost and scalability, with distributed memory approaches using 1D or 2D partitioning of the vertex set or adjacency matrix. The legacy of this data type lies in its universal applicability across computer science, from social networks to computational biology, and its role in enabling efficient algorithmic solutions.
Did You Know?
- The graph data structure implements both undirected and directed graph concepts from graph theory.
- Edges in a directed graph are also sometimes called arrows or arcs.
- Adjacency lists are generally preferred for sparse graphs, while adjacency matrices are preferred for dense graphs.
- In distributed memory models, graph partitioning is an NP-hard problem, so heuristics like 1D and 2D partitioning are used.
The Abstract Contract: Separating Logic from Layout
The graph as an abstract data type separates the question of what you can do with this structure from how it is actually built in memory. The ADT layer specifies the logical form: which operations are permitted, what results they must produce, and the algebraic relationships between them. It says nothing about whether your nodes live in a contiguous block of RAM or are scattered across the heap, connected by pointer chains. This separation is what allows multiple concrete data structures to satisfy the same abstract specification—just as a list ADT can be realized by a linked list or a resizable array, a graph ADT can be embodied by different physical layouts. The efficiency of any given realization is then a property of that concrete implementation, not of the abstract contract, and must be assessed through benchmarks or theoretical simulation rather than assumed from the specification alone.
Modeling the Web of Relationships
A graph, at its core, is a collection of vertices joined by edges, and its distinguishing power lies in capturing the relationships between entities rather than the entities in isolation. This makes the structure a natural model for domains where connections carry as much meaning as the connected points: social networks, computer networks, transportation networks. The abstraction presents a small but consequential set of structural parameters. Edges may be directed or undirected; the overall topology may contain cycles or be constrained to be acyclic. These parameters shape which questions the structure can answer and which traversal strategies are applicable. Crucially, because the graph as an ADT describes only the logical form—what operations are permitted and what results they must yield—it remains entirely agnostic about the physical substrate. Whether the underlying implementation arranges nodes in contiguous memory or links them through stored addresses, whether edges are enumerated in arrays or threaded through pointer chains, is a decision belonging to the concrete data structure, not to the abstract specification.
Traversal as the Defining Operation
The two traversal algorithms most closely associated with graph structures—breadth-first search and depth-first search—illustrate how the abstract data type prescribes behavior without dictating mechanism. The ADT specifies that a traversal must visit nodes in a particular order and produce a particular result; it does not specify whether the working set of unvisited nodes is managed through a stack, a queue, or some other auxiliary structure, nor whether adjacency information is laid out in contiguous memory or linked through pointers. In practice, the choice of concrete data structure for storing the graph often dominates the performance outcome. Rob Pike made the observation that the data structure one selects tends to influence efficiency more than the algorithm does, because the algorithmic logic is frequently self-evident once the problem is understood. For a graph, this means the question of whether edges are stored in arrays or in linked structures, and whether nodes are addressed by computed offsets or by stored references, can carry more weight in the performance profile than the particular traversal strategy employed.
Pointers, Memory, and the Cost of Flexibility
Every concrete implementation of a graph ultimately depends on the computer's ability to store and manipulate memory addresses. A pointer—a bit string that names a location in memory—can itself be stored and passed around by the program, and this capability is what makes linked representations possible. In an array-based approach, elements occupy contiguous memory words, and the address of any element is computed by simple arithmetic on an index; this yields fast, predictable access but demands a rigid, fixed layout. A linked approach, by contrast, stores the addresses of related nodes directly within the structure, granting flexibility for dynamic insertion and removal without relocating existing elements. For a graph, where the dynamic nature of connections means nodes and edges may be added or removed over time, this flexibility is particularly valuable. The tradeoff is that each pointer indirection adds a memory access, and the scattered layout can hurt performance. The implementation subroutines—insertion, deletion, traversal, lookup—must be written against whichever physical layout is chosen, and their efficiency is a property of that concrete realization, not of the abstract graph specification.
Frequently Asked Questions
Who is Graph (abstract data type)?
A Graph is an abstract data type in computer science that formalizes the undirected and directed graph concepts drawn from graph theory. It is built from a finite collection of vertices (nodes) connected by edges, where edges are unordered pairs in the undirected case and ordered pairs in the directed case.
What are Graph (abstract data type)'s powers/role?
Its core operations let you test whether two vertices are adjacent, list a vertex's neighbors, and add or remove both vertices and edges. It also supports reading and writing values attached to individual vertices and edges.
How does Graph (abstract data type)'s story end?
As an abstract data type it has no narrative arc, but its legacy lives on through the concrete implementations—adjacency lists, adjacency matrices, and incidence matrices—that programmers choose to bring it to life. In parallel settings its story continues via shared-memory or distributed-memory layouts using 1D or 2D partitioning.
Why is Graph (abstract data type) important?
It provides the foundational structure for modeling any relationship network—social connections, road maps, dependency chains—making it indispensable across computer science and applied mathematics. Without it, algorithms like shortest-path search, cycle detection, and network flow would lack a clean, unified interface to operate on.
What forms can Graph (abstract data type) take?
The most common concrete representations are the adjacency list, the adjacency matrix, and the incidence matrix, each trading memory footprint against lookup speed. For large-scale or parallel workloads the graph can also be split across shared-memory or distributed-memory architectures using 1D or 2D partitioning schemes.
More in Algorithms And Data Structures 1-24
Spotted an error? Know more?
This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record
