{
  "id": "adjacency-list",
  "title": "Adjacency List",
  "lang": "en",
  "source_lang": "en",
  "summary": "An adjacency list represents a graph by associating each vertex with a collection of its neighboring vertices or incident edges.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Adjacency-list representation"
  ],
  "generated_at": "2026-11-04",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/adjacency-list/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/adjacency-list/",
      "json": "https://aiwiki.page/zh/adjacency-list.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "An adjacency list is a data structure for representing a graph by associating each vertex with a collection of its neighbors or incident edges. It stores the connections that exist rather than reserving a separate location for every possible pair of vertices. This makes it particularly useful for sparse graphs, where the number of edges is small relative to the number of possible connections. Despite its name, an adjacency list need not use linked lists: arrays, resizable arrays, hash tables, and other containers can hold the neighbors."
    },
    {
      "heading": "Definition and structure",
      "text": "Let a graph be \\(G=(V,E)\\), where \\(V\\) is its set of vertices and \\(E\\) its set of edges. For a directed graph, the outgoing adjacency collection of a vertex \\(u\\) represents\n\n\\[\\operatorname{Adj}[u]=\\{v\\in V:(u,v)\\in E\\}.\\]\n\nThis set notation describes a simple graph. Implementations may instead store sequences of edge records, allowing repeated endpoints, edge identifiers, and additional attributes.\n\nAn adjacency-list representation usually has two levels:\n\n- An outer container that locates the adjacency collection for each vertex.\n- An inner container that stores that vertex’s neighbors or edge records.\n\nIf vertices are numbered consecutively, the outer container can be an array. For vertices identified by names or other keys, a hash table can map identifiers to their collections. Vertices with no neighbors must still be represented, for example by empty collections or by entries in a separate vertex registry.\n\nExample\n\nConsider an undirected graph with vertices \\(A,B,C,D,E\\) and edges \\(\\{A,B\\}\\), \\(\\{A,C\\}\\), and \\(\\{B,D\\}\\):\n\nA: B, C\nB: A, D\nC: A\nD: B\nE:\n\nEach edge appears in the collections of both endpoints. The isolated vertex \\(E\\) has an empty collection.\n\nFor the corresponding directed edges \\(A\\to B\\), \\(A\\to C\\), and \\(B\\to D\\), an outgoing adjacency representation is:\n\nA: B, C\nB: D\nC:\nD:\nE:\n\nThe order of neighbors generally has no mathematical significance, although it can affect traversal order and reproducibility."
    },
    {
      "heading": "Graph variants and edge attributes",
      "text": "Directed and undirected graphs\n\nIn an undirected graph without self-loops, each edge contributes two adjacency entries. The degree of a vertex is therefore the length of its adjacency collection.\n\nIn a directed graph, an outgoing list records edges leaving a vertex. Its length gives the vertex’s out-degree, but not its in-degree. Efficient access to incoming edges requires either a separate incoming adjacency structure or another index. Without such a structure, finding all predecessors of a vertex can require scanning the whole graph.\n\nWeighted graphs\n\nFor a weighted graph, each entry can contain a neighbor and a weight:\n\nA: (B, 7), (C, 2)\nB: (D, 5)\n\nMore general edge records can include identifiers, labels, capacities, timestamps, or references to separately stored attributes. Keeping attributes in a shared edge object can avoid duplicating them when an undirected edge is referenced from both endpoints.\n\nParallel edges and self-loops\n\nA multigraph permits several edges between the same endpoints. Adjacency sequences can represent these as separate records, whereas a neighbor set alone collapses them unless multiplicities or edge collections are stored explicitly.\n\nSelf-loop conventions require particular care. In an undirected graph, a self-loop contributes two to the mathematical degree of its vertex. A representation may store it twice to preserve the correspondence between degree and list length, or store it once and handle the degree calculation separately."
    },
    {
      "heading": "Space and operation costs",
      "text": "Write \\(n=|V|\\) and \\(m=|E|\\). Under the usual assumption that each vertex entry and edge record occupies bounded space, an adjacency list uses\n\n\\[\\Theta(n+m)\\]\n\nspace. An outgoing representation of a directed graph stores \\(m\\) edge entries; a conventional undirected representation stores approximately \\(2m\\). The vertex term matters because isolated vertices still require representation.\n\nThe time complexity of individual operations depends on the chosen containers. For an unsorted adjacency sequence, assuming constant-time access to the outer vertex entry:\n\nOperation | Typical cost\nEnumerate all neighbors of \\(u\\) | \\(\\Theta(\\deg(u))\\)\nTest whether \\(u\\) is adjacent to \\(v\\) | \\(O(\\deg(u))\\)\nInsert an entry at the head of a linked list | \\(O(1)\\)\nAppend an entry to a resizable array | Amortized \\(O(1)\\)\nFind and remove an entry by endpoint | \\(O(\\deg(u))\\)\nEnumerate all vertices and edges | \\(\\Theta(n+m)\\)\n\nFor directed outgoing lists, \\(\\deg(u)\\) in these costs means out-degree.\n\nThese bounds require several qualifications:\n\n- Constant-time insertion assumes that checking for an existing duplicate is unnecessary.\n- Deleting an undirected edge generally requires updating both endpoint collections.\n- Removing a known entry can be faster than locating it, depending on the container and available references.\n- Removing a vertex also requires removing its incident edges. With outgoing lists alone, locating its incoming edges can require a full scan.\n- Edge attributes and vertex identifiers of variable size add storage beyond the structural \\(\\Theta(n+m)\\) bound.\n\nThus, “adjacency list” specifies a representation pattern, not one universal set of operation costs."
    },
    {
      "heading": "Implementation choices",
      "text": "Linked lists and resizable arrays\n\nA linked list supports constant-time insertion at its head and can support efficient deletion when suitable references are available. Its disadvantages include pointer overhead, memory-allocation costs, and poor locality when nodes are scattered in memory.\n\nA dynamic array stores neighbors contiguously. It usually provides compact storage and efficient sequential iteration, benefiting from the CPU cache. Appending has amortized constant cost, but deletion may require shifting elements. If order need not be preserved, a known element can instead be replaced by the last element before shrinking the array.\n\nSorted and hashed collections\n\nA sorted adjacency array permits binary search for an endpoint in \\(O(\\log \\deg(u))\\) time, though maintaining order during updates can require linear work.\n\nA hash-based neighbor collection can provide expected constant-time membership tests and updates under suitable hashing assumptions. It introduces additional memory overhead, may have weaker iteration locality, and does not directly preserve parallel edges if each neighbor is stored only once.\n\nCompressed sparse row storage\n\nCompressed sparse row storage, or CSR, packs adjacency data into an offsets array and a contiguous neighbor array. For vertices numbered \\(0,\\ldots,n-1\\), the neighbors of vertex \\(u\\) occupy the half-open interval\n\n\\[[\\operatorname{offset}[u],\\operatorname{offset}[u+1]).\\]\n\nThe offsets array has \\(n+1\\) entries. A weighted representation can keep weights in a parallel array.\n\nCSR reduces per-vertex allocation overhead and supports efficient sequential processing. It is well suited to static graphs, but structural changes can require moving data or rebuilding the representation."
    },
    {
      "heading": "Use in graph algorithms",
      "text": "Adjacency lists allow algorithms to inspect actual edges without checking every possible vertex pair.\n\nBreadth-first search and depth-first search run in \\(O(n+m)\\) time when traversing an entire graph represented this way, assuming constant-time bookkeeping operations. Each vertex is processed a bounded number of times, and each adjacency entry is inspected a bounded number of times. A traversal started from one vertex visits only its reachable portion unless further searches are initiated.\n\nFor the shortest-path problem, Dijkstra’s algorithm commonly uses weighted adjacency lists with a priority queue. With a binary heap and a standard implementation, its running time is \\(O((n+m)\\log n)\\) for nonnegative edge weights.\n\nAdjacency lists are also used in topological sorting, connected-component detection, spanning-tree algorithms, and PageRank. They occur in road networks, dependency graphs, social networks, and program-analysis structures."
    },
    {
      "heading": "Comparison with other representations",
      "text": "An adjacency matrix reserves a position for every ordered pair of vertices. A conventional matrix therefore takes \\(\\Theta(n^2)\\) entries, provides constant-time edge lookup, and typically requires scanning a row of length \\(n\\) to enumerate a vertex’s neighbors.\n\nAdjacency lists instead use space proportional to vertices and edges and enumerate neighbors in time proportional to their number. They are generally attractive for sparse graphs and traversal-heavy workloads. For dense graphs, matrices can be competitive or preferable because of simple indexing, predictable memory access, and efficient bit-packed representations.\n\nAn edge list stores edges as a single collection of endpoint pairs or records. It is convenient for processing or sorting all edges, but finding the neighbors of one vertex requires scanning the collection unless an additional index is built.\n\nThe practical choice depends not only on graph density but also on update frequency, membership-query frequency, edge attributes, memory overhead, and access patterns. A mathematically sparse graph can still incur substantial memory costs if every adjacency entry carries several pointers or separately allocated objects."
    }
  ],
  "links": [
    "data-structure",
    "graph-theory",
    "array-data-structure",
    "hash-table",
    "vertex-degree",
    "time-complexity",
    "linked-list",
    "dynamic-array",
    "cpu-cache",
    "binary-search",
    "breadth-first-search",
    "depth-first-search",
    "shortest-path-problem",
    "dijkstra-algorithm",
    "priority-queue",
    "pagerank",
    "adjacency-matrix"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}