Graph (abstract data type)
In computer science, a graph is an abstract data type that is meant to implement the undirected graph and directed graph concepts from mathematics; specifically, the field of graph theory.
A graph data structure consists of a finite (and possibly mutable) set of vertices or nodes or points, together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph. These pairs are known as edges, arcs, or lines for an undirected graph and as arrows, directed edges, directed arcs, or directed lines for a directed graph. The vertices may be part of the graph structure, or may be external entities represented by integer indices or references.
A graph data structure may also associate to each edge some edge value, such as a symbolic label or a numeric attribute (cost, capacity, length, etc.).
Contents
1 Operations
2 Representations
3 See also
4 References
5 External links
Operations
The basic operations provided by a graph data structure G usually include:[1]
adjacent
(G, x, y): tests whether there is an edge from the vertex x to the vertex y;
neighbors
(G, x): lists all vertices y such that there is an edge from the vertex x to the vertex y;
add_vertex
(G, x): adds the vertex x, if it is not there;
remove_vertex
(G, x): removes the vertex x, if it is there;
add_edge
(G, x, y): adds the edge from the vertex x to the vertex y, if it is not there;
remove_edge
(G, x, y): removes the edge from the vertex x to the vertex y, if it is there;
get_vertex_value
(G, x): returns the value associated with the vertex x;
set_vertex_value
(G, x, v): sets the value associated with the vertex x to v.
Structures that associate values to the edges usually also provide:[1]
get_edge_value
(G, x, y): returns the value associated with the edge (x, y);
set_edge_value
(G, x, y, v): sets the value associated with the edge (x, y) to v.
Representations
Different data structures for the representation of graphs are used in practice:
Adjacency list[2]
- Vertices are stored as records or objects, and every vertex stores a list of adjacent vertices. This data structure allows the storage of additional data on the vertices. Additional data can be stored if edges are also stored as objects, in which case each vertex stores its incident edges and each edge stores its incident vertices.
Adjacency matrix[3]
- A two-dimensional matrix, in which the rows represent source vertices and columns represent destination vertices. Data on edges and vertices must be stored externally. Only the cost for one edge can be stored between each pair of vertices.
Incidence matrix[4]
- A two-dimensional Boolean matrix, in which the rows represent the vertices and columns represent the edges. The entries indicate whether the vertex at a row is incident to the edge at a column.
The following table gives the time complexity cost of performing various operations on graphs, for each of these representations, with |V | the number of vertices and |E | the number of edges.[citation needed] In the matrix representations, the entries encode the cost of following an edge. The cost of edges that are not present are assumed to be ∞.
Adjacency list | Adjacency matrix | Incidence matrix | |
---|---|---|---|
Store graph | O(|V|+|E|){displaystyle O(|V|+|E|)} | O(|V|2){displaystyle O(|V|^{2})} | O(|V|⋅|E|){displaystyle O(|V|cdot |E|)} |
Add vertex | O(1){displaystyle O(1)} | O(|V|2){displaystyle O(|V|^{2})} | O(|V|⋅|E|){displaystyle O(|V|cdot |E|)} |
Add edge | O(1){displaystyle O(1)} | O(1){displaystyle O(1)} | O(|V|⋅|E|){displaystyle O(|V|cdot |E|)} |
Remove vertex | O(|E|){displaystyle O(|E|)} | O(|V|2){displaystyle O(|V|^{2})} | O(|V|⋅|E|){displaystyle O(|V|cdot |E|)} |
Remove edge | O(|V|){displaystyle O(|V|)} | O(1){displaystyle O(1)} | O(|V|⋅|E|){displaystyle O(|V|cdot |E|)} |
Are vertices x and y adjacent (assuming that their storage positions are known)? | O(|V|){displaystyle O(|V|)} | O(1){displaystyle O(1)} | O(|E|){displaystyle O(|E|)} |
Remarks | Slow to remove vertices and edges, because it needs to find all vertices or edges | Slow to add or remove vertices, because matrix must be resized/copied | Slow to add or remove vertices and edges, because matrix must be resized/copied |
Adjacency lists are generally preferred because they efficiently represent sparse graphs. An adjacency matrix is preferred if the graph is dense, that is the number of edges |E | is close to the number of vertices squared, |V |2, or if one must be able to quickly look up if there is an edge connecting two vertices.[5][6]
See also
Graph traversal for graph walking strategies
Graph database for graph (data structure) persistency
Graph rewriting for rule based transformations of graphs (graph data structures)
Graph drawing software for software, systems, and providers of systems for drawing graphs
References
^ ab See, e.g. Goodrich & Tamassia (2015), Section 13.1.2: Operations on graphs, p. 360. For a more detailed set of operations, see Mehlhorn, K.; Näher, S. (1999), "Chapter 6: Graphs and their data structures", LEDA: A platform for combinatorial and geometric computing, Cambridge University Press, pp. 240–282.mw-parser-output cite.citation{font-style:inherit}.mw-parser-output q{quotes:"""""""'""'"}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-limited a,.mw-parser-output .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}.
^ Cormen et al. (2001), pp. 528–529; Goodrich & Tamassia (2015), pp. 361-362.
^ Cormen et al. (2001), pp. 529–530; Goodrich & Tamassia (2015), p. 363.
^ Cormen et al. (2001), Exercise 22.1-7, p. 531.
^ Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001), "Section 22.1: Representations of graphs", Introduction to Algorithms (Second ed.), MIT Press and McGraw-Hill, pp. 527–531, ISBN 0-262-03293-7.
^ Goodrich, Michael T.; Tamassia, Roberto (2015), "Section 13.1: Graph terminology and representations", Algorithm Design and Applications, Wiley, pp. 355–364.
External links
Boost Graph Library: a powerful C++ graph library s.a. Boost (C++ libraries)
- Networkx: a Python graph library
GraphMatcher a java program to align directed/undirected graphs.