Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
626 views

Algorithms and Data Structures

- A tree is an undirected graph where any two vertices are connected by exactly one path. It is connected and contains no cycles. - Trees have n-1 edges if they have n vertices. Adding any edge would create a cycle. - A forest is a disjoint union of trees. It is an undirected, cycle-free graph.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
626 views

Algorithms and Data Structures

- A tree is an undirected graph where any two vertices are connected by exactly one path. It is connected and contains no cycles. - Trees have n-1 edges if they have n vertices. Adding any edge would create a cycle. - A forest is a disjoint union of trees. It is an undirected, cycle-free graph.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 161

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/259398493

Lecture Notes - Algorithms and Data Structures, Part 7: Trees and Graphs

Book · December 2013


DOI: 10.13140/2.1.3060.8965

CITATIONS READS

0 9,220

3 authors, including:

Reiner Creutzburg Jenny Knackmuß


Brandenburg University of Applied Sciences 33 PUBLICATIONS   3 CITATIONS   
465 PUBLICATIONS   351 CITATIONS   
SEE PROFILE
SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Wikipedia Books - free, downloadable, multilingual lecture notes View project

Advanced Cybersecurity and Cyberforensics View project

All content following this page was uploaded by Reiner Creutzburg on 22 December 2013.

The user has requested enhancement of the downloaded file.


Algorithms and Data
Structures
Part 7: Trees and Graphs (Wikipedia Book
2014)

By Wikipedians

Editors: Reiner Creutzburg, Jenny Knackmuß

PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information.
PDF generated at: Sun, 22 Dec 2013 19:38:59 UTC
Contents
Articles
Trees 1
Tree (graph theory) 1
Binary tree 5
Binary search tree 13
Infix notation 20
Complete graph 22
Polish notation 24
Reverse Polish notation 29
Self-balancing binary search tree 34
AVL tree 37
B-tree 42
Heap (data structure) 52
Fibonacci heap 55
Spanning tree 60

Graphs 64
Graph (mathematics) 64
Graph theory 71
Glossary of graph theory 78
Directed graph 88
Adjacency matrix 91
Floyd–Warshall algorithm 94
Shortest path problem 99
Breadth-first search 105
Depth-first search 110
Backtracking 116
Topological sorting 121
Dijkstra's algorithm 125
Greedy algorithm 133
Travelling salesman problem 136

References
Article Sources and Contributors 151
Image Sources, Licenses and Contributors 155

Article Licenses
License 157
1

Trees

Tree (graph theory)


Trees

A labeled tree with 6 vertices and 5 edges

Vertices v

Edges v − 1

Chromatic number 2 if v > 1

• v
• t
[1]
• e

In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two vertices are
connected by exactly one simple path. In other words, any connected graph without simple cycles is a tree. A forest
is a disjoint union of trees.
The various kinds of data structures referred to as trees in computer science are equivalent as undirected graphs to
trees in graph theory, although such data structures are generally rooted trees, thus in fact being directed graphs, and
may also have additional ordering of branches.
The term "tree" was coined in 1857 by the British mathematician Arthur Cayley.[2]

Definitions
A tree is an undirected simple graph G that satisfies any of the following equivalent conditions:
• G is connected and has no cycles.
• G has no cycles, and a simple cycle is formed if any edge is added to G.
• G is connected, but is not connected if any single edge is removed from G.
• G is connected and the 3-vertex complete graph is not a minor of G.
• Any two vertices in G can be connected by a unique simple path.
If G has finitely many vertices, say n of them, then the above statements are also equivalent to any of the following
conditions:
• G is connected and has n − 1 edges.
• G has no simple cycles and has n − 1 edges.
Tree (graph theory) 2

As elsewhere in graph theory, the order-zero graph (graph with no vertices) is generally excluded from
consideration: while it is vacuously connected as a graph (any two vertices can be connected by a path), it is not
0-connected (or even (−1)-connected) in algebraic topology, unlike non-empty trees, and violates the "one more
node than edges" relation.
A leaf is a vertex of degree 1. An internal vertex is a vertex of degree at least 2.
An irreducible (or series-reduced) tree is a tree in which there is no vertex of degree 2.
A forest is an undirected graph, all of whose connected components are trees; in other words, the graph consists of a
disjoint union of trees. Equivalently, a forest is an undirected cycle-free graph. As special cases, an empty graph, a
single tree, and the discrete graph on a set of vertices (that is, the graph with these vertices that has no edges), all are
examples of forests.
The term hedge sometimes refers to an ordered sequence of trees.
A polytree[3] (also known as oriented tree[4][5] or singly connected network[6]) is a directed acyclic graph (DAG)
whose underlying undirected graph is a tree. In other words, if we replace its arcs with edges, we obtain an
undirected graph that is both connected and acyclic.
A directed tree is a directed graph which would be a tree if the directions on the edges were ignored. Some authors
restrict the phrase to the case where the edges are all directed towards a particular vertex, or all directed away from a
particular vertex (see arborescence).
A tree is called a rooted tree if one vertex has been designated the root, in which case the edges have a natural
orientation, towards or away from the root. The tree-order is the partial ordering on the vertices of a tree with u ≤ v
if and only if the unique path from the root to v passes through u. A rooted tree which is a subgraph of some graph G
is a normal tree if the ends of every edge in G are comparable in this tree-order whenever those ends are vertices of
the tree (Diestel 2005, p. 15). Rooted trees, often with additional structure such as ordering of the neighbors at each
vertex, are a key data structure in computer science; see tree data structure. In a context where trees are supposed to
have a root, a tree without any designated root is called a free tree.
In a rooted tree, the parent of a vertex is the vertex connected to it on the path to the root; every vertex except the
root has a unique parent. A child of a vertex v is a vertex of which v is the parent.
A labeled tree is a tree in which each vertex is given a unique label. The vertices of a labeled tree on n vertices are
typically given the labels 1, 2, …, n. A recursive tree is a labeled rooted tree where the vertex labels respect the tree
order (i.e., if u < v for two vertices u and v, then the label of u is smaller than the label of v).
An n-ary tree is a rooted tree for which each vertex has at most n children. 2-ary trees are sometimes called binary
trees, while 3-ary trees are sometimes called ternary trees.
A terminal vertex of a tree is a vertex of degree 1. In a rooted tree, the leaves are all terminal vertices; additionally,
the root, if not a leaf itself, is a terminal vertex if it has precisely one child.

Plane Tree
An ordered tree or plane tree is a rooted tree for which an ordering is specified for the children of each vertex. This
is called a "plane tree" because an ordering of the children is equivalent to an embedding of the tree in the plane,
with the root at the top and the children of each vertex lower than that vertex. Given an embedding of a rooted tree in
the plane, if one fixes a direction of children, say left to right, then an embedding gives an ordering of the children.
Conversely, given an ordered tree, and conventionally drawing the root at the top, then the child nodes in an ordered
tree can be drawn left-to-right, yielding an essentially unique planar embedding .
A leaf in a rooted tree is a vertex of degree 1 that is not the root. A terminal vertex of a tree is a vertex of degree 1.
In a rooted tree, the leaves are all terminal vertices; additionally, the root, if not a leaf itself, is a terminal vertex if it
has precisely one child.
Tree (graph theory) 3

Example
The example tree shown to the right has 6 vertices and 6 − 1 = 5 edges. The unique simple path connecting the
vertices 2 and 6 is 2-4-5-6.

Facts
• Every tree is a bipartite graph and a median graph. Every tree with only countably many vertices is a planar
graph.
• Every connected graph G admits a spanning tree, which is a tree that contains every vertex of G and whose edges
are edges of G.
• Every connected graph with only countably many vertices admits a normal spanning tree (Diestel 2005, Prop.
8.2.4).
• There exist connected graphs with uncountably many vertices which do not admit a normal spanning tree (Diestel
2005, Prop. 8.5.2).
• Every finite tree with n vertices, with n > 1, has at least two terminal vertices (leaves). This minimal number of
terminal vertices is characteristic of path graphs; the maximal number, n − 1, is attained by star graphs.
• For any three vertices in a tree, the three paths between them have exactly one vertex in common.

Enumeration

Labeled trees
Cayley's formula states that there are nn−2 trees on n labeled vertices. It can be proved by first showing that the
number of trees with vertices 1,2,...,n, of degrees d1,d2,...,dn respectively, is the multinomial coefficient

An alternative proof uses Prüfer sequences.


Cayley's formula is the special case of complete graphs in a more general problem of counting spanning trees in an
undirected graph, which is addressed by the matrix tree theorem. The similar problem of counting all the subtrees
regardless of size has been shown to be #P-complete in the general case (Jerrum (1994)).

Unlabeled trees
Counting the number of unlabeled free trees is a harder problem. No closed formula for the number t(n) of trees with
n vertices up to graph isomorphism is known. The first few values of t(n) are:
1, 1, 1, 1, 2, 3, 6, 11, 23, 47, 106, 235, 551, 1301, 3159, ... (sequence A000055 in OEIS).
Otter (1948) proved the asymptotic estimate:

with the values C and α seeming to be approximately 0.534949606… and 2.95576528565… (sequence A051491 in
OEIS), respectively. (Here, means that .) This is a consequence of his asymptotic estimate
for the number of unlabeled rooted trees with n vertices:

with D apparently around 0.43992401257… and the same α as above (cf. Knuth (1997), Chap. 2.3.4.4 and Flajolet &
Sedgewick (2009), Chap. VII.5).
The first few values of r(n) are:[7]
Tree (graph theory) 4

1, 1, 2, 4, 9, 20, 48, 115, 286, 719, 1842, 4766, 12486, 32973, ...

Types of trees
A star graph is a tree which consists of a single internal vertex (and n − 1 leaves). In other words, a star graph of
order n is a tree of order n with as many leaves as possible. Its diameter is at most 2.
A tree with two leaves (the fewest possible) is a path graph; a forest in which all components are isolated nodes and
path graphs is called a linear forest. If all vertices in a tree are within distance one of a central path subgraph, then
the tree is a caterpillar tree. If all vertices are within distance two of a central path subgraph, then the tree is a lobster.

Notes
[1] http:/ / en. wikipedia. org/ w/ index. php?title=Template:Infobox_graph& action=edit
[2] Cayley (1857) "On the theory of the analytical forms called trees," (http:/ / books. google. com/ books?id=MlEEAAAAYAAJ&
pg=PA172#v=onepage& q& f=false) Philosophical Magazine, 4th series, 13 : 172-176.
However it should be mentioned that in 1847, K.G.C. von Staudt, in his book Geometrie der Lage (Nürnberg, (Germany): Bauer und Raspe,
1847), presented a proof of Euler's polyhedron theorem which relies on trees on pages 20-21 (http:/ / books. google. com/
books?id=MzQAAAAAQAAJ& pg=PA20#v=onepage& q& f=false). Also in 1847, the German physicist Gustav Kirchhoff investigated
electrical circuits and found a relation between the number (n) of wires/resistors (branches), the number (m) of junctions (vertices), and the
number (μ) of loops (faces) in the circuit. He proved the relation via an argument relying on trees. See: Kirchhoff, G. R. (1847) "Uber die
Auflösung der Gleichungen auf welche man bei der Untersuchung der linearen Vertheilung galvanischer Ströme geführt wird" (http:/ / books.
google. com/ books?id=gx4AAAAAMAAJ& vq=Kirchoff& pg=PA497#v=onepage& q& f=false) (On the solution of equations to which one
is led by the investigation of the linear distribution of galvanic currents), Annalen der Physik und Chemie, 72 (12) : 497-508.
[3] See .
[4] See .
[5] See .
[6] See .
[7] See .

References
• Dasgupta, Sanjoy (1999), "Learning polytrees" (http://cseweb.ucsd.edu/~dasgupta/papers/poly.pdf), in Proc.
15th Conference on Uncertainty in Artificial Intelligence (UAI 1999), Stockholm, Sweden, July-August 1999,
pp. 134–141.
• Harary, Frank; Sumner, David (1980), "The dichromatic number of an oriented tree", Journal of Combinatorics,
Information & System Sciences 5 (3): 184–187, MR  603363 (http://www.ams.org/
mathscinet-getitem?mr=603363).
• Kim, Jin H.; Pearl, Judea (1983), "A computational model for causal and diagnostic reasoning in inference
engines" (http://ijcai.org/Past Proceedings/IJCAI-83-VOL-1/PDF/041.pdf), in Proc. 8th International Joint
Conference on Artificial Intelligence (IJCAI 1983), Karlsruhe, Germany, August 1983, pp. 190–193.
• Li, Gang (1996), "Generation of Rooted Trees and Free Trees" (http://webhome.cs.uvic.ca/~ruskey/Theses/
GangLiMScThesis.pdf), M.S. Thesis, Dept. of Computer Science, University of Victoria, BC, Canada, p. 9.
• Simion, Rodica (1991), "Trees with 1-factors and oriented trees", Discrete Mathematics 88 (1): 93–104, doi:
10.1016/0012-365X(91)90061-6 (http://dx.doi.org/10.1016/0012-365X(91)90061-6), MR  1099270 (http://
www.ams.org/mathscinet-getitem?mr=1099270).
Tree (graph theory) 5

Further reading
• Diestel, Reinhard (2005), Graph Theory (http://diestel-graph-theory.com/index.html) (3rd ed.), Berlin, New
York: Springer-Verlag, ISBN 978-3-540-26183-4.
• Flajolet, Philippe; Sedgewick, Robert (2009), Analytic Combinatorics, Cambridge University Press,
ISBN 978-0-521-89806-5
• Hazewinkel, Michiel, ed. (2001), "Tree" (http://www.encyclopediaofmath.org/index.php?title=p/t094060),
Encyclopedia of Mathematics, Springer, ISBN 978-1-55608-010-4
• Knuth, Donald E. (November 14, 1997), The Art of Computer Programming Volume 1: Fundamental Algorithms
(3rd ed.), Addison-Wesley Professional
• Jerrum, Mark (1994), "Counting trees in a graph is #P-complete", Information Processing Letters 51 (3):
111–116, doi: 10.1016/0020-0190(94)00085-9 (http://dx.doi.org/10.1016/0020-0190(94)00085-9), ISSN 
0020-0190 (http://www.worldcat.org/issn/0020-0190).
• Otter, Richard (1948), "The Number of Trees", Annals of Mathematics. Second Series 49 (3): 583–599, doi:
10.2307/1969046 (http://dx.doi.org/10.2307/1969046), JSTOR  1969046 (http://www.jstor.org/stable/
1969046).

Binary tree
In computer science, a binary tree is a tree in which each node has
at most two child nodes (denoted as the left child and the right
child). Nodes with children are referred to as parent nodes, and child
nodes may contain references to their parents. Following this
convention, you can define ancestral relationships in a tree: that is,
for example, one node can be an ancestor of another node, a
descendant, or a great-grand-child of another node. The root node
is the ancestor to all nodes of the tree, and any node in the tree can
be reached from the root node. A tree which does not have any node
other than root node is called a null tree. In a binary tree, the degree
of every node can be at most two. A tree with n nodes has exactly A simple binary tree of size 9 and height 3, with a
n−1 branches or degree. root node whose value is 2. The above tree is
unbalanced and not sorted.
Binary trees are used to implement binary search trees and binary
heaps, and is applied for efficient searching and sorting. A binary tree is a special case of a K-ary tree (that is, k=2).

Definitions for rooted trees


• A directed edge refers to the link from the parent to the child (the arrows in the picture of the tree).
• The root node of a tree is the node with no parents. There is at most one root node in a rooted tree.
• A leaf node has no children.
• The depth (or height) of a tree is the length of the path from the root to the deepest node in the tree. A (rooted)
tree with only one node (the root) has a depth of zero.
• Siblings are those nodes that share the same parent node.
• A node p is an ancestor of a node q if p exists on the path from the root node to node q. The node q is then
termed as a descendant of p.
• The size of a node is the number of descendants it has including itself.
• The in-degree of a node is the number of edges arriving at that node.
• The out-degree of a node is the number of edges leaving that node.
Binary tree 6

• The root is the only node in a tree with an in-degree of 0.


• All the leaf nodes have an out-degree of 0.

Types of binary trees


• A rooted binary tree is a tree with a root
node in which every node has at most
two children.
• A full binary tree (sometimes proper
binary tree or 2-tree or strictly binary
tree) is a tree in which every node other
than the leaves has two children. Or,
perhaps more clearly, every node in a
binary tree has exactly (strictly) 0 or 2
children. Sometimes a full tree is
ambiguously defined as a perfect tree
Tree rotations are very common internal operations on self-balancing binary trees.
(see next). Physicists define a binary tree
to mean a full binary tree.[1]

.
• A perfect binary tree is a full binary tree in which all leaves are at
the same depth or same level, and in which every parent has two
children. (This is ambiguously also called a complete binary tree
(see next).) An example of a perfect binary tree is the ancestry chart
of a person to a given depth, as each person has exactly two
biological parents (one mother and one father); note that this
reverses the usual parent/child tree convention, and these trees go in
the opposite direction from usual (root at bottom).
An ancestry chart which maps to a perfect depth 4
• A complete binary tree is a binary tree in which every level, except
binary tree
possibly the last, is completely filled, and all nodes are as far left as
possible. A tree is called an almost complete binary tree or nearly complete binary tree if the exception holds, i.e.
the last level is not completely filled. This type of tree is used as a specialized data structure called a heap.
• An infinite complete binary tree is a tree with a countably infinite number of levels, in which every node has
two children, so that there are 2d nodes at level d. The set of all nodes is countably infinite, but the set of all
infinite paths from the root is uncountable: it has the cardinality of the continuum. These paths correspond by an
order preserving bijection to the points of the Cantor set, or (through the example of the Stern–Brocot tree) to the
set of positive irrational numbers.
• A balanced binary tree is commonly defined as a binary tree in which the depth of the left and right subtrees of
every node differ by 1 or less,[2] although in general it is a binary tree where no leaf is much farther away from
the root than any other leaf. (Different balancing schemes allow different definitions of "much farther".[3]) Binary
trees that are balanced according to this definition have a predictable depth (how many nodes are traversed from
the root to a leaf, root counting as node 0 and subsequent as 1, 2, ..., depth). This depth is equal to the integer part
of where is the number of nodes on the balanced tree. Example 1: balanced tree with 1 node,
(depth = 0). Example 2: balanced tree with 3 nodes, (depth=1). Example 3:
balanced tree with 5 nodes, (depth of tree is 2 nodes).
• A degenerate tree is a tree where for each parent node, there is only one associated child node. This means that
in a performance measurement, the tree will behave like a linked list data structure.
Binary tree 7

Note that this terminology often varies in the literature, especially with respect to the meaning of "complete" and
"full".

Properties of binary trees


• The number of nodes n in a perfect binary tree can be found using this formula: n = 2h+1-1 where h is the depth
of the tree.
• The number of nodes n in a binary tree of height h is at least n = h + 1 and at most n = 2h+1-1 where h is the depth
of the tree.
• The number of leaf nodes l in a perfect binary tree can be found using this formula: l = 2h where h is the depth
of the tree.
• The number of nodes n in a perfect binary tree can also be found using this formula: n = 2l-1 where l is the
number of leaf nodes in the tree.
• The number of null links (i.e., absent children of nodes) in a complete binary tree of n nodes is (n+1).
• The number of internal nodes (i.e., non-leaf nodes or n-l) in a complete binary tree of n nodes is ⌊ n/2 ⌋.
• For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 + 1.

Common operations
There are a variety of different operations that can be performed on binary trees. Some are mutator operations, while
others simply return useful information about the tree.

Insertion
Nodes can be inserted into binary trees in between two other nodes or added after an external node. In binary trees, a
node that is inserted is specified as to which child it is.

External nodes
Say that the external node being added onto is node A. To add a new node after node A, A assigns the new node as
one of its children and the new node assigns node A as its parent.

Internal nodes

Insertion on internal nodes is slightly


more complex than on external nodes.
Say that the internal node is node A
and that node B is the child of A. (If
the insertion is to insert a right child,
then B is the right child of A, and
similarly with a left child insertion.) A
assigns its child to the new node and The process of inserting a node into a binary tree

the new node assigns its parent to A.


Then the new node assigns its child to B and B assigns its parent as the new node.
Binary tree 8

Deletion
Deletion is the process whereby a node is removed from the tree. Only certain nodes in a binary tree can be removed
unambiguously.

Node with zero or one children

Say that the node to delete is node A.


If a node has no children (external
node), deletion is accomplished by
setting the child of A's parent to null. If
it has one child, set the parent of A's
child to A's parent and set the child of
A's parent to A's child.
The process of deleting an internal node in a binary tree

Node with two children


In a binary tree, a node with two children cannot be deleted unambiguously. However, in certain binary trees
(including binary search trees) these nodes can be deleted, though with a rearrangement of the tree structure.

Traversal
Pre-order, in-order, and post-order traversal visit each node in a tree by recursively visiting each node in the left and
right subtrees of the root.

Depth-first order
In depth-first order, we always attempt to visit the node farthest from the root node that we can, but with the caveat
that it must be a child of a node we have already visited. Unlike a depth-first search on graphs, there is no need to
remember all the nodes we have visited, because a tree cannot contain cycles. Pre-order is a special case of this. See
depth-first search for more information.

Breadth-first order
Contrasting with depth-first order is breadth-first order, which always attempts to visit the node closest to the root
that it has not already visited. See breadth-first search for more information. Also called a level-order traversal.
In a complete binary tree, a node's breadth-index (i - (2d - 1)) can be used as traversal instructions from the root.
Reading bitwise from left to right, starting at bit d - 1, where d is the node's distance from the root (d =
floor(log2(i+1))) and the node in question is not the root itself (d > 0). When the breadth-index is masked at bit d - 1,
the bit values 0 and 1 mean to step either left or right, respectively. The process continues by successively
checking the next bit to the right until there are no more. The rightmost bit indicates the final traversal from the
desired node's parent to the node itself. There is a time-space trade-off between iterating a complete binary tree this
way versus each node having pointer/s to its sibling/s.
Binary tree 9

Type theory
In type theory, a binary tree with nodes of type A is defined inductively as TA = μα. 1 + A × α × α.

Definition in graph theory


For each binary tree data structure, there is equivalent rooted binary tree in graph theory.
Graph theorists use the following definition: A binary tree is a connected acyclic graph such that the degree of each
vertex is no more than three. It can be shown that in any binary tree of two or more nodes, there are exactly two
more nodes of degree one than there are of degree three, but there can be any number of nodes of degree two. A
rooted binary tree is such a graph that has one of its vertices of degree no more than two singled out as the root.
With the root thus chosen, each vertex will have a uniquely defined parent, and up to two children; however, so far
there is insufficient information to distinguish a left or right child. If we drop the connectedness requirement,
allowing multiple connected components in the graph, we call such a structure a forest.
Another way of defining binary trees is a recursive definition on directed graphs. A binary tree is either:
• A single vertex.
• A graph formed by taking two binary trees, adding a vertex, and adding an edge directed from the new vertex to
the root of each binary tree.
This also does not establish the order of children, but does fix a specific root node.

Combinatorics
In combinatorics one considers the problem of counting the number of full binary trees of a given size. Here the trees
have no values attached to their nodes (this would just multiply the number of possible trees by an easily determined
factor), and trees are distinguished only by their structure; however the left and right child of any node are
distinguished (if they are different trees, then interchanging them will produce a tree distinct from the original one).
The size of the tree is taken to be the number n of internal nodes (those with two children); the other nodes are leaf
nodes and there are n + 1 of them. The number of such binary trees of size n is equal to the number of ways of fully
parenthesizing a string of n + 1 symbols (representing leaves) separated by n binary operators (representing internal
nodes), so as to determine the argument subexpressions of each operator. For instance for n = 3 one has to
parenthesize a string like , which is possible in five ways:

The correspondence to binary trees should be obvious, and the addition of redundant parentheses (around an already
parenthesized expression or around the full expression) is disallowed (or at least not counted as producing a new
possibility).
There is a unique binary tree of size 0 (consisting of a single leaf), and any other binary tree is characterized by the
pair of its left and right children; if these have sizes i and j respectively, the full tree has size i + j + 1. Therefore the
number of binary trees of size n has the following recursive description , and
for any positive integer n. It follows that is the Catalan number of index n.
The above parenthesized strings should not be confused with the set of words of length 2n in the Dyck language,
which consist only of parentheses in such a way that they are properly balanced. The number of such strings satisfies
the same recursive description (each Dyck word of length 2n is determined by the Dyck subword enclosed by the
initial '(' and its matching ')' together with the Dyck subword remaining after that closing parenthesis, whose lengths
2i and 2j satisfy i + j + 1 = n); this number is therefore also the Catalan number . So there are also five Dyck
words of length 10:
.
Binary tree 10

These Dyck words do not correspond in an obvious way to binary trees. A bijective correspondence can nevertheless
be defined as follows: enclose the Dyck word in an extra pair of parentheses, so that the result can be interpreted as a
Lisp list expression (with the empty list () as only occurring atom); then the dotted-pair expression for that proper list
is a fully parenthesized expression (with NIL as symbol and '.' as operator) describing the corresponding binary tree
(which is in fact the internal representation of the proper list).
The ability to represent binary trees as strings of symbols and parentheses implies that binary trees can represent the
elements of a free magma on a singleton set.

Methods for storing binary trees


Binary trees can be constructed from programming language primitives in several ways.

Nodes and references


In a language with records and references, binary trees are typically constructed by having a tree node structure
which contains some data and references to its left child and its right child. Sometimes it also contains a reference to
its unique parent. If a node has fewer than two children, some of the child pointers may be set to a special null value,
or to a special sentinel node.
In languages with tagged unions such as ML, a tree node is often a tagged union of two types of nodes, one of which
is a 3-tuple of data, left child, and right child, and the other of which is a "leaf" node, which contains no data and
functions much like the null value in a language with pointers.

Arrays
Binary trees can also be stored in breadth-first order as an implicit data structure in arrays, and if the tree is a
complete binary tree, this method wastes no space. In this compact arrangement, if a node has an index i, its children
are found at indices (for the left child) and (for the right), while its parent (if any) is found at index
(assuming the root has index zero). This method benefits from more compact storage and better locality of

reference, particularly during a preorder traversal. However, it is expensive to grow and wastes space proportional to
2h - n for a tree of depth h with n nodes.
This method of storage is often used for binary heaps. No space is wasted because nodes are added in breadth-first
order.
Binary tree 11

Encodings

Succinct encodings
A succinct data structure is one which occupies close to minimum possible space, as established by information
theoretical lower bounds. The number of different binary trees on nodes is , the th Catalan number
(assuming we view trees with identical structure as identical). For large , this is about ; thus we need at least
about bits to encode it. A succinct binary tree therefore would occupy bits.
One simple representation which meets this bound is to visit the nodes of the tree in preorder, outputting "1" for an
internal node and "0" for a leaf. [4] If the tree contains data, we can simply simultaneously store it in a consecutive
array in preorder. This function accomplishes this:

function EncodeSuccinct(node n, bitstring structure, array data) {


if n = nil then
append 0 to structure;
else
append 1 to structure;
append n.data to data;
EncodeSuccinct(n.left, structure, data);
EncodeSuccinct(n.right, structure, data);
}

The string structure has only bits in the end, where is the number of (internal) nodes; we don't even have
to store its length. To show that no information is lost, we can convert the output back to the original tree like this:

function DecodeSuccinct(bitstring structure, array data) {


remove first bit of structure and put it in b
if b = 1 then
create a new node n
remove first element of data and put it in n.data
n.left = DecodeSuccinct(structure, data)
n.right = DecodeSuccinct(structure, data)
return n
else
return nil
}

More sophisticated succinct representations allow not only compact storage of trees but even useful operations on
those trees directly while they're still in their succinct form.

Encoding general trees as binary trees


There is a one-to-one mapping between general ordered trees and binary trees, which in particular is used by Lisp to
represent general ordered trees as binary trees. To convert a general ordered tree to binary tree, we only need to
represent the general tree in left child-sibling way. The result of this representation will be automatically binary tree,
if viewed from a different perspective. Each node N in the ordered tree corresponds to a node N' in the binary tree;
the left child of N' is the node corresponding to the first child of N, and the right child of N' is the node corresponding
to N 's next sibling --- that is, the next node in order among the children of the parent of N. This binary tree
representation of a general order tree is sometimes also referred to as a left child-right sibling binary tree (LCRS
tree), or a doubly chained tree, or a Filial-Heir chain.
Binary tree 12

One way of thinking about this is that each node's children are in a linked list, chained together with their right
fields, and the node only has a pointer to the beginning or head of this list, through its left field.
For example, in the tree on the left, A has the 6 children {B,C,D,E,F,G}. It can be converted into the binary tree on
the right.

The binary tree can be thought of as the original tree tilted sideways, with the black left edges representing first child
and the blue right edges representing next sibling. The leaves of the tree on the left would be written in Lisp as:
(((N O) I J) C D ((P) (Q)) F (M))
which would be implemented in memory as the binary tree on the right, without any letters on those nodes that have
a left child.

Notes
[1] Unitary Symmetry, James D. Louck, World Scientific Pub., 2008
[2] Aaron M. Tenenbaum, et. al Data Structures Using C, Prentice Hall, 1990 ISBN 0-13-199746-7
[3] Paul E. Black (ed.), entry for data structure in Dictionary of Algorithms and Data Structures. U.S. National Institute of Standards and
Technology. 15 December 2004. Online version (http:/ / xw2k. nist. gov/ dads/ / HTML/ balancedtree. html) Accessed 2010-12-19.
[4] http:/ / theory. csail. mit. edu/ classes/ 6. 897/ spring03/ scribe_notes/ L12/ lecture12. pdf

References
• Donald Knuth. The art of computer programming vol 1. Fundamental Algorithms, Third Edition.
Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.3, especially subsections 2.3.1–2.3.2 (pp. 318–348).
• Kenneth A Berman, Jerome L Paul. Algorithms: Parallel, Sequential and Distributed. Course Technology, 2005.
ISBN 0-534-42057-5. Chapter 4. (pp. 113–166).

External links
• Gamedev.net introduction on binary trees (http://www.gamedev.net/page/resources/_/technical/
general-programming/trees-part-2-binary-trees-r1433)
• Binary Tree Proof by Induction (http://www.brpreiss.com/books/opus4/html/page355.html)
• Balanced binary search tree on array How to create bottom-up an Ahnentafel list, or a balanced binary search tree
on array (http://piergiu.wordpress.com/2010/02/21/balanced-binary-search-tree-on-array/)
Binary search tree 13

Binary search tree


Binary search
tree
Type Tree

Time complexity
in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(n)

Insert O(log n) O(n)

Delete O(log n) O(n)

In computer science, a binary search tree (BST), sometimes also


called an ordered or sorted binary tree, is a node-based binary
tree data structure which has the following properties:
• The left subtree of a node contains only nodes with keys less
than the node's key.
• The right subtree of a node contains only nodes with keys
greater than the node's key.
• The left and right subtree each must also be a binary search
tree.
• There must be no duplicate nodes.
Generally, the information represented by each node is a record A binary search tree of size 9 and depth 3, with root 8
rather than a single data element. However, for sequencing and leaves 1, 4, 7 and 13

purposes, nodes are compared according to their keys rather than


any part of their associated records.
The major advantage of binary search trees over other data structures is that the related sorting algorithms and search
algorithms such as in-order traversal can be very efficient.
Binary search trees are a fundamental data structure used to construct more abstract data structures such as sets,
multisets, and associative arrays.

Binary-search-tree property
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key < x.key. If y is a node in the
right subtree of x, then y.key > x.key.

Operations
Operations, such as find, on a binary search tree require comparisons between nodes. These comparisons are made
with calls to a comparator, which is a subroutine that computes the total order (linear order) on any two keys. This
comparator can be explicitly or implicitly defined, depending on the language in which the binary search tree was
implemented. A common comparator is the less-than function, for example, a < b, where a and b are keys of two
nodes a and b in a binary search tree.
Binary search tree 14

Searching
Searching a binary search tree for a specific key can be a recursive or an iterative process.
We begin by examining the root node. If the tree is null, the key we are searching for does not exist in the tree.
Otherwise, if the key equals that of the root, the search is successful and we return the node. If the key is less than
that of the root, we search the left subtree. Similarly, if the key is greater than that of the root, we search the right
subtree. This process is repeated until the key is found or the remaining subtree is null. If the searched key is not
found before a null subtree is reached, then the item must not be present in the tree. This is easily expressed as a
recursive algorithm:

function Find-recursive(key, node): // call initially with node = root


if node = Null or node.key = key then
return node
else if key < node.key then
return Find-recursive(key, node.left)
else
return Find-recursive(key, node.right)

The same algorithm can be implemented iteratively:

function Find(key, root):


current-node := root
while current-node is not Null do
if current-node.key = key then
return current-node
else if key < current-node.key then
current-node := current-node.left
else
current-node := current-node.right

Because in the worst case this algorithm must search from the root of the tree to the leaf farthest from the root, the
search operation takes time proportional to the tree's height (see tree terminology). On average, binary search trees
with n nodes have O(log n) height. However, in the worst case, binary search trees can have O(n) height, when the
unbalanced tree resembles a linked list (degenerate tree).

Insertion
Insertion begins as a search would begin; if the key is not equal to that of the root, we search the left or right subtrees
as before. Eventually, we will reach an external node and add the new key-value pair (here encoded as a record
'newNode') as its right or left child, depending on the node's key. In other words, we examine the root and
recursively insert the new node to the left subtree if its key is less than that of the root, or the right subtree if its key
is greater than or equal to the root.
Here's how a typical binary search tree insertion might be performed in a non-empty tree in C++:

void insert(Node* node, int value) {


if (value < node->key) {
if (node->leftChild == NULL)
node->leftChild = new Node(value);
else
insert(node->leftChild, value);
} else {
Binary search tree 15

if(node->rightChild == NULL)
node->rightChild = new Node(value);
else
insert(node->rightChild, value);
}
}

The above destructive procedural variant modifies the tree in place. It uses only constant heap space (and the
iterative version uses constant stack space as well), but the prior version of the tree is lost. Alternatively, as in the
following Python example, we can reconstruct all ancestors of the inserted node; any reference to the original tree
root remains valid, making the tree a persistent data structure:

def binary_tree_insert(node, key, value):


if node is None:
return TreeNode(None, key, value, None)
if key == node.key:
return TreeNode(node.left, key, value, node.right)
if key < node.key:
return TreeNode(binary_tree_insert(node.left, key, value),
node.key, node.value, node.right)
else:
return TreeNode(node.left, node.key, node.value,
binary_tree_insert(node.right, key, value))

The part that is rebuilt uses O(log n) space in the average case and O(n) in the worst case (see big-O notation).
In either version, this operation requires time proportional to the height of the tree in the worst case, which is O(log
n) time in the average case over all trees, but O(n) time in the worst case.
Another way to explain insertion is that in order to insert a new node in the tree, its key is first compared with that of
the root. If its key is less than the root's, it is then compared with the key of the root's left child. If its key is greater, it
is compared with the root's right child. This process continues, until the new node is compared with a leaf node, and
then it is added as this node's right or left child, depending on its key.
There are other ways of inserting nodes into a binary tree, but this is the only way of inserting nodes at the leaves
and at the same time preserving the BST structure.

Deletion
There are three possible cases to consider:
• Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.
• Deleting a node with one child: Remove the node and replace it with its child.
• Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its
in-order successor node or its in-order predecessor node, R. Replace the value of N with the value of R, then
delete R.
Broadly speaking, nodes with children are harder to delete. As with all binary trees, a node's in-order successor is its
right subtree's left-most child, and a node's in-order predecessor is the left subtree's right-most child. In either case,
this node will have zero or one children. Delete it according to one of the two simpler cases above.
Binary search tree 16

Deleting a node with two children from a binary search tree. First the rightmost node in the left subtree, the inorder predecessor 6, is identified. Its
value is copied into the node being deleted. The inorder predecessor can then be easily deleted because it has at most one child. The same method
works symmetrically using the inorder successor labelled 9.

Consistently using the in-order successor or the in-order predecessor for every instance of the two-child case can
lead to an unbalanced tree, so some implementations select one or the other at different times.
Runtime analysis: Although this operation does not always traverse the tree down to a leaf, this is always a
possibility; thus in the worst case it requires time proportional to the height of the tree. It does not require more even
when the node has two children, since it still follows a single path and does not visit any node twice.

def find_min(self): # Gets minimum node (leftmost leaf) in a subtree


current_node = self
while current_node.left_child:
current_node = current_node.left_child
return current_node

def replace_node_in_parent(self, new_value=None):


if self.parent:
if self == self.parent.left_child:
self.parent.left_child = new_value
else:
self.parent.right_child = new_value
if new_value:
new_value.parent = self.parent

def binary_tree_delete(self, key):


if key < self.key:
self.left_child.binary_tree_delete(key)
elif key > self.key:
self.right_child.binary_tree_delete(key)
else: # delete the key here
if self.left_child and self.right_child: # if both children are
present
successor = self.right_child.find_min()
self.key = successor.key
successor.binary_tree_delete(successor.key)
elif self.left_child: # if the node has only a *left* child
self.replace_node_in_parent(self.left_child)
Binary search tree 17

elif self.right_child: # if the node has only a *right* child


self.replace_node_in_parent(self.right_child)
else: # this node has no children
self.replace_node_in_parent(None)

Traversal
Once the binary search tree has been created, its elements can be retrieved in-order by recursively traversing the left
subtree of the root node, accessing the node itself, then recursively traversing the right subtree of the node,
continuing this pattern with each node in the tree as it's recursively accessed. As with all binary trees, one may
conduct a pre-order traversal or a post-order traversal, but neither are likely to be useful for binary search trees. An
in-order traversal of a binary search tree will always result in a sorted list of node items (numbers, strings or other
comparable items).
The code for in-order traversal in Python is given below. It will call callback for every node in the tree.

def traverse_binary_tree(node, callback):


if node is None:
return
traverse_binary_tree(node.leftChild, callback)
callback(node.value)
traverse_binary_tree(node.rightChild, callback)

Traversal requires O(n) time, since it must visit every node. This algorithm is also O(n), so it is asymptotically
optimal.

Sort
A binary search tree can be used to implement a simple but efficient
sorting algorithm. Similar to heapsort, we insert all the values we wish
to sort into a new ordered data structure—in this case a binary search
tree—and then traverse it in order, building our result:

def build_binary_tree(values):
tree = None
for v in values:
Binary search tree 18

tree = binary_tree_insert(tree, v)
return tree

def get_inorder_traversal(root):
'''
Returns a list containing all the values in the tree, starting at
*root*.
Traverses the tree in-order(leftChild, root, rightChild).
'''
result = []
traverse_binary_tree(root, lambda element: result.append(element))
return result

The worst-case time of build_binary_tree is —if you feed it a sorted list of values, it chains them
into a linked list with no left subtrees. For example, build_binary_tree([1, 2, 3, 4, 5]) yields the
tree (1 (2 (3 (4 (5))))).
There are several schemes for overcoming this flaw with simple binary trees; the most common is the self-balancing
binary search tree. If this same procedure is done using such a tree, the overall worst-case time is O(nlog n), which is
asymptotically optimal for a comparison sort. In practice, the poor cache performance and added overhead in time
and space for a tree-based sort (particularly for node allocation) make it inferior to other asymptotically optimal sorts
such as heapsort for static list sorting. On the other hand, it is one of the most efficient methods of incremental
sorting, adding items to a list over time while keeping the list sorted at all times.

Types
There are many types of binary search trees. AVL trees and red-black trees are both forms of self-balancing binary
search trees. A splay tree is a binary search tree that automatically moves frequently accessed elements nearer to the
root. In a treap (tree heap), each node also holds a (randomly chosen) priority and the parent node has higher priority
than its children. Tango trees are trees optimized for fast searches.
Two other titles describing binary search trees are that of a complete and degenerate tree.
A complete tree is a tree with n levels, where for each level d <= n - 1, the number of existing nodes at level d is
equal to 2d. This means all possible nodes exist at these levels. An additional requirement for a complete binary tree
is that for the nth level, while every node does not have to exist, the nodes that do exist must fill from left to right.
A degenerate tree is a tree where for each parent node, there is only one associated child node. What this means is
that in a performance measurement, the tree will essentially behave like a linked list data structure.
Binary search tree 19

Performance comparisons
D. A. Heger (2004) presented a performance comparison of binary search trees. Treap was found to have the best
average performance, while red-black tree was found to have the smallest amount of performance variations.

Optimal binary search trees


If we do not plan on modifying a search
tree, and we know exactly how often each
item will be accessed, we can construct an
optimal binary search tree, which is a
search tree where the average cost of
looking up an item (the expected search
cost) is minimized.

Even if we only have estimates of the search


costs, such a system can considerably speed
up lookups on average. For example, if you
have a BST of English words used in a spell Tree rotations are very common internal operations in binary trees to keep perfect,
checker, you might balance the tree based or near-to-perfect, internal balance in the tree.

on word frequency in text corpora, placing


words like the near the root and words like agerasia near the leaves. Such a tree might be compared with Huffman
trees, which similarly seek to place frequently used items near the root in order to produce a dense information
encoding; however, Huffman trees only store data elements in leaves and these elements need not be ordered.

If we do not know the sequence in which the elements in the tree will be accessed in advance, we can use splay trees
which are asymptotically as good as any static search tree we can construct for any particular sequence of lookup
operations.
Alphabetic trees are Huffman trees with the additional constraint on order, or, equivalently, search trees with the
modification that all elements are stored in the leaves. Faster algorithms exist for optimal alphabetic binary trees
(OABTs).

References

Further reading
• Paul E. Black, Binary Search Tree (http://www.nist.gov/dads/HTML/binarySearchTree.html) at the NIST
Dictionary of Algorithms and Data Structures.
• Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). "12: Binary search trees,
15.5: Optimal binary search trees". Introduction to Algorithms (2nd ed.). MIT Press & McGraw-Hill.
pp. 253–272, 356–363. ISBN 0-262-03293-7.
• Jarc, Duane J. (3 December 2005). "Binary Tree Traversals" (http://nova.umuc.edu/~jarc/idsv/lesson1.html).
Interactive Data Structure Visualizations. University of Maryland.
• Knuth, Donald (1997). "6.2.2: Binary Tree Searching". The Art of Computer Programming. 3: "Sorting and
Searching" (3rd ed.). Addison-Wesley. pp. 426–458. ISBN 0-201-89685-0.
• Long, Sean. "Binary Search Tree" (http://employees.oneonta.edu/zhangs/PowerPointPlatform/resources/
samples/binarysearchtree.ppt) (PPT). Data Structures and Algorithms Visualization-A PowerPoint Slides Based
Approach. SUNY Oneonta.
• Parlante, Nick (2001). "Binary Trees" (http://cslibrary.stanford.edu/110/BinaryTrees.html). CS Education
Library. Stanford University.
Binary search tree 20

External links
• Literate implementations of binary search trees in various languages (http://en.literateprograms.org/
Category:Binary_search_tree) on LiteratePrograms
• Goleta, Maksim (27 November 2007). "Goletas.Collections" (http://goletas.com/csharp-collections/).
goletas.com. Includes an iterative C# implementation of AVL trees.
• Jansens, Dana. "Persistent Binary Search Trees" (http://cg.scs.carleton.ca/~dana/pbst). Computational
Geometry Lab, School of Computer Science, Carleton University. C implementation using GLib.
• Binary Tree Visualizer (http://btv.melezinek.cz) (JavaScript animation of various BT-based data structures)
• Kovac, Kubo. "Binary Search Trees" (http://people.ksp.sk/~kuko/bak/) (Java applet). Korešpondenčný
seminár z programovania.
• Madru, Justin (18 August 2009). "Binary Search Tree" (http://jdserver.homelinux.org/wiki/
Binary_Search_Tree). JDServer. C++ implementation.
• Tarreau, Willy (2011). "Elastic Binary Trees (ebtree)" (http://1wt.eu/articles/ebtree/). 1wt.eu.
• Binary Search Tree Example in Python (http://code.activestate.com/recipes/286239/)
• "References to Pointers (C++)" (http://msdn.microsoft.com/en-us/library/1sf8shae(v=vs.80).aspx). MSDN.
Microsoft. 2005. Gives an example binary tree implementation.
• Igushev, Eduard. "Binary Search Tree C++ implementation" (http://igushev.com/implementations/
binary-search-tree-cpp/).
• Stromberg, Daniel. "Python Search Tree Empirical Performance Comparison" (http://stromberg.dnsalias.org/
~strombrg/python-tree-and-heap-comparison/).

Infix notation

• Prefix notation
• Infix notation
• Postfix notation

• v
• t
• e [1]

Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style
between the operands they act on (e.g. 2 + 2). It is not as simple to parse by computers as prefix notation ( e.g. + 2 2
) or postfix notation ( e.g. 2 2 + ), but many programming languages use it due to its familiarity.
In infix notation, unlike in prefix or postfix notations, parentheses surrounding groups of operands and operators are
necessary to indicate the intended order in which operations are to be performed. In the absence of parentheses,
certain precedence rules determine the order of operations.
Infix notation may also be distinguished from function notation, where the name of a function suggests a particular
operation, and its arguments are the operands. An example of such a function notation would be S(1,3) in which the
function S denotes addition: S(1,3) = 1+3 = 4.
Infix notation 21

External links
• RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic [2]
• Infix to postfix convertor [3]

References
[1] http:/ / en. wikipedia. org/ w/ index. php?title=Template:Operator_notation& action=edit
[2] http:/ / www. xnumber. com/ xnumber/ rpn_or_adl. htm
[3] http:/ / www. meta-calculator. com/ learning-lab/ how-to-build-scientific-calculator/ infix-to-postifix-convertor. php
Complete graph 22

Complete graph
Complete graph

K7, a complete graph with 7 vertices

Vertices n

Edges

Radius

Diameter

Girth

Automorphisms n! (Sn)

Chromatic number n

Chromatic index n if n is odd


n − 1 if n is even

Spectrum

Properties (n − 1)-regular
Symmetric graph
Vertex-transitive
Edge-transitive
Strongly regular
Integral

Notation Kn

• v
• t
[1]
• e

In the mathematical field of graph theory, a complete graph is a simple undirected graph in which every pair of
distinct vertices is connected by a unique edge.  A complete digraph is a directed graph in which every pair of
distinct vertices is connected by a pair of unique edges (one in each direction).
Graph theory itself is typically dated as beginning with Leonhard Euler's 1736 work on the Seven Bridges of
Königsberg. However, drawings of complete graphs, with their vertices placed on the points of a regular polygon,
appeared already in the 13th century, in the work of Ramon Llull. Such a drawing is sometimes referred to as a
mystic rose.
Complete graph 23

Properties
The complete graph on n vertices is denoted by Kn.  Some sources claim that the letter K in this notation stands for
the German word komplett, but the German name for a complete graph, vollständiger Graph, does not contain the
letter K, and other sources state that the notation honors the contributions of Kazimierz Kuratowski to graph theory.
Kn has n(n − 1)/2 edges (a triangular number), and is a regular graph of degree n − 1.  All complete graphs are their
own maximal cliques.  They are maximally connected as the only vertex cut which disconnects the graph is the
complete set of vertices.  The complement graph of a complete graph is an empty graph.
If the edges of a complete graph are each given an orientation, the resulting directed graph is called a tournament.
The number of matchings of the complete graphs are given by the telephone numbers
1, 1, 2, 4, 10, 26, 76, 232, 764, 2620, 9496, ... (sequence A000085 in OEIS).
These numbers give the largest possible value of the Hosoya index for an n-vertex graph. The number of perfect
matchings of the complete graph Kn (with n even) is given by the double factorial (n − 1)!!.
The crossing numbers up to K27 are known, with K28 requiring either 7233 or 7234 crossings. Further values are
collected by the Rectilinear Crossing Number project. Crossing numbers for K5 through K18 are
1, 3, 9, 19, 36, 62, 102, 153, 229, 324, 447, 603, 798, 1029, ... (sequence A014540 in OEIS).

Geometry and topology


A complete graph with n nodes represents the edges of an (n − 1)-simplex.  Geometrically K3 forms the edge set of a
triangle, K4 a tetrahedron, etc.  The Császár polyhedron, a nonconvex polyhedron with the topology of a torus, has
the complete graph K7 as its skeleton.  Every neighborly polytope in four or more dimensions also has a complete
skeleton.
K1 through K4 are all planar graphs.  However, every planar drawing of a complete graph with five or more vertices
must contain a crossing, and the nonplanar complete graph K5 plays a key role in the characterizations of planar
graphs: by Kuratowski's theorem, a graph is planar if and only if it contains neither K5 nor the complete bipartite
graph K3,3 as a subdivision, and by Wagner's theorem the same result holds for graph minors in place of
subdivisions.  As part of the Petersen family, K6 plays a similar role as one of the forbidden minors for linkless
embedding. In other words, and as Conway and Gordon proved, every embedding of K6 is intrinsically linked, with
at least one pair of linked triangles.  Conway and Gordon also showed that any embedding of K7 contains a knotted
Hamiltonian cycle.

Examples
Complete graphs on n vertices, for n between 1 and 12, are shown below along with the numbers of edges:

K1: 0 K2: 1 K3: 3 K4: 6

K5: 10 K6: 15 K7: 21 K8: 28


Complete graph 24

K9: 36 K10: 45 K11: 55 K12: 66

References

External links
• Weisstein, Eric W., " Complete Graph (http://mathworld.wolfram.com/CompleteGraph.html)", MathWorld.

Polish notation

• Prefix notation
• Infix notation
• Postfix notation

• v
• t
• e [1]

Polish notation, also known as Polish prefix notation or simply prefix notation, is a form of notation for logic,
arithmetic, and algebra. Its distinguishing feature is that it places operators to the left of their operands. If the arity of
the operators is fixed, the result is a syntax lacking parentheses or other brackets that can still be parsed without
ambiguity. The Polish logician Jan Łukasiewicz invented this notation in 1924 in order to simplify sentential logic.
The term Polish notation is sometimes taken (as the opposite of infix notation) to also include Polish postfix notation,
or Reverse Polish notation, in which the operator is placed after the operands.
When Polish notation is used as a syntax for mathematical expressions by interpreters of programming languages, it
is readily parsed into abstract syntax trees and can, in fact, define a one-to-one representation for the same. Because
of this, Lisp (see below) and related programming languages define their entire syntax in terms of prefix notation
(and others use postfix notation).
Here is a quotation from a paper by Jan Łukasiewicz, Remarks on Nicod's Axiom and on "Generalizing Deduction",
page 180.
"I came upon the idea of a parenthesis-free notation in 1924. I used that notation for the first time in my
article Łukasiewicz(1), p. 610, footnote."
The reference cited by Jan Łukasiewicz above is apparently a lithographed report in Polish. The referring paper by
Łukasiewicz Remarks on Nicod's Axiom and on "Generalizing Deduction" was reviewed by H. A. Pogorzelski in the
Journal of Symbolic Logic in 1965.[1]
Alonzo Church mentions this notation in his classic book on mathematical logic as worthy of remark in notational
systems even contrasted to Whitehead and Russell's logical notational exposition and work in Principia
Mathematica.[2]
In Łukasiewicz 1951 book, Aristotle’s Syllogistic from the Standpoint of Modern Formal Logic, he mentions that the
principle of his notation was to write the functors before the arguments to avoid brackets and that he had employed
Polish notation 25

his notation in his logical papers since 1929.[3] He then goes on to cite, as an example, a 1930 paper he wrote with
Alfred Tarski on the sentential calculus.[4]
While no longer used much in logic,[citation needed] Polish notation has since found a place in computer science.

Arithmetic
The expression for adding the numbers 1 and 2 is, in prefix notation, written "+ 1 2" rather than "1 + 2". In more
complex expressions, the operators still precede their operands, but the operands may themselves be nontrivial
expressions including operators of their own. For instance, the expression that would be written in conventional infix
notation as
(5 − 6) * 7
can be written in prefix as
* (− 5 6) 7
Since the simple arithmetic operators are all binary (at least, in arithmetic contexts), any prefix representation thereof
is unambiguous, and bracketing the prefix expression is unnecessary. As such, the previous expression can be further
simplified to
*−567
The processing of the product is deferred until its two operands are available (i.e., 5 minus 6, and 7). As with any
notation, the innermost expressions are evaluated first, but in prefix notation this "innermost-ness" can be conveyed
by order rather than bracketing.
In the classical notation, the parentheses in the infix version were required, since moving them
5 − (6 * 7)
or simply removing them
5−6*7
would change the meaning and result of the overall expression, due to the precedence rule.
Similarly
5 − (6 * 7)
can be written in Polish notation as
−5*67

Computer programming
Prefix notation has seen wide application in Lisp s-expressions, where the brackets are required since the operators
in the language are themselves data (first-class functions). Lisp functions may also have variable arity. The Ambi [5]
programming language uses Polish Notation for arithmetic operations and program construction. The postfix reverse
Polish notation is used in many stack-based programming languages like PostScript and Forth, and is the operating
principle of certain calculators, notably from Hewlett-Packard.
The number of return values of an expression equals the difference between the number of operands in an expression
and the total arity of the operators minus the total number of return values of the operators.
Polish notation 26

Order of operations
Order of operations is defined within the structure of prefix notation and can be easily determined. One thing to keep
in mind is that when executing an operation, the operation is applied to the first operand by the second operand. This
is not an issue with operations that commute, but for non-commutative operations like division or subtraction, this
fact is crucial to the analysis of a statement. For example, the following statement:

/ 10 5 = 2

is read as "divide 10 by 5". Thus the solution is 2, not 1/2 as would be the result of an incorrect analysis.
Prefix notation is especially popular with stack-based operations due to its innate ability to easily distinguish order of
operations without the need for parentheses. To evaluate order of operations under prefix notation, one does not even
need to memorize an operational hierarchy, as with infix notation. Instead, one looks directly to the notation to
discover which operator to evaluate first. Reading an expression from left to right, one first looks for an operator and
proceeds to look for two operands. If another operator is found before two operands are found, then the old operator
is placed aside until this new operator is resolved. This process iterates until an operator is resolved, which must
happen eventually, as there must be one more operand than there are operators in a complete statement. Once
resolved, the operator and the two operands are replaced with a new operand. Because one operator and two
operands are removed and one operand is added, there is a net loss of one operator and one operand, which still
leaves an expression with N operators and N + 1 operands, thus allowing the iterative process to continue. This is the
general theory behind using stacks in programming languages to evaluate a statement in prefix notation, although
there are various algorithms that manipulate the process. Once analyzed, a statement in prefix notation becomes less
intimidating to the human mind as it allows some separation from convention with added convenience. An example
shows the ease with which a complex statement in prefix notation can be deciphered through order of operations:

− * / 15 − 7 + 1 1 3 + 2 + 1 1 =
− * / 15 − 7 2 3 + 2 + 1 1 =
− * / 15 5 3 + 2 + 1 1 =
−* 3 3 + 2 + 1 1 =
− 9 + 2 + 1 1 =
− 9 + 2 2 =
− 9 4 =
5

An equivalent in-fix is as follows: ((15 / (7 − (1 + 1))) * 3) − (2 + (1 + 1)) = 5


Here is an implementation (in pseudocode) of prefix evaluation using a stack. Note that under this implementation
the input string is scanned from right to left. This differs from the algorithm described above in which the string is
processed from left to right. Both algorithms compute the same value for all valid strings.

Scan the given prefix expression from right to left


for each symbol
{
if operand then
push onto stack
if operator then
{
operand1=pop stack
operand2=pop stack
compute operand1 operator operand2
push result onto stack
Polish notation 27

}
}
return top of stack as result

Example
This uses the same expression as before and the algorithm above.
− * / 15 − 7 + 1 1 3 + 2 + 1 1

Token Action Stack Notes

1 Operand 1 Push onto stack.

1 Operand 1 1 Push onto stack.

+ Operator 2 Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.

2 Operand 2 2 Push onto stack.

+ Operator 4 Pop the two operands (2, 2), calculate (2 + 2 = 4) and push onto stack.

3 Operand 3 4 Push onto stack.

1 Operand 1 3 4 Push onto stack.

1 Operand 1 1 3 4 Push onto stack.

+ Operator 2 3 4 Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.

7 Operand 7 2 3 4 Push onto stack.

− Operator 5 3 4 Pop the two operands (7, 2), calculate (7 − 2 = 5) and push onto stack.

15 Operand 15 5 3 4 Push onto stack.

/ Operator 3 3 4 Pop the two operands (15, 5), calculate (15/5 = 3) and push onto stack.

* Operator 9 4 Pop the two operands (3, 3), calculate (3 * 3 = 9) and push onto stack.

− Operator 5 Pop the two operands (9, 4), calculate (9 − 4 = 5) and push onto stack.

The result is at the top of the stack.

Polish notation for logic


The table below shows the core of Jan Łukasiewicz's notation for sentential logic.[citation needed] The "conventional"
notation did not become so until the 1970s and 80s.[citation needed] Some letters in the Polish notation table stand for
particular words in Polish, as shown:

Concept Conventional Polish Polish


notation notation word

Negation Nφ negacja

Conjunction Kφψ koniunkcja

Disjunction Aφψ alternatywa

Material conditional Cφψ implikacja

Biconditional Eφψ ekwiwalencja

Falsum O fałsz

Sheffer stroke Dφψ dysjunkcja

Possibility Mφ możliwość
Polish notation 28

Necessity Lφ konieczność

Universal quantifier Πpφ kwantyfikator ogólny

Existential quantifier Σpφ kwantyfikator szczegółowy

Note that the quantifiers ranged over propositional values in Łukasiewicz's work on many-valued logics.
Bocheński introduced an incompatible system of Polish notation that names all 16 binary connectives of classical
propositional logic.[6]

References
[1] Pogorzelski, H. A., "Reviewed work(s): Remarks on Nicod's Axiom and on "Generalizing Deduction" by Jan Łukasiewicz; Jerzy Słupecki;
Państwowe Wydawnictwo Naukowe" (http:/ / www. jstor. org/ stable/ 2269644), The Journal of Symbolic Logic, Vol. 30, No. 3 (Sep. 1965),
pp. 376–377. The original paper by Jan Łukasiewicz was published in Warsaw in 1961 in a volume edited by Jerzy Słupecki.
[2] – p. 38: "Worthy of remark is the parenthesis-free notation of Jan Łukasiewicz. In this the letters N, A, C, E, K are used in the roles of
negation, disjunction, implication, equivalence, conjunction respectively. ..."
[3] Cf. Łukasiewicz, (1951) Aristotle’s Syllogistic from the Standpoint of Modern Formal Logic, Chapter IV "Aristotle's System in Symbolic
Form" (section on "Explanation of the Symbolism"), p. 78 and on.
[4] Łukasiewicz, Jan; Tarski, Alfred, "Untersuchungen über den Aussagenkalkül" ["Investigations into the sentential calculus"], Comptes Rendus
des séances de la Société des Sciences et des Lettres de Varsovie, Vol, 23 (1930) Cl. III, pp. 31–32
[5] https:/ / code. google. com/ p/ ambi/
[6] Bocheński, Józef Maria (1959). A Precis of Mathematical Logic, translated by Otto Bird from the French and German editions, D. Reidel:
Dordrecht, Holland.

Further reading
• Łukasiewicz, Jan (1957). Aristotle’s Syllogistic from the Standpoint of Modern Formal Logic. Oxford University
Press.
• Łukasiewicz, Jan, "Philosophische Bemerkungen zu mehrwertigen Systemen des Aussagenkalküls", Comptes
rendus des séances de la Société des Sciences et des Lettres de Varsovie, 23:51-77 (1930). Translated by H.
Weber as "Philosophical Remarks on Many-Valued Systems of Propositional Logics", in Storrs McCall, Polish
Logic 1920-1939, Clarendon Press: Oxford (1967).
Reverse Polish notation 29

Reverse Polish notation

• Prefix notation
• Infix notation
• Postfix notation

• v
• t
• e [1]

Reverse Polish notation (RPN) is a mathematical notation in which every operator follows all of its operands, in
contrast to Polish notation, which puts the operator in the prefix position. It is also known as postfix notation and is
parenthesis-free as long as operator arities are fixed. The description "Polish" refers to the nationality of logician Jan
Łukasiewicz, who invented (prefix) Polish notation in the 1920s.
The reverse Polish scheme was proposed in 1954 by Burks, Warren, and Wright[1] and was independently reinvented
by F. L. Bauer and E. W. Dijkstra in the early 1960s to reduce computer memory access and utilize the stack to
evaluate expressions. The algorithms and notation for this scheme were extended by Australian philosopher and
computer scientist Charles Hamblin in the mid-1950s.[2][3]
During the 1970s and 1980s, RPN was known to many calculator users, as it was used in some handheld calculators
of the time designed for advanced users: for example, the HP-10C series and Sinclair Scientific calculators.
In computer science, postfix notation is often used in stack-based and concatenative programming languages. It is
also common in dataflow and pipeline-based systems, including Unix pipelines.
Most of what follows is about binary operators. A unary operator for which the reverse Polish notation is the general
convention is the factorial.

Explanation
In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, one would write "3 4 +"
rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand; so the
expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: first subtract 4 from 3,
then add 5 to that. An advantage of RPN is that it obviates the need for parentheses that are required by infix. While
"3 - 4 * 5" can also be written "3 - (4 * 5)", that means something quite different from "(3 - 4) * 5". In postfix, the
former could be written "3 4 5 * -", which unambiguously means "3 (4 5 *) -" which reduces to "3 20 -"; the latter
could be written "3 4 - 5 *" (or 5 3 4 - *, if you wish to keep similar formatting), which unambiguously means
"(3 4 -) 5 *".
Despite the name, reverse Polish notation is not exactly the reverse of Polish notation, for the operands of
non-commutative operations are still written in the conventional order (e.g. "/ 6 3" in Polish notation and "6 3 /" in
reverse Polish both evaluate to 2, whereas "3 6 /" in reverse Polish notation would evaluate to ½).

Practical implications
In comparison testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to
faster calculations, for two reasons. Because reverse Polish calculators do not need expressions to be parenthesized,
fewer operations need to be entered to perform typical calculations. Additionally, users of reverse Polish calculators
made fewer mistakes than for other types of calculator. Later research clarified that the increased speed from reverse
Polish notation may be attributed to the smaller number of keystrokes needed to enter this notation, rather than to a
Reverse Polish notation 30

smaller cognitive load on its users. However, anecdotal evidence suggests that reverse Polish notation is more
difficult for users to learn than algebraic notation.

Postfix algorithm
The algorithm for evaluating any postfix expression is fairly straightforward:
• While there are input tokens left
• Read the next token from input.
• If the token is a value
• Push it onto the stack.
• Otherwise, the token is an operator (operator here includes both operators and functions).
• It is known a priori that the operator takes n arguments.
• If there are fewer than n values on the stack
• (Error) The user has not input sufficient values in the expression.
• Else, Pop the top n values from the stack.
• Evaluate the operator, with the values as arguments.
• Push the returned results, if any, back onto the stack.
• If there is only one value in the stack
• That value is the result of the calculation.
• Otherwise, there are more values in the stack
• (Error) The user input has too many values.

Example
The infix expression "5 + ((1 + 2) * 4) − 3" can be written down like this in RPN:
512+4*+3-
The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the
list of values the algorithm is "keeping track of" after the Operation given in the middle column has taken place):

Input Operation Stack Comment

5 Push value 5

1 Push value 1
5

2 Push value 2
1
5

+ Add 3 Pop two values (1, 2) and push result (3)


5

4 Push value 4
3
5

* Multiply 12 Pop two values (3, 4) and push result (12)


5

+ Add 17 Pop two values (5, 12) and push result (17)

3 Push value 3
17

− Subtract 14 Pop two values (17, 3) and push result (14)


Reverse Polish notation 31

Result (14)

When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14.
The above example could be rewritten by following the "chain calculation" method described by HP for their series
of RPN calculators:[4]
As was demonstrated in the Algebraic mode, it is usually easier (fewer keystrokes) in working a
problem like this to begin with the arithmetic operations inside the parentheses first.
12+4*5+3−

Converting from infix notation


Edsger Dijkstra invented the shunting-yard algorithm to convert infix expressions to postfix (RPN), so named
because its operation resembles that of a railroad shunting yard.
There are other ways of producing postfix expressions from infix notation. Most operator-precedence parsers can be
modified to produce postfix expressions; in particular, once an abstract syntax tree has been constructed, the
corresponding postfix expression is given by a simple post-order traversal of that tree.

Implementations

History of implementations
The first computers to implement architectures enabling RPN were the English Electric Company's KDF9 machine,
which was announced in 1960 and delivered (i.e. made available commercially) in 1963, and the American
Burroughs B5000, announced in 1961 and also delivered in 1963. One of the designers of the B5000, Robert S.
Barton, later wrote that he developed RPN independently of Hamblin sometime in 1958 while reading a textbook by
"Kopi" (likely Irving Copi, who was at the University of Michigan at the time) on symbolic logic[5][6] and before he
was aware of Hamblin's work.

Hewlett-Packard

Friden introduced RPN to the desktop calculator market with the EC-130 in
June 1963. Hewlett-Packard engineers designed the 9100A Desktop
Calculator in 1968 with RPN. This calculator popularized RPN among the
scientific and engineering communities, even though early advertisements
for the 9100A failed to mention RPN. The HP-35, the world's first
handheld scientific calculator, used RPN in 1972. HP used RPN on every
handheld calculator it sold, whether scientific, financial, or programmable,
until it introduced the HP-10 adding machine calculator in 1977. By this
time HP was the leading manufacturer of calculators for professionals,
including engineers and accountants.

HP introduced an LCD-based line of calculators in the early 1980s that


used RPN, such as the HP-10C, HP-11C, HP-15C, HP-16C, and the
famous financial calculator, the HP-12C. When Hewlett-Packard
introduced a later business calculator, the HP-19B, without RPN, feedback
from financiers and others used to the 12C compelled them to release the A promotional Hewlett-Packard "No
HP-19BII[citation needed], which gave users the option of using algebraic Equals" hat from the 1980s - both a boast
and a reference to RPN.
Reverse Polish notation 32

notation or RPN. From 1990 to 2003 HP manufactured the HP-48 series of graphing RPN calculators and in 2006
introduced the HP-50g with a 131x80 LCD and a 75 MHz ARM CPU that emulates the Saturn CPU of the HP-48
series.
As of 2011, Hewlett-Packard is producing the calculator models 12C, 12C Platinum, 17BII, 20B (financial), 30B
(business), 33S, 35S, 48GII and 50G (scientific) which support RPN.[7]

Prinztronic
Prinz and Prinztronic were own-brand trade names of the British Dixons photographic and electronic goods stores
retail chain, which was later rebranded as Currys Digital stores, and became part of DSG International. A variety of
calculator models was sold in the 1970s under the Prinztronic brand, all made for them by other companies.
Among these was the PROGRAM [8] Programmable Scientific Calculator which featured RPN.

Soviet Union
Soviet programmable calculators (MK-52, MK-61, B3-34 and earlier B3-21[9] models) used RPN for both automatic
mode and programming. Modern Russian calculators MK-161[10] and MK-152,[11] designed and manufactured in
Novosibirsk since 2007 and offered by Semico [12], are backward compatible with them. Their extended architecture
is also based on reverse Polish notation.

Current implementations
Existing implementations using reverse Polish notation include:
• Any stack-oriented programming language, such as:
• Forth
• Factor
• PostScript page description language
• Befunge
• Joy
• Hardware calculators:
• Some Hewlett-Packard science/engineering and business/finance calculators
• Semico calculators
• Software calculators:
• Mac OS X Calculator
• Several Apple iPhone applications e.g. "reverse polish notation calculator"
• Several Android applications e.g. "RealCalc"
• Unix system calculator program dc
• Emacs lisp library package calc
• Xorg calculator (xcalc)
• F-Correlatives in MultiValue dictionary items
• RRDTool, a widely used tabulating and graphing software
• grdmath, a program for algebraic operations on NetCDF grids, part of Generic Mapping Tools (GMT) suite
Reverse Polish notation 33

References
[1] "An Analysis of a Logical Machine Using Parenthesis-Free Notation," (http:/ / www. jstor. org/ pss/ 2001990) by Arthur W. Burks, Don W.
Warren and Jesse B. Wright, 1954
[2] "Charles L. Hamblin and his work" (http:/ / www. csc. liv. ac. uk/ ~peter/ hamblin. html) by Peter McBurney
[3] "Charles L. Hamblin: Computer Pioneer" (http:/ / www. csc. liv. ac. uk/ ~peter/ this-month/ this-month-3-030303. html) by Peter McBurney,
July 27, 2008. "Hamblin soon became aware of the problems of (a) computing mathematical formulae containing brackets, and (b) the
memory overhead in having dealing with memory stores each of which had its own name. One solution to the first problem was Jan
Lukasiewicz's Polish notation, which enables a writer of mathematical notation to instruct a reader the order in which to execute the
operations (e.g. addition, multiplication, etc) without using brackets. Polish notation achieves this by having an operator (+, *, etc) precede
the operands to which it applies, e.g., +ab, instead of the usual, a+b. Hamblin, with his training in formal logic, knew of Lukasiewicz's work."
[4] http:/ / h20219. www2. hp. com/ Hpsub/ downloads/ 17b2pChain. pdf
[5] (http:/ / conservancy. umn. edu/ bitstream/ 107105/ 1/ oh098b5c. pdf) A New Approach to the Design of a Digital Computer (1961)
[6] (http:/ / special. lib. umn. edu/ cbi/ oh/ pdf. phtml?id=21) The Burroughs B5000 Conference (1985) p. 49
[7] HP Calculators (http:/ / www8. hp. com/ us/ en/ products/ calculators/ index. html)
[8] http:/ / www. vintagecalculators. com/ html/ program. html
[9] Elektronika B3-21 (http:/ / www. rskey. org/ detail. asp?manufacturer=Elektronika& model=B3-21) page on RSkey.org
[10] Elektronika MK-161 (http:/ / www. rskey. org/ detail. asp?manufacturer=Elektronika& model=MK-161) page on RSkey.org
[11] MK-152: Old Russian Motive in a New Space Age. (http:/ / arbinada. com/ pmk/ node/ 56)
[12] http:/ / mk. semico. ru/

• Łukasiewicz, Jan (1957). Aristotle’s Syllogistic from the Standpoint of Modern Formal Logic. Oxford University
Press. Reprinted by Garland Publishing in 1987. ISBN 0-8240-6924-2

External links
• RPN or DAL? A brief analysis of Reverse Polish Notation against Direct Algebraic Logic (http://www.
xnumber.com/xnumber/rpn_or_adl.htm) – By James Redin
• RPN at HP Museum (http://hpmuseum.org/rpn.htm)
• Postfix Notation Mini-Lecture (http://spsu.bob-brown.us/web_lectures/postfix/) – By Bob Brown
• Fith: An Alien Conlang With A LIFO Grammar (http://www.langmaker.com/shallowfith.htm) – By Jeffrey
Henning
• Good Ideas, Through the Looking Glass (http://www.inf.ethz.ch/~wirth/Articles/GoodIdeas_origFig.pdf) –
by Niklaus Wirth
• Ambi (http://www.davidpratten.com/ambi) browser-based extensible RPN calculator – By David Pratten
• Online RPN calculator (http://www.meta-calculator.com/learning-lab/reverse-polish-notation-calculator.php)
Provides a visualization of stack as you type!
Self-balancing binary search tree 34

Self-balancing binary search tree


In computer science, a self-balancing (or
height-balanced) binary search tree is any
node-based binary search tree that automatically keeps
its height (maximal number of levels below the root)
small in the face of arbitrary item insertions and
deletions.[1]

These structures provide efficient implementations for


mutable ordered lists, and can be used for other
abstract data structures such as associative arrays,
priority queues and sets.

An example of an unbalanced tree; following the path from the root


to a node takes an average of 3.27 node accesses

The same tree after being height-balanced; the average path effort
decreased to 3.00 node accesses
Self-balancing binary search tree 35

Overview
Most operations on a binary search tree
(BST) take time directly proportional to the
height of the tree, so it is desirable to keep
the height small. A binary tree with height h
can contain at most 20+21+···+2h = 2h+1−1
nodes. It follows that for a tree with n nodes
and height h:

And that implies:


.
Tree rotations are very common internal operations on self-balancing binary trees
In other words, the minimum height of a to keep perfect or near-to-perfect balance.
tree with n nodes is log2(n), rounded down;
that is, :.
However, the simplest algorithms for BST item insertion may yield a tree with height n in rather common situations.
For example, when the items are inserted in sorted key order, the tree degenerates into a linked list with n nodes. The
difference in performance between the two situations may be enormous: for n = 1,000,000, for example, the
minimum height is .
If the data items are known ahead of time, the height can be kept small, in the average sense, by adding values in a
random order, resulting in a random binary search tree. However, there are many situations (such as online
algorithms) where this randomization is not viable.
Self-balancing binary trees solve this problem by performing transformations on the tree (such as tree rotations) at
key times, in order to keep the height proportional to log2(n). Although a certain overhead is involved, it may be
justified in the long run by ensuring fast execution of later operations.
Maintaining the height always at its minimum value is not always viable; it can be proven that any
insertion algorithm which did so would have an excessive overhead.[citation needed] Therefore, most self-balanced
BST algorithms keep the height within a constant factor of this lower bound.
In the asymptotic ("Big-O") sense, a self-balancing BST structure containing n items allows the lookup, insertion,
and removal of an item in O(log n) worst-case time, and ordered enumeration of all items in O(n) time. For some
implementations these are per-operation time bounds, while for others they are amortized bounds over a sequence of
operations. These times are asymptotically optimal among all data structures that manipulate the key only through
comparisons.

Implementations
Popular data structures implementing this type of tree include:
• 2-3 tree
• AA tree
• AVL tree
• Red-black tree
• Scapegoat tree
• Splay tree
• Treap
Self-balancing binary search tree 36

Applications
Self-balancing binary search trees can be used in a natural way to construct and maintain ordered lists, such as
priority queues. They can also be used for associative arrays; key-value pairs are simply inserted with an ordering
based on the key alone. In this capacity, self-balancing BSTs have a number of advantages and disadvantages over
their main competitor, hash tables. One advantage of self-balancing BSTs is that they allow fast (indeed,
asymptotically optimal) enumeration of the items in key order, which hash tables do not provide. One disadvantage
is that their lookup algorithms get more complicated when there may be multiple items with the same key.
Self-balancing BSTs have better worst-case lookup performance than hash tables (O(log n) compared to O(n)), but
have worse average-case performance (O(log n) compared to O(1)).
Self-balancing BSTs can be used to implement any algorithm that requires mutable ordered lists, to achieve optimal
worst-case asymptotic performance. For example, if binary tree sort is implemented with a self-balanced BST, we
have a very simple-to-describe yet asymptotically optimal O(n log n) sorting algorithm. Similarly, many algorithms
in computational geometry exploit variations on self-balancing BSTs to solve problems such as the line segment
intersection problem and the point location problem efficiently. (For average-case performance, however,
self-balanced BSTs may be less efficient than other solutions. Binary tree sort, in particular, is likely to be slower
than merge sort, quicksort, or heapsort, because of the tree-balancing overhead as well as cache access patterns.)
Self-balancing BSTs are flexible data structures, in that it's easy to extend them to efficiently record additional
information or perform new operations. For example, one can record the number of nodes in each subtree having a
certain property, allowing one to count the number of nodes in a certain key range with that property in O(log n)
time. These extensions can be used, for example, to optimize database queries or other list-processing algorithms.

References
[1] Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Second Edition. Addison-Wesley, 1998. ISBN
0-201-89685-0. Section 6.2.3: Balanced Trees, pp.458–481.

External links
• Dictionary of Algorithms and Data Structures: Height-balanced binary search tree (http://www.nist.gov/dads/
HTML/heightBalancedTree.html)
• GNU libavl (http://adtinfo.org/), a LGPL-licensed library of binary tree implementations in C, with
documentation
AVL tree 37

AVL tree
AVL tree
Type Tree

Invented 1962

Invented by G. M. Adelson-Velskii and E. M. Landis

Time complexity
in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)

In computer science, an AVL tree (Adelson-Velskii


and Landis' tree, named after the inventors) is a
self-balancing binary search tree. It was the first such
data structure to be invented.[1] In an AVL tree, the
heights of the two child subtrees of any node differ by
at most one; if at any time they differ by more than
one, rebalancing is done to restore this property.
Lookup, insertion, and deletion all take O(log n) time Example AVL tree
in both the average and worst cases, where n is the
number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by
one or more tree rotations.

The AVL tree is named after its two Soviet inventors, G. M. Adelson-Velskii and E. M. Landis, who published it in
their 1962 paper "An algorithm for the organization of information".[2]
AVL trees are often compared with red-black trees because both support the same set of operations and take O(log n)
time for the basic operations. For lookup-intensive applications, AVL trees are faster than red-black trees because
they are more rigidly balanced. Similar to red-black trees, AVL trees are height-balanced. Both are in general not
weight-balanced nor μ-balanced for any ;[3] that is, sibling nodes can have hugely differing numbers of
descendants.
AVL tree 38

Operations
Basic operations of an AVL tree involve
carrying out the same actions as would be
carried out on an unbalanced binary search
tree, but modifications are followed by zero
or more operations called tree rotations,
which help to restore the height balance of
the subtrees.

Searching
Once a node has been found in a balanced
tree, the next or previous nodes can be Tree rotations

explored in amortized constant time. Some


instances of exploring these "nearby" nodes require traversing up to 2×log(n) links (particularly when moving from
the rightmost leaf of the root's left sub tree to the leftmost leaf of the root's right sub tree; in the example AVL tree,
moving from node 14 to the next but one node 19 takes 4 steps). However, exploring all n nodes of the tree in this
manner would use each link exactly twice: one traversal to enter the sub tree rooted at that node, another to leave that
node's sub tree after having explored it. And since there are n−1 links in any tree, the amortized cost is found to be
2×(n−1)/n, or approximately 2.
AVL tree 39

Insertion
After inserting a node, it is necessary to check each of the
node's ancestors for consistency with the rules of AVL. The
balance factor is calculated as follows: balanceFactor =
height(left subtree) - height(right subtree). For each node
checked, if the balance factor remains −1, 0, or +1 then no
rotations are necessary. However, if balance factor becomes
less than -1 or greater than +1, the subtree rooted at this
node is unbalanced. If insertions are performed serially,
after each insertion, at most one of the following cases
needs to be resolved to restore the entire tree to the rules of
AVL.

Suppose inserting one element causes P's balance factor to


go out of range. It must be that insertion caused the height
of one of P's child nodes to increase by 1 (but not the other).
Without loss of generality, assume that the height of L, P's
left, was increased. The following procedure can restore
balance at P:

Pictorial description of how rotations cause rebalancing tree,


and then retracing one's steps toward the root updating the
balance factor of the nodes. The numbered circles represent the
nodes being balanced. The lettered triangles represent subtrees
which are themselves balanced BSTs

if (balance_factor(L) < 0) {
// In the illustration to the right,
// this is the first step in the left-right case.
rotate_left(L);
}
// This brings us to the left-left case.
rotate_right(P);

The right-left and right-right cases are analogous:

if (balance_factor(R) > 0) {
rotate_right(R);
}
rotate_left(P);

The names of the cases refer to the portion of the tree that is reduced in height.
In order to restore the balance factors of all nodes, first observe that all nodes requiring correction lie along the path
used during the initial insertion. If the above procedure is applied to nodes along this path, starting from the bottom
AVL tree 40

(i.e. the node furthest away from the root), then every node in the tree will again have a balance factor of -1, 0, or 1.

Deletion
Let node X be the node with the value we need to delete, and let node Y be a node in the tree we need to find to take
node X's place, and let node Z be the actual node we take out of the tree.
Steps to consider when deleting a node in an AVL tree are the following:
1. If node X is a leaf or has only one child, skip to step 5. (node Z will be node X)
2. Otherwise, determine node Y by finding the largest node in node X's left sub tree (in-order predecessor) or the
smallest in its right sub tree (in-order successor).
3. Replace node X with node Y (remember, tree structure doesn't change here, only the values). In this step, node X
is essentially deleted when its internal values were overwritten with node Y's.
4. Choose node Z to be the old node Y.
5. Attach node Z's subtree to its parent (if it has a subtree). If node Z's parent is null, update root. (node Z is
currently root)
6. Delete node Z.
7. Retrace the path back up the tree (starting with node Z's parent) to the root, adjusting the balance factors as
needed.
As with all binary trees, a node's in-order successor is the left-most child of its right subtree, and a node's in-order
predecessor is the right-most child of its left subtree. In either case, this node will have zero or one children. Delete it
according to one of the two simpler cases above.

Deleting a node with two children from a binary search tree using the inorder predecessor (rightmost node in the left subtree, labelled 6).

In addition to the balancing described above for insertions, if the balance factor for the tree is 2 and that of the left
subtree is 0, a right rotation must be performed on P. The mirror of this case is also necessary.
The retracing can stop if the balance factor becomes −1 or +1 indicating that the height of that subtree has remained
unchanged. If the balance factor becomes 0 then the height of the subtree has decreased by one and the retracing
needs to continue. If the balance factor becomes −2 or +2 then the subtree is unbalanced and needs to be rotated to
fix it. If the rotation leaves the subtree's balance factor at 0 then the retracing towards the root must continue since
the height of this subtree has decreased by one. This is in contrast to an insertion where a rotation resulting in a
balance factor of 0 indicated that the subtree's height has remained unchanged.
The time required is O(log n) for lookup, plus a maximum of O(log n) rotations on the way back to the root, so the
operation can be completed in O(log n) time.
AVL tree 41

Comparison to other structures


Both AVL trees and red-black trees are self-balancing binary search trees and they are very similar
mathematically.[4] The operations to balance the trees are different, but both occur on the average in O(1) with
maximum in O(log n). The real difference between the two is the limiting height. For a tree of size :
• An AVL tree's height is strictly less than:

where is the golden ratio.


[5]
• A red-black tree's height is at most
AVL trees are more rigidly balanced than red-black trees, leading to slower insertion and removal but faster
retrieval.

References
[1] Robert Sedgewick, Algorithms, Addison-Wesley, 1983, ISBN 0-201-06672-6, page 199, chapter 15: Balanced Trees.
[2] English translation by Myron J. Ricci in Soviet Math. Doklady, 3:1259–1263, 1962.
[3] AVL trees are not weight-balanced? (meaning: AVL trees are not μ-balanced?) (http:/ / cs. stackexchange. com/ questions/ 421/
avl-trees-are-not-weight-balanced)
Thereby: A Binary Tree is called UNIQ-math-0-d20677dabef957ff-QINU -balanced, with UNIQ-math-1-d20677dabef957ff-QINU , if for
every node UNIQ-math-2-d20677dabef957ff-QINU , the inequality

holds and UNIQ-math-4-d20677dabef957ff-QINU is minimal with this property. UNIQ-math-5-d20677dabef957ff-QINU is the number of
nodes below the tree with UNIQ-math-6-d20677dabef957ff-QINU as root (including the root) and UNIQ-math-7-d20677dabef957ff-QINU is
the left child node of UNIQ-math-8-d20677dabef957ff-QINU .
[4] In fact, each AVL tree can be colored red-black.
[5] Proof of asymptotic bounds

Further reading
• Donald Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching, Third Edition.
Addison-Wesley, 1997. ISBN 0-201-89685-0. Pages 458–475 of section 6.2.3: Balanced Trees.

External links
• xdg library (https://github.com/vilkov/libxdg/wiki) by Dmitriy Vilkov: Serializable straight C-implementation
could easily be taken from this library under GNU-LGPL and AFL v2.0 licenses.
• Description from the Dictionary of Algorithms and Data Structures (http://www.nist.gov/dads/HTML/avltree.
html)
• Python Implementation (http://github.com/pgrafov/python-avl-tree/)
• Single C header file by Ian Piumarta (http://piumarta.com/software/tree/)
• AVL Tree Demonstration (http://www.strille.net/works/media_technology_projects/avl-tree_2001/)
• AVL tree applet – all the operations (http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html)
• Fast and efficient implementation of AVL Trees (http://github.com/fbuihuu/libtree)
• PHP Implementation (https://github.com/mondrake/Rbppavl)
• C++ implementation which can be used as an array (http://www.codeproject.com/Articles/12347/
AVL-Binary-Tree-for-C)
• Self balancing AVL tree with Concat and Split operations (http://code.google.com/p/self-balancing-avl-tree/)
B-tree 42

B-tree
B-tree
Type Tree

Invented 1972

Invented by Rudolf Bayer, Edward M. McCreight

Time complexity
in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)

In computer science, a B-tree is a tree data structure that keeps data sorted and allows searches, sequential access,
insertions, and deletions in logarithmic time. The B-tree is a generalization of a binary search tree in that a node can
have more than two children. (Comer 1979, p. 123) Unlike self-balancing binary search trees, the B-tree is optimized
for systems that read and write large blocks of data. It is commonly used in databases and filesystems.

Overview
In B-trees, internal (non-leaf) nodes
can have a variable number of child
nodes within some pre-defined range.
When data is inserted or removed from
a node, its number of child nodes
changes. In order to maintain the
pre-defined range, internal nodes may A B-tree of order 2 (Bayer & McCreight 1972) or order 5 (Knuth 1998).
be joined or split. Because a range of
child nodes is permitted, B-trees do not need re-balancing as frequently as other self-balancing search trees, but may
waste some space, since nodes are not entirely full. The lower and upper bounds on the number of child nodes are
typically fixed for a particular implementation. For example, in a 2-3 B-tree (often simply referred to as a 2-3 tree),
each internal node may have only 2 or 3 child nodes.

Each internal node of a B-tree will contain a number of keys. The keys act as separation values which divide its
subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All
values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all
values in the rightmost subtree will be greater than a2.
Usually, the number of keys is chosen to vary between and , where is the minimum number of keys, and
is the minimum degree or branching factor of the tree. In practice, the keys take up the most space in a node.
The factor of 2 will guarantee that nodes can be split or combined. If an internal node has keys, then adding a
key to that node can be accomplished by splitting the key node into two key nodes and adding the key to the
parent node. Each split node has the required minimum number of keys. Similarly, if an internal node and its
neighbor each have keys, then a key may be deleted from the internal node by combining with its neighbor.
Deleting the key would make the internal node have keys; joining the neighbor would add keys plus one
B-tree 43

more key brought down from the neighbor's parent. The result is an entirely full node of keys.
The number of branches (or child nodes) from a node will be one more than the number of keys stored in the node.
In a 2-3 B-tree, the internal nodes will store either one key (with two child nodes) or two keys (with three child
nodes). A B-tree is sometimes described with the parameters — or simply with the highest
branching order, .
A B-tree is kept balanced by requiring that all leaf nodes be at the same depth. This depth will increase slowly as
elements are added to the tree, but an increase in the overall depth is infrequent, and results in all leaf nodes being
one more node farther away from the root.
B-trees have substantial advantages over alternative implementations when otherwise the time to access the data of a
node greatly exceeds the time spent processing these data, because then the cost of accessing the node may be
amortized over multiple operations within the node. This usually occurs when the node data are in secondary storage
such as disk drives. By maximizing the number of keys within each internal node, the height of the tree decreases
and the number of expensive node accesses is reduced. In addition, rebalancing of the tree occurs less often. The
maximum number of child nodes depends on the information that must be stored for each child node and the size of
a full disk block or an analogous size in secondary storage. While 2-3 B-trees are easier to explain, practical B-trees
using secondary storage need a large number of child nodes to improve performance.

Variants
The term B-tree may refer to a specific design or it may refer to a general class of designs. In the narrow sense, a
B-tree stores keys in its internal nodes but need not store those keys in the records at the leaves. The general class
includes variations such as the B+-tree and the B*-tree.
• In the B+-tree, copies of the keys are stored in the internal nodes; the keys and records are stored in leaves; in
addition, a leaf node may include a pointer to the next leaf node to speed sequential access. (Comer 1979, p. 129)
• The B*-tree balances more neighboring internal nodes to keep the internal nodes more densely packed.(Comer
1979, p. 129) This variant requires non-root nodes to be at least 2/3 full instead of 1/2. (Knuth 1998, p. 488) To
maintain this, instead of immediately splitting up a node when it gets full, its keys are shared with a node next to
it. When both nodes are full, then the two nodes are split into three.
• Counted B-trees store, with each pointer within the tree, the number of elements in the subtree below that
pointer.[1] This allows rapid searches for the Nth record in key order, or counting the number of records between
any two records, and various other related operations.

Etymology unknown
Rudolf Bayer and Ed McCreight invented the B-tree while working at Boeing Research Labs in 1971 (Bayer &
McCreight 1972), but they did not explain what, if anything, the B stands for. Douglas Comer explains:
The origin of "B-tree" has never been explained by the authors. As we shall see, "balanced," "broad," or
"bushy" might apply. Others suggest that the "B" stands for Boeing. Because of his contributions,
however, it seems appropriate to think of B-trees as "Bayer"-trees. (Comer 1979, p. 123 footnote 1)
Donald Knuth speculates on the etymology of B-trees in his May, 1980 lecture on the topic "CS144C classroom
lecture about disk storage and B-trees", suggesting the "B" may have originated from Boeing or from Bayer's
name.[2]
B-tree 44

The database problem


This section describes a problem faced by database designers, outlines a series of increasingly effective solutions to
the problem, and ends by describing how the B-tree solves the problem completely.

Time to search a sorted file


Usually, sorting and searching algorithms have been characterized by the number of comparison operations that must
be performed using order notation. A binary search of a sorted table with records, for example, can be done in
comparisons. If the table had 1,000,000 records, then a specific record could be located with at most 20
comparisons: .
Large databases have historically been kept on disk drives. The time to read a record on a disk drive far exceeds the
time needed to compare keys once the record is available. The time to read a record from a disk drive involves a seek
time and a rotational delay. The seek time may be 0 to 20 or more milliseconds, and the rotational delay averages
about half the rotation period. For a 7200 RPM drive, the rotation period is 8.33 milliseconds. For a drive such as the
Seagate ST3500320NS, the track-to-track seek time is 0.8 milliseconds and the average reading seek time is 8.5
milliseconds.[3] For simplicity, assume reading from disk takes about 10 milliseconds.
Naively, then, the time to locate one record out of a million would take 20 disk reads times 10 milliseconds per disk
read, which is 0.2 seconds.
The time won't be that bad because individual records are grouped together in a disk block. A disk block might be 16
kilobytes. If each record is 160 bytes, then 100 records could be stored in each block. The disk read time above was
actually for an entire block. Once the disk head is in position, one or more disk blocks can be read with little delay.
With 100 records per block, the last 6 or so comparisons don't need to do any disk reads—the comparisons are all
within the last disk block read.
To speed the search further, the first 13 to 14 comparisons (which each required a disk access) must be sped up.

An index speeds the search


A significant improvement can be made with an index. In the example above, initial disk reads narrowed the search
range by a factor of two. That can be improved substantially by creating an auxiliary index that contains the first
record in each disk block (sometimes called a sparse index). This auxiliary index would be 1% of the size of the
original database, but it can be searched more quickly. Finding an entry in the auxiliary index would tell us which
block to search in the main database; after searching the auxiliary index, we would have to search only that one
block of the main database—at a cost of one more disk read. The index would hold 10,000 entries, so it would take
at most 14 comparisons. Like the main database, the last 6 or so comparisons in the aux index would be on the same
disk block. The index could be searched in about 8 disk reads, and the desired record could be accessed in 9 disk
reads.
The trick of creating an auxiliary index can be repeated to make an auxiliary index to the auxiliary index. That would
make an aux-aux index that would need only 100 entries and would fit in one disk block.
Instead of reading 14 disk blocks to find the desired record, we only need to read 3 blocks. Reading and searching
the first (and only) block of the aux-aux index identifies the relevant block in aux-index. Reading and searching that
aux-index block identifies the relevant block in the main database. Instead of 150 milliseconds, we need only 30
milliseconds to get the record.
The auxiliary indices have turned the search problem from a binary search requiring roughly disk reads to
one requiring only disk reads where is the blocking factor (the number of entries per block:
entries per block; reads).
In practice, if the main database is being frequently searched, the aux-aux index and much of the aux index may
reside in a disk cache, so they would not incur a disk read.
B-tree 45

Insertions and deletions cause trouble


If the database does not change, then compiling the index is simple to do, and the index need never be changed. If
there are changes, then managing the database and its index becomes more complicated.
Deleting records from a database doesn't cause much trouble. The index can stay the same, and the record can just be
marked as deleted. The database stays in sorted order. If there are a lot of deletions, then the searching and storage
become less efficient.
Insertions are a disaster in a sorted sequential file because room for the inserted record must be made. Inserting a
record before the first record in the file requires shifting all of the records down one. Such an operation is just too
expensive to be practical.
A trick is to leave some space lying around to be used for insertions. Instead of densely storing all the records in a
block, the block can have some free space to allow for subsequent insertions. Those records would be marked as if
they were "deleted" records.
Now, both insertions and deletions are fast as long as space is available on a block. If an insertion won't fit on the
block, then some free space on some nearby block must be found and the auxiliary indices adjusted. The hope is that
enough space is nearby such that a lot of blocks do not need to be reorganized. Alternatively, some out-of-sequence
disk blocks may be used.

The B-tree uses all those ideas


The B-tree uses all of the ideas described above. In particular, a B-tree:
• keeps keys in sorted order for sequential traversing
• uses a hierarchical index to minimize the number of disk reads
• uses partially full blocks to speed insertions and deletions
• keeps the index balanced with an elegant recursive algorithm
In addition, a B-tree minimizes waste by making sure the interior nodes are at least half full. A B-tree can handle an
arbitrary number of insertions and deletions.

Technical description

Terminology
Unfortunately, the literature on B-trees is not uniform in its terminology. (Folk & Zoellick 1992, p. 362)
Bayer & McCreight (1972), Comer (1979), and others define the order of B-tree as the minimum number of keys in
a non-root node. Folk & Zoellick (1992) points out that terminology is ambiguous because the maximum number of
keys is not clear. An order 3 B-tree might hold a maximum of 6 keys or a maximum of 7 keys. Knuth (1998, p. 483)
avoids the problem by defining the order to be maximum number of children (which is one more than the maximum
number of keys).
The term leaf is also inconsistent. Bayer & McCreight (1972) considered the leaf level to be the lowest level of keys,
but Knuth considered the leaf level to be one level below the lowest keys. (Folk & Zoellick 1992, p. 363) There are
many possible implementation choices. In some designs, the leaves may hold the entire data record; in other designs,
the leaves may only hold pointers to the data record. Those choices are not fundamental to the idea of a B-tree.[4]
There are also unfortunate choices like using the variable k to represent the number of children when k could be
confused with the number of keys.
For simplicity, most authors assume there are a fixed number of keys that fit in a node. The basic assumption is the
key size is fixed and the node size is fixed. In practice, variable length keys may be employed. (Folk & Zoellick
1992, p. 379)
B-tree 46

Definition
According to Knuth's definition, a B-tree of order m is a tree which satisfies the following properties:
1. Every node has at most m children.
2. Every non-leaf node (except root) has at least ⌈m⁄2⌉ children.
3. The root has at least two children if it is not a leaf node.
4. A non-leaf node with k children contains k−1 keys.
5. All leaves appear in the same level, and internal vertices carry no information.
Each internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3
child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1,
all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than
a2.
Internal nodes
Internal nodes are all nodes except for leaf nodes and the root node. They are usually represented as an ordered
set of elements and child pointers. Every internal node contains a maximum of U children and a minimum of
L children. Thus, the number of elements is always 1 less than the number of child pointers (the number of
elements is between L−1 and U−1). U must be either 2L or 2L−1; therefore each internal node is at least half
full. The relationship between U and L implies that two half-full nodes can be joined to make a legal node, and
one full node can be split into two legal nodes (if there’s room to push one element up into the parent). These
properties make it possible to delete and insert new values into a B-tree and adjust the tree to preserve the
B-tree properties.
The root node
The root node’s number of children has the same upper limit as internal nodes, but has no lower limit. For
example, when there are fewer than L−1 elements in the entire tree, the root will be the only node in the tree,
with no children at all.
Leaf nodes
Leaf nodes have the same restriction on the number of elements, but have no children, and no child pointers.
A B-tree of depth n+1 can hold about U times as many items as a B-tree of depth n, but the cost of search, insert, and
delete operations grows with the depth of the tree. As with any balanced tree, the cost grows much more slowly than
the number of elements.
Some balanced trees store values only at leaf nodes, and use different kinds of nodes for leaf nodes and internal
nodes. B-trees keep values in every node in the tree, and may use the same structure for all nodes. However, since
leaf nodes never have children, the B-trees benefit from improved performance if they use a specialized structure.

Best case and worst case heights


Let h be the height of the classic B-tree. Let n > 0 be the number of entries in the tree.[5] Let m be the maximum
number of children a node can have. Each node can have at most m−1 keys.
It can be shown (by induction for example) that a B-tree of height h with all its nodes completely filled has n=mh−1
entries. Hence, the best case height of a B-tree is:

Let d be the minimum number of children an internal (non-root) node can have. For an ordinary B-tree, d=⌈m/2⌉.
The worst case height[citation needed] of a B-tree is:
B-tree 47

Comer (1979, p. 127) and Cormen et al. (2001, pp. 383–384) give a slightly different expression for the worst case
height (perhaps because the root node is considered to have height 0).

Algorithms

Search
Searching is similar to searching a binary search tree. Starting at the root, the tree is recursively traversed from top to
bottom. At each level, the search chooses the child pointer (subtree) whose separation values are on either side of the
search value.
Binary search is typically (but not necessarily) used within nodes to find the separation values and child tree of
interest.

Insertion
All insertions start at a leaf node. To insert a new element, search the tree
to find the leaf node where the new element should be added. Insert the
new element into that node with the following steps:
1. If the node contains fewer than the maximum legal number of
elements, then there is room for the new element. Insert the new
element in the node, keeping the node's elements ordered.
2. Otherwise the node is full, evenly split it into two nodes so:
1. A single median is chosen from among the leaf's elements and the
new element.
2. Values less than the median are put in the new left node and values
greater than the median are put in the new right node, with the
median acting as a separation value.
3. The separation value is inserted in the node's parent, which may
cause it to be split, and so on. If the node has no parent (i.e., the
node was the root), create a new root above this node (increasing
the height of the tree).
If the splitting goes all the way up to the root, it creates a new root with a
single separator value and two children, which is why the lower bound on
the size of internal nodes does not apply to the root. The maximum
number of elements per node is U−1. When a node is split, one element
moves to the parent, but one element is added. So, it must be possible to
divide the maximum number U−1 of elements into two legal nodes. If
this number is odd, then U=2L and one of the new nodes contains A B Tree insertion example with each
iteration. The nodes of this B tree have at most
(U−2)/2 = L−1 elements, and hence is a legal node, and the other contains
3 children (Knuth order 3).
one more element, and hence it is legal too. If U−1 is even, then U=2L−1,
so there are 2L−2 elements in the node. Half of this number is L−1, which
is the minimum number of elements allowed per node.

An improved algorithm (Mond & Raz 1985) supports a single pass down the tree from the root to the node where the
insertion will take place, splitting any full nodes encountered on the way. This prevents the need to recall the parent
nodes into memory, which may be expensive if the nodes are on secondary storage. However, to use this improved
B-tree 48

algorithm, we must be able to send one element to the parent and split the remaining U−2 elements into two legal
nodes, without adding a new element. This requires U = 2L rather than U = 2L−1, which accounts for why some
textbooks impose this requirement in defining B-trees.

Deletion
There are two popular strategies for deletion from a B-tree.
1. Locate and delete the item, then restructure the tree to regain its invariants, OR
2. Do a single pass down the tree, but before entering (visiting) a node, restructure the tree so that once the key to be
deleted is encountered, it can be deleted without triggering the need for any further restructuring
The algorithm below uses the former strategy.
There are two special cases to consider when deleting an element:
1. The element in an internal node is a separator for its child nodes
2. Deleting an element may put its node under the minimum number of elements and children
The procedures for these cases are in order below.

Deletion from a leaf node


1. Search for the value to delete.
2. If the value is in a leaf node, simply delete it from the node.
3. If underflow happens, rebalance the tree as described in section "Rebalancing after deletion" below.

Deletion from an internal node


Each element in an internal node acts as a separation value for two subtrees, therefore we need to find a replacement
for separation. Note that the largest element in the left subtree is still less than the separator. Likewise, the smallest
element in the right subtree is still greater than the separator. Both of those elements are in leaf nodes, and either one
can be the new separator for the two subtrees. Algorithmically described below:
1. Choose a new separator (either the largest element in the left subtree or the smallest element in the right subtree),
remove it from the leaf node it is in, and replace the element to be deleted with the new separator.
2. The previous step deleted an element (the new separator) from a leaf node. If that leaf node is now deficient (has
fewer than the required number of nodes), then rebalance the tree starting from the leaf node.

Rebalancing after deletion


Rebalancing starts from a leaf and proceeds toward the root until the tree is balanced. If deleting an element from a
node has brought it under the minimum size, then some elements must be redistributed to bring all nodes up to the
minimum. Usually, the redistribution involves moving an element from a sibling node that has more than the
minimum number of nodes. That redistribution operation is called a rotation. If no sibling can spare a node, then the
deficient node must be merged with a sibling. The merge causes the parent to lose a separator element, so the parent
may become deficient and need rebalancing. The merging and rebalancing may continue all the way to the root.
Since the minimum element count doesn't apply to the root, making the root be the only deficient node is not a
problem. The algorithm to rebalance the tree is as follows:[citation needed]
• If the deficient node's right sibling exists and has more than the minimum number of elements, then rotate left
1. Copy the separator from the parent to the end of the deficient node (the separator moves down; the deficient
node now has the minimum number of elements)
2. Replace the separator in the parent with the first element of the right sibling (right sibling loses one node but
still has at least the minimum number of elements)
3. The tree is now balanced
B-tree 49

• Otherwise, if the deficient node's left sibling exists and has more than the minimum number of elements, then
rotate right
1. Copy the separator from the parent to the start of the deficient node (the separator moves down; deficient node
now has the minimum number of elements)
2. Replace the separator in the parent with the last element of the left sibling (left sibling loses one node but still
has at least the minimum number of elements)
3. The tree is now balanced
• Otherwise, if both immediate siblings have only the minimum number of elements, then merge with a sibling
sandwiching their separator taken off from their parent
1. Copy the separator to the end of the left node (the left node may be the deficient node or it may be the sibling
with the minimum number of elements)
2. Move all elements from the right node to the left node (the left node now has the maximum number of
elements, and the right node – empty)
3. Remove the separator from the parent along with its empty right child (the parent loses an element)
• If the parent is the root and now has no elements, then free it and make the merged node the new root (tree
becomes shallower)
• Otherwise, if the parent has fewer than the required number of elements, then rebalance the parent
Note: The rebalancing operations are different for B+-trees (e.g., rotation is different because parent has copy of the key) and B*-tree (e.g.,
three siblings are merged into two siblings).

Sequential access
While freshly loaded databases tend to have good sequential behavior, this behavior becomes increasingly difficult
to maintain as a database grows, resulting in more random I/O and performance challenges.

Initial construction
In applications, it is frequently useful to build a B-tree to represent a large existing collection of data and then update
it incrementally using standard B-tree operations. In this case, the most efficient way to construct the initial B-tree is
not to insert every element in the initial collection successively, but instead to construct the initial set of leaf nodes
directly from the input, then build the internal nodes from these. This approach to B-tree construction is called
bulkloading. Initially, every leaf but the last one has one extra element, which will be used to build the internal
nodes.[citation needed]
For example, if the leaf nodes have maximum size 4 and the initial collection is the integers 1 through 24, we would
initially construct 4 leaf nodes containing 5 values each and 1 which contains 4 values:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

We build the next level up from the leaves by taking the last element from each leaf node except the last one. Again,
each node except the last will contain one extra value. In the example, suppose the internal nodes contain at most 2
values (3 child pointers). Then the next level up of internal nodes would be:

20
5 10 15

1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24
B-tree 50

This process is continued until we reach a level with only one node and it is not overfilled. In the example only the
root level remains:

15

20
5 10

1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24

In filesystems
In addition to its use in databases, the B-tree is also used in filesystems to allow quick random access to an arbitrary
block in a particular file. The basic problem is turning the file block address into a disk block (or perhaps to a
cylinder-head-sector) address.
Some operating systems require the user to allocate the maximum size of the file when the file is created. The file
can then be allocated as contiguous disk blocks. Converting to a disk block: the operating system just adds the file
block address to the starting disk block of the file. The scheme is simple, but the file cannot exceed its created size.
Other operating systems allow a file to grow. The resulting disk blocks may not be contiguous, so mapping logical
blocks to physical blocks is more involved.
MS-DOS, for example, used a simple File Allocation Table (FAT). The FAT has an entry for each disk block,[6] and
that entry identifies whether its block is used by a file and if so, which block (if any) is the next disk block of the
same file. So, the allocation of each file is represented as a linked list in the table. In order to find the disk address of
file block , the operating system (or disk utility) must sequentially follow the file's linked list in the FAT. Worse,
to find a free disk block, it must sequentially scan the FAT. For MS-DOS, that was not a huge penalty because the
disks and files were small and the FAT had few entries and relatively short file chains. In the FAT12 filesystem
(used on floppy disks and early hard disks), there were no more than 4,080 [7] entries, and the FAT would usually be
resident in memory. As disks got bigger, the FAT architecture began to confront penalties. On a large disk using
FAT, it may be necessary to perform disk reads to learn the disk location of a file block to be read or written.
TOPS-20 (and possibly TENEX) used a 0 to 2 level tree that has similarities to a B-tree[citation needed]. A disk block
was 512 36-bit words. If the file fit in a 512 (29) word block, then the file directory would point to that physical disk
block. If the file fit in 218 words, then the directory would point to an aux index; the 512 words of that index would
either be NULL (the block isn't allocated) or point to the physical address of the block. If the file fit in 227 words,
then the directory would point to a block holding an aux-aux index; each entry would either be NULL or point to an
aux index. Consequently, the physical disk block for a 227 word file could be located in two disk reads and read on
the third.
Apple's filesystem HFS+, Microsoft's NTFS, AIX (jfs2) and some Linux filesystems, such as btrfs and Ext4, use
B-trees.
B*-trees are used in the HFS and Reiser4 file systems.
B-tree 51

Variations

Access concurrency
Lehman and Yao showed that all read locks could be avoided (and thus concurrent access greatly improved) by
linking the tree blocks at each level together with a "next" pointer. This results in a tree structure where both
insertion and search operations descend from the root to the leaf. Write locks are only required as a tree block is
modified. This maximizes access concurrency by multiple users, an important consideration for databases and/or
other B-tree based ISAM storage methods. The cost associated with this improvement is that empty pages cannot be
removed from the btree during normal operations. (However, see [8] for various strategies to implement node
merging, and source code at.)
United States Patent 5283894, granted in 1994, appears to show a way to use a 'Meta Access Method' [9] to allow
concurrent B+Tree access and modification without locks. The technique accesses the tree 'upwards' for both
searches and updates by means of additional in-memory indexes that point at the blocks in each level in the block
cache. No reorganization for deletes is needed and there are no 'next' pointers in each block as in Lehman and Yao.

Notes
[1] Counted B-Trees (http:/ / www. chiark. greenend. org. uk/ ~sgtatham/ algorithms/ cbtree. html), retrieved 2010-01-25
[2] Knuth's video lectures from Stanford (http:/ / scpd. stanford. edu/ knuth/ index. jsp)
[3] Seagate Technology LLC, Product Manual: Barracuda ES.2 Serial ATA, Rev. F., publication 100468393, 2008 (http:/ / www. seagate. com/
staticfiles/ support/ disc/ manuals/ NL35 Series & BC ES Series/ Barracuda ES. 2 Series/ 100468393f. pdf), page 6
[4] avoided the issue by saying an index element is a (physically adjacent) pair of (x, a) where x is the key, and a is some associated information.
The associated information might be a pointer to a record or records in a random access, but what it was didn't really matter. states, "For this
paper the associated information is of no further interest."
[5] If n is zero, then no root node is needed, so the height of an empty tree is not well defined.
[6] For FAT, what is called a "disk block" here is what the FAT documentation calls a "cluster", which is fixed-size group of one or more
contiguous whole physical disk sectors. For the purposes of this discussion, a cluster has no significant difference from a physical sector.
[7] Two of these were reserved for special purposes, so only 4078 could actually represent disk blocks (clusters).
[8] http:/ / www. dtic. mil/ cgi-bin/ GetTRDoc?AD=ADA232287& Location=U2& doc=GetTRDoc. pdf
[9] Lockless Concurrent B+Tree (http:/ / www. freepatentsonline. com/ 5283894. html)

References
• Bayer, R.; McCreight, E. (1972), "Organization and Maintenance of Large Ordered Indexes" (http://www.
minet.uni-jena.de/dbis/lehre/ws2005/dbs1/Bayer_hist.pdf), Acta Informatica 1 (3): 173–189
• Comer, Douglas (June 1979), "The Ubiquitous B-Tree", Computing Surveys 11 (2): 123–137, doi:
10.1145/356770.356776 (http://dx.doi.org/10.1145/356770.356776), ISSN  0360-0300 (http://www.
worldcat.org/issn/0360-0300).
• Cormen, Thomas; Leiserson, Charles; Rivest, Ronald; Stein, Clifford (2001), Introduction to Algorithms (Second
ed.), MIT Press and McGraw-Hill, pp. 434–454, ISBN 0-262-03293-7. Chapter 18: B-Trees.
• Folk, Michael J.; Zoellick, Bill (1992), File Structures (2nd ed.), Addison-Wesley, ISBN 0-201-55713-4
• Knuth, Donald (1998), Sorting and Searching, The Art of Computer Programming, Volume 3 (Second ed.),
Addison-Wesley, ISBN 0-201-89685-0. Section 6.2.4: Multiway Trees, pp. 481–491. Also, pp. 476–477 of
section 6.2.3 (Balanced Trees) discusses 2-3 trees.
• Mond, Yehudit; Raz, Yoav (1985), "Concurrency Control in B+-Trees Databases Using Preparatory Operations"
(http://www.informatik.uni-trier.de/~ley/db/conf/vldb/MondR85.html), VLDB'85, Proceedings of 11th
International Conference on Very Large Data Bases: 331–334.
B-tree 52

Original papers
• Bayer, Rudolf; McCreight, E. (July 1970), Organization and Maintenance of Large Ordered Indices,
Mathematical and Information Sciences Report No. 20, Boeing Scientific Research Laboratories.
• Bayer, Rudolf (1971), "Binary B-Trees for Virtual Memory", Proceedings of 1971 ACM-SIGFIDET Workshop
on Data Description, Access and Control, San Diego, California. November 11–12, 1971.

External links
• B-Tree animation applet (http://slady.net/java/bt/view.php) by slady
• B-tree and UB-tree on Scholarpedia (http://www.scholarpedia.org/article/B-tree_and_UB-tree) Curator: Dr
Rudolf Bayer
• B-Trees: Balanced Tree Data Structures (http://www.bluerwhite.org/btree)
• NIST's Dictionary of Algorithms and Data Structures: B-tree (http://www.nist.gov/dads/HTML/btree.html)
• B-Tree Tutorial (http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html)
• The InfinityDB BTree implementation (http://www.boilerbay.com/infinitydb/
TheDesignOfTheInfinityDatabaseEngine.htm)
• Cache Oblivious B(+)-trees (http://supertech.csail.mit.edu/cacheObliviousBTree.html)
• Dictionary of Algorithms and Data Structures entry for B*-tree (http://www.nist.gov/dads/HTML/bstartree.
html)
• Open Data Structures - Section 14.2 - B-Trees (http://opendatastructures.org/versions/edition-0.1e/ods-java/
14_2_B_Trees.html)
• Counted B-Trees (http://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html)

Heap (data structure)


In computer science, a heap is a specialized tree-based
data structure that satisfies the heap property: If A is a
parent node of B then the key of node A is ordered with
respect to the key of node B with the same ordering
applying across the heap. Either the keys of parent nodes
are always greater than or equal to those of the children
and the highest key is in the root node (this kind of heap
is called max heap) or the keys of parent nodes are less
than or equal to those of the children and the lowest key
is in the root node (min heap). Heaps are crucial in
several efficient graph algorithms such as Dijkstra's
algorithm, and in the sorting algorithm heapsort. Example of a complete binary max-heap with node keys being
integers from 1 to 100
Note that, as shown in the graphic, there is no implied
ordering between siblings or cousins and no implied
sequence for an in-order traversal (as there would be in, e.g., a binary search tree). The heap relation mentioned
above applies only between nodes and their immediate parents. The maximum number of children each node can
have depends on the type of heap, but in many types it is at most two, which is known as a "binary heap".
The heap is one maximally efficient implementation of an abstract data type called a priority queue, and in fact
priority queues are often referred to as "heaps", regardless of how they may be implemented. Note that despite the
similarity of the name "heap" to "stack" and "queue", the latter two are abstract data types, while a heap is a specific
Heap (data structure) 53

data structure, and "priority queue" is the proper term for the abstract data type.[citation needed]
A heap data structure should not be confused with the heap which is a common name for the pool of memory from
which dynamically allocated memory is allocated. The term was originally used only for the data structure.

Implementation and operations


Heaps are usually implemented in an array, and do not require pointers between elements.
The operations commonly performed with a heap are:
• create-heap: create an empty heap
• heapify: create a heap out of given array of elements
• find-max or find-min: find the maximum item of a max-heap or a minimum item of a min-heap, respectively (aka,
peek)
• delete-max or delete-min: removing the root node of a max- or min-heap, respectively
• increase-key or decrease-key: updating a key within a max- or min-heap, respectively
• insert: adding a new key to the heap
• merge: joining two heaps to form a valid new heap containing all the elements of both.
Different types of heaps implement the operations in different ways, but notably, insertion is often done by adding
the new element at the end of the heap in the first available free space. This will tend to violate the heap property,
and so the elements are then reordered until the heap property has been reestablished. Construction of a binary (or
d-ary) heap out of a given array of elements may be performed faster than a sequence of consecutive insertions into
an originally empty heap using the classic Floyd's algorithm, with the worst-case number of comparisons equal to 2N
− 2s2(N) − e2(N) (for a binary heap), where s2(N) is the sum of all digits of the binary representation of N and e2(N)
is the exponent of 2 in the prime factorization of N.

Variants
• 2-3 heap
• Beap
• Binary heap
• Binomial heap
• Brodal queue
• d-ary heap
• Fibonacci heap
• Leftist heap
• Pairing heap
• Skew heap
• Soft heap
• Weak heap
• Leaf heap
• Radix heap
• Randomized meldable heap
Heap (data structure) 54

Comparison of theoretic bounds for variants


The following time complexities[1] are amortized (worst-time) time complexity for entries marked by an asterisk, and
regular worst case time complexities for all other entries. O(f) gives asymptotic upper bound and Θ(f) is
asymptotically tight bound (see Big O notation). Function names assume a min-heap.

Operation Binary Binomial Fibonacci Pairing [2] RP [3]


Brodal*** Strict Fibonacci Heap

find-min Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1)

delete-min Θ(log n) Θ(log n) O(log n)* O(log n)* O(log n) O(log n)* O(log n)

insert Θ(log n) O(log n) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1)

decrease-key Θ(log n) Θ(log n) Θ(1)* O(log n)* Θ(1) Θ(1)* Θ(1)

merge Θ(n) O(log n)** Θ(1) Θ(1) Θ(1) Θ(1) Θ(1)

(*) Amortized time


(**) Where n is the size of the larger heap
(***) Brodal and Okasaki later describe a persistent variant with the same bounds except for decrease-key, which is
not supported. Heaps with n elements can be constructed bottom-up in O(n).

Applications
The heap data structure has many applications.
• Heapsort: One of the best sorting methods being in-place and with no quadratic worst-case scenarios.
• Selection algorithms: A heap allows access to the min or max element in constant time, and other selections (such
as median or kth-element) can be done in sub-linear time on data that is in a heap.
• Graph algorithms: By using heaps as internal traversal data structures, run time will be reduced by polynomial
order. Examples of such problems are Prim's minimal spanning tree algorithm and Dijkstra's shortest path
problem.
Full and almost full binary heaps may be represented in a very space-efficient way using an array alone. The first (or
last) element will contain the root. The next two elements of the array contain its children. The next four contain the
four children of the two child nodes, etc. Thus the children of the node at position n would be at positions 2n and
2n+1 in a one-based array, or 2n+1 and 2n+2 in a zero-based array. This allows moving up or down the tree by doing
simple index computations. Balancing a heap is done by swapping elements which are out of order. As we can build
a heap from an array without requiring extra memory (for the nodes, for example), heapsort can be used to sort an
array in-place.

Implementations
• The C++ Standard Template Library provides the make_heap, push_heap and pop_heap algorithms for
heaps (usually implemented as binary heaps), which operate on arbitrary random access iterators. It treats the
iterators as a reference to an array, and uses the array-to-heap conversion. It also provides the container adaptor
priority_queue, which wraps these facilities in a container-like class. However, there is no standard support
for the decrease/increase-key operation.
• The Boost C++ libraries include a heaps library. Unlike the STL it supports decrease and increase operations, and
supports additional types of heap: specifically, it supports d-ary, binomial, Fibonacci, pairing and skew heaps.
• The Java 2 platform (since version 1.5) provides the binary heap implementation with class
java.util.PriorityQueue<E> [4] in Java Collections Framework.
• Python has a heapq [5] module that implements a priority queue using a binary heap.
Heap (data structure) 55

• PHP has both max-heap (SplMaxHeap) and min-heap (SplMinHeap) as of version 5.3 in the Standard PHP
Library.
• Perl has implementations of binary, binomial, and Fibonacci heaps in the Heap [6] distribution available on
CPAN.
• The Go library contains a heap [7] package with heap algorithms that operate on an arbitrary type that satisfy a
given interface.
• Apple's Core Foundation library contains a CFBinaryHeap [8] structure.

References
[1] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGraw-Hill.
[2] http:/ / www. cs. au. dk/ ~gerth/ papers/ soda96. pdf
[3] http:/ / www. cs. au. dk/ ~gerth/ papers/ stoc12. pdf
[4] http:/ / docs. oracle. com/ javase/ 6/ docs/ api/ java/ util/ PriorityQueue. html
[5] http:/ / docs. python. org/ library/ heapq. html
[6] https:/ / metacpan. org/ module/ Heap
[7] http:/ / golang. org/ pkg/ container/ heap/
[8] https:/ / developer. apple. com/ library/ mac/ #documentation/ CoreFoundation/ Reference/ CFBinaryHeapRef/ Reference/ reference. html

External links
• Heap (http://mathworld.wolfram.com/Heap.html) at Wolfram MathWorld

Fibonacci heap
In computer science, a Fibonacci heap is a heap data structure consisting of a collection of trees. It has a better
amortized running time than a binomial heap. Fibonacci heaps were developed by Michael L. Fredman and Robert
E. Tarjan in 1984 and first published in a scientific journal in 1987. The name of Fibonacci heap comes from
Fibonacci numbers which are used in the running time analysis.
Find-minimum is O(1) amortized time.[1] Operations insert, decrease key, and merge (union) work in constant
amortized time. Operations delete and delete minimum work in O(log n) amortized time. This means that starting
from an empty data structure, any sequence of a operations from the first group and b operations from the second
group would take O(a + b log n) time. In a binomial heap such a sequence of operations would take O((a + b) log
(n)) time. A Fibonacci heap is thus better than a binomial heap when b is asymptotically smaller than a.
Using Fibonacci heaps for priority queues improves the asymptotic running time of important algorithms, such as
Dijkstra's algorithm for computing the shortest path between two nodes in a graph.
Fibonacci heap 56

Structure
A Fibonacci heap is a collection of trees satisfying the
minimum-heap property, that is, the key of a child is
always greater than or equal to the key of the parent.
This implies that the minimum key is always at the root
of one of the trees. Compared with binomial heaps, the
structure of a Fibonacci heap is more flexible. The trees
do not have a prescribed shape and in the extreme case
the heap can have every element in a separate tree. This
flexibility allows some operations to be executed in a
"lazy" manner, postponing the work for later
operations. For example merging heaps is done simply
by concatenating the two lists of trees, and operation
Figure 1. Example of a Fibonacci heap. It has three trees of degrees
decrease key sometimes cuts a node from its parent and 0, 1 and 3. Three vertices are marked (shown in blue). Therefore the
forms a new tree. potential of the heap is 9 (3 trees + 2 * marked-vertices).

However at some point some order needs to be


introduced to the heap to achieve the desired running time. In particular, degrees of nodes (here degree means the
number of children) are kept quite low: every node has degree at most O(log n) and the size of a subtree rooted in a
node of degree k is at least Fk + 2, where Fk is the kth Fibonacci number. This is achieved by the rule that we can cut
at most one child of each non-root node. When a second child is cut, the node itself needs to be cut from its parent
and becomes the root of a new tree (see Proof of degree bounds, below). The number of trees is decreased in the
operation delete minimum, where trees are linked together.

As a result of a relaxed structure, some operations can take a long time while others are done very quickly. For the
amortized running time analysis we use the potential method, in that we pretend that very fast operations take a little
bit longer than they actually do. This additional time is then later combined and subtracted from the actual running
time of slow operations. The amount of time saved for later use is measured at any given moment by a potential
function. The potential of a Fibonacci heap is given by
Potential = t + 2m
where t is the number of trees in the Fibonacci heap, and m is the number of marked nodes. A node is marked if at
least one of its children was cut since this node was made a child of another node (all roots are unmarked).
Thus, the root of each tree in a heap has one unit of time stored. This unit of time can be used later to link this tree
with another tree at amortized time 0. Also, each marked node has two units of time stored. One can be used to cut
the node from its parent. If this happens, the node becomes a root and the second unit of time will remain stored in it
as in any other root.

Implementation of operations
To allow fast deletion and concatenation, the roots of all trees are linked using a circular, doubly linked list. The
children of each node are also linked using such a list. For each node, we maintain its number of children and
whether the node is marked. Moreover we maintain a pointer to the root containing the minimum key.
Operation find minimum is now trivial because we keep the pointer to the node containing it. It does not change the
potential of the heap, therefore both actual and amortized cost is constant. As mentioned above, merge is
implemented simply by concatenating the lists of tree roots of the two heaps. This can be done in constant time and
the potential does not change, leading again to constant amortized time.
Fibonacci heap 57

Operation insert works by creating a new heap with one element and doing merge. This takes constant time, and the
potential increases by one, because the number of trees increases. The amortized cost is thus still constant.
Operation extract minimum (same as delete minimum) operates in three
phases. First we take the root containing the minimum element and
remove it. Its children will become roots of new trees. If the number of
children was d, it takes time O(d) to process all new roots and the
potential increases by d−1. Therefore the amortized running time of this
phase is O(d) = O(log n).

Fibonacci heap from Figure 1 after first phase


of extract minimum. Node with key 1 (the
minimum) was deleted and its children were
added as separate trees.

However to complete the extract minimum operation, we need to update the


pointer to the root with minimum key. Unfortunately there may be up to n roots
we need to check. In the second phase we therefore decrease the number of roots
by successively linking together roots of the same degree. When two roots u and v
have the same degree, we make one of them a child of the other so that the one
with the smaller key remains the root. Its degree will increase by one. This is
repeated until every root has a different degree. To find trees of the same degree
efficiently we use an array of length O(log n) in which we keep a pointer to one
root of each degree. When a second root is found of the same degree, the two are
Fibonacci heap from Figure 1 after linked and the array is updated. The actual running time is O(log n + m) where m
extract minimum is completed.
is the number of roots at the beginning of the second phase. At the end we will
First, nodes 3 and 6 are linked
together. Then the result is linked
have at most O(log n) roots (because each has a different degree). Therefore the
with tree rooted at node 2. Finally, difference in the potential function from before this phase to after it is: O(log n) −
the new minimum is found. m, and the amortized running time is then at most O(log n + m) + O(log n) − m =
O(log n). Since we can scale up the units of potential stored at insertion in each
node by the constant factor in the O(m) part of the actual cost for this phase.

In the third phase we check each of the remaining roots and find the minimum. This takes O(log n) time and the
potential does not change. The overall amortized running time of extract minimum is therefore O(log n).
Operation decrease key will take the node, decrease
the key and if the heap property becomes violated (the
new key is smaller than the key of the parent), the node
is cut from its parent. If the parent is not a root, it is
marked. If it has been marked already, it is cut as well
and its parent is marked. We continue upwards until we
reach either the root or an unmarked node. In the Fibonacci heap from Figure 1 after decreasing key of node 9 to 0.
process we create some number, say k, of new trees. This node as well as its two marked ancestors are cut from the tree
rooted at 1 and placed as new roots.
Each of these new trees except possibly the first one
was marked originally but as a root it will become
unmarked. One node can become marked. Therefore the number of marked nodes changes by −(k − 1) + 1 = − k + 2.
Combining these 2 changes, the potential changes by 2(−k + 2) + k = −k + 4. The actual time to perform the cutting
was O(k), therefore the amortized running time is constant.
Fibonacci heap 58

Finally, operation delete can be implemented simply by decreasing the key of the element to be deleted to minus
infinity, thus turning it into the minimum of the whole heap. Then we call extract minimum to remove it. The
amortized running time of this operation is O(log n).

Proof of degree bounds


The amortized performance of a Fibonacci heap depends on the degree (number of children) of any tree root being
O(log n), where n is the size of the heap. Here we show that the size of the (sub)tree rooted at any node x of degree d
in the heap must have size at least Fd+2, where Fk is the kth Fibonacci number. The degree bound follows from this
and the fact (easily proved by induction) that for all integers , where
. (We then have , and taking the log to base of both sides
gives as required.)
Consider any node x somewhere in the heap (x need not be the root of one of the main trees). Define size(x) to be the
size of the tree rooted at x (the number of descendants of x, including x itself). We prove by induction on the height
of x (the length of a longest simple path from x to a descendant leaf), that size(x) ≥ Fd+2, where d is the degree of x.
Base case: If x has height 0, then d = 0, and size(x) = 1 = F2.
Inductive case: Suppose x has positive height and degree d>0. Let y1, y2, ..., yd be the children of x, indexed in order
of the times they were most recently made children of x (y1 being the earliest and yd the latest), and let c1, c2, ..., cd
be their respective degrees. We claim that ci ≥ i-2 for each i with 2≤i≤d: Just before yi was made a child of x,
y1,...,yi−1 were already children of x, and so x had degree at least i−1 at that time. Since trees are combined only
when the degrees of their roots are equal, it must have been that yi also had degree at least i-1 at the time it became a
child of x. From that time to the present, yi can only have lost at most one child (as guaranteed by the marking
process), and so its current degree ci is at least i−2. This proves the claim.
Since the heights of all the yi are strictly less than that of x, we can apply the inductive hypothesis to them to get
size(yi) ≥ Fci+2 ≥ F(i−2)+2 = Fi. The nodes x and y1 each contribute at least 1 to size(x), and so we have

A routine induction proves that for any , which gives the desired lower bound on

size(x).

Worst case
Although the total running time of a sequence of operations starting with an empty structure is bounded by the
bounds given above, some (very few) operations in the sequence can take very long to complete (in particular delete
and delete minimum have linear running time in the worst case). For this reason Fibonacci heaps and other amortized
data structures may not be appropriate for real-time systems. It is possible to create a data structure which has the
same worst case performance as the Fibonacci heap has amortized performance. However the resulting structure (a
Brodal queue) is, in the words of the creator, "quite complicated" and "[not] applicable in practice."
Fibonacci heap 59

Summary of running times


Common Effect Unsorted Sorted Self-balancing Binary Binomial Fibonacci Brodal Pairing heap
Operations Linked Linked binary search heap heap heap queue
List List tree

insert(data,key) Adds data to O(1) O(n) O(log n) O(log n) O(log n) O(1) O(1) O(1)
the queue,
tagged with
key

findMin() -> Returns O(n) O(1) O(log n) or O(1) O(log n) O(1) O(1) O(1)
key,data key,data O(1) (**) or O(1)
corresponding (**)
to min-value
key

deleteMin() Deletes data O(n) O(1) O(log n) O(log n) O(log n) O(log n)* O(log O(log n)*
corresponding n)
to min-value
key

delete(node) Deletes data O(1) O(1) O(log n) O(log n) O(log n) O(log n)* O(log O(log n)*
corresponding n)
to given key,
given a
pointer to the
node being
deleted

decreaseKey(node) Decreases O(1) O(n) O(log n) O(log n) O(log n) O(1)* O(1) Unknown but bounded:
the key of a
node, given a *
pointer to the
node being
modified

merge(heap1,heap2) Merges two O(1) O(m + O(m O(m O(log O(1) O(1) O(1)
-> heap3 heaps into a n) log(n+m)) log(n+m)) (m+n))
third

(*)Amortized time
(**)With trivial modification to store an additional pointer to the minimum element

References
[1] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and
McGraw-Hill, 2001. ISBN 0-262-03293-7. Chapter 20: Fibonacci Heaps, pp.476–497. Third edition p518.

External links
• Java applet simulation of a Fibonacci heap (http://www.cs.yorku.ca/~aaw/Jason/FibonacciHeapAnimation.
html)
• MATLAB implementation of Fibonacci heap (http://www.mathworks.com/matlabcentral/fileexchange/
30072-fibonacci-heap)
• De-recursived and memory efficient C implementation of Fibonacci heap (http://www.labri.fr/perso/pelegrin/
code/#fibonacci) (free/libre software, CeCILL-B license (http://www.cecill.info/licences/
Licence_CeCILL-B_V1-en.html))
• C++ template Fibonacci heap, with demonstration (http://ideone.com/9jYnv)
Fibonacci heap 60

• Ruby implementation of the Fibonacci heap (with tests) (http://github.com/evansenter/f_heap)


• Pseudocode of the Fibonacci heap algorithm (http://www.cs.princeton.edu/~wayne/cs423/fibonacci/
FibonacciHeapAlgorithm.html)
• Efficient C++ Fibonacci heap (http://stackoverflow.com/q/504823/194609)
• Various Java Implementations for Fibonacci heap (http://stackoverflow.com/q/6273833/194609)

Spanning tree
In the mathematical field of graph theory, a spanning tree T of a
connected, undirected graph G is a tree that includes all of the vertices
and some or all of the edges of G.

Definitions
A tree is a connected undirected graph with no cycles. It is a spanning
tree of a graph G if it spans G (that is, it includes every vertex of G)
and is a subgraph of G (every edge in the tree belongs to G). A
spanning tree of a connected graph G can also be defined as a maximal
set of edges of G that contains no cycle, or as a minimal set of edges
that connect all vertices.
A spanning tree (blue heavy edges) of a grid
graph
Fundamental cycles
Adding just one edge to a spanning tree will create a cycle; such a cycle is called a fundamental cycle. There is a
distinct fundamental cycle for each edge; thus, there is a one-to-one correspondence between fundamental cycles and
edges not in the spanning tree. For a connected graph with V vertices, any spanning tree will have V − 1 edges, and
thus, a graph of E edges and one of its spanning trees will have E − V + 1 fundamental cycles. For any given
spanning tree the set of all E − V + 1 fundamental cycles forms a cycle basis, a basis for the cycle space.[1]

Fundamental cutsets
Dual to the notion of a fundamental cycle is the notion of a fundamental cutset. By deleting just one edge of the
spanning tree, the vertices are partitioned into two disjoint sets. The fundamental cutset is defined as the set of edges
that must be removed from the graph G to accomplish the same partition. Thus, each spanning tree defines a set of
V − 1 fundamental cutsets, one for each edge of the spanning tree.[2]
The duality between fundamental cutsets and fundamental cycles is established by noting that cycle edges not in the
spanning tree can only appear in the cutsets of the other edges in the cycle; and vice versa: edges in a cutset can only
appear in those cycles containing the edge corresponding to the cutset. This duality can also be expressed using the
theory of matroids, according to which a spanning tree is a base of the graphic matroid, a fundamental cycle is the
unique circuit within the set formed by adding one element to the base, and fundamental cutsets are defined in the
same way from the dual matroid.
Spanning tree 61

Spanning forests
In graphs that are not connected, there can be no spanning tree, and one must consider spanning forests instead.
Here there are two competing definitions:
• Some authors consider a spanning forest to be a maximal acyclic subgraph of the given graph, or equivalently a
graph consisting of a spanning tree in each connected component of the graph.[3]
• For other authors, a spanning forest is a forest that spans all of the vertices, meaning only that each vertex of the
graph is a vertex in the forest. For this definition, even a connected graph may have a disconnected spanning
forest, such as the forest in which each vertex forms a single-vertex tree.
To avoid confusion between these two definitions, Gross & Yellen (2005) suggest the term "full spanning forest" for
a spanning forest with the same connectivity as the given graph, while Bondy & Murty (2008) instead call this kind
of forest a "maximal spanning forest".[4]

Counting spanning trees


The number t(G) of spanning trees of a connected graph is a
well-studied invariant.

In specific graphs
In some cases, it is easy to calculate t(G) directly:
• If G is itself a tree, then t(G) = 1.
• When G is the cycle graph Cn with n vertices, then t(G) = n.
• For a complete graph with n vertices, Cayley's formula gives the
number of spanning trees as nn − 2.
• If G is the complete bipartite graph , then
.
• For the n-dimensional hypercube graph , the number of

spanning trees is .
Cayley's formula counts the number of spanning
trees on a complete graph. There are
trees in , trees
In arbitrary graphs in , and trees in .

More generally, for any graph G, the number t(G) can be calculated in
polynomial time as the determinant of a matrix derived from the graph, using Kirchhoff's matrix-tree theorem.
Specifically, to compute t(G), one constructs a square matrix in which the rows and columns are both indexed by the
vertices of G. The entry in row i and column j is one of three values:
• The degree of vertex i, if i = j,
• −1, if vertices i and j are adjacent, or
• 0, if vertices i and j are different from each other but not adjacent.
The resulting matrix is singular, so its determinant is zero. However, deleting the row and column for an arbitrarily
chosen vertex leads to a smaller matrix whose determinant is exactly t(G).

Deletion-contraction
If G is a graph or multigraph and e is an arbitrary edge of G, then the number t(G) of spanning trees of G satisfies the
deletion-contraction recurrence t(G) = t(G − e) + t(G/e), where G − e is the multigraph obtained by deleting e and
G/e is the contraction of G by e.[5] The term t(G − e) in this formula counts the spanning trees of G that do not use
Spanning tree 62

edge e, and the term t(G/e) counts the spanning trees of G that use e.
In this formula, if the given graph G is a multigraph, or if a contraction causes two vertices to be connected to each
other by multiple edges, then the redundant edges should not be removed, as that would lead to the wrong total. For
instance a bond graph connecting two vertices by k edges has k different spanning trees, each consisting of a single
one of these edges.

Tutte polynomial
The Tutte polynomial of a graph can be defined as a sum, over the spanning trees of the graph, of terms computed
from the "internal activity" and "external activity" of the tree. Its value at the arguments (1,1) is the number of
spanning trees or, in a disconnected graph, the number of maximal spanning forest.[6]
The Tutte polynomial can also be computed using a deletion-contraction recurrence, but its computational
complexity is high: for many values of its arguments, computing it exactly is #P-complete, and it is also hard to
approximate with a guaranteed approximation ratio. The point (1,1), at which it can be evaluated using Kirchhoff's
theorem, is one of the few exceptions.[7]

Algorithms

Construction
A single spanning tree of a graph can be found in linear time by either depth-first search or breadth-first search. Both
of these algorithms explore the given graph, starting from an arbitrary vertex v, by looping through the neighbors of
the vertices they discover and adding each unexplored neighbor to a data structure to be explored later. They differ in
whether this data structure is a stack (in the case of depth-first search) or a queue (in the case of breadth-first search).
In either case, one can form a spanning tree by connecting each vertex, other than the root vertex v, to the vertex
from which it was discovered. This tree is known as a depth-first search tree or a breadth-first search tree according
to the graph exploration algorithm used to construct it. Depth-first search trees are a special case of a class of
spanning trees called Trémaux trees, named after the 19th-century discoverer of depth-first search.
Spanning trees are important in parallel and distributed computing, as a way of maintaining communications
between a set of processors; see for instance the Spanning Tree Protocol used by OSI link layer devices. However,
the depth-first and breadth-first methods for constructing spanning trees on sequential computers are not well suited
for parallel and distributed computers. Instead, researchers have devised several more specialized algorithms for
finding spanning trees in these models of computation.[8]

Optimization
In certain fields of graph theory it is often useful to find a minimum spanning tree of a weighted graph. Other
optimization problems on spanning trees have also been studied, including the maximum spanning tree, the
minimum tree that spans at least k vertices, the spanning tree with the fewest edges per vertex, the spanning tree with
the largest number of leaves, the spanning tree with the fewest leaves (closely related to the Hamiltonian path
problem), the minimum diameter spanning tree, and the minimum dilation spanning tree.
Optimal spanning tree problems have also been studied for finite sets of points in a geometric space such as the
Euclidean plane. For such an input, a spanning tree is again a tree that has as its vertices the given points. The quality
of the tree is measured in the same way as in a graph, using the Euclidean distance between pairs of points as the
weight for each edge. Thus, for instance, a Euclidean minimum spanning tree is the same as a graph minimum
spanning tree in a complete graph with Euclidean edge weights. However, it is not necessary to construct this graph
in order to solve the optimization problem; the Euclidean minimum spanning tree problem, for instance, can be
solved more efficiently in O(n log n) time by constructing the Delaunay triangulation and then applying a linear time
planar graph minimum spanning tree algorithm to the resulting triangulation.
Spanning tree 63

Randomization
A spanning tree chosen randomly from among all the spanning trees with equal probability is called a uniform
spanning tree. Wilson's algorithm can be used to generate uniform spanning trees in polynomial time by a process of
taking a random walk on the given graph and erasing the cycles created by this walk.
An alternative model for generating spanning trees randomly but not uniformly is the random minimal spanning tree.
In this model, the edges of the graph are assigned random weights and then the minimum spanning tree of the
weighted graph is constructed.

Enumeration
Because a graph may have exponentially many spanning trees, it is not possible to list them all in polynomial time.
However, algorithms are known for listing all spanning trees in polynomial time per tree.

In infinite graphs
Every finite connected graph has a spanning tree. However, for infinite connected graphs, the existence of spanning
trees is equivalent to the axiom of choice. An infinite graph is connected if each pair of its vertices forms the pair of
endpoints of a finite path. As with finite graphs, a tree is a connected graph with no finite cycles, and a spanning tree
can be defined either as a maximal acyclic set of edges or as a tree that contains every vertex.
The trees within a graph may be partially ordered by their subgraph relation, and any infinite chain in this partial
order has an upper bound (the union of the trees in the chain). Zorn's lemma, one of many equivalent statements to
the axiom of choice, states that a partial order in which all chains are upper bounded must have a maximal element;
in the partial order on the trees of the graph, this maximal element must be a spanning tree. Therefore, if Zorn's
lemma is true, every infinite connected graph has a spanning tree.
In the other direction, given a family of sets, it is possible to construct an infinite graph such that every spanning tree
of the graph corresponds to a choice function of the family of sets. Therefore, if every infinite connected graph has a
spanning tree, then the axiom of choice is true.[9]

Notes
[1] , pp. 65–67.
[2] , pp. 67–69.
[3] ;.
[4] ;.
[5] , p. 109.
[6] , p. 351.
[7] ;.
[8] ;;.
[9] . See in particular Theorem 2.1, pp. 192–193 (http:/ / books. google. com/ books?id=kIKW18ENfUMC& pg=PA192).
64

Graphs

Graph (mathematics)
In mathematics, and more specifically in graph theory,
a graph is a representation of a set of objects where
some pairs of objects are connected by links. The
interconnected objects are represented by mathematical
abstractions called vertices, and the links that connect
some pairs of vertices are called edges. Typically, a
graph is depicted in diagrammatic form as a set of dots
for the vertices, joined by lines or curves for the edges.
Graphs are one of the objects of study in discrete
mathematics.

The edges may be directed or undirected. For example, A drawing of a labeled graph on 6 vertices and 7 edges.
if the vertices represent people at a party, and there is
an edge between two people if they shake hands, then this is an undirected graph, because if person A shook hands
with person B, then person B also shook hands with person A. In contrast, if there is an edge from person A to
person B when person A knows of person B, then this graph is directed, because knowledge of someone is not
necessarily a symmetric relation (that is, one person knowing another person does not necessarily imply the reverse;
for example, many fans may know of a celebrity, but the celebrity is unlikely to know of all their fans). This latter
type of graph is called a directed graph and the edges are called directed edges or arcs.

Vertices are also called nodes or points, and edges are also called arcs or lines. Graphs are the basic subject studied
by graph theory. The word "graph" was first used in this sense by J.J. Sylvester in 1878.

Definitions
Definitions in graph theory vary. The following are some of the more basic ways of defining graphs and related
mathematical structures.

Graph
In the most common sense of the term,[1] a graph is an ordered pair G = (V, E) comprising a set V of vertices or
nodes together with a set E of edges or lines, which are 2-element subsets of V (i.e., an edge is related with two
vertices, and the relation is represented as an unordered pair of the vertices with respect to the particular edge). To
avoid ambiguity, this type of graph may be described precisely as undirected and simple.
Other senses of graph stem from different conceptions of the edge set. In one more generalized notion,[2] E is a set
together with a relation of incidence that associates with each edge two vertices. In another generalized notion, E is a
multiset of unordered pairs of (not necessarily distinct) vertices. Many authors call this type of object a multigraph or
pseudograph.
All of these variants and others are described more fully below.
The vertices belonging to an edge are called the ends, endpoints, or end vertices of the edge. A vertex may exist in
a graph and not belong to an edge.
Graph (mathematics) 65

V and E are usually taken to be finite, and many of the well-known results are not true (or are rather different) for
infinite graphs because many of the arguments fail in the infinite case. The order of a graph is (the number of
vertices). A graph's size is , the number of edges. The degree of a vertex is the number of edges that connect to
it, where an edge that connects to the vertex at both ends (a loop) is counted twice.
For an edge {u, v}, graph theorists usually use the somewhat shorter notation uv.

Adjacency relation
The edges E of an undirected graph G induce a symmetric binary relation ~ on V that is called the adjacency relation
of G. Specifically, for each edge {u, v} the vertices u and v are said to be adjacent to one another, which is denoted
u ~ v.

Types of graphs

Distinction in terms of the main definition


As stated above, in different contexts it may be useful to define the term graph with different degrees of generality.
Whenever it is necessary to draw a strict distinction, the following terms are used. Most commonly, in modern texts
in graph theory, unless stated otherwise, graph means "undirected simple finite graph" (see the definitions below).

A directed graph.

A simple undirected graph with three vertices and three edges. Each vertex has degree two, so this is also a regular
graph.

Undirected graph
An undirected graph is one in which edges have no orientation. The edge (a, b) is identical to the edge (b, a), i.e.,
they are not ordered pairs, but sets {u, v} (or 2-multisets) of vertices. The maximum number of edges in an
undirected graph without a self-loop is n(n - 1)/2.

Directed graph
A directed graph or digraph is an ordered pair D = (V, A) with
• V a set whose elements are called vertices or nodes, and
• A a set of ordered pairs of vertices, called arcs, directed edges, or arrows.
An arc a = (x, y) is considered to be directed from x to y; y is called the head and x is called the tail of the arc; y is
said to be a direct successor of x, and x is said to be a direct predecessor of y. If a path leads from x to y, then y is
said to be a successor of x and reachable from x, and x is said to be a predecessor of y. The arc (y, x) is called the
arc (x, y) inverted.
A directed graph D is called symmetric if, for every arc in D, the corresponding inverted arc also belongs to D. A
symmetric loopless directed graph D = (V, A) is equivalent to a simple undirected graph G = (V, E), where the pairs
Graph (mathematics) 66

of inverse arcs in A correspond 1-to-1 with the edges in E; thus the edges in G number |E| = |A|/2, or half the number
of arcs in D.
A variation on this definition is the oriented graph, in which at most one of (x, y) and (y, x) may be arcs.

Mixed graph
A mixed graph G is a graph in which some edges may be directed and some may be undirected. It is written as an
ordered triple G = (V, E, A) with V, E, and A defined as above. Directed and undirected graphs are special cases.

Multigraph
A loop is an edge (directed or undirected) which starts and ends on the same vertex; these may be permitted or not
permitted according to the application. In this context, an edge with two different ends is called a link.
The term "multigraph" is generally understood to mean that multiple edges (and sometimes loops) are allowed.
Where graphs are defined so as to allow loops and multiple edges, a multigraph is often defined to mean a graph
without loops,[3] however, where graphs are defined so as to disallow loops and multiple edges, the term is often
defined to mean a "graph" which can have both multiple edges and loops,[4] although many use the term
"pseudograph" for this meaning.[5]

Quiver
A quiver or "multidigraph" is a directed graph which may have more than one arrow from a given source to a given
target. A quiver may also have directed loops in it.

Simple graph
As opposed to a multigraph, a simple graph is an undirected graph that has no loops (edges connected at both ends to
the same vertex) and no more than one edge between any two different vertices. In a simple graph the edges of the
graph form a set (rather than a multiset) and each edge is a distinct pair of vertices. In a simple graph with n vertices
every vertex has a degree that is less than n (the converse, however, is not true — there exist non-simple graphs with
n vertices in which every vertex has a degree smaller than n).

Weighted graph
A graph is a weighted graph if a number (weight) is assigned to each edge. Such weights might represent, for
example, costs, lengths or capacities, etc. depending on the problem at hand. Some authors call such a graph a
network.

Half-edges, loose edges


In certain situations it can be helpful to allow edges with only one end, called half-edges, or no ends (loose edges);
see for example signed graphs and biased graphs.

Important graph classes

Regular graph
A regular graph is a graph where each vertex has the same number of neighbours, i.e., every vertex has the same
degree or valency. A regular graph with vertices of degree k is called a k‑regular graph or regular graph of degree k.
Graph (mathematics) 67

Complete graph

Complete graphs have the feature that each pair of vertices has an edge connecting
them.

Finite and infinite graphs

A finite graph is a graph G = (V, E) such that V and E are finite sets. An infinite
graph is one with an infinite set of vertices or edges or both.
Most commonly in graph theory it is implied that the graphs discussed are finite. If
the graphs are infinite, that is usually specifically stated.
A complete graph with 5 vertices.
Each vertex has an edge to every
Graph classes in terms of connectivity other vertex.

In an undirected graph G, two vertices u and v are called connected if G contains a


path from u to v. Otherwise, they are called disconnected. A graph is called connected if every pair of distinct
vertices in the graph is connected; otherwise, it is called disconnected.
A graph is called k-vertex-connected or k-edge-connected if no set of k-1 vertices (respectively, edges) exists that,
when removed, disconnects the graph. A k-vertex-connected graph is often called simply k-connected.
A directed graph is called weakly connected if replacing all of its directed edges with undirected edges produces a
connected (undirected) graph. It is strongly connected or strong if it contains a directed path from u to v and a
directed path from v to u for every pair of vertices u, v.

Category of all graphs


The category of all graphs is the slice category where is the functor taking a set to
.

Properties of graphs
Two edges of a graph are called adjacent if they share a common vertex. Two arrows of a directed graph are called
consecutive if the head of the first one is at the nock (notch end) of the second one. Similarly, two vertices are called
adjacent if they share a common edge (consecutive if they are at the notch and at the head of an arrow), in which
case the common edge is said to join the two vertices. An edge and a vertex on that edge are called incident.
The graph with only one vertex and no edges is called the trivial graph. A graph with only vertices and no edges is
known as an edgeless graph. The graph with no vertices and no edges is sometimes called the null graph or empty
graph, but the terminology is not consistent and not all mathematicians allow this object.
In a weighted graph or digraph, each edge is associated with some value, variously called its cost, weight, length or
other term depending on the application; such graphs arise in many contexts, for example in optimal routing
problems such as the traveling salesman problem.
Normally, the vertices of a graph, by their nature as elements of a set, are distinguishable. This kind of graph may be
called vertex-labeled. However, for many questions it is better to treat vertices as indistinguishable; then the graph
may be called unlabeled. (Of course, the vertices may be still distinguishable by the properties of the graph itself,
e.g., by the numbers of incident edges). The same remarks apply to edges, so graphs with labeled edges are called
edge-labeled graphs. Graphs with labels attached to edges or vertices are more generally designated as labeled.
Consequently, graphs in which vertices are indistinguishable and edges are indistinguishable are called unlabeled.
(Note that in the literature the term labeled may apply to other kinds of labeling, besides that which serves only to
distinguish different vertices or edges.)
Graph (mathematics) 68

Examples
• The diagram at right is a graphic representation of the following
graph:
V = {1, 2, 3, 4, 5, 6}
E = {{1, 2}, {1, 5}, {2, 3}, {2, 5}, {3, 4}, {4, 5}, {4, 6}}.
• In category theory a small category can be represented by a directed
multigraph in which the objects of the category represented as
vertices and the morphisms as directed edges. Then, the functors
between categories induce some, but not necessarily all, of the A graph with six nodes.
digraph morphisms of the graph.
• In computer science, directed graphs are used to represent knowledge (e.g., Conceptual graph), finite state
machines, and many other discrete structures.
• A binary relation R on a set X defines a directed graph. An element x of X is a direct predecessor of an element y
of X iff xRy.

Important graphs
Basic examples are:
• In a complete graph, each pair of vertices is joined by an edge; that is, the graph contains all possible edges.
• In a bipartite graph, the vertex set can be partitioned into two sets, W and X, so that no two vertices in W are
adjacent and no two vertices in X are adjacent. Alternatively, it is a graph with a chromatic number of 2.
• In a complete bipartite graph, the vertex set is the union of two disjoint sets, W and X, so that every vertex in W is
adjacent to every vertex in X but there are no edges within W or X.
• In a linear graph or path graph of length n, the vertices can be listed in order, v0, v1, ..., vn, so that the edges are
vi−1vi for each i = 1, 2, ..., n. If a linear graph occurs as a subgraph of another graph, it is a path in that graph.
• In a cycle graph of length n ≥ 3, vertices can be named v1, ..., vn so that the edges are vi−1vi for each i = 2,...,n in
addition to vnv1. Cycle graphs can be characterized as connected 2-regular graphs. If a cycle graph occurs as a
subgraph of another graph, it is a cycle or circuit in that graph.
• A planar graph is a graph whose vertices and edges can be drawn in a plane such that no two of the edges
intersect (i.e., embedded in a plane).
• A tree is a connected graph with no cycles.
• A forest is a graph with no cycles (i.e. the disjoint union of one or more trees).
More advanced kinds of graphs are:
• The Petersen graph and its generalizations
• Perfect graphs
• Cographs
• Chordal graphs
• Other graphs with large automorphism groups: vertex-transitive, arc-transitive, and distance-transitive graphs.
• Strongly regular graphs and their generalization distance-regular graphs.
Graph (mathematics) 69

Operations on graphs
There are several operations that produce new graphs from old ones, which might be classified into the following
categories:
• Elementary operations, sometimes called "editing operations" on graphs, which create a new graph from the
original one by a simple, local change, such as addition or deletion of a vertex or an edge, merging and splitting of
vertices, etc.
• Graph rewrite operations replacing the occurrence of some pattern graph within the host graph by an instance of
the corresponding replacement graph.
• Unary operations, which create a significantly new graph from the old one. Examples:
• Line graph
• Dual graph
• Complement graph
• Binary operations, which create new graph from two initial graphs. Examples:
• Disjoint union of graphs
• Cartesian product of graphs
• Tensor product of graphs
• Strong product of graphs
• Lexicographic product of graphs

Generalizations
In a hypergraph, an edge can join more than two vertices.
An undirected graph can be seen as a simplicial complex consisting of 1-simplices (the edges) and 0-simplices (the
vertices). As such, complexes are generalizations of graphs since they allow for higher-dimensional simplices.
Every graph gives rise to a matroid.
In model theory, a graph is just a structure. But in that case, there is no limitation on the number of edges: it can be
any cardinal number, see continuous graph.
In computational biology, power graph analysis introduces power graphs as an alternative representation of
undirected graphs.
In geographic information systems, geometric networks are closely modeled after graphs, and borrow many concepts
from graph theory to perform spatial analysis on road networks or utility grids.

Notes
[1] See, for instance, Iyanaga and Kawada, 69 J, p. 234 or Biggs, p. 4.
[2] See, for instance, Graham et al., p. 5.
[3] For example, see Balakrishnan, p. 1, Gross (2003), p. 4, and Zwillinger, p. 220.
[4] For example, see. Bollobás, p. 7 and Diestel, p. 25.
[5] Gross (1998), p. 3, Gross (2003), p. 205, Harary, p.10, and Zwillinger, p. 220.

References
• Balakrishnan, V. K. (1997-02-01). Graph Theory (1st ed.). McGraw-Hill. ISBN 0-07-005489-4.
• Berge, Claude (1958). Théorie des graphes et ses applications (in French). Dunod, Paris: Collection Universitaire
de Mathématiques, II. pp. viii+277. Translation: -. Dover, New York: Wiley. 2001 [1962].
• Biggs, Norman (1993). Algebraic Graph Theory (2nd ed.). Cambridge University Press. ISBN 0-521-45897-8.
• Bollobás, Béla (2002-08-12). Modern Graph Theory (1st ed.). Springer. ISBN 0-387-98488-7.
Graph (mathematics) 70

• Bang-Jensen, J.; Gutin, G. (2000). Digraphs: Theory, Algorithms and Applications (http://www.cs.rhul.ac.uk/
books/dbook/). Springer.
• Diestel, Reinhard (2005). Graph Theory (http://diestel-graph-theory.com/GrTh.html) (3rd ed.). Berlin, New
York: Springer-Verlag. ISBN 978-3-540-26183-4.
• Graham, R.L., Grötschel, M., and Lovász, L, ed. (1995). Handbook of Combinatorics. MIT Press.
ISBN 0-262-07169-X.
• Gross, Jonathan L.; Yellen, Jay (1998-12-30). Graph Theory and Its Applications. CRC Press.
ISBN 0-8493-3982-0.
• Gross, Jonathan L., & Yellen, Jay, ed. (2003-12-29). Handbook of Graph Theory. CRC. ISBN 1-58488-090-2.
• Harary, Frank (January 1995). Graph Theory. Addison Wesley Publishing Company. ISBN 0-201-41033-8.
• Iyanaga, Shôkichi; Kawada, Yukiyosi (1977). Encyclopedic Dictionary of Mathematics. MIT Press.
ISBN 0-262-09016-3.
• Zwillinger, Daniel (2002-11-27). CRC Standard Mathematical Tables and Formulae (31st ed.). Chapman &
Hall/CRC. ISBN 1-58488-291-3.

Further reading
• Trudeau, Richard J. (1993). Introduction to Graph Theory (http://store.doverpublications.com/0486678709.
html) (Corrected, enlarged republication. ed.). New York: Dover Publications. ISBN 978-0-486-67870-2.
Retrieved 8 August 2012.

External links
Library resources about
Graph(mathematics)

• Resources in your library (http://tools.wmflabs.org/ftl/cgi-bin/ftl?st=wp&su=Graph+(mathematics))

• Weisstein, Eric W., " Graph (http://mathworld.wolfram.com/Graph.html)", MathWorld.


Graph theory 71

Graph theory
In mathematics and computer science, graph theory is
the study of graphs, which are mathematical structures
used to model pairwise relations between objects. A
"graph" in this context is made up of "vertices" or
"nodes" and lines called edges that connect them. A
graph may be undirected, meaning that there is no
distinction between the two vertices associated with
each edge, or its edges may be directed from one vertex
to another; see graph (mathematics) for more detailed
definitions and for other variations in the types of graph
that are commonly considered. Graphs are one of the
A drawing of a graph
prime objects of study in discrete mathematics.

Refer to the glossary of graph theory for basic definitions in graph theory.

Applications
Graphs can be used to model many types of relations and processes in physical, biological, social and information
systems. Many practical problems can be represented by graphs.
In computer science, graphs are used to represent networks of communication, data organization, computational
devices, the flow of computation, etc. For instance, the link structure of a website can be represented by a directed
graph, in which the vertices represent web pages and directed edges represent links from one page to another. A
similar approach can be taken to problems in travel, biology, computer chip design, and many other fields. The
development of algorithms to handle graphs is therefore of major interest in computer science. The transformation of
graphs is often formalized and represented by graph rewrite systems. Complementary to graph transformation
systems focusing on rule-based in-memory manipulation of graphs are graph databases geared towards
transaction-safe, persistent storing and querying of graph-structured data.
Graph-theoretic methods, in various forms, have proven particularly useful in linguistics, since natural language
often lends itself well to discrete structure. Traditionally, syntax and compositional semantics follow tree-based
structures, whose expressive power lies in the principle of compositionality, modeled in a hierarchical graph. More
contemporary approaches such as head-driven phrase structure grammar model the syntax of natural language using
typed feature structures, which are directed acyclic graphs. Within lexical semantics, especially as applied to
computers, modeling word meaning is easier when a given word is understood in terms of related words; semantic
networks are therefore important in computational linguistics. Still other methods in phonology (e.g. optimality
theory, which uses lattice graphs) and morphology (e.g. finite-state morphology, using finite-state transducers) are
common in the analysis of language as a graph. Indeed, the usefulness of this area of mathematics to linguistics has
borne organizations such as TextGraphs [1], as well as various 'Net' projects, such as WordNet, VerbNet, and others.
Graph theory is also used to study molecules in chemistry and physics. In condensed matter physics, the three
dimensional structure of complicated simulated atomic structures can be studied quantitatively by gathering statistics
on graph-theoretic properties related to the topology of the atoms. In chemistry a graph makes a natural model for a
molecule, where vertices represent atoms and edges bonds. This approach is especially used in computer processing
of molecular structures, ranging from chemical editors to database searching. In statistical physics, graphs can
represent local connections between interacting parts of a system, as well as the dynamics of a physical process on
such systems. Graphs are also used to represent the micro-scale channels of porous media, in which the vertices
represent the pores and the edges represent the smaller channels connecting the pores.
Graph theory 72

Graph theory is also widely used in sociology as a way, for example, to measure actors' prestige or to explore rumor
spreading, notably through the use of social network analysis software. Under the umbrella of social networks are
many different types of graphs: Acquaintanceship and friendship graphs describe whether people know each other.
Influence graphs model whether certain people can influence the behavior of others. Finally, collaboration graphs
model whether two people work together in a particular way, such as acting in a movie together.
Likewise, graph theory is useful in biology and conservation efforts where a vertex can represent regions where
certain species exist (or habitats) and the edges represent migration paths, or movement between the regions. This
information is important when looking at breeding patterns or tracking the spread of disease, parasites or how
changes to the movement can affect other species.
In mathematics, graphs are useful in geometry and certain parts of topology such as knot theory. Algebraic graph
theory has close links with group theory.
A graph structure can be extended by assigning a weight to each edge of the graph. Graphs with weights, or
weighted graphs, are used to represent structures in which pairwise connections have some numerical values. For
example if a graph represents a road network, the weights could represent the length of each road.

History
The paper written by Leonhard Euler on the Seven Bridges of
Königsberg and published in 1736 is regarded as the first paper in
the history of graph theory. This paper, as well as the one written
by Vandermonde on the knight problem, carried on with the
analysis situs initiated by Leibniz. Euler's formula relating the
number of edges, vertices, and faces of a convex polyhedron was
studied and generalized by Cauchy and L'Huillier, and is at the
origin of topology.

More than one century after Euler's paper on the bridges of


Königsberg and while Listing introduced topology, Cayley was led
The Königsberg Bridge problem
by the study of particular analytical forms arising from differential
calculus to study a particular class of graphs, the trees. This study
had many implications in theoretical chemistry. The involved techniques mainly concerned the enumeration of
graphs having particular properties. Enumerative graph theory then rose from the results of Cayley and the
fundamental results published by Pólya between 1935 and 1937 and the generalization of these by De Bruijn in
1959. Cayley linked his results on trees with the contemporary studies of chemical composition. The fusion of the
ideas coming from mathematics with those coming from chemistry is at the origin of a part of the standard
terminology of graph theory.

In particular, the term "graph" was introduced by Sylvester in a paper published in 1878 in Nature, where he draws
an analogy between "quantic invariants" and "co-variants" of algebra and molecular diagrams:[2]
"[...] Every invariant and co-variant thus becomes expressible by a graph precisely identical with a Kekuléan
diagram or chemicograph. [...] I give a rule for the geometrical multiplication of graphs, i.e. for constructing a
graph to the product of in- or co-variants whose separate graphs are given. [...]" (italics as in the original).
The first textbook on graph theory was written by Dénes Kőnig, and published in 1936. Another book by Frank
Harary, published in 1969, was "considered the world over to be the definitive textbook on the subject", and enabled
mathematicians, chemists, electrical engineers and social scientists to talk to each other. Harary donated all of the
royalties to fund the Pólya Prize.
Graph theory 73

One of the most famous and stimulating problems in graph theory is the four color problem: "Is it true that any map
drawn in the plane may have its regions colored with four colors, in such a way that any two regions having a
common border have different colors?" This problem was first posed by Francis Guthrie in 1852 and its first written
record is in a letter of De Morgan addressed to Hamilton the same year. Many incorrect proofs have been proposed,
including those by Cayley, Kempe, and others. The study and the generalization of this problem by Tait, Heawood,
Ramsey and Hadwiger led to the study of the colorings of the graphs embedded on surfaces with arbitrary genus.
Tait's reformulation generated a new class of problems, the factorization problems, particularly studied by Petersen
and Kőnig. The works of Ramsey on colorations and more specially the results obtained by Turán in 1941 was at the
origin of another branch of graph theory, extremal graph theory.
The four color problem remained unsolved for more than a century. In 1969 Heinrich Heesch published a method for
solving the problem using computers.[3] A computer-aided proof produced in 1976 by Kenneth Appel and Wolfgang
Haken makes fundamental use of the notion of "discharging" developed by Heesch. The proof involved checking the
properties of 1,936 configurations by computer, and was not fully accepted at the time due to its complexity. A
simpler proof considering only 633 configurations was given twenty years later by Robertson, Seymour, Sanders and
Thomas.
The autonomous development of topology from 1860 and 1930 fertilized graph theory back through the works of
Jordan, Kuratowski and Whitney. Another important factor of common development of graph theory and topology
came from the use of the techniques of modern algebra. The first example of such a use comes from the work of the
physicist Gustav Kirchhoff, who published in 1845 his Kirchhoff's circuit laws for calculating the voltage and
current in electric circuits.
The introduction of probabilistic methods in graph theory, especially in the study of Erdős and Rényi of the
asymptotic probability of graph connectivity, gave rise to yet another branch, known as random graph theory, which
has been a fruitful source of graph-theoretic results.

Drawing graphs
Graphs are represented graphically by drawing a dot or circle for every vertex, and drawing an arc between two
vertices if they are connected by an edge. If the graph is directed, the direction is indicated by drawing an arrow.
A graph drawing should not be confused with the graph itself (the abstract, non-visual structure) as there are several
ways to structure the graph drawing. All that matters is which vertices are connected to which others by how many
edges and not the exact layout. In practice it is often difficult to decide if two drawings represent the same graph.
Depending on the problem domain some layouts may be better suited and easier to understand than others.
The pioneering work of W. T. Tutte was very influential in the subject of graph drawing. Among other
achievements, he introduced the use of linear algebraic methods to obtain graph drawings.
Graph drawing also can be said to encompass problems that deal with the crossing number and its various
generalizations. The crossing number of a graph is the minimum number of intersections between edges that a
drawing of the graph in the plane must contain. For a planar graph, the crossing number is zero by definition.
Drawings on surfaces other than the plane are also studied.

Graph-theoretic data structures


There are different ways to store graphs in a computer system. The data structure used depends on both the graph
structure and the algorithm used for manipulating the graph. Theoretically one can distinguish between list and
matrix structures but in concrete applications the best structure is often a combination of both. List structures are
often preferred for sparse graphs as they have smaller memory requirements. Matrix structures on the other hand
provide faster access for some applications but can consume huge amounts of memory.
Graph theory 74

List structures include the incidence list, an array of pairs of vertices, and the adjacency list, which separately lists
the neighbors of each vertex: Much like the incidence list, each vertex has a list of which vertices it is adjacent to.
Matrix structures include the incidence matrix, a matrix of 0's and 1's whose rows represent vertices and whose
columns represent edges, and the adjacency matrix, in which both the rows and columns are indexed by vertices. In
both cases a 1 indicates two adjacent objects and a 0 indicates two non-adjacent objects. The Laplacian matrix is a
modified form of the adjacency matrix that incorporates information about the degrees of the vertices, and is useful
in some calculations such as Kirchhoff's theorem on the number of spanning trees of a graph. The distance matrix,
like the adjacency matrix, has both its rows and columns indexed by vertices, but rather than containing a 0 or a 1 in
each cell it contains the length of a shortest path between two vertices.

Problems in graph theory

Enumeration
There is a large literature on graphical enumeration: the problem of counting graphs meeting specified conditions.
Some of this work is found in Harary and Palmer (1973).

Subgraphs, induced subgraphs, and minors


A common problem, called the subgraph isomorphism problem, is finding a fixed graph as a subgraph in a given
graph. One reason to be interested in such a question is that many graph properties are hereditary for subgraphs,
which means that a graph has the property if and only if all subgraphs have it too. Unfortunately, finding maximal
subgraphs of a certain kind is often an NP-complete problem.
• Finding the largest complete graph is called the clique problem (NP-complete).
A similar problem is finding induced subgraphs in a given graph. Again, some important graph properties are
hereditary with respect to induced subgraphs, which means that a graph has a property if and only if all induced
subgraphs also have it. Finding maximal induced subgraphs of a certain kind is also often NP-complete. For
example,
• Finding the largest edgeless induced subgraph, or independent set, called the independent set problem
(NP-complete).
Still another such problem, the minor containment problem, is to find a fixed graph as a minor of a given graph. A
minor or subcontraction of a graph is any graph obtained by taking a subgraph and contracting some (or no) edges.
Many graph properties are hereditary for minors, which means that a graph has a property if and only if all minors
have it too. A famous example:
• A graph is planar if it contains as a minor neither the complete bipartite graph (See the Three-cottage
problem) nor the complete graph .
Another class of problems has to do with the extent to which various species and generalizations of graphs are
determined by their point-deleted subgraphs, for example:
• The reconstruction conjecture.
Graph theory 75

Graph coloring
Many problems have to do with various ways of coloring graphs, for example:
• The four-color theorem
• The strong perfect graph theorem
• The Erdős–Faber–Lovász conjecture(unsolved)
• The total coloring conjecture, also called Behzad's conjecture) (unsolved)
• The list coloring conjecture (unsolved)
• The Hadwiger conjecture (graph theory) (unsolved)

Subsumption and unification


Constraint modeling theories concern families of directed graphs related by a partial order. In these applications,
graphs are ordered by specificity, meaning that more constrained graphs—which are more specific and thus contain a
greater amount of information—are subsumed by those that are more general. Operations between graphs include
evaluating the direction of a subsumption relationship between two graphs, if any, and computing graph unification.
The unification of two argument graphs is defined as the most general graph (or the computation thereof) that is
consistent with (i.e. contains all of the information in) the inputs, if such a graph exists; efficient unification
algorithms are known.
For constraint frameworks which are strictly compositional, graph unification is the sufficient satisfiability and
combination function. Well-known applications include automatic theorem proving and modeling the elaboration of
linguistic structure.

Route problems
• Hamiltonian path and cycle problems
• Minimum spanning tree
• Route inspection problem (also called the "Chinese Postman Problem")
• Seven Bridges of Königsberg
• Shortest path problem
• Steiner tree
• Three-cottage problem
• Traveling salesman problem (NP-hard)

Network flow
There are numerous problems arising especially from applications that have to do with various notions of flows in
networks, for example:
• Max flow min cut theorem

Visibility graph problems


• Museum guard problem

Covering problems
Covering problems in graphs are specific instances of subgraph-finding problems, and they tend to be closely related
to the clique problem or the independent set problem.
• Set cover problem
• Vertex cover problem
Graph theory 76

Decomposition problems
Decomposition, defined as partitioning the edge set of a graph (with as many vertices as necessary accompanying the
edges of each part of the partition), has a wide variety of question. Often, it is required to decompose a graph into
subgraphs isomorphic to a fixed graph; for instance, decomposing a complete graph into Hamiltonian cycles. Other
problems specify a family of graphs into which a given graph should be decomposed, for instance, a family of
cycles, or decomposing a complete graph Kn into n − 1 specified trees having, respectively, 1, 2, 3, ..., n − 1 edges.
Some specific decomposition problems that have been studied include:
• Arboricity, a decomposition into as few forests as possible
• Cycle double cover, a decomposition into a collection of cycles covering each edge exactly twice
• Edge coloring, a decomposition into as few matchings as possible
• Graph factorization, a decomposition of a regular graph into regular subgraphs of given degrees

Graph classes
Many problems involve characterizing the members of various classes of graphs. Some examples of such questions
are below:
• Enumerating the members of a class
• Characterizing a class in terms of forbidden substructures
• Ascertaining relationships among classes (e.g., does one property of graphs imply another)
• Finding efficient algorithms to decide membership in a class
• Finding representations for members of a class.

Notes
[1] http:/ / www. textgraphs. org
[2] John Joseph Sylvester (1878), Chemistry and Algebra. Nature, volume 17, page 284. . Online version (http:/ / www. archive. org/ stream/
nature15unkngoog#page/ n312/ mode/ 1up). Retrieved 2009-12-30.
[3] Heinrich Heesch: Untersuchungen zum Vierfarbenproblem. Mannheim: Bibliographisches Institut 1969.

References
• Berge, Claude (1958), Théorie des graphes et ses applications, Collection Universitaire de Mathématiques II,
Paris: Dunod. English edition, Wiley 1961; Methuen & Co, New York 1962; Russian, Moscow 1961; Spanish,
Mexico 1962; Roumanian, Bucharest 1969; Chinese, Shanghai 1963; Second printing of the 1962 first English
edition, Dover, New York 2001.
• Biggs, N.; Lloyd, E.; Wilson, R. (1986), Graph Theory, 1736–1936, Oxford University Press.
• Bondy, J.A.; Murty, U.S.R. (2008), Graph Theory, Springer, ISBN 978-1-84628-969-9.
• Bondy, Riordan, O.M (2003), Mathematical results on scale-free random graphs in "Handbook of Graphs and
Networks" (S. Bornholdt and H.G. Schuster (eds)), Wiley VCH, Weinheim, 1st ed..
• Chartrand, Gary (1985), Introductory Graph Theory, Dover, ISBN 0-486-24775-9.
• Gibbons, Alan (1985), Algorithmic Graph Theory, Cambridge University Press.
• Reuven Cohen, Shlomo Havlin (2010), Complex Networks: Structure, Robustness and Function, Cambridge
University Press
• Golumbic, Martin (1980), Algorithmic Graph Theory and Perfect Graphs, Academic Press.
• Harary, Frank (1969), Graph Theory, Reading, MA: Addison-Wesley.
• Harary, Frank; Palmer, Edgar M. (1973), Graphical Enumeration, New York, NY: Academic Press.
• Mahadev, N.V.R.; Peled, Uri N. (1995), Threshold Graphs and Related Topics, North-Holland.
• Mark Newman (2010), Networks: An Introduction, Oxford University Press.
Graph theory 77

External links

Online textbooks
• Phase Transitions in Combinatorial Optimization Problems, Section 3: Introduction to Graphs (http://arxiv.org/
pdf/cond-mat/0602129) (2006) by Hartmann and Weigt
• Digraphs: Theory Algorithms and Applications (http://www.cs.rhul.ac.uk/books/dbook/) 2007 by Jorgen
Bang-Jensen and Gregory Gutin
• Graph Theory, by Reinhard Diestel (http://diestel-graph-theory.com/index.html)

Other resources
• Hazewinkel, Michiel, ed. (2001), "Graph theory" (http://www.encyclopediaofmath.org/index.php?title=p/
g045010), Encyclopedia of Mathematics, Springer, ISBN 978-1-55608-010-4
• Graph theory tutorial (http://www.utm.edu/departments/math/graph/)
• A searchable database of small connected graphs (http://www.gfredericks.com/main/sandbox/graphs)
• Image gallery: graphs (http://web.archive.org/web/20060206155001/http://www.nd.edu/~networks/
gallery.htm)
• Concise, annotated list of graph theory resources for researchers (http://www.babelgraph.org/links.html)
• rocs (http://www.kde.org/applications/education/rocs/) — a graph theory IDE
• The Social Life of Routers (http://www.orgnet.com/SocialLifeOfRouters.pdf) — non-technical paper
discussing graphs of people and computers
• Graph Theory Software (http://graphtheorysoftware.com/) — tools to teach and learn graph theory
• Online books (http://tools.wmflabs.org/ftl/cgi-bin/ftl?st=&su=Graph+theory&library=OLBP), and library
resources in your library (http://tools.wmflabs.org/ftl/cgi-bin/ftl?st=&su=Graph+theory) and in other
libraries (http://tools.wmflabs.org/ftl/cgi-bin/ftl?st=&su=Graph+theory&library=0CHOOSE0) about graph
theory
Glossary of graph theory 78

Glossary of graph theory


Graph theory is a growing area in mathematical research, and has a large specialized vocabulary. Some authors use
the same word with different meanings. Some authors use different words to mean the same thing. This page
attempts to describe the majority of current usage.

Basics
A graph G consists of two types of elements, namely vertices and edges. Every edge has two endpoints in the set of
vertices, and is said to connect or join the two endpoints. An edge can thus be defined as a set of two vertices (or an
ordered pair, in the case of a directed graph - see Section Direction). The two endpoints of an edge are also said to
be adjacent to each other.
Alternative models of graphs exist; e.g., a graph may be thought of as a
Boolean binary function over the set of vertices or as a square
(0,1)-matrix.
A vertex is simply drawn as a node or a dot. The vertex set of G is
usually denoted by V(G), or V when there is no danger of confusion.
The order of a graph is the number of its vertices, i.e. |V(G)|.
An edge (a set of two elements) is drawn as a line connecting two
vertices, called endpoints or (less often) endvertices. An edge with
endvertices x and y is denoted by xy (without any symbol in between).
The edge set of G is usually denoted by E(G), or E when there is no
danger of confusion.
In this pseudograph the blue edges are loops and
The size of a graph is the number of its edges, i.e. |E(G)|. the red edges are multiple edges of multiplicity
A loop is an edge whose endpoints are the same vertex. A link has two 2 and 3. The multiplicity of the graph is 3.

distinct endvertices. An edge is multiple if there is another edge with


the same endvertices; otherwise it is simple. The multiplicity of an edge is the number of multiple edges sharing the
same end vertices; the multiplicity of a graph, the maximum multiplicity of its edges. A graph is a simple graph if
it has no multiple edges or loops, a multigraph if it has multiple edges, but no loops, and a multigraph or
pseudograph if it contains both multiple edges and loops (the literature is highly inconsistent).

When stated without any qualification, a graph is usually assumed to be simple, except in the literature of category
theory, where it refers to a quiver.
Graphs whose edges or vertices have names or labels are known as labeled, those without as unlabeled. Graphs with
labeled vertices only are vertex-labeled, those with labeled edges only are edge-labeled. The difference between a
labeled and an unlabeled graph is that the latter has no specific set of vertices or edges; it is regarded as another way
to look upon an isomorphism type of graphs. (Thus, this usage distinguishes between graphs with identifiable vertex
or edge sets on the one hand, and isomorphism types or classes of graphs on the other.)
(Graph labeling usually refers to the assignment of labels (usually natural numbers, usually distinct) to the edges
and vertices of a graph, subject to certain rules depending on the situation. This should not be confused with a
graph's merely having distinct labels or names on the vertices.)
Glossary of graph theory 79

A hyperedge is an edge that is allowed to take on any number of


vertices, possibly more than 2. A graph that allows any hyperedge is
called a hypergraph. A simple graph can be considered a special case
of the hypergraph, namely the 2-uniform hypergraph. However, when
stated without any qualification, an edge is always assumed to consist
of at most 2 vertices, and a graph is never confused with a hypergraph.

A non-edge (or anti-edge) is an edge that is not present in the graph.


More formally, for two vertices and , is a non-edge in a A labeled simple graph with vertex set V = {1, 2,
graph whenever is not an edge in . This means that 3, 4, 5, 6} and edge set E = {{1,2}, {1,5}, {2,3},
{2,5}, {3,4}, {4,5}, {4,6}}.
there is either no edge between the two vertices or (for directed graphs)
at most one of and from is an arc in G.
Occasionally the term cotriangle or anti-triangle is used for a set of three vertices none of which are connected.
The complement of a graph G is a graph with the same vertex set as G but with an edge set such that xy is an
edge in if and only if xy is not an edge in G.
An edgeless graph or empty graph or null graph is a graph with zero or more vertices, but no edges. The empty
graph or null graph may also be the graph with no vertices and no edges. If it is a graph with no edges and any
number of vertices, it may be called the null graph on vertices. (There is no consistency at all in the
literature.)
A graph is infinite if it has infinitely many vertices or edges or both; otherwise the graph is finite. An infinite graph
where every vertex has finite degree is called locally finite. When stated without any qualification, a graph is usually
assumed to be finite. See also continuous graph.
Two graphs G and H are said to be isomorphic, denoted by G ~ H, if there is a one-to-one correspondence, called an
isomorphism, between the vertices of the graph such that two vertices are adjacent in G if and only if their
corresponding vertices are adjacent in H. Likewise, a graph G is said to be homomorphic to a graph H if there is a
mapping, called a homomorphism, from V(G) to V(H) such that if two vertices are adjacent in G then their
corresponding vertices are adjacent in H.

Subgraphs
A subgraph of a graph G is a graph whose vertex set is a subset of that of G, and whose adjacency relation is a
subset of that of G restricted to this subset. In the other direction, a supergraph of a graph G is a graph of which G is
a subgraph. We say a graph G contains another graph H if some subgraph of G is H or is isomorphic to H.
A subgraph H is a spanning subgraph, or factor, of a graph G if it has the same vertex set as G. We say H spans G.
A subgraph H of a graph G is said to be induced (or full) if, for any pair of vertices x and y of H, xy is an edge of H
if and only if xy is an edge of G. In other words, H is an induced subgraph of G if it has exactly the edges that appear
in G over the same vertex set. If the vertex set of H is the subset S of V(G), then H can be written as G[S] and is said
to be induced by S.
A graph G is minimal with some property P provided that G has property P and no proper subgraph of G has
property P. In this definition, the term subgraph is usually understood to mean "induced subgraph." The notion of
maximality is defined dually: G is maximal with P provided that P(G) and G has no proper supergraph H such that
P(H).
A graph that does not contain H as an induced subgraph is said to be H-free, and more generally if is a family of
graphs then the graphs that do not contain any induced subgraph isomorphic to a member of are called -free.
For example the triangle-free graphs are the graphs that do not have a triangle graph as an induced subgraph. Many
important classes of graphs can be defined by sets of forbidden subgraphs, the graphs that are not in the class and are
Glossary of graph theory 80

minimal with respect to subgraphs, induced subgraphs, or graph minors.


A universal graph in a class K of graphs is a simple graph in which every element in K can be embedded as a
subgraph.

Walks
A walk is an alternating sequence of vertices and edges, beginning and ending with a vertex, where each vertex is
incident to both the edge that precedes it and the edge that follows it in the sequence, and where the vertices that
precede and follow an edge are the end vertices of that edge. A walk is closed if its first and last vertices are the
same, and open if they are different.
The length l of a walk is the number of edges that it uses. For an open walk, l = n–1, where n is the number of
vertices visited (a vertex is counted each time it is visited). For a closed walk, l = n (the start/end vertex is listed
twice, but is not counted twice). In the example graph, (1, 2, 5, 1, 2, 3) is an open walk with length 5, and (4, 5, 2, 1,
5, 4) is a closed walk of length 5.
A trail is a walk in which all the edges are distinct. A closed trail has been called a tour or circuit, but these are not
universal, and the latter is often reserved for a regular subgraph of degree two.
Traditionally, a path referred to what is now usually known as an open
walk. Nowadays, when stated without any qualification, a path is
usually understood to be simple, meaning that no vertices (and thus no
edges) are repeated. (The term chain has also been used to refer to a
walk in which all vertices and edges are distinct.) In the example
graph, (5, 2, 1) is a path of length 2. The closed equivalent to this type
of walk, a walk that starts and ends at the same vertex but otherwise
has no repeated vertices or edges, is called a cycle. Like path, this term
traditionally referred to any closed walk, but now is usually understood
to be simple by definition. In the example graph, (1, 5, 2, 1) is a cycle
of length 3. (A cycle, unlike a path, is not allowed to have length 0.)
A directed tour. This is not a simple cycle, since
Paths and cycles of n vertices are often denoted by Pn and Cn, the blue vertices are used twice.
respectively. (Some authors use the length instead of the number of
vertices, however.)

C1 is a loop, C2 is a digon (a pair of parallel undirected edges in a multigraph, or a pair of antiparallel edges in a
directed graph), and C3 is called a triangle.
A cycle that has odd length is an odd cycle; otherwise it is an even cycle. One theorem is that a graph is bipartite if
and only if it contains no odd cycles. (See complete bipartite graph.)
A graph is acyclic if it contains no cycles; unicyclic if it contains exactly one cycle; and pancyclic if it contains
cycles of every possible length (from 3 to the order of the graph).
A wheel graph is a graph with n vertices (n ≥ 4), formed by connecting a single vertex to all vertices of Cn.
The girth of a graph is the length of a shortest (simple) cycle in the graph; and the circumference, the length of a
longest (simple) cycle. The girth and circumference of an acyclic graph are defined to be infinity ∞.
A path or cycle is Hamiltonian (or spanning) if it uses all vertices exactly once. A graph that contains a Hamiltonian
path is traceable; and one that contains a Hamiltonian path for any given pair of (distinct) end vertices is a
Hamiltonian connected graph. A graph that contains a Hamiltonian cycle is a Hamiltonian graph.
A trail or circuit (or cycle) is Eulerian if it uses all edges precisely once. A graph that contains an Eulerian trail is
traversable. A graph that contains an Eulerian circuit is an Eulerian graph.
Glossary of graph theory 81

Two paths are internally disjoint (some people call it independent) if they do not have any vertex in common,
except the first and last ones.
A theta graph is the union of three internally disjoint (simple) paths that have the same two distinct end vertices. A
theta0 graph has seven vertices which can be arranged as the vertices of a regular hexagon plus an additional vertex
in the center. The eight edges are the perimeter of the hexagon plus one diameter.

Trees
A tree is a connected acyclic simple graph. For directed graphs, each
vertex has at most one incoming edge. A vertex of degree 1 is called a
leaf, or pendant vertex. An edge incident to a leaf is a leaf edge, or
pendant edge. (Some people define a leaf edge as a leaf and then
define a leaf vertex on top of it. These two sets of definitions are often
used interchangeably.) A non-leaf vertex is an internal vertex.
Sometimes, one vertex of the tree is distinguished, and called the root;
in this case, the tree is called rooted. Rooted trees are often treated as
directed acyclic graphs with the edges pointing away from the root.

A subtree of the tree T is a connected subgraph of T.


A forest is an acyclic simple graph. For directed graphs, each vertex
has at most one incoming edge. (That is, a tree with the connectivity
requirement removed; a graph containing multiple disconnected trees.)
A labeled tree with 6 vertices and 5 edges. Nodes
A subforest of the forest F is a subgraph of F. 1, 2, 3, and 6 are leaves, while 4 and 5 are
internal vertices.
A spanning tree is a spanning subgraph that is a tree. Every graph has
a spanning forest. But only a connected graph has a spanning tree.
A special kind of tree called a star is K1,k. An induced star with 3 edges is a claw.
A caterpillar is a tree in which all non-leaf nodes form a single path.
A k-ary tree is a rooted tree in which every internal vertex has no more than k children. A 1-ary tree is just a path. A
2-ary tree is also called a binary tree.

Cliques
The complete graph Kn of order n is a simple graph with n vertices in
which every vertex is adjacent to every other. The example graph to
the right is complete. The complete graph on n vertices is often
denoted by Kn. It has n(n-1)/2 edges (corresponding to all possible
choices of pairs of vertices).
A clique in a graph is a set of pairwise adjacent vertices. Since any
subgraph induced by a clique is a complete subgraph, the two terms
and their notations are usually used interchangeably. A k-clique is a
clique of order k. In the example graph above, vertices 1, 2 and 5 form
a 3-clique, or a triangle. A maximal clique is a clique that is not a
subset of any other clique (some authors reserve the term clique for
maximal cliques). K5, a complete graph. If a subgraph looks like
this, the vertices in that subgraph form a clique of
The clique number ω(G) of a graph G is the order of a largest clique size 5.
in G.
Glossary of graph theory 82

Strongly connected component


A related but weaker concept is that of a strongly connected component. Informally, a strongly connected component
of a directed graph is a subgraph where all nodes in the subgraph are reachable by all other nodes in the subgraph.
Reachability between nodes is established by the existence of a path between the nodes.
A directed graph can be decomposed into strongly connected components by running the depth-first search (DFS)
algorithm twice: first, on the graph itself and next on the transpose graph in decreasing order of the finishing times
of the first DFS. Given a directed graph G, the transpose GT is the graph G with all the edge directions reversed.

Hypercubes
A hypercube graph is a regular graph with 2n vertices, 2n−1n edges, and n edges touching each vertex. It can
be obtained as the one-dimensional skeleton of the geometric hypercube.

Knots
A knot in a directed graph is a collection of vertices and edges with the property that every vertex in the knot has
outgoing edges, and all outgoing edges from vertices in the knot terminate at other vertices in the knot. Thus it is
impossible to leave the knot while following the directions of the edges.

Minors
A minor of is an injection from to such that every edge in
corresponds to a path (disjoint from all other such paths) in such that every vertex in is in one or more paths,
or is part of the injection from to . This can alternatively be phrased in terms of contractions, which are
operations which collapse a path and all vertices on it into a single edge (see Minor (graph theory)).

Embedding
An embedding of is an injection from to such that every edge in
corresponds to a path (disjoint from all other such paths) in .

Adjacency and degree


In graph theory, degree, especially that of a vertex, is usually a measure of immediate adjacency.
An edge connects two vertices; these two vertices are said to be incident to that edge, or, equivalently, that edge
incident to those two vertices. All degree-related concepts have to do with adjacency or incidence.
The degree, or valency, dG(v) of a vertex v in a graph G is the number of edges incident to v, with loops being
counted twice. A vertex of degree 0 is an isolated vertex. A vertex of degree 1 is a leaf. In the labelled simple graph
example, vertices 1 and 3 have a degree of 2, vertices 2, 4 and 5 have a degree of 3, and vertex 6 has a degree of 1. If
E is finite, then the total sum of vertex degrees is equal to twice the number of edges.
The total degree of a graph is equal to two times the number of edges, loops included. This means that for a graph
with 3 vertices with each vertex having a degree of two (i.e. a triangle) the total degree would be six (e.g. 3 x 2 = 6).
The handshaking lemma states that the total degree is always 2n, where n is the number of edges.
A degree sequence is a list of degrees of a graph in non-increasing order (e.g. d1 ≥ d2 ≥ … ≥ dn). A sequence of
non-increasing integers is realizable if it is a degree sequence of some graph.
Two vertices u and v are called adjacent if an edge exists between them. We denote this by u ~ v or u ↓ v. In the
above graph, vertices 1 and 2 are adjacent, but vertices 2 and 4 are not. The set of neighbors of v, that is, vertices
adjacent to v not including v itself, forms an induced subgraph called the (open) neighborhood of v and denoted
NG(v). When v is also included, it is called a closed neighborhood and denoted by NG[v]. When stated without any
Glossary of graph theory 83

qualification, a neighborhood is assumed to be open. The subscript G is usually dropped when there is no danger of
confusion; the same neighborhood notation may also be used to refer to sets of adjacent vertices rather than the
corresponding induced subgraphs. In the example graph, vertex 1 has two neighbors: vertices 2 and 5. For a simple
graph, the number of neighbors that a vertex has coincides with its degree.
A dominating set of a graph is a vertex subset whose closed neighborhood includes all vertices of the graph. A
vertex v dominates another vertex u if there is an edge from v to u. A vertex subset V dominates another vertex
subset U if every vertex in U is adjacent to some vertex in V. The minimum size of a dominating set is the
domination number γ(G).
In computers, a finite, directed or undirected graph (with n vertices, say) is often represented by its adjacency
matrix: an n-by-n matrix whose entry in row i and column j gives the number of edges from the i-th to the j-th
vertex.
Spectral graph theory studies relationships between the properties of a graph and its adjacency matrix or other
matrices associated with the graph.
The maximum degree Δ(G) of a graph G is the largest degree over all vertices; the minimum degree δ(G), the
smallest.
A graph in which every vertex has the same degree is regular. It is k-regular if every vertex has degree k. A
0-regular graph is an independent set. A 1-regular graph is a matching. A 2-regular graph is a vertex disjoint union of
cycles. A 3-regular graph is said to be cubic, or trivalent.
A k-factor is a k-regular spanning subgraph. A 1-factor is a perfect matching. A partition of edges of a graph into
k-factors is called a k-factorization. A k-factorable graph is a graph that admits a k-factorization.
A graph is biregular if it has unequal maximum and minimum degrees and every vertex has one of those two
degrees.
A strongly regular graph is a regular graph such that any adjacent vertices have the same number of common
neighbors as other adjacent pairs and that any nonadjacent vertices have the same number of common neighbors as
other nonadjacent pairs.

Independence
In graph theory, the word independent usually carries the connotation of pairwise disjoint or mutually nonadjacent.
In this sense, independence is a form of immediate nonadjacency. An isolated vertex is a vertex not incident to any
edges. An independent set, or coclique, or stable set or staset, is a set of vertices of which no pair is adjacent. Since
the graph induced by any independent set is an empty graph, the two terms are usually used interchangeably. In the
example at the top of this page, vertices 1, 3, and 6 form an independent set; and 2 and 4 form another one.
Two subgraphs are edge disjoint if they have no edges in common. Similarly, two subgraphs are vertex disjoint if
they have no vertices (and thus, also no edges) in common. Unless specified otherwise, a set of disjoint subgraphs
are assumed to be pairwise vertex disjoint.
The independence number α(G) of a graph G is the size of the largest independent set of G.
A graph can be decomposed into independent sets in the sense that the entire vertex set of the graph can be
partitioned into pairwise disjoint independent subsets. Such independent subsets are called partite sets, or simply
parts.
A graph that can be decomposed into two partite sets bipartite; three sets, tripartite; k sets, k-partite; and an
unknown number of sets, multipartite. An 1-partite graph is the same as an independent set, or an empty graph. A
2-partite graph is the same as a bipartite graph. A graph that can be decomposed into k partite sets is also said to be
k-colourable.
Glossary of graph theory 84

A complete multipartite graph is a graph in which vertices are adjacent if and only if they belong to different partite
sets. A complete bipartite graph is also referred to as a biclique; if its partite sets contain n and m vertices,
respectively, then the graph is denoted Kn,m.
A k-partite graph is semiregular if each of its partite sets has a uniform degree; equipartite if each partite set has the
same size; and balanced k-partite if each partite set differs in size by at most 1 with any other.
The matching number of a graph G is the size of a largest matching, or pairwise vertex disjoint edges, of
G.
A spanning matching, also called a perfect matching is a matching that covers all vertices of a graph.

Complexity
Complexity of a graph denotes the quantity of information that a graph contained, and can be measured in several
ways. For example, by counting the number of its spanning trees, or the value of a certain formula involving the
number of vertices, edges, and proper paths in a graph.

Connectivity
Connectivity extends the concept of adjacency and is essentially a form (and measure) of concatenated adjacency.
If it is possible to establish a path from any vertex to any other vertex of a graph, the graph is said to be connected;
otherwise, the graph is disconnected. A graph is totally disconnected if there is no path connecting any pair of
vertices. This is just another name to describe an empty graph or independent set.
A cut vertex, or articulation point, is a vertex whose removal disconnects the remaining subgraph. A cut set, or
vertex cut or separating set, is a set of vertices whose removal disconnects the remaining subgraph. A bridge is an
analogous edge (see below).
If it is always possible to establish a path from any vertex to every other even after removing any k - 1 vertices, then
the graph is said to be k-vertex-connected or k-connected. Note that a graph is k-connected if and only if it contains
k internally disjoint paths between any two vertices. The example graph above is connected (and therefore
1-connected), but not 2-connected. The vertex connectivity or connectivity κ(G) of a graph G is the minimum
number of vertices that need to be removed to disconnect G. The complete graph Kn has connectivity n - 1 for n > 1;
and a disconnected graph has connectivity 0.
In network theory, a giant component is a connected subgraph that contains a majority of the entire graph's nodes.
A bridge, or cut edge or isthmus, is an edge whose removal disconnects a graph. (For example, all the edges in a tree
are bridges.) A cut vertex is an analogous vertex (see above). A disconnecting set is a set of edges whose removal
increases the number of components. An edge cut is the set of all edges which have one vertex in some proper
vertex subset S and the other vertex in V(G)\S. Edges of K3 form a disconnecting set but not an edge cut. Any two
edges of K3 form a minimal disconnecting set as well as an edge cut. An edge cut is necessarily a disconnecting set;
and a minimal disconnecting set of a nonempty graph is necessarily an edge cut. A bond is a minimal (but not
necessarily minimum), nonempty set of edges whose removal disconnects a graph.
A graph is k-edge-connected if any subgraph formed by removing any k - 1 edges is still connected. The edge
connectivity κ'(G) of a graph G is the minimum number of edges needed to disconnect G. One well-known result is
that κ(G) ≤ κ'(G) ≤ δ(G).
A component is a maximally connected subgraph. A block is either a maximally 2-connected subgraph, a bridge
(together with its vertices), or an isolated vertex. A biconnected component is a 2-connected component.
An articulation point (also known as a separating vertex) of a graph is a vertex whose removal from the graph
increases its number of connected components. A biconnected component can be defined as a subgraph induced by a
maximal set of nodes that has no separating vertex.
Glossary of graph theory 85

Distance
The distance dG(u, v) between two (not necessary distinct) vertices u and v in a graph G is the length of a shortest
path between them. The subscript G is usually dropped when there is no danger of confusion. When u and v are
identical, their distance is 0. When u and v are unreachable from each other, their distance is defined to be infinity ∞.
The eccentricity εG(v) of a vertex v in a graph G is the maximum distance from v to any other vertex. The diameter
diam(G) of a graph G is the maximum eccentricity over all vertices in a graph; and the radius rad(G), the minimum.
When there are two components in G, diam(G) and rad(G) defined to be infinity ∞. Trivially, diam(G) ≤ 2 rad(G).
Vertices with maximum eccentricity are called peripheral vertices. Vertices of minimum eccentricity form the
center. A tree has at most two center vertices.
The Wiener index of a vertex v in a graph G, denoted by WG(v) is the sum of distances between v and all others.
The Wiener index of a graph G, denoted by W(G), is the sum of distances over all pairs of vertices. An undirected
graph's Wiener polynomial is defined to be Σ qd(u,v) over all unordered pairs of vertices u and v. Wiener index and
Wiener polynomial are of particular interest to mathematical chemists.
The k-th power Gk of a graph G is a supergraph formed by adding an edge between all pairs of vertices of G with
distance at most k. A second power of a graph is also called a square.
A k-spanner is a spanning subgraph, S, in which every two vertices are at most k times as far apart on S than on G.
The number k is the dilation. k-spanner is used for studying geometric network optimization.

Genus
A crossing is a pair of intersecting edges. A graph is embeddable on a surface if its vertices and edges can be
arranged on it without any crossing. The genus of a graph is the lowest genus of any surface on which the graph can
embed.
A planar graph is one which can be drawn on the (Euclidean) plane without any crossing; and a plane graph, one
which is drawn in such fashion. In other words, a planar graph is a graph of genus 0. The example graph is planar;
the complete graph on n vertices, for n> 4, is not planar. Also, a tree is necessarily a planar graph.
When a graph is drawn without any crossing, any cycle that surrounds a region without any edges reaching from the
cycle into the region forms a face. Two faces on a plane graph are adjacent if they share a common edge. A dual, or
planar dual when the context needs to be clarified, G* of a plane graph G is a graph whose vertices represent the
faces, including any outerface, of G and are adjacent in G* if and only if their corresponding faces are adjacent in G.
The dual of a planar graph is always a planar pseudograph (e.g. consider the dual of a triangle). In the familiar case
of a 3-connected simple planar graph G (isomorphic to a convex polyhedron P), the dual G* is also a 3-connected
simple planar graph (and isomorphic to the dual polyhedron P*).
Furthermore, since we can establish a sense of "inside" and "outside" on a plane, we can identify an "outermost"
region that contains the entire graph if the graph does not cover the entire plane. Such outermost region is called an
outer face. An outerplanar graph is one which can be drawn in the planar fashion such that its vertices are all
adjacent to the outer face; and an outerplane graph, one which is drawn in such fashion.
The minimum number of crossings that must appear when a graph is drawn on a plane is called the crossing
number.
The minimum number of planar graphs needed to cover a graph is the thickness of the graph.
Glossary of graph theory 86

Weighted graphs and networks


A weighted graph associates a label (weight) with every edge in the graph. Weights are usually real numbers. They
may be restricted to rational numbers or integers. Certain algorithms require further restrictions on weights; for
instance, Dijkstra's algorithm works properly only for positive weights. The weight of a path or the weight of a tree
in a weighted graph is the sum of the weights of the selected edges. Sometimes a non-edge is labeled by a special
weight representing infinity. Sometimes the word cost is used instead of weight. When stated without any
qualification, a graph is always assumed to be unweighted. In some writing on graph theory the term network is a
synonym for a weighted graph. A network may be directed or undirected, it may contain special vertices (nodes),
such as source or sink. The classical network problems include:
• minimum cost spanning tree,
• shortest paths,
• maximal flow (and the max-flow min-cut theorem)

Direction
A directed arc, or directed edge, is an ordered pair of endvertices that can be represented graphically as an arrow
drawn between the endvertices. In such an ordered pair the first vertex is called the initial vertex or tail; the second
one is called the terminal vertex or head (because it appears at the arrow head). An undirected edge disregards any
sense of direction and treats both endvertices interchangeably. A loop in a digraph, however, keeps a sense of
direction and treats both head and tail identically. A set of arcs are multiple, or parallel, if they share the same head
and the same tail. A pair of arcs are anti-parallel if one's head/tail is the other's tail/head. A digraph, or directed
graph, or oriented graph, is analogous to an undirected graph except that it contains only arcs. A mixed graph may
contain both directed and undirected edges; it generalizes both directed and undirected graphs. When stated without
any qualification, a graph is almost always assumed to be undirected.
A digraph is called simple if it has no loops and at most one arc between any pair of vertices. When stated without
any qualification, a digraph is usually assumed to be simple. A quiver is a directed graph which is specifically
allowed, but not required, to have loops and more than one arc between any pair of vertices.
In a digraph Γ, we distinguish the out degree dΓ+(v), the number of edges leaving a vertex v, and the in degree
dΓ-(v), the number of edges entering a vertex v. If the graph is oriented, the degree dΓ(v) of a vertex v is equal to the
sum of its out- and in- degrees. When the context is clear, the subscript Γ can be dropped. Maximum and minimum
out degrees are denoted by Δ+(Γ) and δ+(Γ); and maximum and minimum in degrees, Δ-(Γ) and δ-(Γ).
An out-neighborhood, or successor set, N+Γ(v) of a vertex v is the set of heads of arcs going from v. Likewise, an
in-neighborhood, or predecessor set, N-Γ(v) of a vertex v is the set of tails of arcs going into v.
A source is a vertex with 0 in-degree; and a sink, 0 out-degree.
A vertex v dominates another vertex u if there is an arc from v to u. A vertex subset S is out-dominating if every
vertex not in S is dominated by some vertex in S; and in-dominating if every vertex in S is dominated by some
vertex not in S.
A kernel in a (possibly directed) graph G is an independent set S such that every vertex in V(G) \ S dominates some
vertex in S. In undirected graphs, kernels are maximal independent sets.[1] A digraph is kernel perfect if every
induced sub-digraph has a kernel.[2]
An Eulerian digraph is a digraph with equal in- and out-degrees at every vertex.
The zweieck of an undirected edge is the pair of diedges and which form the simple
dicircuit.
An orientation is an assignment of directions to the edges of an undirected or partially directed graph. When stated
without any qualification, it is usually assumed that all undirected edges are replaced by a directed one in an
Glossary of graph theory 87

orientation. Also, the underlying graph is usually assumed to be undirected and simple.
A tournament is a digraph in which each pair of vertices is connected by exactly one arc. In other words, it is an
oriented complete graph.
A directed path, or just a path when the context is clear, is an oriented simple path such that all arcs go the same
direction, meaning all internal vertices have in- and out-degrees 1. A vertex v is reachable from another vertex u if
there is a directed path that starts from u and ends at v. Note that in general the condition that u is reachable from v
does not imply that v is also reachable from u.
If v is reachable from u, then u is a predecessor of v and v is a successor of u. If there is an arc from u to v, then u is
a direct predecessor of v, and v is a direct successor of u.
A digraph is strongly connected if every vertex is reachable from every other following the directions of the arcs.
On the contrary, a digraph is weakly connected if its underlying undirected graph is connected. A weakly connected
graph can be thought of as a digraph in which every vertex is "reachable" from every other but not necessarily
following the directions of the arcs. A strong orientation is an orientation that produces a strongly connected digraph.
A directed cycle, or just a cycle when the context is clear, is an oriented simple cycle such that all arcs go the same
direction, meaning all vertices have in- and out-degrees 1. A digraph is acyclic if it does not contain any directed
cycle. A finite, acyclic digraph with no isolated vertices necessarily contains at least one source and at least one sink.
An arborescence, or out-tree or branching, is an oriented tree in which all vertices are reachable from a single
vertex. Likewise, an in-tree is an oriented tree in which a single vertex is reachable from every other one.

Directed acyclic graphs


The partial order structure of directed acyclic graphs (or DAGs) gives them their own terminology.
If there is a directed edge from u to v, then we say u is a parent of v and v is a child of u. If there is a directed path
from u to v, we say u is an ancestor of v and v is a descendant of u.
The moral graph of a DAG is the undirected graph created by adding an (undirected) edge between all parents of
the same node (sometimes called marrying), and then replacing all directed edges by undirected edges. A DAG is
perfect if, for each node, the set of parents is complete (i.e. no new edges need to be added when forming the moral
graph).

Colouring
Vertices in graphs can be given colours to identify or label them.
Although they may actually be rendered in diagrams in different
colours, working mathematicians generally pencil in numbers or letters
(usually numbers) to represent the colours.
Given a graph G (V,E) a k-colouring of G is a map ϕ : V → {1, ..., k}
with the property that (u, v) ∈ E ⇒ ϕ(u) ≠ ϕ(v) - in other words, every
vertex is assigned a colour with the condition that adjacent vertices
cannot be assigned the same colour.
The chromatic number χ(G) is the smallest k for which G has a This graph is an example of a 4-critical graph. Its
chromatic number is 4 but all of its proper
k-colouring.
subgraphs have a chromatic number less than 4.
Given a graph and a colouring, the colour classes of the graph are the This graph is also planar
sets of vertices given the same colour.
A graph is called k-critical if its chromatic number is k but all of its proper subgraphs have chromatic number less
than k. An odd cycle is 3-critical, and the complete graph on k vertices is k-critical.
Glossary of graph theory 88

Various
A graph invariant is a property of a graph G, usually a number or a polynomial, that depends only on the
isomorphism class of G. Examples are the order, genus, chromatic number, and chromatic polynomial of a graph.

References
[1] Bondy, J.A., Murty, U.S.R., Graph Theory, p. 298
[2] Béla Bollobás, Modern Graph theory, p. 298

• Bollobás, Béla (1998). Modern Graph Theory. New York: Springer-Verlag. ISBN 0-387-98488-7. [Packed with
advanced topics followed by a historical overview at the end of each chapter.]
• Diestel, Reinhard (2005). Graph Theory (http://www.math.uni-hamburg.de/home/diestel/books/graph.
theory/) (3rd edition ed.). Graduate Texts in Mathematics, vol. 173, Springer-Verlag. ISBN 3-540-26182-6.
[Standard textbook, most basic material and some deeper results, exercises of various difficulty and notes at the
end of each chapter; known for being quasi error-free.]
• West, Douglas B. (2001). Introduction to Graph Theory (2ed). Upper Saddle River: Prentice Hall. ISBN
0-13-014400-2. [Tons of illustrations, references, and exercises. The most complete introductory guide to the
subject.]
• Weisstein, Eric W., " Graph (http://mathworld.wolfram.com/Graph.html)", MathWorld.
• Zaslavsky, Thomas. Glossary of signed and gain graphs and allied areas. Electronic Journal of Combinatorics,
Dynamic Surveys in Combinatorics, # DS 8. http://www.combinatorics.org/Surveys/

Directed graph
In mathematics, and more specifically in graph theory, a directed graph (or
digraph) is a graph, or set of nodes connected by edges, where the edges have a
direction associated with them. In formal terms, a digraph is a pair
(sometimes ) of:[1]
• a set V, whose elements are called vertices or nodes,
• a set A of ordered pairs of vertices, called arcs, directed edges, or arrows (and
sometimes simply edges with the corresponding set named E instead of A).
A directed graph.
It differs from an ordinary or undirected graph, in that the latter is defined in terms
of unordered pairs of vertices, which are usually called edges.

Sometimes a digraph is called a simple digraph to distinguish it from a directed multigraph, in which the arcs
constitute a multiset, rather than a set, of ordered pairs of vertices. Also, in a simple digraph loops are disallowed. (A
loop is an arc that pairs a vertex to itself.) On the other hand, some texts allow loops, multiple arcs, or both in a
digraph.
Directed graph 89

Basic terminology
An arc is considered to be directed from to ; is called the head and is called the tail of the
arc; is said to be a direct successor of , and is said to be a direct predecessor of . If a path made up of
one or more successive arcs leads from to , then is said to be a successor of , and is said to be a
predecessor of . The arc is called the arc inverted.
An orientation of a simple undirected graph is obtained by assigning a direction to each edge. Any directed graph
constructed this way is called an "oriented graph". A directed graph is an oriented simple graph if and only if it has
neither self-loops nor 2-cycles.[2]
A weighted digraph is a digraph with weights assigned to its arcs, similar to a weighted graph. In the context of
graph theory a digraph with weighted edges is called a network.
The adjacency matrix of a digraph (with loops and multiple arcs) is the integer-valued matrix with rows and columns
corresponding to the nodes, where a nondiagonal entry is the number of arcs from node i to node j, and the
diagonal entry is the number of loops at node i. The adjacency matrix of a digraph is unique up to identical
permutation of rows and columns.
Another matrix representation for a digraph is its incidence matrix.
See Glossary of graph theory#Direction for more definitions.

Indegree and outdegree


For a node, the number of head endpoints adjacent to a node is called
the indegree of the node and the number of tail endpoints adjacent to a
node is its outdegree (called "branching factor" in trees).

The indegree is denoted and the outdegree as A


vertex with is called a source, as it is the origin of
each of its incident edges. Similarly, a vertex with is
called a sink. A digraph with vertices labeled (indegree,
The degree sum formula states that, for a directed graph, outdegree)

If for every node v ∈ V, , the graph is called a balanced digraph.[3]

Digraph connectivity
A digraph G is called weakly connected (or just connected[4]) if the undirected underlying graph obtained by
replacing all directed edges of G with undirected edges is a connected graph. A digraph is strongly connected or
strong if it contains a directed path from u to v and a directed path from v to u for every pair of vertices u,v. The
strong components are the maximal strongly connected subgraphs.

Classes of digraphs
A directed graph G is called symmetric if, for every arc that belongs to G, the corresponding reversed arc also
belongs to G. A symmetric, loopless directed graph is equivalent to an undirected graph with the edges replaced by
pairs of inverse arcs; thus the number of edges is equal to the number of arcs halved.
Directed graph 90

An acyclic directed graph, acyclic digraph, or directed acyclic graph is a directed graph
with no directed cycles. Special cases of acyclic directed graphs include multitrees
(graphs in which no two directed paths from a single starting node meet back at the same
ending node), oriented trees or polytrees (digraphs formed by orienting the edges of
undirected acyclic graphs), and rooted trees (oriented trees in which all edges of the
underlying undirected tree are directed away from the roots).
A simple acyclic directed
graph

A tournament is an oriented graph obtained by choosing a direction for each edge in an


undirected complete graph.
In the theory of Lie groups, a quiver Q is a directed graph serving as the domain of, and
thus characterizing the shape of, a representation V defined as a functor, specifically an
object of the functor category FinVctKF(Q) where F(Q) is the free category on Q
consisting of paths in Q and FinVctK is the category of finite dimensional vector spaces
over a field K. Representations of a quiver label its vertices with vector spaces and its A tournament on 4 vertices
edges (and hence paths) compatibly with linear transformations between them, and
transform via natural transformations.

Notes
[1] . , Section 1.10. , Section 10.
[2] , Section 1.10.
[3] ;.
[4] p. 19 in the 2007 edition; p. 20 in the 2nd edition (2009).

References
• Bang-Jensen, Jørgen; Gutin, Gregory (2000), Digraphs: Theory, Algorithms and Applications (http://www.cs.
rhul.ac.uk/books/dbook/), Springer, ISBN 1-85233-268-9
(the corrected 1st edition of 2007 is now freely available on the authors' site; the 2nd edition appeared in 2009
ISBN 1-84800-997-6).
• Bondy, John Adrian; Murty, U. S. R. (1976), Graph Theory with Applications (http://www.ecp6.jussieu.fr/
pageperso/bondy/books/gtwa/gtwa.html), North-Holland, ISBN 0-444-19451-7.
• Diestel, Reinhard (2005), Graph Theory (http://www.math.uni-hamburg.de/home/diestel/books/graph.
theory/) (3rd ed.), Springer, ISBN 3-540-26182-6 (the electronic 3rd edition is freely available on author's site).
• Harary, Frank; Norman, Robert Z.; Cartwright, Dorwin (1965), Structural Models: An Introduction to the Theory
of Directed Graphs, New York: Wiley.
• Number of directed graphs (or digraphs) with n nodes. (http://oeis.org/A000273)
Adjacency matrix 91

Adjacency matrix
In mathematics and computer science, an adjacency matrix is a means of representing which vertices (or nodes) of
a graph are adjacent to which other vertices. Another matrix representation for a graph is the incidence matrix.
Specifically, the adjacency matrix of a finite graph G on n vertices is the n × n matrix where the non-diagonal entry
aij is the number of edges from vertex i to vertex j, and the diagonal entry aii, depending on the convention, is either
once or twice the number of edges (loops) from vertex i to itself. Undirected graphs often use the latter convention of
counting loops twice, whereas directed graphs typically use the former convention. There exists a unique adjacency
matrix for each isomorphism class of graphs (up to permuting rows and columns), and it is not the adjacency matrix
of any other isomorphism class of graphs. In the special case of a finite simple graph, the adjacency matrix is a
(0,1)-matrix with zeros on its diagonal. If the graph is undirected, the adjacency matrix is symmetric.
The relationship between a graph and the eigenvalues and eigenvectors of its adjacency matrix is studied in spectral
graph theory.

Examples
The convention followed here is that an adjacent edge counts 1 in the matrix for an undirected graph.

Labeled graph Adjacency matrix

Coordinates are 1-6.

The Nauru graph


Coordinates are 0-23.
White fields are zeros, colored fields are ones.

Directed Cayley graph of S4


As the graph is directed,
the matrix is not symmetric.

• The adjacency matrix of a complete graph contains all ones except along the diagonal where there are only zeros.
• The adjacency matrix of an empty graph is a zero matrix.
Adjacency matrix 92

Adjacency matrix of a bipartite graph


The adjacency matrix of a bipartite graph whose parts have and vertices has the form

where is an matrix, and represents the zero matrix. Clearly, the matrix uniquely represents the
bipartite graphs. It is sometimes called the biadjacency matrix. Formally, let be a bipartite graph
with parts and . The biadjacency matrix is the 0-1 matrix in which
iff .
If is a bipartite multigraph or weighted graph then the elements are taken to be the number of edges between
the vertices or the weight of the edge respectively.

Properties
The adjacency matrix of an undirected simple graph is symmetric, and therefore has a complete set of real
eigenvalues and an orthogonal eigenvector basis. The set of eigenvalues of a graph is the spectrum of the graph.
Suppose two directed or undirected graphs and with adjacency matrices and are given. and
are isomorphic if and only if there exists a permutation matrix such that

In particular, and are similar and therefore have the same minimal polynomial, characteristic polynomial,
eigenvalues, determinant and trace. These can therefore serve as isomorphism invariants of graphs. However, two
graphs may possess the same set of eigenvalues but not be isomorphic. [1]
If A is the adjacency matrix of the directed or undirected graph G, then the matrix An (i.e., the matrix product of n
copies of A) has an interesting interpretation: the entry in row i and column j gives the number of (directed or
undirected) walks of length n from vertex i to vertex j. This implies, for example, that the number of triangles in an
undirected graph G is exactly the trace of A3 divided by 6.
The main diagonal of every adjacency matrix corresponding to a graph without loops has all zero entries. Note that
here 'loops' means, for example A→A, not 'cycles' such as A→B→A.
For -regular graphs, d is also an eigenvalue of A for the vector , and is connected if and
only if the multiplicity of is 1. It can be shown that is also an eigenvalue of A if G is a connected bipartite
graph. The above are results of Perron–Frobenius theorem.

Variations
An (a, b, c)-adjacency matrix A of a simple graph has Aij = a if ij is an edge, b if it is not, and c on the diagonal. The
Seidel adjacency matrix is a (−1,1,0)-adjacency matrix. This matrix is used in studying strongly regular graphs and
two-graphs.
The distance matrix has in position (i,j) the distance between vertices vi and vj . The distance is the length of a
shortest path connecting the vertices. Unless lengths of edges are explicitly provided, the length of a path is the
number of edges in it. The distance matrix resembles a high power of the adjacency matrix, but instead of telling
only whether or not two vertices are connected (i.e., the connection matrix, which contains boolean values), it gives
the exact distance between them.
Adjacency matrix 93

Data structures
For use as a data structure, the main alternative to the adjacency matrix is the adjacency list. Because each entry in
the adjacency matrix requires only one bit, it can be represented in a very compact way, occupying only bytes
of contiguous space, where is the number of vertices. Besides avoiding wasted space, this compactness
encourages locality of reference.
However, for a sparse graph, adjacency lists require less storage space, because they do not waste any space to
represent edges that are not present. Using a naïve array implementation on a 32-bit computer, an adjacency list for
an undirected graph requires about bytes of storage, where is the number of edges.
Noting that a simple graph can have at most edges, allowing loops, we can let denote the density of
the graph. Then, , or the adjacency list representation occupies more space precisely when .
Thus a graph must be sparse indeed to justify an adjacency list representation.
Besides the space tradeoff, the different data structures also facilitate different operations. Finding all vertices
adjacent to a given vertex in an adjacency list is as simple as reading the list. With an adjacency matrix, an entire
row must instead be scanned, which takes O(n) time. Whether there is an edge between two given vertices can be
determined at once with an adjacency matrix, while requiring time proportional to the minimum degree of the two
vertices with the adjacency list.

References
[1] Godsil, Chris; Royle, Gordon Algebraic Graph Theory, Springer (2001), ISBN 0-387-95241-1, p.164

Further reading
• 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.
• Godsil, Chris; Royle, Gordon (2001). Algebraic Graph Theory. New York: Springer. ISBN 0-387-95241-1.

External links
• Fluffschack (http://www.x2d.org/java/projects/fluffschack.jnlp) — an educational Java web start game
demonstrating the relationship between adjacency matrices and graphs.
• Open Data Structures - Section 12.1 - AdjacencyMatrix: Representing a Graph by a Matrix (http://
opendatastructures.org/versions/edition-0.1e/ods-java/12_1_AdjacencyMatrix_Repres.html)
• McKay, Brendan. "Description of graph6 and sparse6 encodings" (http://cs.anu.edu.au/~bdm/data/formats.
txt).
• Café math : Adjacency Matrices of Graphs (http://www.cafemath.fr/mathblog/article.
php?page=GoodWillHunting.php) : Application of the adjacency matrices to the computation generating series of
walks.
FloydWarshall algorithm 94

Floyd–Warshall algorithm
Floyd–Warshall algorithm
Class All-pairs shortest path problem (for weighted graphs)

Data structure Graph

Worst case performance

Best case performance

Worst case space complexity

Graph and tree


search
algorithms
• α–β
• A*
• B*
• Backtracking
• Beam
• Bellman–Ford
• Best-first
• Bidirectional
• Borůvka
• Branch & bound
• BFS
• British Museum
• D*
• DFS
• Depth-limited
• Dijkstra
• Edmonds
• Floyd–Warshall
• Fringe search
• Hill climbing
• IDA*
• Iterative deepening
• Kruskal
• Johnson
• Lexicographic BFS
• Prim
• SMA*
• Uniform-cost
Listings

• Graph algorithms
• Search algorithms
• List of graph algorithms

Related topics
FloydWarshall algorithm 95

• Dynamic programming
• Graph traversal
• Tree traversal
• Search games

• v
• t
• e [1]

In computer science, the Floyd–Warshall algorithm (also known as Floyd's algorithm, Roy–Warshall algorithm,
Roy–Floyd algorithm, or the WFI algorithm) is a graph analysis algorithm for finding shortest paths in a weighted
graph with positive or negative edge weights (but with no negative cycles, see below) and also for finding transitive
closure of a relation . A single execution of the algorithm will find the lengths (summed weights) of the shortest
paths between all pairs of vertices, though it does not return details of the paths themselves.
The Floyd-Warshall algorithm was published in its currently recognized form by Robert Floyd in 1962. However, it
is essentially the same as algorithms previously published by Bernard Roy in 1959 and also by Stephen Warshall in
1962 for finding the transitive closure of a graph. The modern formulation of Warshall's algorithm as three nested
for-loops was first described by Peter Ingerman, also in 1962.
The algorithm is an example of dynamic programming.

Algorithm
The Floyd–Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able
to do this with Θ(|V|3) comparisons in a graph. This is remarkable considering that there may be up to Ω(|V|2) edges
in the graph, and every combination of edges is tested. It does so by incrementally improving an estimate on the
shortest path between two vertices, until the estimate is optimal.
Consider a graph G with vertices V numbered 1 through N. Further consider a function shortestPath(i, j, k) that
returns the shortest possible path from i to j using vertices only from the set {1,2,...,k} as intermediate points along
the way. Now, given this function, our goal is to find the shortest path from each i to each j using only vertices 1
to k + 1.
For each of these pairs of vertices, the true shortest path could be either (1) a path that only uses vertices in the set
{1, ..., k} or (2) a path that goes from i to k + 1 and then from k + 1 to j. We know that the best path from i to j that
only uses vertices 1 through k is defined by shortestPath(i, j, k), and it is clear that if there were a better path from i
to k + 1 to j, then the length of this path would be the concatenation of the shortest path from i to k + 1 (using
vertices in {1, ..., k}) and the shortest path from k + 1 to j (also using vertices in {1, ..., k}).
If is the weight of the edge between vertices i and j, we can define shortestPath(i, j, k + 1) in terms of the
following recursive formula: the base case is

and the recursive case is

This formula is the heart of the Floyd–Warshall algorithm. The algorithm works by first computing
shortestPath(i, j, k) for all (i, j) pairs for k = 1, then k = 2, etc. This process continues until k = n, and we have found
the shortest path for all (i, j) pairs using any intermediate vertices. Pseudocode for this basic version follows:

1 let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)


2 for each vertex v
3 dist[v][v] ← 0
4 for each edge (u,v)
FloydWarshall algorithm 96

5 dist[u][v] ← w(u,v) // the weight of the edge (u,v)


6 for k from 1 to |V|
7 for i from 1 to |V|
8 for j from 1 to |V|
9 if dist[i][j] > dist[i][k] + dist[k][j]
10 dist[i][j] ← dist[i][k] + dist[k][j]
11 end if

Example
The algorithm above is executed on the graph on the left below:

Prior to the first iteration of the outer loop, labeled k=0 above, the only known paths correspond to the single edges
in the graph. At k=1, paths that go through the vertex 1 are found: in particular, the path 2→1→3 is found, replacing
the path 2→3 which has fewer edges but is longer. At k=2, paths going through the vertices {1,2} are found. The red
and blue boxes show how the path 4→2→1→3 is assembled from the two known paths 4→2 and 2→1→3
encountered in previous iterations, with 2 in the intersection. The path 4→2→3 is not considered, because 2→1→3
is the shortest path encountered so far from 2 to 3. At k=3, paths going through the vertices {1,2,3} are found.
Finally, at k=4, all shortest paths are found.

Behavior with negative cycles


A negative cycle is a cycle whose edges sum to a negative value. There is no shortest path between any pair of
vertices i, j which form part of a negative cycle, because path-lengths from i to j can be arbitrarily small (negative).
For numerically meaningful output, the Floyd–Warshall algorithm assumes that there are no negative cycles.
Nevertheless, if there are negative cycles, the Floyd–Warshall algorithm can be used to detect them. The intuition is
as follows:
• The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices (i, j), including where
i = j;
• Initially, the length of the path (i,i) is zero;
• A path {(i,k), (k,i)} can only improve upon this if it has length less than zero, i.e. denotes a negative cycle;
• Thus, after the algorithm, (i,i) will be negative if there exists a negative-length path from i back to i.
Hence, to detect negative cycles using the Floyd–Warshall algorithm, one can inspect the diagonal of the path
matrix, and the presence of a negative number indicates that the graph contains at least one negative cycle.
Obviously, in an undirected graph a negative edge creates a negative cycle (i.e., a closed walk) involving its incident
vertices.
FloydWarshall algorithm 97

Path reconstruction
The Floyd–Warshall algorithm typically only provides the lengths of the paths between all pairs of vertices. With
simple modifications, it is possible to create a method to reconstruct the actual path between any two endpoint
vertices. While one may be inclined to store the actual path from each vertex to each other vertex, this is not
necessary, and in fact, is very costly in terms of memory. For each vertex, one need only store the information about
the highest index intermediate vertex one must pass through if one wishes to arrive at any given vertex. Therefore,
information to reconstruct all paths can be stored in a single |V| × |V| matrix next where next[i][j] represents the
highest index vertex one must travel through if one intends to take the shortest path from i to j.
To implement this, when a new shortest path is found between two vertices, the matrix containing the paths is
updated. The next matrix is updated along with the matrix of minimum distances dist, so at completion both tables
are complete and accurate, and any entries which are infinite in the dist table will be null in the next table. The path
from i to j is the path from i to next[i][j], followed by the path from next[i][j] to j. These two shorter paths are
determined recursively. This modified algorithm runs with the same time and space complexity as the unmodified
algorithm.

let dist be a |V| × |V| array of minimum distances initialized to ∞ (infinity)


let next be a |V| × |V| array of vertex indices initialized to null

procedure FloydWarshallWithPathReconstruction ()
for each vertex v
dist[v][v] ← 0
for each edge (u,v)
dist[u][v] ← w(u,v) // the weight of the edge (u,v)
for k from 1 to |V|
for i from 1 to |V|
for j from 1 to |V|
if dist[i][k] + dist[k][j] < dist[i][j] then
dist[i][j] ← dist[i][k] + dist[k][j]
next[i][j] ← k

function Path (i, j)


if dist[i][j] = ∞ then
return "no path"
var intermediate ← next[i][j]
if intermediate = null then
return " " // the direct edge from i to j gives the shortest path
else
return Path(i, intermediate) + intermediate + Path(intermediate, j)
FloydWarshall algorithm 98

Analysis
Let n be |V|, the number of vertices. To find all n2 of shortestPath(i,j,k) (for all i and j) from those of
shortestPath(i,j,k−1) requires 2n2 operations. Since we begin with shortestPath(i,j,0) = edgeCost(i,j) and compute the
sequence of n matrices shortestPath(i,j,1), shortestPath(i,j,2), …, shortestPath(i,j,n), the total number of operations
used is n · 2n2 = 2n3. Therefore, the complexity of the algorithm is Θ(n3).

Applications and generalizations


The Floyd–Warshall algorithm can be used to solve the following problems, among others:
• Shortest paths in directed graphs (Floyd's algorithm).
• Transitive closure of directed graphs (Warshall's algorithm). In Warshall's original formulation of the algorithm,
the graph is unweighted and represented by a Boolean adjacency matrix. Then the addition operation is replaced
by logical conjunction (AND) and the minimum operation by logical disjunction (OR).
• Finding a regular expression denoting the regular language accepted by a finite automaton (Kleene's algorithm)
• Inversion of real matrices (Gauss–Jordan algorithm)
• Optimal routing. In this application one is interested in finding the path with the maximum flow between two
vertices. This means that, rather than taking minima as in the pseudocode above, one instead takes maxima. The
edge weights represent fixed constraints on flow. Path weights represent bottlenecks; so the addition operation
above is replaced by the minimum operation.
• Fast computation of Pathfinder networks.
• Widest paths/Maximum bandwidth paths

Implementations
Implementations are available for many programming languages.
• For C++, in the boost::graph [2] library
• For C#, at QuickGraph [3]
• For Java, in the Apache Commons Graph [4] library
• For JavaScript, at Turb0JS [5]
• For MATLAB, in the Matlab_bgl [6] package
• For Perl, in the Graph [7] module
• For PHP, on page [8] and PL/pgSQL, on page [9] at Microshell
• For Python, in the NetworkX library
• For R, in package e1071 [10]
• For Ruby, in script [11]

References
[1] http:/ / en. wikipedia. org/ w/ index. php?title=Template:Graph_search_algorithm& action=edit
[2] http:/ / www. boost. org/ libs/ graph/ doc/
[3] http:/ / www. codeplex. com/ quickgraph
[4] http:/ / commons. apache. org/ sandbox/ commons-graph/
[5] http:/ / www. turb0js. com/ a/ Floyd%E2%80%93Warshall_algorithm
[6] http:/ / www. mathworks. com/ matlabcentral/ fileexchange/ 10922
[7] https:/ / metacpan. org/ module/ Graph
[8] http:/ / www. microshell. com/ programming/ computing-degrees-of-separation-in-social-networking/ 2/
[9] http:/ / www. microshell. com/ programming/ floyd-warshal-algorithm-in-postgresql-plpgsql/ 3/
[10] http:/ / cran. r-project. org/ web/ packages/ e1071/ index. html
[11] https:/ / github. com/ chollier/ ruby-floyd
FloydWarshall algorithm 99

• Cormen, Thomas H.; Leiserson, Charles E., Rivest, Ronald L. (1990). Introduction to Algorithms (1st ed.). MIT
Press and McGraw-Hill. ISBN 0-262-03141-8.
• Section 26.2, "The Floyd–Warshall algorithm", pp. 558–565;
• Section 26.4, "A general framework for solving path problems in directed graphs", pp. 570–576.
• Floyd, Robert W. (June 1962). "Algorithm 97: Shortest Path". Communications of the ACM 5 (6): 345. doi:
10.1145/367766.368168 (http://dx.doi.org/10.1145/367766.368168).
• Ingerman, Peter Z. (November 1962). "Algorithm 141: Path Matrix". Template:Communications of the ACM 5
(11): 556. doi: 10.1145/368996.369016 (http://dx.doi.org/10.1145/368996.369016).
• Kleene, S. C. (1956). "Representation of events in nerve nets and finite automata". In C. E. Shannon and J.
McCarthy. Automata Studies. Princeton University Press. pp. 3–42.
• Warshall, Stephen (January 1962). "A theorem on Boolean matrices". Journal of the ACM 9 (1): 11–12. doi:
10.1145/321105.321107 (http://dx.doi.org/10.1145/321105.321107).
• Kenneth H. Rosen (2003). Discrete Mathematics and Its Applications, 5th Edition. Addison Wesley.
ISBN 0-07-119881-4 (ISE) Check |isbn= value (help). Unknown parameter |ISBN status= ignored
(help)
• Roy, Bernard (1959). "Transitivité et connexité.". C. R. Acad. Sci. Paris 249: 216–218.

External links
• Interactive animation of the Floyd–Warshall algorithm (http://www.pms.informatik.uni-muenchen.de/lehre/
compgeometry/Gosper/shortest_path/shortest_path.html#visualization)
• The Floyd–Warshall algorithm in C#, as part of QuickGraph (http://quickgraph.codeplex.com/)
• Visualization of Floyd's algorithm (http://students.ceid.upatras.gr/~papagel/english/java_docs/allmin.htm)

Shortest path problem


In graph theory, the shortest path problem is the problem of finding a
path between two vertices (or nodes) in a graph such that the sum of
the weights of its constituent edges is minimized.
This is analogous to the problem of finding the shortest path between
two intersections on a road map: the graph's vertices correspond to
intersections and the edges correspond to road segments, each
weighted by the length of its road segment.

(6, 4, 5, 1) and (6, 4, 3, 2, 1) are both paths


Definition between vertices 6 and 1.

The shortest path problem can be defined for graphs whether


undirected, directed, or mixed. It is defined here for undirected graphs; for directed graphs the definition of path
requires that consecutive vertices be connected by an appropriate directed edge.
Shortest path problem 100

Two vertices are adjacent when they are both incident


to a common edge. A path in an undirected graph is a
sequence of vertices
such
that is adjacent to for . Such a
path is called a path of length from to .
(The are variables; their numbering here relates to
their position in the sequence and needs not to relate to
any canonical labeling of the vertices.)
Shortest path (A, C, E, D, F) between vertices A and F in the
weighted directed graph.

Let be the edge incident to both and . Given a real-valued weight function , and an
undirected (simple) graph , the shortest path from to is the path (where

and ) that over all possible minimizes the sum When each edge in the graph has unit

weight or , this is equivalent to finding the path with fewest edges.


The problem is also sometimes called the single-pair shortest path problem, to distinguish it from the following
variations:
• The single-source shortest path problem, in which we have to find shortest paths from a source vertex v to all
other vertices in the graph.
• The single-destination shortest path problem, in which we have to find shortest paths from all vertices in the
directed graph to a single destination vertex v. This can be reduced to the single-source shortest path problem by
reversing the arcs in the directed graph.
• The all-pairs shortest path problem, in which we have to find shortest paths between every pair of vertices v, v'
in the graph.
These generalizations have significantly more efficient algorithms than the simplistic approach of running a
single-pair shortest path algorithm on all relevant pairs of vertices.

Algorithms
The most important algorithms for solving this problem are:
• Dijkstra's algorithm solves the single-source shortest path problems.
• Bellman–Ford algorithm solves the single-source problem if edge weights may be negative.
• A* search algorithm solves for single pair shortest path using heuristics to try to speed up the search.
• Floyd–Warshall algorithm solves all pairs shortest paths.
• Johnson's algorithm solves all pairs shortest paths, and may be faster than Floyd–Warshall on sparse graphs.
• Viterbi algorithm solves the shortest stochastic path problem with an additional probabilistic weight on each node.
Additional algorithms and associated evaluations may be found in Cherkassky et al.
Shortest path problem 101

Road networks
A road network can be considered as a graph with positive weights. The nodes represent road junctions and each
edge of the graph is associated with a road segment between two junctions. The weight of an edge may correspond to
the length of the associated road segment, the time needed to traverse the segment or the cost of traversing the
segment. Using directed edges it is also possible to model one-way streets. Such graphs are special in the sense that
some edges are more important than others for long distance travel (e.g. highways). This property has been
formalized using the notion of highway dimension.[1] There are a great number of algorithms that exploit this
property and are therefore able to compute the shortest path a lot quicker than would be possible on general graphs.
All of these algorithms work in two phases. In the first phase, the graph is preprocessed without knowing the source
or target node. This phase may take several days for realistic data and some techniques. The second phase is the
query phase. In this phase, source and target node are known. The running time of the second phase is generally less
than a second. The idea is that the road network is static, so the preprocessing phase can be done once and used for a
large number of queries on the same road network.
The algorithm with the fastest known query time is called hub labeling and is able to compute shortest path on the
road networks of Europe or the USA in a fraction of a microsecond.[2] Other techniques that have been used are:
• ALT
• Arc Flags
• Contraction hierarchies
• Transit Node Routing
• Reach based Pruning
• Labeling

Single-source shortest paths

Directed unweighted graphs

Algorithm Time complexity Author

Breadth-first search O(E)

This list is incomplete; you can help by expanding it [3].

Directed graphs with nonnegative weights

Algorithm Time Author


complexity

Ford 1956
O(V2EL)

Bellman–Ford algorithm O(VE) Bellman 1958, Moore 1959

Dantzig 1958, Dantzig 1960, Minty (cf. Pollack & Wiebenson 1960), Whiting &
O(V2 log V)
Hillier 1960

Dijkstra's algorithm with list Leyzorek et al. 1957, Dijkstra 1959


O(V2)

Dijkstra's algorithm with modified binary O((E + V) log V)


heap

... ... ...

Dijkstra's algorithm with Fibonacci heap O(E + V log V) Fredman & Tarjan 1984, Fredman & Tarjan 1987

O(E log log L) Johnson 1982, Karlsson & Poblete 1983


Shortest path problem 102

Gabow's algorithm O(E logE/V L) Gabow 1983b, Gabow 1985b

O(E + V√log L) Ahuja et al. 1990

This list is incomplete; you can help by expanding it [3].

Directed graphs with arbitrary weights

Algorithm Time complexity Author

Bellman–Ford algorithm O(VE) Bellman 1958, Moore 1959

This list is incomplete; you can help by expanding it [3].

All-pairs shortest paths


The all-pairs shortest paths problem for unweighted directed graphs was introduced by Shimbel (1953), who
observed that it could be solved by a linear number of matrix multiplications, taking a total time of O(V4).
Subsequent algorithms handle edge weights (which may possibly be negative), and are faster. The Floyd–Warshall
algorithm takes O(V3) time, and Johnson's algorithm (a combination of the Bellman–Ford and Dijkstra algorithms)
takes O(VE + V2 log V).

Applications
Shortest path algorithms are applied to automatically find directions between physical locations, such as driving
directions on web mapping websites like Mapquest or Google Maps. For this application fast specialized algorithms
are available.
If one represents a nondeterministic abstract machine as a graph where vertices describe states and edges describe
possible transitions, shortest path algorithms can be used to find an optimal sequence of choices to reach a certain
goal state, or to establish lower bounds on the time needed to reach a given state. For example, if vertices represents
the states of a puzzle like a Rubik's Cube and each directed edge corresponds to a single move or turn, shortest path
algorithms can be used to find a solution that uses the minimum possible number of moves.
In a networking or telecommunications mindset, this shortest path problem is sometimes called the min-delay path
problem and usually tied with a widest path problem. For example, the algorithm may seek the shortest (min-delay)
widest path, or widest shortest (min-delay) path.
A more lighthearted application is the games of "six degrees of separation" that try to find the shortest path in graphs
like movie stars appearing in the same film.
Other applications, often studied in operations research, include plant and facility layout, robotics, transportation,
and VLSI design".

Related problems
For shortest path problems in computational geometry, see Euclidean shortest path.
The travelling salesman problem is the problem of finding the shortest path that goes through every vertex exactly
once, and returns to the start. Unlike the shortest path problem, which can be solved in polynomial time in graphs
without negative cycles, the travelling salesman problem is NP-complete and, as such, is believed not to be
efficiently solvable for large sets of data (see P = NP problem). The problem of finding the longest path in a graph is
also NP-complete.
Shortest path problem 103

The Canadian traveller problem and the stochastic shortest path problem are generalizations where either the graph
isn't completely known to the mover, changes over time, or where actions (traversals) are probabilistic.
The shortest multiple disconnected path is a representation of the primitive path network within the framework of
Reptation theory.
The widest path problem seeks a path so that the minimum label of any edge is as large as possible.

Linear programming formulation


There is a natural linear programming formulation for the shortest path problem, given below. It is very simple
compared to most other uses of linear programs in discrete optimization, however it illustrates connections to other
concepts.
Given a directed graph (V, A) with source node s, target node t, and cost wij for each arc (i, j) in A, consider the
program with variables xij

minimize subject to and for all i,

This LP has the special property that it is integral; more specifically, every basic optimal solution (when one exists)
has all variables equal to 0 or 1, and the set of edges whose variables equal 1 form an s-t dipath. See Ahuja et al. for
one proof, although the origin of this approach dates back to mid-20th century.
The dual for this linear program is
maximize yt − ys subject to for all ij, yj − yi ≤ wij
and feasible duals correspond to the concept of a consistent heuristic for the A* algorithm for shortest paths. For any
feasible dual y the reduced costs are nonnegative and A* essentially runs Dijkstra's
algorithm on these reduced costs.

References
[1] Abraham, Ittai; Fiat, Amos; Goldberg, Andrew V.; Werneck, Renato F. "Highway Dimension, Shortest Paths, and Provably Efficient
Algorithms" (http:/ / research. microsoft. com/ pubs/ 115272/ soda10. pdf research. microsoft. com/ pubs/ 115272/ soda10. pdf). ACM-SIAM
Symposium on Discrete Algorithms, pages 782-793, 2010.
[2] Abraham, Ittai; Delling, Daniel; Goldberg, Andrew V.; Werneck, Renato F. research.microsoft.com/pubs/142356/HL-TR.pdf "A Hub-Based
Labeling Algorithm for Shortest Paths on Road Networks" (http:/ / research. microsoft. com/ pubs/ 142356/ HL-TR. pdf). Symposium on
Experimental Algorithms, pages 230-241, 2011.
[3] http:/ / en. wikipedia. org/ w/ index. php?title=Shortest_path_problem& action=edit

• Bellman, Richard (1958). "On a routing problem". Quarterly of Applied Mathematics 16: 87–90. MR  0102435
(http://www.ams.org/mathscinet-getitem?mr=0102435).
• Cormen, Thomas H.; Leiserson, Charles E., Rivest, Ronald L., Stein, Clifford (2001) [1990]. "Single-Source
Shortest Paths and All-Pairs Shortest Paths". Introduction to Algorithms (2nd ed.). MIT Press and McGraw-Hill.
pp. 580–642. ISBN 0-262-03293-7.
• Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs" (http://www-m3.ma.tum.de/twiki/
pub/MN0506/WebHome/dijkstra.pdf). Numerische Mathematik 1: 269–271. doi: 10.1007/BF01386390 (http://
dx.doi.org/10.1007/BF01386390).
• Fredman, Michael Lawrence; Tarjan, Robert E. (1984). "Fibonacci heaps and their uses in improved network
optimization algorithms" (http://www.computer.org/portal/web/csdl/doi/10.1109/SFCS.1984.715934).
25th Annual Symposium on Foundations of Computer Science. IEEE. pp. 338–346. doi:
10.1109/SFCS.1984.715934 (http://dx.doi.org/10.1109/SFCS.1984.715934). ISBN 0-8186-0591-X.
Shortest path problem 104

• Fredman, Michael Lawrence; Tarjan, Robert E. (1987). "Fibonacci heaps and their uses in improved network
optimization algorithms" (http://portal.acm.org/citation.cfm?id=28874). Journal of the Association for
Computing Machinery 34 (3): 596–615. doi: 10.1145/28869.28874 (http://dx.doi.org/10.1145/28869.28874).
• Leyzorek, M.; Gray, R. S.; Johnson, A. A.; Ladew, W. C.; Meaker, S. R., Jr.; Petry, R. M.; Seitz, R. N. (1957).
Investigation of Model Techniques — First Annual Report — 6 June 1956 — 1 July 1957 — A Study of Model
Techniques for Communication Systems. Cleveland, Ohio: Case Institute of Technology.
• Moore, E. F. (1959). "The shortest path through a maze". Proceedings of an International Symposium on the
Theory of Switching (Cambridge, Massachusetts, 2–5 April 1957). Cambridge: Harvard University Press.
pp. 285–292.
• Shimbel, Alfonso (1953). "Structural parameters of communication networks". Bulletin of Mathematical
Biophysics 15 (4): 501–507. doi: 10.1007/BF02476438 (http://dx.doi.org/10.1007/BF02476438).

Further reading
• D. Frigioni; A. Marchetti-Spaccamela and U. Nanni (1998). "Fully dynamic output bounded single source
shortest path problem" (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.32.9856). Proc. 7th
Annu. ACM-SIAM Symp. Discrete Algorithms. Atlanta, GA. pp. 212–221.
Breadth-first search 105

Breadth-first search
Breadth-first search

Order in which the nodes are expanded

Class Search algorithm

Data structure Graph

Worst case performance

Worst case space complexity

In graph theory, breadth-first search (BFS) is a strategy for searching in a graph when search is limited to
essentially two operations: (a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the
currently visited node. The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those
neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on. Compare BFS with the
equivalent, but more memory-efficient Iterative deepening depth-first search and contrast with depth-first search.

Animated example of a breadth-first search


Breadth-first search 106

Algorithm

The breadth-first tree obtained when running BFS on the given map
An example map of Germany with some connections between cities and starting in Frankfurt

Graph and tree


search
algorithms
• α–β
• A*
• B*
• Backtracking
• Beam
• Bellman–Ford
• Best-first
• Bidirectional
• Borůvka
• Branch & bound
• BFS
• British Museum
• D*
• DFS
• Depth-limited
• Dijkstra
• Edmonds
• Floyd–Warshall
• Fringe search
• Hill climbing
• IDA*
• Iterative deepening
• Kruskal
• Johnson
• Lexicographic BFS
• Prim
Breadth-first search 107

• SMA*
• Uniform-cost
Listings

• Graph algorithms
• Search algorithms
• List of graph algorithms

Related topics

• Dynamic programming
• Graph traversal
• Tree traversal
• Search games

• v
• t
• e [1]

The algorithm uses a queue data structure to store intermediate results as it traverses the graph, as follows:
1. Enqueue the root node
2. Dequeue a node and examine it
• If the element sought is found in this node, quit the search and return a result.
• Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.
3. If the queue is empty, every node on the graph has been examined – quit the search and return "not found".
4. If the queue is not empty, repeat from Step 2.
Note: Using a stack instead of a queue would turn this algorithm into a depth-first search.

Pseudocode
Input: A graph G and a root v of G

1 procedure BFS(G,v) is
2 create a queue Q
3 create a set V
4 enqueue v onto Q
5 add v to V
6 while Q is not empty loop
7 t ← Q.dequeue()
8 if t is what we are looking for then
9 return t
10 end if
11 for all edges e in G.adjacentEdges(t) loop
12 u ← G.adjacentVertex(t,e)
13 if u is not in V then
14 add u to V
15 enqueue u onto Q
16 end if
17 end loop
18 end loop
19 return none
20 end BFS
Breadth-first search 108

Features

Space complexity
When the number of vertices in the graph is known ahead of time, and additional data structures are used to
determine which vertices have already been added to the queue, the space complexity can be expressed as
where is the cardinality of the set of vertices. If the graph is represented by an Adjacency list it occupies
[1]
space in memory, while an Adjacency matrix representation occupies .[2]

Time complexity
[3]
The time complexity can be expressed as since every vertex and every edge will be explored in
the worst case. Note: may vary between and , depending on how sparse the input graph
is (assuming that the graph is connected).

Applications
Breadth-first search can be used to solve many problems in graph theory, for example:
• Finding all nodes within one connected component
• Copying Collection, Cheney's algorithm
• Finding the shortest path between two nodes u and v (with path length measured by number of edges)
• Testing a graph for bipartiteness
• (Reverse) Cuthill–McKee mesh numbering
• Ford–Fulkerson method for computing the maximum flow in a flow network
• Serialization/Deserialization of a binary tree vs serialization in sorted order, allows the tree to be re-constructed in
an efficient manner.
• Construction of the failure function of the Aho-Corasick pattern matcher.

Finding connected components


The set of nodes reached by a BFS (breadth-first search) form the connected component containing the starting node.

Testing bipartiteness
BFS can be used to test bipartiteness, by starting the search at any vertex and giving alternating labels to the vertices
visited during the search. That is, give label 0 to the starting vertex, 1 to all its neighbours, 0 to those neighbours'
neighbours, and so on. If at any step a vertex has (visited) neighbours with the same label as itself, then the graph is
not bipartite. If the search ends without such a situation occurring, then the graph is bipartite.
Breadth-first search 109

References
[1] Cormen, Thomas H., Charles E. Leiserson, and Ronald L. Rivest. p.590
[2] Cormen, Thomas H., Charles E. Leiserson, and Ronald L. Rivest. p.591
[3] Cormen, Thomas H., Charles E. Leiserson, and Ronald L. Rivest. p.597

• Knuth, Donald E. (1997), The Art Of Computer Programming Vol 1. 3rd ed. (http://www-cs-faculty.stanford.
edu/~knuth/taocp.html), Boston: Addison-Wesley, ISBN 0-201-89683-4

External links
• Breadth-First Explanation and Example (http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch14.
html#QQ1-46-92)
Depth-first search 110

Depth-first search
Depth-first search

Order in which the nodes are visited

Class Search algorithm

Data structure Graph

Worst case performance for explicit graphs traversed without repetition, for implicit graphs with branching factor b
searched to depth d
Worst case space complexity if entire graph is traversed without repetition, O(longest path length searched) for implicit graphs
without elimination of duplicate nodes

Graph and tree


search
algorithms
• α–β
• A*
• B*
• Backtracking
• Beam
• Bellman–Ford
• Best-first
• Bidirectional
• Borůvka
• Branch & bound
• BFS
• British Museum
• D*
• DFS
• Depth-limited
• Dijkstra
• Edmonds
• Floyd–Warshall
Depth-first search 111

• Fringe search
• Hill climbing
• IDA*
• Iterative deepening
• Kruskal
• Johnson
• Lexicographic BFS
• Prim
• SMA*
• Uniform-cost
Listings

• Graph algorithms
• Search algorithms
• List of graph algorithms

Related topics

• Dynamic programming
• Graph traversal
• Tree traversal
• Search games

• v
• t
• e [1]

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the
root (selecting some node as the root in the graph case) and explores as far as possible along each branch before
backtracking.
A version of depth-first search was investigated in the 19th century by French mathematician Charles Pierre
Trémaux[1] as a strategy for solving mazes.

Properties
The time and space analysis of DFS differs according to its application area. In theoretical computer science, DFS is
typical used to traverse an entire graph, and takes time , linear in the size of the graph. In these applications
it also uses space in the worst case to store the stack of vertices on the current search path as well as the set
of already-visited vertices. Thus, in this setting, the time and space bounds are the same as for breadth-first search
and the choice of which of these two algorithms to use depends less on their complexity and more on the different
properties of the vertex orderings the two algorithms produce.
For applications of DFS to search problems in artificial intelligence, however, the graph to be searched is often either
too large to visit in its entirety or even infinite, and DFS may suffer from non-termination when the length of a path
in the search tree is infinite. Therefore, the search is only performed to a limited depth, and due to limited memory
availability one typically does not use data structures that keep track of the set of all previously visited vertices. In
this case, the time is still linear in the number of expanded vertices and edges (although this number is not the same
as the size of the entire graph because some vertices may be searched more than once and others not at all) but the
space complexity of this variant of DFS is only proportional to the depth limit, much smaller than the space needed
for searching to the same depth using breadth-first search. For such applications, DFS also lends itself much better to
heuristic methods of choosing a likely-looking branch. When an appropriate depth limit is not known a priori,
iterative deepening depth-first search applies DFS repeatedly with a sequence of increasing limits; in the artificial
intelligence mode of analysis, with a branching factor greater than one, iterative deepening increases the running
Depth-first search 112

time by only a constant factor over the case in which the correct depth limit is known due to the geometric growth of
the number of nodes per level.
DFS may be also used to collect a sample of graph nodes. However, incomplete DFS, similarly to incomplete BFS,
is biased towards nodes of high degree.

Example
For the following graph:

a depth-first search starting at A, assuming that the left edges in the shown graph are chosen before right edges, and
assuming the search remembers previously visited nodes and will not repeat them (since this is a small graph), will
visit the nodes in the following order: A, B, D, F, E, C, G. The edges traversed in this search form a Trémaux tree, a
structure with important applications in graph theory.
Performing the same search without remembering previously visited nodes results in visiting nodes in the order A, B,
D, F, E, A, B, D, F, E, etc. forever, caught in the A, B, D, F, E cycle and never reaching C or G.
Iterative deepening is one technique to avoid this infinite loop and would reach all nodes.

Output of a depth-first search


A convenient description of a depth first search of
a graph is in terms of a spanning tree of the
vertices reached during the search. Based on this
spanning tree, the edges of the original graph can
be divided into three classes: forward edges,
which point from a node of the tree to one of its
descendants, back edges, which point from a node
to one of its ancestors, and cross edges, which do
neither. Sometimes tree edges, edges which
belong to the spanning tree itself, are classified
separately from forward edges. If the original The four types of edges defined by a spanning tree
graph is undirected then all of its edges are tree
edges or back edges.
Depth-first search 113

Vertex orderings
It is also possible to use the depth-first search to linearly order the vertices of the original graph (or tree). There are
three common ways of doing this:
• A preordering is a list of the vertices in the order that they were first visited by the depth-first search algorithm.
This is a compact and natural way of describing the progress of the search, as was done earlier in this article. A
preordering of an expression tree is the expression in Polish notation.
• A postordering is a list of the vertices in the order that they were last visited by the algorithm. A postordering of
an expression tree is the expression in reverse Polish notation.
• A reverse postordering is the reverse of a postordering, i.e. a list of the vertices in the opposite order of their last
visit. Reverse postordering is not the same as preordering. For example, when searching the directed graph

beginning at node A, one visits the nodes in sequence, to produce lists either A B D B A C A, or A C D C A B
A (depending upon whether the algorithm chooses to visit B or C first). Note that repeat visits in the form of
backtracking to a node, to check if it has still unvisited neighbours, are included here (even if it is found to
have none). Thus the possible preorderings are A B D C and A C D B (order by node's leftmost occurrence in
above list), while the possible reverse postorderings are A C B D and A B C D (order by node's rightmost
occurrence in above list). Reverse postordering produces a topological sorting of any directed acyclic graph.
This ordering is also useful in control flow analysis as it often represents a natural linearization of the control
flow. The graph above might represent the flow of control in a code fragment like

if (A) then {
B
} else {
C
}
D

and it is natural to consider this code in the order A B C D or A C B D, but not natural to use the order A B D
C or A C D B.
Depth-first search 114

Pseudocode
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
A recursive implementation of DFS:[2]

1 procedure DFS(G,v):
2 label v as discovered
3 for all edges from v to w in G.adjacentEdges(v) do
4 if vertex w is not labeled as discovered then
5 recursively call DFS(G,w)

A non-recursive implementation of DFS:[3]

1 procedure DFS-iterative(G,v):
2 let S be a stack
3 S.push(v)
4 while S is not empty
5 v ← S.pop()
6 if v is not labeled as discovered:
7 label v as discovered
8 for all edges from v to w in G.adjacentEdges(v) do
9 S.push(w)

These two variations of DFS visit the neighbors of each vertex in the opposite order from each other: the first
neighbor of v visited by the recursive variation is the first one in the list of adjacent edges, while in the iterative
variation the first visited neighbor is the last one in the list of adjacent edges. The non-recursive implementation is
similar to breadth-first search but differs from it in two ways: it uses a stack instead of a queue, and it delays
checking whether a vertex has been discovered until the vertex is popped from the stack rather than making this
check before pushing the vertex.

Applications
Algorithms that use depth-first search as a building
block include:
• Finding connected components.
• Topological sorting.
• Finding 2-(edge or vertex)-connected
components.
• Finding 3-(edge or vertex)-connected
components.
• Finding the bridges of a graph.
• Generating words in order to plot the Limit Set
of a Group.
• Finding strongly connected components. Randomized algorithm similar to depth-first search used in generating a
• Planarity testing maze.

• Solving puzzles with only one solution, such as


mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the
visited set.)
Depth-first search 115

• Maze generation may use a randomized depth-first search.


• Finding biconnectivity in graphs.

Notes
[1] Charles Pierre Trémaux (1859–1882) École Polytechnique of Paris (X:1876), French engineer of the telegraph
in Public conference, December 2, 2010 – by professor Jean Pelletier-Thibert in Académie de Macon (Burgundy – France) – (Abstract
published in the Annals academic, March 2011 – ISSN: 0980-6032)
[2] Goodrich and Tamassia; Cormen, Leiserson, Rivest, and Stein
[3] Kleinberg and Tardos

References
• Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms,
Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 22.3: Depth-first search,
pp. 540–549.
• Goodrich, Michael T.; Tamassia, Roberto (2001), Algorithm Design: Foundations, Analysis, and Internet
Examples, Wiley, ISBN 0-471-38365-1
• Kleinberg, Jon; Tardos, Éva (2006), Algorithm Design, Addison Wesley, pp. 92–94
• Knuth, Donald E. (1997), The Art Of Computer Programming Vol 1. 3rd ed (http://www-cs-faculty.stanford.
edu/~knuth/taocp.html), Boston: Addison-Wesley, ISBN 0-201-89683-4, OCLC  155842391 (http://www.
worldcat.org/oclc/155842391)

External links
• Depth-First Explanation and Example (http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch14.
html)
• C++ Boost Graph Library: Depth-First Search (http://www.boost.org/libs/graph/doc/depth_first_search.
html)
• Depth-First Search Animation (for a directed graph) (http://www.cs.duke.edu/csed/jawaa/DFSanim.html)
• Depth First and Breadth First Search: Explanation and Code (http://www.kirupa.com/developer/actionscript/
depth_breadth_search.htm)
• QuickGraph (http://quickgraph.codeplex.com/Wiki/View.aspx?title=Depth First Search Example), depth first
search example for .Net
• Depth-first search algorithm illustrated explanation (Java and C++ implementations) (http://www.algolist.net/
Algorithms/Graph_algorithms/Undirected/Depth-first_search)
• YAGSBPL – A template-based C++ library for graph search and planning (http://code.google.com/p/yagsbpl/
)
Backtracking 116

Backtracking
Graph and tree
search
algorithms
• α–β
• A*
• B*
• Backtracking
• Beam
• Bellman–Ford
• Best-first
• Bidirectional
• Borůvka
• Branch & bound
• BFS
• British Museum
• D*
• DFS
• Depth-limited
• Dijkstra
• Edmonds
• Floyd–Warshall
• Fringe search
• Hill climbing
• IDA*
• Iterative deepening
• Kruskal
• Johnson
• Lexicographic BFS
• Prim
• SMA*
• Uniform-cost
Listings

• Graph algorithms
• Search algorithms
• List of graph algorithms

Related topics

• Dynamic programming
• Graph traversal
• Tree traversal
• Search games

• v
• t
• e [1]

Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that
incrementally builds candidates to the solutions, and abandons each partial candidate c ("backtracks") as soon as it
Backtracking 117

determines that c cannot possibly be completed to a valid solution.


The classic textbook example of the use of backtracking is the eight queens puzzle, that asks for all arrangements of
eight chess queens on a standard chessboard so that no queen attacks any other. In the common backtracking
approach, the partial candidates are arrangements of k queens in the first k rows of the board, all in different rows
and columns. Any partial solution that contains two mutually attacking queens can be abandoned, since it cannot
possibly be completed to a valid solution.
Backtracking can be applied only for problems which admit the concept of a "partial candidate solution" and a
relatively quick test of whether it can possibly be completed to a valid solution. It is useless, for example, for
locating a given value in an unordered table. When it is applicable, however, backtracking is often much faster than
brute force enumeration of all complete candidates, since it can eliminate a large number of candidates with a single
test.
Backtracking is an important tool for solving constraint satisfaction problems, such as crosswords, verbal arithmetic,
Sudoku, and many other puzzles. It is often the most convenient (if not the most efficient[citation needed]) technique for
parsing, for the knapsack problem and other combinatorial optimization problems. It is also the basis of the so-called
logic programming languages such as Icon, Planner and Prolog. Backtracking is also utilized in the (diff) difference
engine for the MediaWiki software[citation needed].
Backtracking depends on user-given "black box procedures" that define the problem to be solved, the nature of the
partial candidates, and how they are extended into complete candidates. It is therefore a metaheuristic rather than a
specific algorithm – although, unlike many other meta-heuristics, it is guaranteed to find all solutions to a finite
problem in a bounded amount of time.
The term "backtrack" was coined by American mathematician D. H. Lehmer in the 1950s. The pioneer
string-processing language SNOBOL (1962) may have been the first to provide a built-in general backtracking
facility.

Description of the method


The backtracking algorithm enumerates a set of partial candidates that, in principle, could be completed in various
ways to give all the possible solutions to the given problem. The completion is done incrementally, by a sequence of
candidate extension steps.
Conceptually, the partial candidates are the nodes of a tree structure, the potential search tree. Each partial candidate
is the parent of the candidates that differ from it by a single extension step; the leaves of the tree are the partial
candidates that cannot be extended any further.
The backtracking algorithm traverses this search tree recursively, from the root down, in depth-first order. At each
node c, the algorithm checks whether c can be completed to a valid solution. If it cannot, the whole sub-tree rooted at
c is skipped (pruned). Otherwise, the algorithm (1) checks whether c itself is a valid solution, and if so reports it to
the user; and (2) recursively enumerates all sub-trees of c. The two tests and the children of each node are defined by
user-given procedures.
Therefore, the actual search tree that is traversed by the algorithm is only a part of the potential tree. The total cost
of the algorithm is the number of nodes of the actual tree times the cost of obtaining and processing each node. This
fact should be considered when choosing the potential search tree and implementing the pruning test.
Backtracking 118

Pseudocode
In order to apply backtracking to a specific class of problems, one must provide the data P for the particular instance
of the problem that is to be solved, and six procedural parameters, root, reject, accept, first, next, and output. These
procedures should take the instance data P as a parameter and should do the following:
1. 'root(P): return the partial candidate at the root of the search tree.
2. reject(P,c): return true only if the partial candidate c is not worth completing.
3. accept(P,c): return true if c is a solution of P, and false otherwise.
4. first(P,c): generate the first extension of candidate c.
5. next(P,s): generate the next alternative extension of a candidate, after the extension s.
6. output(P,c): use the solution c of P, as appropriate to the application.
The backtracking algorithm reduces then to the call bt(root(P)), where bt is the following recursive procedure:

procedure bt(c)
if reject(P,c) then return
if accept(P,c) then output(P,c)
s ← first(P,c)
while s ≠ Λ do
bt(s)
s ← next(P,s)

Usage considerations
The reject procedure should be a boolean-valued function that returns true only if it is certain that no possible
extension of c is a valid solution for P. If the procedure cannot reach a definite conclusion, it should return false. An
incorrect true result may cause the bt procedure to miss some valid solutions. The procedure may assume that
reject(P,t) returned false for every ancestor t of c in the search tree.
On the other hand, the efficiency of the backtracking algorithm depends on reject returning true for candidates that
are as close to the root as possible. If reject always returns false, the algorithm will still find all solutions, but it will
be equivalent to a brute-force search.
The accept procedure should return true if c is a complete and valid solution for the problem instance P, and false
otherwise. It may assume that the partial candidate c and all its ancestors in the tree have passed the reject test.
Note that the general pseudo-code above does not assume that the valid solutions are always leaves of the potential
search tree. In other words, it admits the possibility that a valid solution for P can be further extended to yield other
valid solutions.
The first and next procedures are used by the backtracking algorithm to enumerate the children of a node c of the
tree, that is, the candidates that differ from c by a single extension step. The call first(P,c) should yield the first child
of c, in some order; and the call next(P,s) should return the next sibling of node s, in that order. Both functions
should return a distinctive "null" candidate, denoted here by 'Λ', if the requested child does not exist.
Together, the root, first, and next functions define the set of partial candidates and the potential search tree. They
should be chosen so that every solution of P occurs somewhere in the tree, and no partial candidate occurs more than
once. Moreover, they should admit an efficient and effective reject predicate.
Backtracking 119

Early stopping variants


The pseudo-code above will call output for all candidates that are a solution to the given instance P. The algorithm is
easily modified to stop after finding the first solution, or a specified number of solutions; or after testing a specified
number of partial candidates, or after spending a given amount of CPU time.

Examples
Typical examples are
• Puzzles such as eight queens puzzle, crosswords, verbal arithmetic,
Sudoku, Peg Solitaire.
• Combinatorial optimization problems such as parsing and the
knapsack problem.
• Logic programming languages such as Icon, Planner and Prolog,
which use backtracking internally to generate answers.
• Backtracking is also utilized in the "diff" (version comparing)
engine for the MediaWiki software.
• Parsing
• Knapsack problem
Sudoku puzzle solved by backtracking.
Below is an example for the constraint satisfaction problem:

Constraint satisfaction
The general constraint satisfaction problem consists in finding a list of integers x = (x[1],x[2], ..., x[n]), each in some
range {1, 2, ..., m}, that satisfies some arbitrary constraint (boolean function) F.
For this class of problems, the instance data P would be the integers m and n, and the predicate F. In a typical
backtracking solution to this problem, one could define a partial candidate as a list of integers c = (c[1],c[2], ... c[k]),
for any k between 0 and n, that are to be assigned to the first k variables x[1],x[2], ..., x[k]). The root candidate would
then be the empty list (). The first and next procedures would then be

function first(P,c)
k ← length(c)
if k = n
then return Λ
else return (c[1], c[2], ..., c[k], 1)

function next(P,s)
k ← length(s)
if s[k] = m
then return Λ
else return (s[1], s[2], ..., s[k-1], 1 + s[k])

Here "length(c)" is the number of elements in the list c.


The call reject(P,c) should return true if the constraint F cannot be satisfied by any list of n integers that begins with
the k elements of c. For backtracking to be effective, there must be a way to detect this situation, at least for some
candidates c, without enumerating all those mn-k n-tuples.
For example, if F is the conjunction of several boolean predicates, F = F[1] F[2] F[p], and each F[i]
depends only on a small subset of the variables x[1], ..., x[n], then the reject procedure could simply check the terms
Backtracking 120

F[i] that depend only on variables x[1], ..., x[k], and return true if any of those terms returns false. In fact, reject
needs only check those terms that do depend on x[k], since the terms that depend only on x[1], ..., x[k-1] will have
been tested further up in the search tree.
Assuming that reject is implemented as above, then accept(P,c) needs only check whether c is complete, that is,
whether it has n elements.
It is generally better to order the list of variables so that it begins with the most critical ones (i.e. the ones with fewest
value options, or which have a greater impact on subsequent choices).
One could also allow the next function to choose which variable should be assigned when extending a partial
candidate, based on the values of the variables already assigned by it. Further improvements can be obtained by the
technique of constraint propagation.
In addition to retaining minimal recovery values used in backing up, backtracking implementations commonly keep
a variable trail, to record value change history. An efficient implementation will avoid creating a variable trail entry
between two successive changes when there is no choice point, as the backtracking will erase all of the changes as a
single operation.
An alternative to the variable trail is to keep a timestamp of when the last change was made to the variable. The
timestamp is compared to the timestamp of a choice point. If the choice point has an associated time later than that of
the variable, it is unnecessary to revert the variable when the choice point is backtracked, as it was changed before
the choice point occurred.

Notes

References
• Gilles Brassard, Paul Bratley (1995). Fundamentals of Algorithmics. Prentice-Hall.

External links
• HBmeyer.de (http://www.hbmeyer.de/backtrack/backtren.htm), Interactive animation of a backtracking
algorithm
• Solving Combinatorial Problems with STL and Backtracking (http://www.drdobbs.com/cpp/
solving-combinatorial-problems-with-stl/184401194), Article and C++ source code for a generic implementation
of backtracking
• Sample Java Code (http://github.com/kapild/Permutations), Sample code for backtracking of 8 Queens
problem.
Topological sorting 121

Topological sorting
In computer science, a topological sort (sometimes abbreviated topsort or toposort) or topological ordering of a
directed graph is a linear ordering of its vertices such that for every directed edge uv from vertex u to vertex v, u
comes before v in the ordering. For instance, the vertices of the graph may represent tasks to be performed, and the
edges may represent constraints that one task must be performed before another; in this application, a topological
ordering is just a valid sequence for the tasks. A topological ordering is possible if and only if the graph has no
directed cycles, that is, if it is a directed acyclic graph (DAG). Any DAG has at least one topological ordering, and
algorithms are known for constructing a topological ordering of any DAG in linear time.

Examples
The canonical application of topological sorting (topological order) is in scheduling a sequence of jobs or tasks based
on their dependencies; topological sorting algorithms were first studied in the early 1960s in the context of the PERT
technique for scheduling in project management (Jarnagin 1960). The jobs are represented by vertices, and there is
an edge from x to y if job x must be completed before job y can be started (for example, when washing clothes, the
washing machine must finish before we put the clothes to dry). Then, a topological sort gives an order in which to
perform the jobs.
In computer science, applications of this type arise in instruction scheduling, ordering of formula cell evaluation
when recomputing formula values in spreadsheets, logic synthesis, determining the order of compilation tasks to
perform in makefiles, data serialization, and resolving symbol dependencies in linkers. It is also used to decide in
which order to load tables with foreign keys in databases.

The graph shown to the left has many valid topological sorts, including:
• 7, 5, 3, 11, 8, 2, 9, 10 (visual left-to-right, top-to-bottom)
• 3, 5, 7, 8, 11, 2, 9, 10 (smallest-numbered available vertex first)
• 3, 7, 8, 5, 11, 10, 2, 9 (because we can)
• 5, 7, 3, 8, 11, 10, 9, 2 (fewest edges first)
• 7, 5, 11, 3, 10, 8, 9, 2 (largest-numbered available vertex first)
• 7, 5, 11, 2, 3, 8, 9, 10 (attempting top-to-bottom, left-to-right)

Algorithms
The usual algorithms for topological sorting have running time linear in the number of nodes plus the number of
edges ( ).
One of these algorithms, first described by Kahn (1962), works by choosing vertices in the same order as the
eventual topological sort. First, find a list of "start nodes" which have no incoming edges and insert them into a set S;
at least one such node must exist in an acyclic graph. Then:

L ← Empty list that will contain the sorted elements


S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
Topological sorting 122

add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)

If the graph is a DAG, a solution will be contained in the list L (the solution is not necessarily unique). Otherwise,
the graph must have at least one cycle and therefore a topological sorting is impossible.
Note that, reflecting the non-uniqueness of the resulting sort, the structure S can be simply a set or a queue or a stack.
Depending on the order that nodes n are removed from set S, a different solution is created. A variation of Kahn's
algorithm that breaks ties lexicographically forms a key component of the Coffman–Graham algorithm for parallel
scheduling and layered graph drawing.
An alternative algorithm for topological sorting is based on depth-first search. The algorithm loops through each
node of the graph, in an arbitrary order, initiating a depth-first search that terminates when it hits any node that has
already been visited since the beginning of the topological sort:

L ← Empty list that will contain the sorted nodes


while there are unmarked nodes do
select an unmarked node n
visit(n)
function visit(node n)
if n has a temporary mark then stop (not a DAG)
if n is not marked (i.e. has not been visited yet) then
mark n temporarily
for each node m with an edge from n to m do
visit(m)
mark n permanently
add n to head of L

Note that each node n gets prepended to the output list L only after considering all other nodes on which n depends
(all ancestral nodes of n in the graph). Specifically, when the algorithm adds node n, we are guaranteed that all nodes
on which n depends are already in the output list L: they were added to L either by the preceding recursive call to
visit(), or by an earlier call to visit(). Since each edge and node is visited once, the algorithm runs in linear time. This
depth-first-search-based algorithm is the one described by Cormen et al. (2001); it seems to have been first described
in print by Tarjan (1976).
Topological sorting 123

Complexity
The computational complexity of the problem of computing a topological ordering of a directed acyclic graph is
NC2; that is, it can be computed in O(log2 n) time on a parallel computer using a polynomial number O(nk) of
processors, for some constant k (Cook 1985).

Uniqueness
If a topological sort has the property that all pairs of consecutive vertices in the sorted order are connected by edges,
then these edges form a directed Hamiltonian path in the DAG. If a Hamiltonian path exists, the topological sort
order is unique; no other order respects the edges of the path. Conversely, if a topological sort does not form a
Hamiltonian path, the DAG will have two or more valid topological orderings, for in this case it is always possible to
form a second valid ordering by swapping two consecutive vertices that are not connected by an edge to each other.
Therefore, it is possible to test in linear time whether a unique ordering exists, and whether a Hamiltonian path
exists, despite the NP-hardness of the Hamiltonian path problem for more general directed graphs (Vernet &
Markenzon 1997).

Relation to partial orders


Topological orderings are also closely related to the concept of a linear extension of a partial order in mathematics.
A partially ordered set is just a set of objects together with a definition of the "≤" inequality relation, satisfying the
axioms of reflexivity (x ≤ x), antisymmetry (if x ≤ y and y ≤x then x = y) and transitivity (if x ≤ y and y ≤ z, then
x ≤ z). A total order is a partial order in which, for every two objects x and y in the set, either x ≤ y or y ≤ x. Total
orders are familiar in computer science as the comparison operators needed to perform comparison sorting
algorithms. For finite sets, total orders may be identified with linear sequences of objects, where the "≤" relation is
true whenever the first object precedes the second object in the order; a comparison sorting algorithm may be used to
convert a total order into a sequence in this way. A linear extension of a partial order is a total order that is
compatible with it, in the sense that, if x ≤ y in the partial order, then x ≤ y in the total order as well.
One can define a partial ordering from any DAG by letting the set of objects be the vertices of the DAG, and
defining x ≤ y to be true, for any two vertices x and y, whenever there exists a directed path from x to y; that is,
whenever y is reachable from x. With these definitions, a topological ordering of the DAG is the same thing as a
linear extension of this partial order. Conversely, any partial ordering may be defined as the reachability relation in a
DAG. One way of doing this is to define a DAG that has a vertex for every object in the partially ordered set, and an
edge xy for every pair of objects for which x ≤ y. An alternative way of doing this is to use the transitive reduction of
the partial ordering; in general, this produces DAGs with fewer edges, but the reachability relation in these DAGs is
still the same partial order. By using these constructions, one can use topological ordering algorithms to find linear
extensions of partial orders.

References
• Cook, Stephen A. (1985), "A Taxonomy of Problems with Fast Parallel Algorithms", Information and Control 64
(1–3): 2–22, doi:10.1016/S0019-9958(85)80041-3 [1].
• Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001), "Section 22.4: Topological
sort", Introduction to Algorithms (2nd ed.), MIT Press and McGraw-Hill, pp. 549–552, ISBN 0-262-03293-7.
• Jarnagin, M. P. (1960), Automatic machine methods of testing PERT networks for consistency, Technical
Memorandum No. K-24/60, Dahlgren, Virginia: U. S. Naval Weapons Laboratory.
• Kahn, Arthur B. (1962), "Topological sorting of large networks", Communications of the ACM 5 (11): 558–562,
doi:10.1145/368996.369025 [2].
Topological sorting 124

• Tarjan, Robert E. (1976), "Edge-disjoint spanning trees and depth-first search", Acta Informatica 6 (2): 171–185,
doi:10.1007/BF00268499 [3].
• Vernet, Oswaldo; Markenzon, Lilian (1997), "Hamiltonian problems for reducible flowgraphs", Proc. 17th
International Conference of the Chilean Computer Science Society (SCCC '97), pp. 264–267,
doi:10.1109/SCCC.1997.637099 [4].

External links
• NIST Dictionary of Algorithms and Data Structures: topological sort [5]
• Weisstein, Eric W., "TopologicalSort [6]", MathWorld.

References
[1] http:/ / dx. doi. org/ 10. 1016%2FS0019-9958%2885%2980041-3
[2] http:/ / dx. doi. org/ 10. 1145%2F368996. 369025
[3] http:/ / dx. doi. org/ 10. 1007%2FBF00268499
[4] http:/ / dx. doi. org/ 10. 1109%2FSCCC. 1997. 637099
[5] http:/ / www. nist. gov/ dads/ HTML/ topologicalSort. html
[6] http:/ / mathworld. wolfram. com/ TopologicalSort. html
Dijkstra's algorithm 125

Dijkstra's algorithm
Dijkstra's algorithm

Dijkstra's algorithm. It picks the unvisited vertex with the lowest-distance, calculates the distance through it to each unvisited neighbor, and updates
the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors.

Class Search algorithm

Data structure Graph

Worst case performance

Graph and tree


search
algorithms
• α–β
• A*
• B*
• Backtracking
• Beam
• Bellman–Ford
• Best-first
• Bidirectional
• Borůvka
• Branch & bound
• BFS
• British Museum
• D*
• DFS
• Depth-limited
• Dijkstra
• Edmonds
• Floyd–Warshall
• Fringe search
• Hill climbing
• IDA*
• Iterative deepening
• Kruskal
• Johnson
• Lexicographic BFS
• Prim
Dijkstra's algorithm 126

• SMA*
• Uniform-cost
Listings

• Graph algorithms
• Search algorithms
• List of graph algorithms

Related topics

• Dynamic programming
• Graph traversal
• Tree traversal
• Search games

• v
• t
• e [1]

Dijkstra's algorithm, conceived by computer scientist Edsger Dijkstra in 1956 and published in 1959, is a graph
search algorithm that solves the single-source shortest path problem for a graph with non-negative edge path costs,
producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms.
For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path)
between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex
to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been
determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances
between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between
one city and all other cities. As a result, the shortest path first is widely used in network routing protocols, most
notably IS-IS and OSPF (Open Shortest Path First).
Dijkstra's original algorithm does not use a min-priority queue and runs in (where is the number of
vertices). The idea of this algorithm is also given in (Leyzorek et al. 1957). The implementation based on a
min-priority queue implemented by a Fibonacci heap and running in (where is the
number of edges) is due to (Fredman & Tarjan 1984). This is asymptotically the fastest known single-source
shortest-path algorithm for arbitrary directed graphs with unbounded non-negative weights.
Dijkstra's algorithm 127

Algorithm
Let the node at which we are starting be called the initial node. Let the
distance of node Y be the distance from the initial node to Y.
Dijkstra's algorithm will assign some initial distance values and will try
to improve them step by step.
1. Assign to every node a tentative distance value: set it to zero for our
initial node and to infinity for all other nodes.
2. Mark all nodes unvisited. Set the initial node as current. Create a set
of the unvisited nodes called the unvisited set consisting of all the
nodes.
3. For the current node, consider all of its unvisited neighbors and
calculate their tentative distances. For example, if the current node
A is marked with a distance of 6, and the edge connecting it with a Illustration of Dijkstra's algorithm search for
neighbor B has length 2, then the distance to B (through A) will be 6 finding path from a start node (lower left, red) to
a goal node (upper right, green) in a robot motion
+ 2 = 8.
planning problem. Open nodes represent the
4. When we are done considering all of the neighbors of the current "tentative" set. Filled nodes are visited ones, with
node, mark the current node as visited and remove it from the color representing the distance: the greener, the
farther. Nodes in all the different directions are
unvisited set. A visited node will never be checked again.
explored uniformly, appearing as a more-or-less
5. If the destination node has been marked visited (when planning a circular wavefront as Dijkstra's algorithm uses a
route between two specific nodes) or if the smallest tentative heuristic identically equal to 0.
distance among the nodes in the unvisited set is infinity (when
planning a complete traversal; occurs when there is no connection between the initial node and remaining
unvisited nodes), then stop. The algorithm has finished.
6. Select the unvisited node that is marked with the smallest tentative distance, and set it as the new "current node"
then go back to step 3.

Description
Note: For ease of understanding, this discussion uses the terms intersection, road and map — however,
formally these terms are vertex, edge and graph, respectively.
Suppose you would like to find the shortest path between two intersections on a city map, a starting point and a
destination. The order is conceptually simple: to start, mark the distance to every intersection on the map with
infinity. This is done not to imply there is an infinite distance, but to note that intersection has not yet been visited;
some variants of this method simply leave the intersection unlabeled. Now, at each iteration, select a current
intersection. For the first iteration, the current intersection will be the starting point and the distance to it (the
intersection's label) will be zero. For subsequent iterations (after the first), the current intersection will be the closest
unvisited intersection to the starting point—this will be easy to find.
From the current intersection, update the distance to every unvisited intersection that is directly connected to it. This
is done by determining the sum of the distance between an unvisited intersection and the value of the current
intersection, and relabeling the unvisited intersection with this value if it is less than its current value. In effect, the
intersection is relabeled if the path to it through the current intersection is shorter than the previously known paths.
To facilitate shortest path identification, in pencil, mark the road with an arrow pointing to the relabeled intersection
if you label/relabel it, and erase all others pointing to it. After you have updated the distances to each neighboring
intersection, mark the current intersection as visited and select the unvisited intersection with lowest distance (from
the starting point) – or the lowest label—as the current intersection. Nodes marked as visited are labeled with the
shortest path from the starting point to it and will not be revisited or returned to.
Dijkstra's algorithm 128

Continue this process of updating the neighboring intersections with the shortest distances, then marking the current
intersection as visited and moving onto the closest unvisited intersection until you have marked the destination as
visited. Once you have marked the destination as visited (as is the case with any visited intersection) you have
determined the shortest path to it, from the starting point, and can trace your way back, following the arrows in
reverse.
Of note is the fact that this algorithm makes no attempt to direct "exploration" towards the destination as one might
expect. Rather, the sole consideration in determining the next "current" intersection is its distance from the starting
point. This algorithm, therefore "expands outward" from the starting point, interactively considering every node that
is closer in terms of shortest path distance until it reaches the destination. When understood in this way, it is clear
how the algorithm necessarily finds the shortest path, however, it may also reveal one of the algorithm's weaknesses:
its relative slowness in some topologies.

Pseudocode
In the following algorithm, the code u := vertex in Q with smallest dist[], searches for the vertex
u in the vertex set Q that has the least dist[u] value. That vertex is removed from the set Q and returned to the
user. dist_between(u, v) calculates the length between the two neighbor-nodes u and v. The variable alt
on lines 17 & 19 is the length of the path from the root node to the neighbor node v if it were to go through u. If this
path is shorter than the current shortest path recorded for v, that current path is replaced with this alt path. The
previous array is populated with a pointer to the "next-hop" node on the source graph to get the shortest route to
the source.
1 function Dijkstra(Graph, source):

2 for each vertex v in Graph: // Initializations

3 dist[v] := infinity; // Mark distances from source to v as not yet computed

4 visited[v] := false; // Mark all nodes as unvisited

5 previous[v] := undefined; // Previous node in optimal path from source

6 end for

8 dist[source] := 0; // Distance from source to itself is zero

9 insert source into Q; // Start off with the source node

10

11 while Q is not empty: // The main loop

12 u := vertex in Q with smallest distance in dist[] and has not been visited; // Source node in first case

13 remove u from Q;

14 visited[u] := true // mark this node as visited

15

16 for each neighbor v of u:

17 alt := dist[u] + dist_between(u, v); // accumulate shortest dist from source

18 if alt < dist[v]:

19 dist[v] := alt; // keep the shortest dist from src to v

20 previous[v] := u;

21 if !visited[v]:

22 insert v into Q; // Add unvisited v into the Q to be processed

23 end if

24 end if

25 end for

26 end while
Dijkstra's algorithm 129

27 return dist;

28 endfunction

If we are only interested in a shortest path between vertices source and target, we can terminate the search at
line 12 if u = target. Now we can read the shortest path from source to target by reverse iteration:
1 S := empty sequence

2 u := target

3 while previous[u] is defined: // Construct the shortest path with a stack S

4 insert u at the beginning of S // Push the vertex into the stack

5 u := previous[u] // Traverse from target to source

6 end while ;

Now sequence S is the list of vertices constituting one of the shortest paths from source to target, or the
empty sequence if no path exists.
A more general problem would be to find all the shortest paths between source and target (there might be
several different ones of the same length). Then instead of storing only a single node in each entry of previous[]
we would store all nodes satisfying the relaxation condition. For example, if both r and source connect to
target and both of them lie on different shortest paths through target (because the edge cost is the same in
both cases), then we would add both r and source to previous[target]. When the algorithm completes,
previous[] data structure will actually describe a graph that is a subset of the original graph with some edges
removed. Its key property will be that if the algorithm was run with some starting node, then every path from that
node to any other node in the new graph will be the shortest path between those nodes in the original graph, and all
paths of that length from the original graph will be present in the new graph. Then to actually find all these shortest
paths between two given nodes we would use a path finding algorithm on the new graph, such as depth-first search.

Using a priority queue


A min-priority queue is an abstract data structure that provides 3 basic operations : add_with_priority(),
decrease_priority() and extract_min(). As mentioned earlier, using such a data structure can lead to
fastest computing times than using a basic queue. Notably, Fibonacci heap (Fredman & Tarjan 1984) or Brodal
queue offer optimal implementations for those 3 operations. As the algorithm is slightly different, we mention it
here, in pseudo-code as well :

1 function Dijkstra(Graph, source):


2 dist[source] := 0 // Initializations
3 for each vertex v in Graph:
4 if v ≠ source
5 dist[v] := infinity // Unknown distance from source to v
6 previous[v] := undefined // Predecessor of v
7 end if
8 PQ.add_with_priority(v,dist[v])
9 end for
10
11
12 while PQ is not empty: // The main loop
13 u := PQ.extract_min() // Remove and return best vertex
14 for each neighbor v of u: // where v has not yet been removed from PQ.
15 alt = dist[u] + length(u, v)
16 if alt < dist[v] // Relax the edge (u,v)
Dijkstra's algorithm 130

17 dist[v] := alt
18 previous[v] := u
19 PQ.decrease_priority(v,alt)
20 end if
21 end for
22 end while
23 return previous[]

It should be noted that other data structures can be used to achieve even faster computing times in practice.

Running time
An upper bound of the running time of Dijkstra's algorithm on a graph with edges and vertices can be
expressed as a function of and using big-O notation.
For any implementation of vertex set the running time is in , where and
are times needed to perform decrease key and extract minimum operations in set , respectively.
The simplest implementation of the Dijkstra's algorithm stores vertices of set in an ordinary linked list or array,
and extract minimum from is simply a linear search through all vertices in . In this case, the running time is
.
For sparse graphs, that is, graphs with far fewer than edges, Dijkstra's algorithm can be implemented more
efficiently by storing the graph in the form of adjacency lists and using a self-balancing binary search tree, binary
heap, pairing heap, or Fibonacci heap as a priority queue to implement extracting minimum efficiently. With a
self-balancing binary search tree or binary heap, the algorithm requires time (which is
dominated by , assuming the graph is connected). To avoid look-up in decrease-key step
on a vanilla binary heap, it is necessary to maintain a supplementary index mapping each vertex to the heap's index
(and keep it up to date as priority queue changes), making it take only time instead. The Fibonacci
heap improves this to .
Note that for directed acyclic graphs, it is possible to find shortest paths from a given starting vertex in linear time,
by processing the vertices in a topological order, and calculating the path length for each vertex to be the minimum
length obtained via any of its incoming edges.[1]

Related problems and algorithms


The functionality of Dijkstra's original algorithm can be extended with a variety of modifications. For example,
sometimes it is desirable to present solutions which are less than mathematically optimal. To obtain a ranked list of
less-than-optimal solutions, the optimal solution is first calculated. A single edge appearing in the optimal solution is
removed from the graph, and the optimum solution to this new graph is calculated. Each edge of the original solution
is suppressed in turn and a new shortest-path calculated. The secondary solutions are then ranked and presented after
the first optimal solution.
Dijkstra's algorithm is usually the working principle behind link-state routing protocols, OSPF and IS-IS being the
most common ones.
Unlike Dijkstra's algorithm, the Bellman–Ford algorithm can be used on graphs with negative edge weights, as long
as the graph contains no negative cycle reachable from the source vertex s. The presence of such cycles means there
is no shortest path, since the total weight becomes lower each time the cycle is traversed. It is possible to adapt
Dijkstra's algorithm to handle negative weight edges by combining it with the Bellman-Ford algorithm (to remove
negative edges and detect negative cycles), such an algorithm is called Johnson's algorithm.
Dijkstra's algorithm 131

The A* algorithm is a generalization of Dijkstra's algorithm that cuts down on the size of the subgraph that must be
explored, if additional information is available that provides a lower bound on the "distance" to the target. This
approach can be viewed from the perspective of linear programming: there is a natural linear program for computing
shortest paths, and solutions to its dual linear program are feasible if and only if they form a consistent heuristic
(speaking roughly, since the sign conventions differ from place to place in the literature). This feasible dual /
consistent heuristic defines a non-negative reduced cost and A* is essentially running Dijkstra's algorithm with these
reduced costs. If the dual satisfies the weaker condition of admissibility, then A* is instead more akin to the
Bellman–Ford algorithm.
The process that underlies Dijkstra's algorithm is similar to the greedy process used in Prim's algorithm. Prim's
purpose is to find a minimum spanning tree that connects all nodes in the graph; Dijkstra is concerned with only two
nodes. Prim's does not evaluate the total weight of the path from the starting node, only the individual path.
Breadth-first search can be viewed as a special-case of Dijkstra's algorithm on unweighted graphs, where the priority
queue degenerates into a FIFO queue.

Dynamic programming perspective


From a dynamic programming point of view, Dijkstra's algorithm is a successive approximation scheme that solves
the dynamic programming functional equation for the shortest path problem by the Reaching method.[2]
In fact, Dijkstra's explanation of the logic behind the algorithm, namely
Problem 2. Find the path of minimum total length between two given nodes and .
We use the fact that, if is a node on the minimal path from to , knowledge of the latter implies the
knowledge of the minimal path from to .
is a paraphrasing of Bellman's famous Principle of Optimality in the context of the shortest path problem.

Notes
[1] http:/ / www. boost. org/ doc/ libs/ 1_44_0/ libs/ graph/ doc/ dag_shortest_paths. html
[2] Online version of the paper with interactive computational modules. (http:/ / www. ifors. ms. unimelb. edu. au/ tutorial/ dijkstra_new/ index.
html)

References
• Dijkstra, E. W. (1959). "A note on two problems in connexion with graphs" (http://www-m3.ma.tum.de/twiki/
pub/MN0506/WebHome/dijkstra.pdf). Numerische Mathematik 1: 269–271. doi: 10.1007/BF01386390 (http://
dx.doi.org/10.1007/BF01386390).
• Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001). "Section 24.3: Dijkstra's
algorithm". Introduction to Algorithms (Second ed.). MIT Press and McGraw–Hill. pp. 595–601.
ISBN 0-262-03293-7.
• Fredman, Michael Lawrence; Tarjan, Robert E. (1984). "Fibonacci heaps and their uses in improved network
optimization algorithms" (http://www.computer.org/portal/web/csdl/doi/10.1109/SFCS.1984.715934).
25th Annual Symposium on Foundations of Computer Science. IEEE. pp. 338–346. doi:
10.1109/SFCS.1984.715934 (http://dx.doi.org/10.1109/SFCS.1984.715934).
• Fredman, Michael Lawrence; Tarjan, Robert E. (1987). "Fibonacci heaps and their uses in improved network
optimization algorithms" (http://portal.acm.org/citation.cfm?id=28874). Journal of the Association for
Computing Machinery 34 (3): 596–615. doi: 10.1145/28869.28874 (http://dx.doi.org/10.1145/28869.28874).
• Zhan, F. Benjamin; Noon, Charles E. (February 1998). "Shortest Path Algorithms: An Evaluation Using Real
Road Networks". Transportation Science 32 (1): 65–73. doi: 10.1287/trsc.32.1.65 (http://dx.doi.org/10.1287/
trsc.32.1.65).
Dijkstra's algorithm 132

• Leyzorek, M.; Gray, R. S.; Johnson, A. A.; Ladew, W. C.; Meaker, Jr., S. R.; Petry, R. M.; Seitz, R. N. (1957).
Investigation of Model Techniques — First Annual Report — 6 June 1956 — 1 July 1957 — A Study of Model
Techniques for Communication Systems. Cleveland, Ohio: Case Institute of Technology.
• Knuth, D.E. (1977). "A Generalization of Dijkstra's Algorithm". Information Processing Letters 6 (1): 1–5.

External links
• C/C++
• Dijkstra's Algorithm in C++ (https://github.com/xtaci/algorithms/blob/master/include/dijkstra.h)
• Implementation in Boost C++ library (http://www.boost.org/doc/libs/1_43_0/libs/graph/doc/
dijkstra_shortest_paths.html)
• Dijkstra's Algorithm in C Programming Language (http://www.rawbytes.com/dijkstras-algorithm-in-c/)
• Java
• Applet by Carla Laffra of Pace University (http://www.dgp.toronto.edu/people/JamesStewart/270/9798s/
Laffra/DijkstraApplet.html)
• Visualization of Dijkstra's Algorithm (http://students.ceid.upatras.gr/~papagel/english/java_docs/
minDijk.htm)
• Shortest Path Problem: Dijkstra's Algorithm (http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/
Dijkstra.shtml)
• Dijkstra's Algorithm Applet (http://www.unf.edu/~wkloster/foundations/DijkstraApplet/DijkstraApplet.
htm)
• Open Source Java Graph package with implementation of Dijkstra's Algorithm (http://code.google.com/p/
annas/)
• A Java library for path finding with Dijkstra's Algorithm and example Applet (http://www.stackframe.com/
software/PathFinder)
• Dijkstra's algorithm as bidirectional version in Java (https://github.com/graphhopper/graphhopper/tree/
90879ad05c4dfedf0390d44525065f727b043357/core/src/main/java/com/graphhopper/routing)
• C#/.Net
• Dijkstra's Algorithm in C# (http://www.codeproject.com/KB/recipes/ShortestPathCalculation.aspx)
• Fast Priority Queue Implementation of Dijkstra's Algorithm in C# (http://www.codeproject.com/KB/
recipes/FastHeapDijkstra.aspx)
• QuickGraph, Graph Data Structures and Algorithms for .NET (http://quickgraph.codeplex.com/)
• Dijkstra's Algorithm Simulation (http://optlab-server.sce.carleton.ca/POAnimations2007/DijkstrasAlgo.html)
• Oral history interview with Edsger W. Dijkstra (http://purl.umn.edu/107247), Charles Babbage Institute
University of Minnesota, Minneapolis.
• Animation of Dijkstra's algorithm (http://www.cs.sunysb.edu/~skiena/combinatorica/animations/dijkstra.
html)
• Haskell implementation of Dijkstra's Algorithm (http://bonsaicode.wordpress.com/2011/01/04/
programming-praxis-dijkstra’s-algorithm/) on Bonsai code
• Implementation in T-SQL (http://hansolav.net/sql/graphs.html)
• A MATLAB program for Dijkstra's algorithm (http://www.mathworks.com/matlabcentral/fileexchange/
20025-advanced-dijkstras-minimum-path-algorithm)
• Step through Dijkstra's Algorithm in an online JavaScript Debugger (http://www.turb0js.com/a/
Dijkstra's_Algorithm)
Greedy algorithm 133

Greedy algorithm
A greedy algorithm is an algorithm that follows
the problem solving heuristic of making the
locally optimal choice at each stage with the
hope of finding a global optimum. In many
problems, a greedy strategy does not in general
produce an optimal solution, but nonetheless a
greedy heuristic may yield locally optimal
solutions that approximate a global optimal
solution in a reasonable time.

For example, a greedy strategy for the traveling


salesman problem (which is of a high
computational complexity) is the following
heuristic: "At each stage visit an unvisited city Greedy algorithms determine the minimum number of coins to give while
nearest to the current city". This heuristic need making change. These are the steps a human would take to emulate a greedy
not find a best solution but terminates in a algorithm to represent 36 cents using only coins with values {1, 5, 10, 20}.
The coin of the highest value, less than the remaining change owed, is the
reasonable number of steps; finding an optimal
local optimum. (Note that in general the change-making problem requires
solution typically requires unreasonably many dynamic programming or integer programming to find an optimal solution;
steps. In mathematical optimization, greedy However, most currency systems, including the Euro and US Dollar, are
algorithms solve combinatorial problems having special cases where the greedy strategy does find an optimum solution.)

the properties of matroids.

Specifics
In general, greedy algorithms have five components:
1. A candidate set, from which a solution is created
2. A selection function, which chooses the best candidate to be added to the solution
3. A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
4. An objective function, which assigns a value to a solution, or a partial solution, and
5. A solution function, which will indicate when we have discovered a complete solution
Greedy algorithms produce good solutions on some mathematical problems, but not on others. Most problems for
which they work, will have two properties:
Greedy choice property
We can make whatever choice seems best at the moment and then solve the subproblems that arise later. The
choice made by a greedy algorithm may depend on choices made so far but not on future choices or all the
solutions to the subproblem. It iteratively makes one greedy choice after another, reducing each given problem
into a smaller one. In other words, a greedy algorithm never reconsiders its choices. This is the main difference
from dynamic programming, which is exhaustive and is guaranteed to find the solution. After every stage,
dynamic programming makes decisions based on all the decisions made in the previous stage, and may
reconsider the previous stage's algorithmic path to solution.
Optimal substructure
"A problem exhibits optimal substructure if an optimal solution to the problem contains optimal solutions to
the sub-problems."[1]
Greedy algorithm 134

Cases of failure
Examples on how a greedy algorithm may fail to achieve the optimal solution.

Starting at A, a greedy algorithm will find the local maximum at "m", oblivious of the global maximum at "M".

With a goal of reaching the largest-sum, at each step, the greedy algorithm will choose what appears to be the
optimal immediate choice, so it will choose 12 instead of 3 at the second step, and will not reach the best solution,
which contains 99.
For many other problems, greedy algorithms fail to produce the optimal solution, and may even produce the unique
worst possible solution. One example is the traveling salesman problem mentioned above: for each number of cities,
there is an assignment of distances between the cities for which the nearest neighbor heuristic produces the unique
worst possible tour.[2]
Imagine the coin example with only 25-cent, 10-cent, and 4-cent coins. The greedy algorithm would not be able to
make change for 41 cents, since after committing to use one 25-cent coin and one 10-cent coin it would be
impossible to use 4-cent coins for the balance of 6 cents, whereas a person or a more sophisticated algorithm could
make change for 41 cents with one 25-cent coin and four 4-cent coins.
Greedy algorithm 135

Types
Greedy algorithms can be characterized as being 'short sighted', and as 'non-recoverable'. They are ideal only for
problems which have 'optimal substructure'. Despite this, greedy algorithms are best suited for simple problems (e.g.
giving change). It is important, however, to note that the greedy algorithm can be used as a selection algorithm to
prioritize options within a search, or branch and bound algorithm. There are a few variations to the greedy algorithm:
• Pure greedy algorithms
• Orthogonal greedy algorithms
• Relaxed greedy algorithms

Applications
Greedy algorithms mostly (but not always) fail to find the globally optimal solution, because they usually do not
operate exhaustively on all the data. They can make commitments to certain choices too early which prevent them
from finding the best overall solution later. For example, all known greedy coloring algorithms for the graph
coloring problem and all other NP-complete problems do not consistently find optimum solutions. Nevertheless, they
are useful because they are quick to think up and often give good approximations to the optimum.
If a greedy algorithm can be proven to yield the global optimum for a given problem class, it typically becomes the
method of choice because it is faster than other optimization methods like dynamic programming. Examples of such
greedy algorithms are Kruskal's algorithm and Prim's algorithm for finding minimum spanning trees, Dijkstra's
algorithm for finding single-source shortest paths, and the algorithm for finding optimum Huffman trees.
The theory of matroids, and the more general theory of greedoids, provide whole classes of such algorithms.
Greedy algorithms appear in network routing as well. Using greedy routing, a message is forwarded to the
neighboring node which is "closest" to the destination. The notion of a node's location (and hence "closeness") may
be determined by its physical location, as in geographic routing used by ad hoc networks. Location may also be an
entirely artificial construct as in small world routing and distributed hash table.

Examples
• The activity selection problem is characteristic to this class of problems, where the goal is to pick the maximum
number of activities that do not clash with each other.
• In the Macintosh computer game Crystal Quest the objective is to collect crystals, in a fashion similar to the
travelling salesman problem. The game has a demo mode, where the game uses a greedy algorithm to go to every
crystal. The artificial intelligence does not account for obstacles, so the demo mode often ends quickly.
• The matching pursuit is an example of greedy algorithm applied on signal approximation.
• A greedy algorithm finds the optimal solution to Malfatti's problem of finding three disjoint circles within a given
triangle that maximize the total area of the circles; it is conjectured that the same greedy algorithm is optimal for
any number of circles.
• A greedy algorithm is used to construct a Huffman tree during Huffman coding where it finds an optimal solution.
• In decision tree learning greedy algorithms are commonly used however they are not guaranteed to find the
optimal solution.
Greedy algorithm 136

Notes
[1] Introduction to Algorithms (Cormen, Leiserson, Rivest, and Stein) 2001, Chapter 16 "Greedy Algorithms".
[2] (G. Gutin, A. Yeo and A. Zverovich, 2002)

References
• Introduction to Algorithms (Cormen, Leiserson, and Rivest) 1990, Chapter 17 "Greedy Algorithms" p. 329.
• Introduction to Algorithms (Cormen, Leiserson, Rivest, and Stein) 2001, Chapter 16 "Greedy Algorithms".
• G. Gutin, A. Yeo and A. Zverovich, Traveling salesman should not be greedy: domination analysis of greedy-type
heuristics for the TSP. Discrete Applied Mathematics 117 (2002), 81–86.
• J. Bang-Jensen, G. Gutin and A. Yeo, When the greedy algorithm fails. Discrete Optimization 1 (2004), 121–127.
• G. Bendall and F. Margot, Greedy Type Resistance of Combinatorial Problems, Discrete Optimization 3 (2006),
288–298.

External links
• Greedy algorithm visualization (http://yuval.bar-or.org/index.php?item=9) A visualization of a greedy
solution to the N-Queens puzzle by Yuval Baror.
• Python greedy coin (http://www.oreillynet.com/onlamp/blog/2008/04/python_greedy_coin_changer_alg.
html) example by Noah Gift.

Travelling salesman problem


The travelling salesman problem (TSP) asks the following question: Given a list of cities and the distances
between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the
origin city? It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical
computer science.
The problem was first formulated in 1930 and is one of the most intensively studied problems in optimization. It is
used as a benchmark for many optimization methods. Even though the problem is computationally difficult, a large
number of heuristics and exact methods are known, so that some instances with tens of thousands of cities can be
solved.
The TSP has several applications even in its purest formulation, such as planning, logistics, and the manufacture of
microchips. Slightly modified, it appears as a sub-problem in many areas, such as DNA sequencing. In these
applications, the concept city represents, for example, customers, soldering points, or DNA fragments, and the
concept distance represents travelling times or cost, or a similarity measure between DNA fragments. In many
applications, additional constraints such as limited resources or time windows make the problem considerably
harder. TSP is a special case of the travelling purchaser problem.
In the theory of computational complexity, the decision version of the TSP (where, given a length L, the task is to
decide whether the graph has any tour shorter than L) belongs to the class of NP-complete problems. Thus, it is
likely that the worst-case running time for any algorithm for the TSP increases superpolynomially (or perhaps
exponentially) with the number of cities.
Travelling salesman problem 137

History
The origins of the travelling salesman problem are unclear. A handbook for travelling salesmen from 1832 mentions
the problem and includes example tours through Germany and Switzerland, but contains no mathematical
treatment.[1]
The travelling salesman problem was defined in the 1800s by the Irish
mathematician W. R. Hamilton and by the British mathematician
Thomas Kirkman. Hamilton’s Icosian Game was a recreational puzzle
based on finding a Hamiltonian cycle.[2] The general form of the TSP
appears to have been first studied by mathematicians during the 1930s
in Vienna and at Harvard, notably by Karl Menger, who defines the
problem, considers the obvious brute-force algorithm, and observes the
non-optimality of the nearest neighbour heuristic:

We denote by messenger problem (since in practice this question


should be solved by each postman, anyway also by many
travelers) the task to find, for finitely many points whose
pairwise distances are known, the shortest route connecting the
points. Of course, this problem is solvable by finitely many
trials. Rules which would push the number of trials below the William Rowan Hamilton
number of permutations of the given points, are not known. The
rule that one first should go from the starting point to the closest point, then to the point closest to this, etc., in
general does not yield the shortest route.[3]

Hassler Whitney at Princeton University introduced the name travelling salesman problem soon after.[4]
In the 1950s and 1960s, the problem became increasingly popular in scientific circles in Europe and the USA.
Notable contributions were made by George Dantzig, Delbert Ray Fulkerson and Selmer M. Johnson at the RAND
Corporation in Santa Monica, who expressed the problem as an integer linear program and developed the cutting
plane method for its solution. With these new methods they solved an instance with 49 cities to optimality by
constructing a tour and proving that no other tour could be shorter. In the following decades, the problem was
studied by many researchers from mathematics, computer science, chemistry, physics, and other sciences.
Richard M. Karp showed in 1972 that the Hamiltonian cycle problem was NP-complete, which implies the
NP-hardness of TSP. This supplied a mathematical explanation for the apparent computational difficulty of finding
optimal tours.
Great progress was made in the late 1970s and 1980, when Grötschel, Padberg, Rinaldi and others managed to
exactly solve instances with up to 2392 cities, using cutting planes and branch-and-bound.
In the 1990s, Applegate, Bixby, Chvátal, and Cook developed the program Concorde that has been used in many
recent record solutions. Gerhard Reinelt published the TSPLIB in 1991, a collection of benchmark instances of
varying difficulty, which has been used by many research groups for comparing results. In 2006, Cook and others
computed an optimal tour through an 85,900-city instance given by a microchip layout problem, currently the largest
solved TSPLIB instance. For many other instances with millions of cities, solutions can be found that are guaranteed
to be within 2-3% of an optimal tour.
Travelling salesman problem 138

Description

As a graph problem
TSP can be modelled as an undirected weighted graph, such that cities
are the graph's vertices, paths are the graph's edges, and a path's
distance is the edge's length. It is a minimization problem starting and
finishing at a specified vertex after having visited each other vertex
exactly once. Often, the model is a complete graph (i.e. each pair of
vertices is connected by an edge). If no path exists between two cities,
adding an arbitrarily long edge will complete the graph without
affecting the optimal tour.

Asymmetric and symmetric Symmetric TSP with four cities

In the symmetric TSP, the distance between two cities is the same in
each opposite direction, forming an undirected graph. This symmetry halves the number of possible solutions. In the
asymmetric TSP, paths may not exist in both directions or the distances might be different, forming a directed graph.
Traffic collisions, one-way streets, and airfares for cities with different departure and arrival fees are examples of
how this symmetry could break down.

Related problems
• An equivalent formulation in terms of graph theory is: Given a complete weighted graph (where the vertices
would represent the cities, the edges would represent the roads, and the weights would be the cost or distance of
that road), find a Hamiltonian cycle with the least weight.
• The requirement of returning to the starting city does not change the computational complexity of the problem,
see Hamiltonian path problem.
• Another related problem is the bottleneck traveling salesman problem (bottleneck TSP): Find a Hamiltonian cycle
in a weighted graph with the minimal weight of the weightiest edge. The problem is of considerable practical
importance, apart from evident transportation and logistics areas. A classic example is in printed circuit
manufacturing: scheduling of a route of the drill machine to drill holes in a PCB. In robotic machining or drilling
applications, the "cities" are parts to machine or holes (of different sizes) to drill, and the "cost of travel" includes
time for retooling the robot (single machine job sequencing problem).
• The generalized traveling salesman problem deals with "states" that have (one or more) "cities" and the salesman
has to visit exactly one "city" from each "state". Also known as the "traveling politician problem". One
application is encountered in ordering a solution to the cutting stock problem in order to minimise knife changes.
Another is concerned with drilling in semiconductor manufacturing, see e.g., U.S. Patent 7,054,798 [5].
Surprisingly, Behzad and Modarres demonstrated that the generalised traveling salesman problem can be
transformed into a standard traveling salesman problem with the same number of cities, but a modified distance
matrix.
• The sequential ordering problem deals with the problem of visiting a set of cities where precedence relations
between the cities exist.
• The traveling purchaser problem deals with a purchaser who is charged with purchasing a set of products. He can
purchase these products in several cities, but at different prices and not all cities offer the same products. The
objective is to find a route between a subset of the cities, which minimizes total cost (travel cost + purchasing
cost) and which enables the purchase of all required products.
Travelling salesman problem 139

Integer linear programming formulation


TSP can be formulated as an integer linear program.[6][7][8] Let equal 1 if the path goes from city i to city j, and
0 otherwise, for cities 0, ..., n. Let for i = 1, ..., n be artificial variables, and let be the distance from city i to
city j. Then the integer linear programming problem can be written as

The first set of equalities requires that each city 0, ..., n be arrived at from exactly one other city, and the second set
of equalities requires that from each city 1, ..., n there is a departure to exactly one other city. (These constraints
together also imply that there is exactly one departure from city 0.) The last constraints enforce that there is only a
single tour covering all cities, and not two or more disjointed tours that only collectively cover all cities. To prove
this, it is shown below (1) that every feasible solution contains only one closed sequence of cities, and (2) that for
every single tour covering all cities, there are values for the dummy variables that satisfy the constraints.
To prove that every feasible solution contains only one closed sequence of cities, it suffices to show that every
subtour in a feasible solution passes through city 0 (noting that the equalities ensure there can only be one such tour).
For if we sum all the inequalities corresponding to for any subtour of k steps not passing through city 0, we
obtain which is a contradiction.
It now must be shown that for every single tour covering all cities, there are values for the dummy variables that
satisfy the constraints.
Without loss of generality, define the tour as originating (and ending) at city 0. Choose if city i is visited in
step t (i, t = 1, 2, ..., n). Then since can be no greater than n and can be no less than 1;
hence the constraints are satisfied whenever For
satisfying the constraint.

Computing a solution
The traditional lines of attack for the NP-hard problems are the following:
• Devising algorithms for finding exact solutions (they will work reasonably fast only for small problem sizes).
• Devising "suboptimal" or heuristic algorithms, i.e., algorithms that deliver either seemingly or probably good
solutions, but which could not be proved to be optimal.
• Finding special cases for the problem ("subproblems") for which either better or exact heuristics are possible.
Travelling salesman problem 140

Computational complexity
The problem has been shown to be NP-hard (more precisely, it is complete for the complexity class FPNP; see
function problem), and the decision problem version ("given the costs and a number x, decide whether there is a
round-trip route cheaper than x") is NP-complete. The bottleneck travelling salesman problem is also NP-hard. The
problem remains NP-hard even for the case when the cities are in the plane with Euclidean distances, as well as in a
number of other restrictive cases. Removing the condition of visiting each city "only once" does not remove the
NP-hardness, since it is easily seen that in the planar case there is an optimal tour that visits each city only once
(otherwise, by the triangle inequality, a shortcut that skips a repeated visit would not increase the tour length).

Complexity of approximation
In the general case, finding a shortest travelling salesman tour is NPO-complete. If the distance measure is a metric
and symmetric, the problem becomes APX-complete and Christofides’s algorithm approximates it within 1.5.
If the distances are restricted to 1 and 2 (but still are a metric) the approximation ratio becomes 8/7.[9] In the
asymmetric, metric case, only logarithmic performance guarantees are known, the best current algorithm achieves
performance ratio 0.814 log n; it is an open question if a constant factor approximation exists.
The corresponding maximization problem of finding the longest travelling salesman tour is approximable within
63/38. If the distance function is symmetric, the longest tour can be approximated within 4/3 by a deterministic
algorithm and within by a randomised algorithm.

Exact algorithms
The most direct solution would be to try all permutations (ordered combinations) and see which one is cheapest
(using brute force search). The running time for this approach lies within a polynomial factor of , the
factorial of the number of cities, so this solution becomes impractical even for only 20 cities. One of the earliest
applications of dynamic programming is the Held–Karp algorithm that solves the problem in time .[10]
Improving these time bounds seems to be difficult. For example, it has not been determined whether an exact
algorithm for TSP that runs in time exists.
Other approaches include:
• Various branch-and-bound algorithms, which can be used to process TSPs containing 40–60 cities.
• Progressive improvement algorithms which use techniques reminiscent of linear programming. Works well for up
to 200 cities.
• Implementations of branch-and-bound and problem-specific cut generation (branch-and-cut); this is the method of
choice for solving large instances. This approach holds the current record, solving an instance with 85,900 cities,
see Applegate et al. (2006).
An exact solution for 15,112 German towns from TSPLIB was found in 2001 using the cutting-plane method
proposed by George Dantzig, Ray Fulkerson, and Selmer M. Johnson in 1954, based on linear programming. The
computations were performed on a network of 110 processors located at Rice University and Princeton University
(see the Princeton external link). The total computation time was equivalent to 22.6 years on a single 500 MHz
Alpha processor. In May 2004, the travelling salesman problem of visiting all 24,978 towns in Sweden was solved: a
tour of length approximately 72,500 kilometers was found and it was proven that no shorter tour exists.[11]
In March 2005, the travelling salesman problem of visiting all 33,810 points in a circuit board was solved using
Concorde TSP Solver: a tour of length 66,048,945 units was found and it was proven that no shorter tour exists. The
computation took approximately 15.7 CPU-years (Cook et al. 2006). In April 2006 an instance with 85,900 points
was solved using Concorde TSP Solver, taking over 136 CPU-years, see Applegate et al. (2006).
Travelling salesman problem 141

Heuristic and approximation algorithms


Various heuristics and approximation algorithms, which quickly yield good solutions have been devised. Modern
methods can find solutions for extremely large problems (millions of cities) within a reasonable time which are with
a high probability just 2–3% away from the optimal solution.
Several categories of heuristics are recognized.

Constructive heuristics
The nearest neighbor (NN) algorithm (or so-called greedy algorithm) lets the salesman choose the nearest unvisited
city as his next move. This algorithm quickly yields an effectively short route. For N cities randomly distributed on a
plane, the algorithm on average yields a path 25% longer than the shortest possible path.[12] However, there exist
many specially arranged city distributions which make the NN algorithm give the worst route (Gutin, Yeo, and
Zverovich, 2002). This is true for both asymmetric and symmetric TSPs (Gutin and Yeo, 2007). Rosenkrantz et al.
[1977] showed that the NN algorithm has the approximation factor for instances satisfying the triangle
inequality. A variation of NN algorithm, called Nearest Fragment (NF) operator, which connects a group (fragment)
of nearest unvisited cities, can find shorter route with successive iterations.[13] The NF operator can also be applied
on an initial solution obtained by NN algorithm for further improvement in an elitist model, where only better
solutions are accepted.
Constructions based on a minimum spanning tree have an approximation ratio of 2. The Christofides algorithm
achieves a ratio of 1.5.
The bitonic tour of a set of points is the minimum-perimeter monotone polygon that has the points as its vertices; it
can be computed efficiently by dynamic programming.
Another constructive heuristic, Match Twice and Stitch (MTS) (Kahng, Reda 2004 [14]), performs two sequential
matchings, where the second matching is executed after deleting all the edges of the first matching, to yield a set of
cycles. The cycles are then stitched to produce the final tour.

Iterative improvement
Pairwise exchange
The pairwise exchange or 2-opt technique involves iteratively removing two edges and replacing these with
two different edges that reconnect the fragments created by edge removal into a new and shorter tour. This is a
special case of the k-opt method. Note that the label Lin–Kernighan is an often heard misnomer for 2-opt.
Lin–Kernighan is actually the more general k-opt method.
k-opt heuristic, or Lin–Kernighan heuristics
Take a given tour and delete k mutually disjoint edges. Reassemble the remaining fragments into a tour,
leaving no disjoint subtours (that is, don't connect a fragment's endpoints together). This in effect simplifies
the TSP under consideration into a much simpler problem. Each fragment endpoint can be connected to 2k − 2
other possibilities: of 2k total fragment endpoints available, the two endpoints of the fragment under
consideration are disallowed. Such a constrained 2k-city TSP can then be solved with brute force methods to
find the least-cost recombination of the original fragments. The k-opt technique is a special case of the V-opt
or variable-opt technique. The most popular of the k-opt methods are 3-opt, and these were introduced by Shen
Lin of Bell Labs in 1965. There is a special case of 3-opt where the edges are not disjoint (two of the edges are
adjacent to one another). In practice, it is often possible to achieve substantial improvement over 2-opt without
the combinatorial cost of the general 3-opt by restricting the 3-changes to this special subset where two of the
removed edges are adjacent. This so-called two-and-a-half-opt typically falls roughly midway between 2-opt
and 3-opt, both in terms of the quality of tours achieved and the time required to achieve those tours.
V-opt heuristic
Travelling salesman problem 142

The variable-opt method is related to, and a generalization of the k-opt method. Whereas the k-opt methods
remove a fixed number (k) of edges from the original tour, the variable-opt methods do not fix the size of the
edge set to remove. Instead they grow the set as the search process continues. The best known method in this
family is the Lin–Kernighan method (mentioned above as a misnomer for 2-opt). Shen Lin and Brian
Kernighan first published their method in 1972, and it was the most reliable heuristic for solving travelling
salesman problems for nearly two decades. More advanced variable-opt methods were developed at Bell Labs
in the late 1980s by David Johnson and his research team. These methods (sometimes called
Lin–Kernighan–Johnson) build on the Lin–Kernighan method, adding ideas from tabu search and
evolutionary computing. The basic Lin–Kernighan technique gives results that are guaranteed to be at least
3-opt. The Lin–Kernighan–Johnson methods compute a Lin–Kernighan tour, and then perturb the tour by
what has been described as a mutation that removes at least four edges and reconnecting the tour in a different
way, then v-opting the new tour. The mutation is often enough to move the tour from the local minimum
identified by Lin–Kernighan. V-opt methods are widely considered the most powerful heuristics for the
problem, and are able to address special cases, such as the Hamilton Cycle Problem and other non-metric TSPs
that other heuristics fail on. For many years Lin–Kernighan–Johnson had identified optimal solutions for all
TSPs where an optimal solution was known and had identified the best known solutions for all other TSPs on
which the method had been tried.

Randomised improvement
Optimized Markov chain algorithms which use local searching heuristic sub-algorithms can find a route extremely
close to the optimal route for 700 to 800 cities.
TSP is a touchstone for many general heuristics devised for combinatorial optimization such as genetic algorithms,
simulated annealing, Tabu search, ant colony optimization, river formation dynamics (see swarm intelligence) and
the cross entropy method.

Ant colony optimization


Artificial intelligence researcher Marco Dorigo described in 1997 a method of heuristically generating "good
solutions" to the TSP using a simulation of an ant colony called ACS (Ant Colony System).[15] It models behavior
observed in real ants to find short paths between food sources and their nest, an emergent behaviour resulting from
each ant's preference to follow trail pheromones deposited by other ants.
ACS sends out a large number of virtual ant agents to explore many possible routes on the map. Each ant
probabilistically chooses the next city to visit based on a heuristic combining the distance to the city and the amount
of virtual pheromone deposited on the edge to the city. The ants explore, depositing pheromone on each edge that
they cross, until they have all completed a tour. At this point the ant which completed the shortest tour deposits
virtual pheromone along its complete tour route (global trail updating). The amount of pheromone deposited is
inversely proportional to the tour length: the shorter the tour, the more it deposits.
Travelling salesman problem 143

Ant Colony Optimization Algorithm for a TSP


with 7 cities: Red and thick lines in the
pheromone map indicate presence of more
pheromone

Special cases

Metric TSP
In the metric TSP, also known as delta-TSP or Δ-TSP, the intercity distances satisfy the triangle inequality.
A very natural restriction of the TSP is to require that the distances between cities form a metric to satisfy the
triangle inequality; that is the direct connection from A to B is never farther than the route via intermediate C:
.
The edge spans then build a metric on the set of vertices. When the cities are viewed as points in the plane, many
natural distance functions are metrics, and so many natural instances of TSP satisfy this constraint.
The following are some examples of metric TSPs for various metrics.
• In the Euclidean TSP (see below) the distance between two cities is the Euclidean distance between the
corresponding points.
• In the rectilinear TSP the distance between two cities is the sum of the differences of their x- and y-coordinates.
This metric is often called the Manhattan distance or city-block metric.
• In the maximum metric, the distance between two points is the maximum of the absolute values of differences of
their x- and y-coordinates.
The last two metrics appear for example in routing a machine that drills a given set of holes in a printed circuit
board. The Manhattan metric corresponds to a machine that adjusts first one co-ordinate, and then the other, so the
time to move to a new point is the sum of both movements. The maximum metric corresponds to a machine that
adjusts both co-ordinates simultaneously, so the time to move to a new point is the slower of the two movements.
Travelling salesman problem 144

In its definition, the TSP does not allow cities to be visited twice, but many applications do not need this constraint.
In such cases, a symmetric, non-metric instance can be reduced to a metric one. This replaces the original graph with
a complete graph in which the inter-city distance is replaced by the shortest path between and in the
original graph.
The span of the minimum spanning tree of the network is a natural lower bound for the span of the optimal route,
because deleting any edge of the optimal route yields a Hamiltonian path, which is a spanning tree in . In the TSP
with triangle inequality case it is possible to prove upper bounds in terms of the minimum spanning tree and design
an algorithm that has a provable upper bound on the span of the route. The first published (and the simplest) example
follows:
1. Construct a minimum spanning tree for .
2. Duplicate all edges of . That is, wherever there is an edge from u to v, add a second edge from v to u. This
gives us an Eulerian graph .
3. Find an Eulerian circuit in . Clearly, its span is twice the span of the tree.
4. Convert the Eulerian circuit of into a Hamiltonian cycle of in the following way: walk along , and
each time you are about to come into an already visited vertex, skip it and try to go to the next one (along ).
It is easy to prove that the last step works. Moreover, thanks to the triangle inequality, each skipping at Step 4 is in
fact a shortcut; i.e., the length of the cycle does not increase. Hence it gives us a TSP tour no more than twice as long
as the optimal one.
The Christofides algorithm follows a similar outline but combines the minimum spanning tree with a solution of
another problem, minimum-weight perfect matching. This gives a TSP tour which is at most 1.5 times the optimal.
The Christofides algorithm was one of the first approximation algorithms, and was in part responsible for drawing
attention to approximation algorithms as a practical approach to intractable problems. As a matter of fact, the term
"algorithm" was not commonly extended to approximation algorithms until later; the Christofides algorithm was
initially referred to as the Christofides heuristic.

Euclidean TSP
The Euclidean TSP, or planar TSP, is the TSP with the distance being the ordinary Euclidean distance.
The Euclidean TSP is a particular case of the metric TSP, since distances in a plane obey the triangle inequality.
Like the general TSP, the Euclidean TSP is NP-hard. With discretized metric (distances rounded up to an integer),
the problem is NP-complete.[16] However, in some respects it seems to be easier than the general metric TSP. For
example, the minimum spanning tree of the graph associated with an instance of the Euclidean TSP is a Euclidean
minimum spanning tree, and so can be computed in expected O(n log n) time for n points (considerably less than the
number of edges). This enables the simple 2-approximation algorithm for TSP with triangle inequality above to
operate more quickly.
In general, for any c > 0, where d is the number of dimensions in the Euclidean space, there is a polynomial-time
algorithm that finds a tour of length at most (1 + 1/c) times the optimal for geometric instances of TSP in
time; this is called a polynomial-time approximation scheme (PTAS).[17] Sanjeev Arora

and Joseph S. B. Mitchell were awarded the Gödel Prize in 2010 for their concurrent discovery of a PTAS for the
Euclidean TSP.
In practice, heuristics with weaker guarantees continue to be used.
Travelling salesman problem 145

Asymmetric TSP
In most cases, the distance between two nodes in the TSP network is the same in both directions. The case where the
distance from A to B is not equal to the distance from B to A is called asymmetric TSP. A practical application of an
asymmetric TSP is route optimisation using street-level routing (which is made asymmetric by one-way streets,
slip-roads, motorways, etc.).

Solving by conversion to symmetric TSP


Solving an asymmetric TSP graph can be somewhat complex. The following is a 3×3 matrix containing all possible
path weights between the nodes A, B and C. One option is to turn an asymmetric matrix of size N into a symmetric
matrix of size 2N.[18]

A B C

A 1 2

B 6 3

C 5 4

|+ Asymmetric path weights


To double the size, each of the nodes in the graph is duplicated, creating a second ghost node. Using duplicate points
with very low weights, such as −∞, provides a cheap route "linking" back to the real node and allowing symmetric
evaluation to continue. The original 3×3 matrix shown above is visible in the bottom left and the inverse of the
original in the top-right. Both copies of the matrix have had their diagonals replaced by the low-cost hop paths,
represented by −∞.

A B C A′ B′ C′

A −∞ 6 5

B 1 −∞ 4

C 2 3 −∞

A′ −∞ 1 2

B′ 6 −∞ 3

C′ 5 4 −∞

|+ Symmetric path weights


The original 3×3 matrix would produce two Hamiltonian cycles (a path that visits every node once), namely
A-B-C-A [score 9] and A-C-B-A [score 12]. Evaluating the 6×6 symmetric version of the same problem now
produces many paths, including A-A′-B-B′-C-C′-A, A-B′-C-A′-A, A-A′-B-C′-A [all score 9 – ∞].
The important thing about each new sequence is that there will be an alternation between dashed (A′,B′,C′) and
un-dashed nodes (A, B, C) and that the link to "jump" between any related pair (A-A′) is effectively free. A version of
the algorithm could use any weight for the A-A′ path, as long as that weight is lower than all other path weights
present in the graph. As the path weight to "jump" must effectively be "free", the value zero (0) could be used to
represent this cost—if zero is not being used for another purpose already (such as designating invalid paths). In the
two examples above, non-existent paths between nodes are shown as a blank square.
Travelling salesman problem 146

Benchmarks
For benchmarking of TSP algorithms, TSPLIB [19] is a library of sample instances of the TSP and related problems
is maintained, see the TSPLIB external reference. Many of them are lists of actual cities and layouts of actual printed
circuits.

Human performance on TSP


The TSP, in particular the Euclidean variant of the problem, has attracted the attention of researchers in cognitive
psychology. It is observed that humans are able to produce good quality solutions quickly. The first issue of the
Journal of Problem Solving [20] is devoted to the topic of human performance on TSP.

TSP path length for random pointset in a square


Suppose N points are randomly distributed in a 1×1 square with N>>1. Consider many such squares. Suppose we
want to know the average of the shortest path length (i.e. TSP solution) of each square.

Lower bound

is a lower bound obtained by assuming i be a point in the tour sequence and i has its next neighbor as its

latter in the path.

is a better lower bound obtained by assuming i's latter is i's next, and i's former is i's after next.

is an even better lower bound obtained by dividing the path sequence into two parts as before_i and behind_i

with each part containing N/2 points, and then deleting the before_i part to form a diluted pointset (see discussion).
• David S. Johnson[21] obtained a lower bound by computer experiment:
, where 0.522 comes from the points near square boundary which have fewer neighbors.
• Christine L. Valenzuela and Antonia J. Jones [22] obtained another lower bound by computer experiment:

Analyst's travelling salesman problem


There is an analogous problem in geometric measure theory which asks the following: under what conditions may a
subset E of Euclidean space be contained in a rectifiable curve (that is, when is there a curve with finite length that
visits every point in E)? This problem is known as the analyst's travelling salesman problem or the geometric
travelling salesman problem.

Free software for solving TSP


Travelling salesman problem 147

Name License API Brief info


(alphabetically) language

[23] free for only requires a linear solver installation for its MILP subproblem
Concorde
academic executable

[24] ? C an ANSI C implementation a dynamic programming based algorithm developed by Balas


DynOpt
and Simonetti, approximate solution

[25] research only C an effective implementation of the Lin-Kernighan heuristic for Euclidean traveling
LKH
salesman problem

OpenOpt BSD Python exact and approximate solvers, STSP / ATSP, can handle multigraphs, constraints,
[26]
multiobjective problems, see its TSP page for details and examples

[27] GPL R infrastructure and solvers for STSP / ATSP, interface to Concorde
R TSP package

TSP Solver and GPL C++ branch and bound algorithm


[28]
Generator

[29] ? C approximate solution of the STSP using the "pgapack" package


TSPGA

Popular culture
Travelling Salesman, by director Timothy Lanzone, is the story of 4 mathematicians hired by the U.S. government to
solve the most elusive problem in computer-science history: P vs. NP.

Notes
[1] "Der Handlungsreisende – wie er sein soll und was er zu tun hat, um Aufträge zu erhalten und eines glücklichen Erfolgs in seinen Geschäften
gewiß zu sein – von einem alten Commis-Voyageur" (The traveling salesman — how he must be and what he should do in order to get
comissions and be sure of the happy success in his business — by an old commis-voyageur)
[2] A discussion of the early work of Hamilton and Kirkman can be found in Graph Theory 1736–1936
[3] Cited and English translation in . Original German: "Wir bezeichnen als Botenproblem (weil diese Frage in der Praxis von jedem Postboten,
übrigens auch von vielen Reisenden zu lösen ist) die Aufgabe, für endlich viele Punkte, deren paarweise Abstände bekannt sind, den kürzesten
die Punkte verbindenden Weg zu finden. Dieses Problem ist natürlich stets durch endlich viele Versuche lösbar. Regeln, welche die Anzahl
der Versuche unter die Anzahl der Permutationen der gegebenen Punkte herunterdrücken würden, sind nicht bekannt. Die Regel, man solle
vom Ausgangspunkt erst zum nächstgelegenen Punkt, dann zu dem diesem nächstgelegenen Punkt gehen usw., liefert im allgemeinen nicht
den kürzesten Weg."
[4] A detailed treatment of the connection between Menger and Whitney as well as the growth in the study of TSP can be found in Alexander
Schrijver's 2005 paper "On the history of combinatorial optimization (till 1960). Handbook of Discrete Optimization (K. Aardal, G.L.
Nemhauser, R. Weismantel, eds.), Elsevier, Amsterdam, 2005, pp. 1–68. PS (http:/ / homepages. cwi. nl/ ~lex/ files/ histco. ps), PDF (http:/ /
homepages. cwi. nl/ ~lex/ files/ histco. pdf)
[5] http:/ / www. google. com/ patents/ US7054798
[6] , pp.308-309.
[7] Tucker, A. W. (1960), "On Directed Graphs and Integer Programs", IBM Mathematical research Project (Princeton University)
[8] Dantzig, George B. (1963), Linear Programming and Extensions, Princeton, NJ: PrincetonUP, pp. 545–7, ISBN 0-691-08000-3, sixth
printing, 1974.
[9] Berman & Karpinski (2006).
[10] , ,
[11] Work by David Applegate, AT&T Labs – Research, Robert Bixby, ILOG and Rice University, Vašek Chvátal, Concordia University,
William Cook, University of Waterloo, and Keld Helsgaun, Roskilde University is discussed on their project web page hosted by the
University of Waterloo and last updated in June 2004, here (http:/ / www. math. uwaterloo. ca/ tsp/ sweden/ )
[12] Johnson, D.S. and McGeoch, L.A.. "The traveling salesman problem: A case study in local optimization", Local search in combinatorial
optimization, 1997, 215-310
[13] S. S. Ray, S. Bandyopadhyay and S. K. Pal, "Genetic Operators for Combinatorial Optimization in TSP and Microarray Gene Ordering,"
Applied Intelligence, 2007, 26(3). pp. 183-195.
[14] A. B. Kahng and S. Reda, "Match Twice and Stitch: A New TSP Tour Construction Heuristic," Operations Research Letters, 2004, 32(6).
pp. 499–509. http:/ / dx. doi. org/ 10. 1016/ j. orl. 2004. 04. 001
Travelling salesman problem 148

[15] Marco Dorigo. Ant Colonies for the Traveling Salesman Problem. IRIDIA, Université Libre de Bruxelles. IEEE Transactions on
Evolutionary Computation, 1(1):53–66. 1997. http:/ / citeseer. ist. psu. edu/ 86357. html
[16] Papadimitriou (1977).
[17] Arora (1998).
[18] Roy Jonker and Ton Volgenant. "Transforming asymmetric into symmetric traveling salesman problems". Operations Research Letters
2:161–163, 1983.
[19] http:/ / comopt. ifi. uni-heidelberg. de/ software/ TSPLIB95/
[20] http:/ / docs. lib. purdue. edu/ jps/
[21] David S. Johnson (http:/ / www. research. att. com/ ~dsj/ papers/ HKsoda. pdf)
[22] Christine L. Valenzuela and Antonia J. Jones (http:/ / users. cs. cf. ac. uk/ Antonia. J. Jones/ Papers/ EJORHeldKarp/ HeldKarp. pdf)
[23] http:/ / www. math. uwaterloo. ca/ tsp/ concorde. html
[24] http:/ / www. andrew. cmu. edu/ user/ neils/ tsp/
[25] http:/ / www. akira. ruc. dk/ ~keld/ research/ LKH/
[26] http:/ / openopt. org/ TSP
[27] http:/ / tsp. r-forge. r-project. org/
[28] http:/ / tspsg. info/
[29] http:/ / www. rz. uni-karlsruhe. de/ ~lh71/

References
• Applegate, D. L.; Bixby, R. M.; Chvátal, V.; Cook, W. J. (2006), The Traveling Salesman Problem,
ISBN 0-691-12993-2.
• Arora, Sanjeev (1998), "Polynomial time approximation schemes for Euclidean traveling salesman and other
geometric problems", Journal of the ACM 45 (5): 753–782, doi: 10.1145/290179.290180 (http://dx.doi.org/10.
1145/290179.290180), MR  1668147 (http://www.ams.org/mathscinet-getitem?mr=1668147).
• Bellman, R. (1960), "Combinatorial Processes and Dynamic Programming", in Bellman, R., Hall, M., Jr. (eds.),
Combinatorial Analysis, Proceedings of Symposia in Applied Mathematics 10, American Mathematical Society,
pp. 217–249.
• Bellman, R. (1962), "Dynamic Programming Treatment of the Travelling Salesman Problem", J. Assoc. Comput.
Mach. 9: 61–63, doi: 10.1145/321105.321111 (http://dx.doi.org/10.1145/321105.321111).
• Berman, Piotr; Karpinski, Marek (2006), "8/7-approximation algorithm for (1,2)-TSP", Proc. 17th ACM-SIAM
Symposium on Discrete Algorithms (SODA '06), pp. 641–648, doi: 10.1145/1109557.1109627 (http://dx.doi.
org/10.1145/1109557.1109627), ISBN 0898716055, ECCC  TR05-069 (http://eccc.uni-trier.de/report/2005/
069/).
• Christofides, N. (1976), Worst-case analysis of a new heuristic for the travelling salesman problem, Technical
Report 388, Graduate School of Industrial Administration, Carnegie-Mellon University, Pittsburgh.
• Hassin, R.; Rubinstein, S. (2000), "Better approximations for max TSP", Information Processing Letters 75 (4):
181–186, doi: 10.1016/S0020-0190(00)00097-1 (http://dx.doi.org/10.1016/S0020-0190(00)00097-1).
• Held, M.; Karp, R. M. (1962), "A Dynamic Programming Approach to Sequencing Problems", Journal of the
Society for Industrial and Applied Mathematics 10 (1): 196–210, doi: 10.1137/0110015 (http://dx.doi.org/10.
1137/0110015).
• Kaplan, H.; Lewenstein, L.; Shafrir, N.; Sviridenko, M. (2004), "Approximation Algorithms for Asymmetric TSP
by Decomposing Directed Regular Multigraphs", In Proc. 44th IEEE Symp. on Foundations of Comput. Sci,
pp. 56–65.
• Kosaraju, S. R.; Park, J. K.; Stein, C. (1994), "Long tours and short superstrings'", Proc. 35th Ann. IEEE Symp. on
Foundations of Comput. Sci, IEEE Computer Society, pp. 166–177.
• Orponen, P.; Mannila, H. (1987), "On approximation preserving reductions: Complete problems and robust
measures'", Technical Report C-1987–28, Department of Computer Science, University of Helsinki.
• Padberg, M.; Rinaldi, G. (1991), "A Branch-and-Cut Algorithm for the Resolution of Large-Scale Symmetric
Traveling Salesman Problems", Siam Review: 60–100, doi: 10.1137/1033004 (http://dx.doi.org/10.1137/
1033004).
Travelling salesman problem 149

• Papadimitriou, Christos H. (1977), "The Euclidean traveling salesman problem is NP-complete", Theoretical
Computer Science 4 (3): 237–244, doi: 10.1016/0304-3975(77)90012-3 (http://dx.doi.org/10.1016/
0304-3975(77)90012-3), MR  0455550 (http://www.ams.org/mathscinet-getitem?mr=0455550).
• Papadimitriou, C. H.; Yannakakis, M. (1993), "The traveling salesman problem with distances one and two",
Math. Oper. Res. 18: 1–11, doi: 10.1287/moor.18.1.1 (http://dx.doi.org/10.1287/moor.18.1.1).
• Serdyukov, A. I. (1984), "An algorithm with an estimate for the traveling salesman problem of the maximum'",
Upravlyaemye Sistemy 25: 80–86.
• Woeginger, G.J. (2003), "Exact Algorithms for NP-Hard Problems: A Survey", Combinatorial Optimization –
Eureka, You Shrink! Lecture notes in computer science, vol. 2570, Springer, pp. 185–207.

Further reading
• Adleman, Leonard (1994), "Molecular Computation of Solutions To Combinatorial Problems" (http://www.usc.
edu/dept/molecular-science/papers/fp-sci94.pdf), Science 266 (5187): 1021–4, Bibcode: 1994Sci...266.1021A
(http://adsabs.harvard.edu/abs/1994Sci...266.1021A), doi: 10.1126/science.7973651 (http://dx.doi.org/
10.1126/science.7973651), PMID  7973651 (http://www.ncbi.nlm.nih.gov/pubmed/7973651)
• Arora, S. (1998), "Polynomial time approximation schemes for Euclidean traveling salesman and other geometric
problems" (http://graphics.stanford.edu/courses/cs468-06-winter/Papers/arora-tsp.pdf), Journal of the ACM
45 (5): 753–782, doi: 10.1145/290179.290180 (http://dx.doi.org/10.1145/290179.290180).
• Babin, Gilbert; Deneault, Stéphanie; Laportey, Gilbert (2005), Improvements to the Or-opt Heuristic for the
Symmetric Traveling Salesman Problem (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.89.
9953), Cahiers du GERAD, G-2005-02, Montreal: Group for Research in Decision Analysis.
• Cook, William (2011), In Pursuit of the Travelling Salesman: Mathematics at the Limits of Computation,
Princeton University Press, ISBN 978-0-691-15270-7.
• Cook, William; Espinoza, Daniel; Goycoolea, Marcos (2007), "Computing with domino-parity inequalities for the
TSP", INFORMS Journal on Computing 19 (3): 356–365, doi: 10.1287/ijoc.1060.0204 (http://dx.doi.org/10.
1287/ijoc.1060.0204).
• Cormen, T. H.; Leiserson, C. E.; Rivest, R. L.; Stein, C. (2001), "35.2: The traveling-salesman problem",
Introduction to Algorithms (2nd ed.), MIT Press and McGraw-Hill, pp. 1027–1033, ISBN 0-262-03293-7.
• Dantzig, G. B.; Fulkerson, R.; Johnson, S. M. (1954), "Solution of a large-scale traveling salesman problem",
Operations Research 2 (4): 393–410, doi: 10.1287/opre.2.4.393 (http://dx.doi.org/10.1287/opre.2.4.393),
JSTOR  166695 (http://www.jstor.org/stable/166695).
• Garey, M. R.; Johnson, D. S. (1979), "A2.3: ND22–24", Computers and Intractability: A Guide to the Theory of
NP-Completeness, W.H. Freeman, pp. 211–212, ISBN 0-7167-1045-5.
• Goldberg, D. E. (1989), "Genetic Algorithms in Search, Optimization & Machine Learning", Reading:
Addison-Wesley (New York: Addison-Wesley), Bibcode: 1989gaso.book.....G (http://adsabs.harvard.edu/abs/
1989gaso.book.....G), ISBN 0-201-15767-5.
• Gutin, G.; Yeo, A.; Zverovich, A. (2002), "Traveling salesman should not be greedy: domination analysis of
greedy-type heuristics for the TSP", Discrete Applied Mathematics 117 (1–3): 81–86, doi:
10.1016/S0166-218X(01)00195-0 (http://dx.doi.org/10.1016/S0166-218X(01)00195-0).
• Gutin, G.; Punnen, A. P. (2006), The Traveling Salesman Problem and Its Variations, Springer,
ISBN 0-387-44459-9.
• Johnson, D. S.; McGeoch, L. A. (1997), "The Traveling Salesman Problem: A Case Study in Local
Optimization", in Aarts, E. H. L.; Lenstra, J. K., Local Search in Combinatorial Optimisation, John Wiley and
Sons Ltd, pp. 215–310.
• Lawler, E. L.; Lenstra, J. K.; Rinnooy Kan, A. H. G.; Shmoys, D. B. (1985), The Traveling Salesman Problem: A
Guided Tour of Combinatorial Optimization, John Wiley & Sons, ISBN 0-471-90413-9.
Travelling salesman problem 150

• MacGregor, J. N.; Ormerod, T. (1996), "Human performance on the traveling salesman problem" (http://www.
psych.lancs.ac.uk/people/uploads/TomOrmerod20030716T112601.pdf), Perception & Psychophysics 58 (4):
527–539, doi: 10.3758/BF03213088 (http://dx.doi.org/10.3758/BF03213088).
• Mitchell, J. S. B. (1999), "Guillotine subdivisions approximate polygonal subdivisions: A simple polynomial-time
approximation scheme for geometric TSP, k-MST, and related problems" (http://citeseer.ist.psu.edu/622594.
html), SIAM Journal on Computing 28 (4): 1298–1309, doi: 10.1137/S0097539796309764 (http://dx.doi.org/
10.1137/S0097539796309764).
• Rao, S.; Smith, W. (1998), "Approximating geometrical graphs via 'spanners' and 'banyans'", Proc. 30th Annual
ACM Symposium on Theory of Computing, pp. 540–550.
• Rosenkrantz, Daniel J.; Stearns, Richard E.; Lewis, Philip M., II (1977), "An Analysis of Several Heuristics for
the Traveling Salesman Problem", SIAM Journal on Computing 6 (5): 563–581, doi: 10.1137/0206041 (http://
dx.doi.org/10.1137/0206041).
• Vickers, D.; Butavicius, M.; Lee, M.; Medvedev, A. (2001), "Human performance on visually presented traveling
salesman problems", Psychological Research 65 (1): 34–45, doi: 10.1007/s004260000031 (http://dx.doi.org/
10.1007/s004260000031), PMID  11505612 (http://www.ncbi.nlm.nih.gov/pubmed/11505612).
• Walshaw, Chris (2000), A Multilevel Approach to the Travelling Salesman Problem, CMS Press.
• Walshaw, Chris (2001), A Multilevel Lin-Kernighan-Helsgaun Algorithm for the Travelling Salesman Problem,
CMS Press.

External links
• Traveling Salesman Problem (http://www.math.uwaterloo.ca/tsp/index.html) at University of Waterloo
• TSPLIB (http://www.iwr.uni-heidelberg.de/groups/comopt/software/TSPLIB95/) at the University of
Heidelberg
• Traveling Salesman Problem (http://demonstrations.wolfram.com/TravelingSalesmanProblem/) by Jon
McLoone at the Wolfram Demonstrations Project
• Source code library for the travelling salesman problem (http://www.adaptivebox.net/CILib/code/
tspcodes_link.html)
• TSP solvers in R (http://tsp.r-forge.r-project.org/) for symmetric and asymmetric TSPs. Implements various
insertion, nearest neighbor and 2-opt heuristics and an interface to Concorde and Chained Lin-Kernighan
heuristics.
• Traveling Salesman movie (on IMDB) (http://www.imdb.com/title/tt1801123/)
• " Traveling Salesman in Python and Linear Optimization, IBM Developerworks with Source Code (http://www.
ibm.com/developerworks/cloud/library/cl-optimizepythoncloud1/index.html)" by Noah Gift
Article Sources and Contributors 151

Article Sources and Contributors


Tree (graph theory)  Source: https://en.wikipedia.org/w/index.php?oldid=585944353  Contributors: 3D-Grabber, Adking80, Algebraist, Almit39, Altenmann, Andreas Kaufmann, Andrevan,
Angr, Anna Lincoln, Anonymous Dissident, Arunkumar, AxelBoldt, Battamer, Baudway, Booyabazooka, Breno, Bryan Derksen, Charles Matthews, Cwkmail, Dagh, David Eppstein, Dbenbenn,
Debivort, Dominus, Dysprosia, Edelac, Edemaine, Ezrakilty, Gadykozma, George100, Giftlite, Gurch, Hermel, Hippophaë, Hyad, Illia Connell, Inhumandecency, Ioakar, JP.Martin-Flatin,
Jaredwf, Jitse Niesen, Jon Awbrey, Josh Cherry, Justin W Smith, Karl-Henner, Knutux, Kuszi, Kzollman, Lambiam, Linas, Lssilva, MSGJ, Marsupilcoalt, MathMartin, Maxal, McKay, Mdd,
Michael Hardy, Michael Slone, Miguel, Mihirpmehta, Mikhail Dvorkin, Miym, N4nojohn, Naerbnic, Nbarth, Neilc, Nguyen Thanh Quang, Obradovic Goran, OdedSchramm, Patrick, Paul
August, PaulTanenbaum, Philippe Giabbanelli, Philly jawn, PhotoBox, Physis, Pjvpjv, Policron, Poor Yorick, Purdygb, R'n'B, RaitisMath, Rp, Salgueiro, Salix alba, Sms97, Sori, Spinningspark,
StaticVision, Sternkampf, Telfordbuck, Timwi, Tizio, Tobias Bergemann, Topology Expert, Tosha, TricksterWolf, Trovatore, Twri, Uv1234, Vanished User 0001, Victor Yus, Wrstark, XJaM,
Xcelerate, Xnn, Yaris678, Ylloh, Zarvok, Zaslav, Zero0000, ‫ﺳﻌﯽ‬, 98 anonymous edits

Binary tree  Source: https://en.wikipedia.org/w/index.php?oldid=585749649  Contributors: 15turnsm, 209.157.137.xxx, 7, ABCD, Aarsalankhalid, Abu adam, Adam majewski, Adijk,
Ahoerstemeier, Ahy1, Airplaneman, Alex.vatchenko, Alienus, Allefant, Altenmann, Andreas Kaufmann, AndrewKepert, AnotherPerson 2, Antaeus Feldspar, Aroundthewayboy, Ateeq.s, B4hand,
Behrangsa, Beland, Belovedeagle, Bender2k14, Bhadani, BigDunc, Biplavd IIC, Bkell, BlckKnght, Bluebusy, Bobo192, Bojan1989, Bonadea, Brentsmith101, Brianga, Bruce1ee, Caltas, Calvin
zcx, Card Zero, Cbraga, Cdiggins, Charles Matthews, Chicodroid, Chris the speller, Chris857, ChrisGualtieri, Ck lostsword, Classicalecon, Cncplyr, Coelacan, Conversion script, Cybercobra,
Cyhawk, Czar.pino, DMacks, Daira Hopwood, Darangho, David Eppstein, David Shay, Dawynn, Dcoetzee, Dguido, Djcollom, Dkasak, Dominus, Dontdoit, Doriftu, Doug s, Dr. Sunglasses,
Drano, DuaneBailey, Duoduoduo, Dysprosia, Ekeb, Encognito, Ferkelparade, Fraggle81, Frankrod44, Frozendice, FvdP, Garyzx, Gdevanla, Giftlite, Gilliam, Gimmetrow, Graham87, Gsmodi,
Happyuk, Hazmat2, Heirpixel, ISTB351, IanS1967, Ilana, Itchy Archibald, JabberWok, Jafet, Jean-Baptiste Mestelan, Jerome Charles Potts, Jerryobject, JimsMaher, Joery2210, John Quincy
Adding Machine, Jonfore, Jonson22, Josell2, Josephskeller, Jprg1966, Jruderman, Julesd, Julierr, Justin W Smith, Kamath.nakul, Kamirao, KaragouniS, Kbolino, Kgashok, Kgautam28,
Klilidiplomus, Krishna.91, Kuru, LC, LandruBek, Liao, Liftarn, LightningDragon, Linas, LithiumBreather, Loisel, LokiClock, Lone boatman, Loool, Loopwhile1, Lotje, MONGO,
Mahahahaneapneap, Malleus Fatuorum, Marc van Leeuwen, Mark Renier, Martinp23, Materialscientist, MathijsM, Matir, Maurice Carbonaro, Mbdeir, Mboverload, Mcld, Mdd, Mdnahas,
Metricopolus, Mhayes46, Michael Angelkovich, Michael Hardy, Michael Slone, Microbizz, Mike Christie, Minesweeper, Mjm1964, Mrwojo, Nbarth, Neomagic100, Nippoo, Noldoaran,
Nonexistent, Oblivious, Oli Filth, Ontariolot, Opelio, Orphic, Otterdam, ParticleMan, Petrb, Pgk, Philip Trueman, Pit, Pohl, Pp007, Ppelleti, Pratyya Ghosh, RG2, RadioFan, Rahulgatm, Rege,
Reinderien, Rhanekom, Rich Farmbrough, Rohitgoyal100, Roreuqnoc, Roybristow, Rspeer, Ryuunoshounen, Rzelnik, Rōnin, SGBailey, Sapeur, Seaphoto, Shentino, Shinjixman, Shmomuffin,
Shoujun, Silver hr, Simeon, Smallpond, SmartGuy Old, Someone else, SpaceFlight89, Spottedowl, Srchvrs, Sss41, Stickee, Sun Creator, Taemyr, TakuyaMurata, Tarquin, Tarrahatikas, Tdhsmith,
The Thing That Should Not Be, Thrapper, Vegaswikian, Vegpuff, WadeSimMiser, Waggers, Wavelength, Widr, WillNess, Wtarreau, Wælgæst wæfre, XJaM, Xevior, Xnn, Ynhockey,
Yuubinbako, Zero sharp, Zetawoof, Zipdisc, Zvn, 509 anonymous edits

Binary search tree  Source: https://en.wikipedia.org/w/index.php?oldid=586993210  Contributors: 4get, Abednigo, Abu adam, Adamuu, AgentSnoop, Ahy1, AlanSherlock, Alansohn, Alexsh,
Allan McInnes, Andreas Kaufmann, Anoopjohnson, Avermapub, Awu, BAxelrod, BPositive, Banaticus, Beetstra, Bernard François, Bkell, Booyabazooka, Bryan Derksen, Burakov, Butros,
CRGreathouse, Calbaer, Capricorn42, Casted, Cedar101, Chery, Cochito, Conversion script, Cybdestroyer, Cybercobra, Cyc115, D6, Damian Yerrick, Dan Wang, Danadocus, Dcoetzee,
DevastatorIIC, Dicklyon, Dimchord, Djcollom, Dk1027, Doctordiehard, Doradus, Dysprosia, Dzikasosna, Ecb29, Embanner, Enochlau, Evil Monkey, Ezhiki, Farazbhinder, Frankrod44, Fredrik,
Func, GRHooked, Gaius Cornelius, Garoth, Giftlite, Glenn, Googl, Gorffy, GregorB, Grunt, Hadal, Ham Pastrami, Hathawayc, Havardk, Hdanak, Hu12, Hyad, IgushevEdward, Ilana, Ivan
Kuckir, Ixfd64, JForget, Jabanabba, James pic, Jayaneethatj, Jdm64, Jdurham6, Jerryobject, Jianhui67, Jihlim, Jin, JingguoYao, Jms49, Jogers, Josell2, Joshua Issac, Kaidul, Karl-Henner, Kate,
Kewlito, Konnetikut, Konstantin Pest, Kragen, Kulp, Kurapix, LOL, Lanov, Liao, LilHelpa, LittleDan, Loren.wilton, Madhan virgo, Matekm, Mathgorges, MatrixFrog, Maximaximax, Maximus
Rex, Mb1000, MclareN212, MegaHasher, Metalmax, Mgius, Michael Hardy, Michael Slone, Mikeputnam, Mindmatrix, Minesweeper, MladenWiki, Moe Epsilon, MrOllie, MrSomeone, Mrwojo,
Mtahmed, Mweber, Nakarumaka, Nerdgerl, Neurodivergent, Nils schmidt hamburg, Nixdorf, Nneonneo, Nomen4Omen, Nux, Ohnoitsjamie, Oleg Alexandrov, Oli Filth, Oliphaunt, One half
3544, Oni Lukos, Onomou, Ontariolot, Oskar Sigvardsson, P0nc, Phil Boswell, PhilipMW, Phishman3579, Pion, Postdlf, Qiq, Qleem, Quuxplusone, Qwertyus, RJK1984, Rafikamal, Rdemar,
Regnaron, Rhanekom, Richardj311, Rolpa, RoySmith, Rudo.Thomas, Ruud Koot, Ryuunoshounen, S3000, SPTWriter, Salrizvy, SchumacherTechnologies, Shailen.sobhee, Shen, Shmomuffin,
Shuri org, Sketch-The-Fox, Skier Dude, Smallman12q, Solsan88, Spadgos, Spiff, Sss41, SteveAyre, Super fish2, Svick, Swapsy, Taejo, Taw, Tbvdm, The Parting Glass, TheMandarin,
Theone256, Thesevenseas, Theta4, Timwi, Tobias Bergemann, Tomt22, TrainUnderwater, Trevor Andersen, VKokielov, Vdm, Vectorpaladin13, Vocaro, Vromascanu, Wavelength, WikHead,
WikiWizard, WillNess, Wmayner, Wtarreau, Wtmitchell, Wulfskin, X1024, Xevior, Yaderbh, Your Lord and Master, ZeroOne, ‫ﻣﺎﻧﻲ‬, 405 anonymous edits

Infix notation  Source: https://en.wikipedia.org/w/index.php?oldid=578343640  Contributors: Andreas Kaufmann, AxelBoldt, Beao, Beisteel, Blahedo, Charles Matthews, Chris Q, Damian
Yerrick, Docu, FiP, Frietjes, Georg Peter, GermanX, Gesslein, Hyacinth, Jeandré du Toit, Jncraton, Kbk, Krischik, MC10, Michael Hardy, Obradovic Goran, Patrick, Salsa Shark, SimpleBeep,
Spayrard, Stereo, TechControl, Tom harrison, VKokielov, Warnet, Wernher, Wik, Wikicgc, Woohookitty, Zumbo, 39 anonymous edits

Complete graph  Source: https://en.wikipedia.org/w/index.php?oldid=580983368  Contributors: 16@r, 4, Alpt, Andre Engels, AnonMoos, Anton Khorev, Arthur Rubin, AxelBoldt, Bact,
Bender2k14, BiT, Booyabazooka, CRGreathouse, ChrisGualtieri, Coe McSweet, Cool3, DHN, DVdm, Da Joe, David Eppstein, Dbenbenn, Dcoetzee, Dominus, DoostdarWKP, Dysprosia,
EdPeggJr, Ejrh, El Roih, Fred Bradstadt, Fridemar, Gabbe, Gcm, GermanX, Giftlite, Gustavb, HaeB, Howard McCay, Imran, Incnis Mrsi, Isnow, Kelson, Knutux, KoenDelaere, Koko90, Kostmo,
Lupin, Mailer diablo, Maksim-e, MathMartin, McKay, Mgreenbe, Michael Hardy, Mindmatrix, Nyttend, Obradovic Goran, Omnipaedista, PierreAbbat, Professor Fiendish, R. J. Mathar,
Radagast3, Salix alba, Thehotelambush, Tomo, Tomruen, Twri, Wild Lion, WojciechSwiderski, XJaM, Yecril, நெடுஞ்செழியன், 36 anonymous edits

Polish notation  Source: https://en.wikipedia.org/w/index.php?oldid=575709483  Contributors: 8ty3hree, A D Monroe III, Ahoerstemeier, Akumiszcza, Al Lemos, AlanUS, Altenmann, Andreas
Kaufmann, Anton Mravcek, Appleseed, Awis, Awotter, BD2412, Beao, Big Bob the Finder, Bkell, Blahedo, Bmckeeman, Bobo192, Borromean-ring, Centrx, Charles Matthews, Charliearcuri,
Chowbok, Chris Q, Classicalecon, Conversion script, Cybercobra, DHN, Dan Gluck, Darolew, David Edgar, Dicklyon, Dominus, Dougher, Drpratten, Dtwong, Dysprosia, Ed Poor,
Elhalconingles, EmilJ, Emperorbma, FiP, Filemon, Francvs, Frietjes, Furby100, Furrykef, Giftlite, Gregbard, Hammertime, Heirpixel, Injinera, InsérerNombreHere, Isacdaavid, Its me Abdul,
Jgroenen, Jibjibjib, Joe9320, Listowy, LokiClock, Ludvikus, Martin Hampl, Masterzora, Michael Hardy, Mo-Al, Moskvax, Msh210, NSHSedit, NeilN, Newbyguesses, Ninly, Obradovic Goran,
PGSONIC, Pernambuko, Phoenix-forgotten, Piotrus, Radagast3, Rajakhr, Romanc19s, Ruakh, Shawn K. Quinn, Sintau.tayua, Sopoforic, Spug, Spyzzj, Stephen Morley, Swolowski, Szopen,
Tabletop, Theacademe, Thumperward, Thv, Tietew, Tobias Bergemann, Tom harrison, Turgidson, VKokielov, Vilcxjo, Waldir, Walkie, Warnet, Wdscxsj, Wikiklrsc, Ztobor, 102 anonymous
edits

Reverse Polish notation  Source: https://en.wikipedia.org/w/index.php?oldid=586270973  Contributors: 212.25.85.xxx, Aarondigital, Ahankinson, Ahoerstemeier, Akldb, Akumiszcza, Al
Lemos, Alan012, Alexswilliams, Algebra, Andreas Kaufmann, Anocito, Anomie, Anton Mravcek, Arch dude, Arda Xi, Arvindn, Atlant, Autocorrelation, Autopilot, AxelBoldt, Badger Drink,
Bbrownspsu, Beao, BenBaker, Bfinn, Blahedo, Blainster, Blazar, Booyabazooka, Brandon, Breadmanpaul, BryanMWoods, Btx40, Bug, Calliopejen1, Caracho, Cfinseth, Charles Gaudette,
Charles Matthews, CharlesBarouch, Christian List, Chronodm, Classicalecon, Clementi, Clotungas, Colonel panic, Conversion script, Count Coolio, CyberSkull, Cybercobra, Cyhatch, Damezi,
Damian Yerrick, Danroa, Dave Bass, David Eppstein, David Haslam, Dead Horsey, Derek Ross, Dicklyon, DivisionByZero, Djfly, Dlrohrer2003, Dmharvey, Dnalor, DougsTech, Dr.K.,
Drpratten, Ds13, Długosz, Echosmoke, Egbert35, Ehrenkater, Epistemenical, Ergasiophobiac, Eritain, Ethefor, Everyking, Frap, Frietjes, Fsch0203, Gadfium, Gc9580, Gecg, Ghettoblaster,
Giftlite, GrahamN, Graue, Grue, Guido del Confuso, Haji akhundov, Happyuk, Helikophis, HelloAnnyong, Hes Nikke, Homerjay, Hydrargyrum, Ian Joyner, Inquam, InvaderJim42, Ioioyoo,
Iridescent, J.delanoy, Jack Merridew, Jafet, Jalwikip, JamesHenstridge, Jan1nad, Jeffrey Smith, Jfmantis, Jgade, JhanCRUSH, Jim10701, Jitse Niesen, Joe Cetina, Joel D. Reid, JorgePeixoto, Jose
Ramos, Joshua Issac, Jrlevine, Jwdietrich2, KIAaze, Karl Palmen, Keenan Pepper, Kenyon, Khazar2, Kisss256, Kmmertes, Kraenar, Krischik, Kwamikagami, LMB, LOL, Laocoön11, Le Nordiq,
Leonard G., Leszek Jańczuk, Levin, LilHelpa, Linkato1, Listowy, Loadmaster, Lone boatman, Lovibond, Lserni, LucasVB, Luther93, Lysdexia, M5, MC10, MER-C, MRPPA, MagnusA,
Makushline, Malveira, Marcusyoder, Matthiaspaul, Mcculley, Mclean007, Meertn, MementoVivere, Mercy11, Michael Hardy, Michael miceli, MichaelHaeckel, Mike65535, Mikeblas,
Mikeeg555, Miko3k, Mikus, Minna Sora no Shita, Mlpkr, Mogism, Mohitgupta.ynr, MrVooWho, N2e, Nitroboon, Norm mit, Nutmegmagi, Obradovic Goran, Overthinkingly, Patrick, Paul D.
Anderson, Pernambuko, Perpenso, PhilipMW, Phys, Pilaf, Prakash Nadkarni, Preslethe, Quibik, R'n'B, Rgrg, Rich Janis, Rjwilmsi, Rybuns, Saffy21, Salix alba, Sam Hocevar, Samrolken,
Scorchsaber, Scs, Sekky, Serasuna, Shreyasonline, Smyth, Soap, Stromaric, Stualden, SuperTails92, Svick, Swpb, Syzdek, Szopen, Tarquin, The Wordsmith, The pudding,
TheIncredibleEdibleOompaLoompa, Thumperward, TimBentley, Tobias Bergemann, Tom harrison, Uirauna, UnKnown X, Uriyan, Uruiamme, Wendy.krieger, Wernher, Whaa?, Wikiklrsc,
WiseWoman, WolfgangRieger, X!, Xanzzibar, Zooterkin, 370 anonymous edits

Self-balancing binary search tree  Source: https://en.wikipedia.org/w/index.php?oldid=558782822  Contributors: ABCD, ActuallyRationalThinker, Alansohn, Andreas Kaufmann,
Andreas4965, Angela, Baluba, Bluebusy, CRGreathouse, Chochopk, Cybercobra, DJ Clayworth, David Eppstein, Dcoetzee, Diberri, Drilnoth, Dysprosia, Enochlau, Fredrik, Funandtrvl, G PViB,
Intgr, Jacob grace, Jacobolus, Jafet, Japanese Searobin, Jeltz, Jochen Burghardt, Jon Awbrey, Jorge Stolfi, Josell2, Jruderman, Kdau, Lamro, Larkinzhang1993, Larrycz, Light current, Mdd,
Michael Hardy, Momet, Moskvax, MrDrBob, Neilc, Noldoaran, Pgan002, Plastikspork, Plyd, Qwertyus, RJFJR, Ripe, Shlomif, Sriganeshs, Steaphan Greene, SteveJothen, Widefox, Wolfgang42,
Wolfkeeper, 53 anonymous edits

AVL tree  Source: https://en.wikipedia.org/w/index.php?oldid=586909199  Contributors: Adamd1008, Adamuu, Aent, Agrawalyogesh, Akerbos, Alex Kapranoff, AlexGreat, Altenmann, Anant
sogani, Andreas Kaufmann, Andrew Weintraub, Apanag, Astral, Auntof6, Avicennasis, Aviggiano, Axe-Lander, BenBac, Benoit fraikin, Binnacle, Bkil, Blackllotus, BlckKnght, Bluebusy,
Byrial, Castorvx, Caviare, ChrisGualtieri, ChrisMP1, Codingrecipes, Compusense, Conversion script, CostinulAT, Cybercobra, Cyhawk, Daewoollama, Damian Yerrick, Danielx, Darangho,
David.Mestel, Dawynn, Dcamp314, Dcoetzee, Denisarona, Devsathish, Dicklyon, DmitriyVilkov, Docboat, Doradus, Drilnoth, Dtrebbien, Dysprosia, Eeb379, Eleuther, Enviroboy, Epachamo,
Article Sources and Contributors 152

Euchiasmus, Evil Monkey, Flyingspuds, Fredrik, Funandtrvl, FvdP, G PViB, G0gogcsc300, Gaius Cornelius, Geek84, Geoff55, Gnowor, Graham87, Greenrd, Greg Tyler, Gruu, Gulliveig, Gurch,
Gökhan, II MusLiM HyBRiD II, Iamnitin, Ichimonji10, Infrogmation, Intgr, InverseHypercube, J.delanoy, Jac16888, Jeepday, Jeff02, Jennavecia, Jim1138, Jirka6, Jll, Jochen Burghardt,
Joeyadams, Josell2, Jpopesculian, KGasso, Kain2396, Kdau, Kenyon, Kingpin13, Kjkolb, Ksulli10, Kukolar, Kuru, Kushalbiswas777, LOL, Lankiveil, Larry V, Leszek Jańczuk, Leuko, M,
MarkHeily, Materialscientist, MattyIX, Mauritsmaartendejong, Mckaysalisbury, Mellerho, Merovingian, Michael Hardy, Michael M Clarke, Michael miceli, Mike Rosoft, Mikm, Minesweeper,
Mjkoo, MladenWiki, Mnogo, Moberg, Mohammad ahad, Momet, Mr.Berna, Msanchez1978, Mtanti, Mzruya, NawlinWiki, Neilc, Nguyễn Hữu Dung, Nixdorf, Nnemo, Noldoaran,
Nomen4Omen, Northamerica1000, Nysin, Obradovic Goran, Oleg Alexandrov, Oliversisson, Ommiy-Pangaeus, Orimosenzon, Paul D. Anderson, Pavel Vozenilek, Pedrito, Pgan002, Philvarner,
Phishman3579, Pnorcks, Poor Yorick, Ppkhoa, RJFJR, Ravitkhurana, Resper, Rockslave, Ruud Koot, ST47, Safety Cap, Sebculture, Seyen, Shlomif, Shmomuffin, Smalljim, Srivesh, Ste4k,
Svick, Tamfang, Tide rolls, Titodutta, Tobias Bergemann, Toby Douglass, Topbanana, Tphyahoo, Tsemii, Tsoft, UnwashedMeme, Uw.Antony, Vektor330, Vlad.c.manea, West.andrew.g, Xevior,
Yelnatz, Yksyksyks, Zian, 410 anonymous edits

B-tree  Source: https://en.wikipedia.org/w/index.php?oldid=586794045  Contributors: 128.139.197.xxx, ABCD, AaronSw, Abrech, Aednichols, Ahy1, Alansohn, Alaric, Alfalfahotshots,
AlistairMcMillan, Altenmann, Altes, AlyM, Anakin101, Anders Kaseorg, Andreas Kaufmann, Andytwigg, AnnaFrance, Antimatter15, Appoose, Aubrey Jaffer, Avono, BAxelrod, Battamer,
Beeson, Betzaar, Bezenek, Billinghurst, Bkell, Bladefistx2, Bor4kip, Bovineone, Bryan Derksen, Btwied, CanadianLinuxUser, Carbuncle, CarlFeynman, Cbraga, Chadloder, Charles Matthews,
Chmod007, Chris the speller, CiaPan, Ciphergoth, Ck lostsword, ContivityGoddess, Conversion script, Cp3149, Ctxppc, Curps, Cutter, Cybercobra, Daev, DanielKlein24, Dcoetzee, Decrease789,
Dlae, Dmn, Don4of4, Dpotter, Dravecky, Dysprosia, EEMIV, Ed g2s, Eddvella, Edward, Ethan, Fabriciodosanjossilva, FatalError, Fgdafsdgfdsagfd, Flying Bishop, Fragglet, Fredrik,
FreplySpang, Fresheneesz, FvdP, Gdr, Giftlite, Glrx, GoodPeriodGal, Ham Pastrami, Hao2lian, Hariva, Hbent, Headbomb, Hobsonlane, I do not exist, Inquisitus, Iohannes Animosus, JCLately,
JWSchmidt, Jacosi, Jeff Wheeler, Jirka6, Jjdawson7, Joahnnes, Joe07734, John ch fr, John lindgren, John of Reading, Jojalozzo, Jorge Stolfi, JoshuSasori, Jy00912345, Kate, Ketil, Kinema,
Kinu, Knutux, Kovianyo, Kpjas, Kukolar, Lamdk, Lee J Haywood, Leibniz, Levin, Lfstevens, Liotier, Lklundin, Loadmaster, Lsmll, Luna Santin, M Murphy1993, MIT Trekkie, MachineRebel,
Makkuro, Malbrain, Matttoothman, Merit 07, Meurondb, Mfwitten, Mhss, Michael Angelkovich, Michael Hardy, Mikeblas, Mindmatrix, Minesweeper, Mnogo, MoA)gnome, MorgothX, Mrnaz,
Mrwojo, NGPriest, Nayuki, Neilc, Nishantjr, Noodlez84, Norm mit, Nyq, Oldsharp, Oli Filth, P199, PKT, Patmorin, Paushali, Peter bertok, Pgan002, Phishman3579, Postrach, Priyank bolia,
PrologFan, Psyphen, Ptheoch, Pyschobbens, Quadrescence, Qutezuce, R. S. Shaw, RMcPhillip, Redrose64, Rich Farmbrough, Rp, Rpajares, Ruud Koot, Sandeep.a.v, Sandman@llgp.org,
Seanhalle, SickTwist, Simon04, SirSeal, Slady, Slike, Spiff, Ssbohio, Stephan Leclercq, Stevemidgley, Strake, Ta bu shi da yu, Talldean, Teles, Tentinator, The Fifth Horseman, Tjdw, Tobias
Bergemann, TomYHChan, Trusilver, Tuolumne0, Twimoki, Uday, Uw.Antony, Verbal, Vyroglyph, Wantnot, Widr, WinampLlama, Wipe, Wkailey, Wolfkeeper, Wout.mertens, Wsloand,
Wtanaka, Wtmitchell, Yakushima, Zearin, 453 anonymous edits

Heap (data structure)  Source: https://en.wikipedia.org/w/index.php?oldid=585540611  Contributors: 209.157.137.xxx, 28421u2232nfenfcenc, AaronEmi, Abdull, Ablonus, Abu adam, Ahy1,
Alwar.sumit, AncientPC, Andre.holzner, Andreas Kaufmann, Apokrif, Aragorn2, Ascánder, Atkinson 291, B4hand, B7j0c, BMB, Beland, Bender2k14, Bluebusy, Boleslav Bobcik, Bradgib,
Burakov, C7protal, Cannona, Charles Matthews, Chipchap, Chris857, ChrisGualtieri, Christian List, Christian75, Clevera, Cobi, Conversion script, Cool 1 love, Cybercobra, Daira Hopwood,
DarkPhoenix, David Eppstein, DavidCary, DavidHarkness, Dcoetzee, Derbeth, Derek Ross, Dfeuer, Doradus, DrBob, Drpaule, Dysprosia, Eric Le Bigot, Ermishin, Flamholz, Frecklefoot,
Fredrik, Fresheneesz, Garyzx, Giftlite, Ginsuloft, Graham87, Grubbiv, Gruntler, Guy Harris, Hadi Payami, Ham Pastrami, Hari6389, HereToHelp, Hiiiiiiiiiiiiiiiiiiiii, Hupili, Ilyathemuromets,
ImPerfection, Incompetence, Inhumandecency, Iron Wallaby, Itmozart, Jafet, Jaseemabid, Jfroelich, Jim1138, Jimfbleak, Jirka6, Jll, Jmnbatista, Jogloran, Jurohi, Ketil3, Kibiru, Konryd, Kragen,
Krun, Kukolar, LC, LOL, LeoNerd, Loadmaster, Mahanga, Manizzle23, Mark Renier, Max sang, Mcmlxxxi, Mdd, Mediator Scientiae, Mellum, Mindmatrix, Mlouns, Mlpkr, Moe Epsilon,
Musiphil, NawlinWiki, Nbarth, Ninjagecko, Noldoaran, Notheruser, Oleg Alexandrov, Oli Filth, Oskar Sigvardsson, OverLeg, P.t-the.g, Paddu, PatheticCopyEditor, Patmorin, Pgan002,
Pinguin.tk, Plaga701, Prasi90, Prophile, Psyced, Psyphen, Public Menace, Quietbritishjim, Qutezuce, Quuxplusone, Qwertyus, Racerdogjack, Reedy, Rhanekom, Ruud Koot, SabbeRubbish,
Sae1962, Samuel Marks, Sbisolo, Schellhammer, Sergio91pt, Shizhao, Silly rabbit, Sligocki, Softsundude, Spoon!, Stryder29, Sunny1304, Suruena, Svick, Tgdwyer, Theemathas, Thumperward,
Timeroot, Timwi, Tom W.M., Took, Tristan Schmelcher, Tristanreid, Two Bananas, Uncle Milty, Vecter, Vikingstad, Wavelength, Wiesmann, Wikitamino, Wolfkeeper, Wsloand, Xcez-be,
Yuval madar, Лев Дубовой, 191 anonymous edits

Fibonacci heap  Source: https://en.wikipedia.org/w/index.php?oldid=581707597  Contributors: Aaron Rotenberg, Adam Zivner, AnnedeKoning, Antiuser, Aquiel, Arjun024, Arkitus, Bporopat,
Brona, Charles Matthews, ChrisGualtieri, Coliso, Crashie, Creidieki, David Eppstein, Dcoetzee, DekuDekuplex, DerHexer, Droll, Dysprosia, Eliashedberg, Ereiniona, Erel Segal, Fredrik,
Fresheneesz, Funandtrvl, Gene91, Gimmetrow, Hao2lian, Hiiiiiiiiiiiiiiiiiiiii, Japanese Searobin, Jirka6, Jrouquie, Kxx, LOL, Lars Washington, Lexusuns, Martin.carames, Mdk wiki, Michael
Hardy, Mild Bill Hiccup, Miym, Mkorpela, MorganGreen, MrBananaGrabber, Myasuda, Nanobear, Ninjagecko, Novamo, O.Koslowski, Oleksandr Shturmov, P0nc, PatheticCopyEditor, Peatar,
Phil Boswell, Poor Yorick, Qutezuce, Ravik, Rjwilmsi, RobinMessage, Ruud Koot, Safenner1, Softsundude, Templatetypedef, The Real Marauder, Theemathas, Thw1309, Trimutius,
Vonehrenheim, Wik, Wsloand, Zeno Gantner, 124 anonymous edits

Spanning tree  Source: https://en.wikipedia.org/w/index.php?oldid=584205699  Contributors: 16@r, AS, Alexsh, Altenmann, Ankitsocute, Anthony Appleyard, Antony.stubbs, Austinmohr,
Bkell, Blathnaid, Chinju, Cybercobra, DHN, Daira Hopwood, David Eppstein, Dcoetzee, Dougher, Dysprosia, Gadykozma, Giftlite, Goooopy, Haham hanuka, HsuanHan, Ichensky, JLeander,
Joshxyz, Jrobinjapan, KelvinMo, Knutux, Kvng, LOL, Linas, Luoq, MathMartin, McKay, Mgreenbe, Mhym, Michael Hardy, Mohitsinghr, Momotaro, Msh210, Neelix, Oleg Alexandrov, PM800,
Perelaar, Pjvpjv, Pwhitey86, RuM, Ruud Koot, Ruy Pugliesi, Sinofreeman, Suacsecu, Svick, Thecheesykid, Tizio, Tobias Bergemann, Twri, Uffish, Vlospf, XJaM, Zarvok, 64 anonymous edits

Graph (mathematics)  Source: https://en.wikipedia.org/w/index.php?oldid=585642226  Contributors: 3ICE, A.amitkumar, ABF, Abdull, Ahoerstemeier, Aitias, Ajraddatz, Aknorals,
Akutagawa10, Algont, Altenmann, Amintora, Anabus, Anand jeyahar, Andrewsky00, Andros 1337, Andyman100, Athenray, Barras, BenRG, Bethnim, Bhadani, BiT, Bkell, Bobo192,
Booyabazooka, Borgx, Brentdax, Burnin1134, CRGreathouse, Can't sleep, clown will eat me, Catgut, Cbdorsett, Cbuckley, CentroBabbage, Ch'marr, Chewings72, Chinasaur, Chmod007, Chris
the speller, Chronist, Citrus538, Cjfsyntropy, Cornflake pirate, Corti, Crisófilax, Cybercobra, D.Lazard, DARTH SIDIOUS 2, DRAGON BOOSTER, Danrah, David Eppstein, Davidfstr,
Dbenbenn, Dcoetzee, Ddxc, Debamf, Debeolaurus, Den fjättrade ankan, Deor, Dicklyon, Discospinster, Djk3, Dockfish, Dreamster, Dtrebbien, Dureo, Dysprosia, E mraedarab, Edgars2007,
Editor70, Ekren, Erhudy, Eric Lengyel, Falcon8765, Gaius Cornelius, Gandalf61, Gauge, Gene.arboit, George100, Giftlite, Grolmusz, Gutin, Gwaihir, HMSSolent, Hairy Dude, Hannes Eder,
Hans Adler, Hans Dunkelberg, Harp, Headbomb, Henry Delforn (old), Huynl, Ijdejter, Ilia Kr., Ilya, Inka 888, Insanity Incarnate, J.delanoy, JNW, JP.Martin-Flatin, Jason Quinn, Jeff Erickson,
Jiang, Joeblakesley, Joel B. Lewis, Jokes Free4Me, Jon Awbrey, Jon har, JonDePlume, Joriki, Jpeeling, Jpiw, JuPitEer, Jwanders, Karada, Karlscherer3, Kerrick Staley, Kine, Kingmash, Knutux,
Kracekumar, Kruusamägi, Kuru, L Kensington, Lambyte, Liao, Libcub, LuckyWizard, Lugia2453, MER-C, MRG90, Maghnus, Magister Mathematicae, Mahanga, Manning Bartlett, Marc van
Leeuwen, Maschen, Mate2code, Materialscientist, MathMartin, Matt Crypto, Mayooranathan, McGeddon, McKay, Mdd, Michael Hardy, Michael Slone, MikeBorkowski, Minder2k, Mindmatrix,
Mitmaro, Miym, Mountain, Mrjohncummings, Myasuda, Mymyhoward16, Nat2, Neilc, Netrapt, Nihonjoe, Nina Cerutti, Nowhither, Nsk92, Ohnoitsjamie, Oliphaunt, Oxymoron83, Pafcu,
Paleorthid, Patrick, Paul August, PaulTanenbaum, Peter Kwok, Peterl, Phegyi81, PhotoBox, Pinethicket, Possum, Powerthirst123, Prunesqualer, Quaeler, R'n'B, Radagast3, RandomAct, RaseaC,
Repied, Requestion, Rettetast, Rgclegg, Rich Farmbrough, Rjwilmsi, RobertBorgersen, Robertsteadman, RobinK, Royerloic, Salix alba, Sdrucker, Shadowjams, Shanel, Siddhant, Silversmith,
SixWingedSeraph, Someguy1221, SophomoricPedant, Sswn, Stevertigo, Struthious Bandersnatch, Stux, Suchap, Super-Magician, Svick, THEN WHO WAS PHONE?, Tamfang, Tangi-tamma,
Tbc2, Tempodivalse, Tgv8925, The Anome, Theone256, TimBentley, Timflutre, Timrollpickering, Tman159, Tobias Bergemann, Tom harrison, Tomhubbard, Tomo, Tompw, Tomruen, Tosha,
Tslocum, Twri, Tyw7, UKoch, Ulric1313, Urdutext, Vacio, Vanischenu, Vaughan Pratt, VictorPorton, Voedin, Void-995, W, Wandrer2, Wesley Moy, West.andrew.g, Wgunther, White Trillium,
WikiDao, Wikidsp, Willking1979, Wknight94, Wshun, XJaM, Xavexgoem, Xiong, Yath, Yecril, Yitzhak, Ylloh, Yloreander, Zachlipton, Zaslav, Zenek.k, Zero0000, Zocky, Zven, Канеюку,
Пика Пика, 455 anonymous edits

Graph theory  Source: https://en.wikipedia.org/w/index.php?oldid=586540700  Contributors: 4368a, APH, Aaditya 7, Aaronzat, Abeg92, Agro1986, Ajweinstein, Aknxy, Akutagawa10, Alan
Au, Alansohn, Alcidesfonseca, Alex Bakharev, Allens, Almi, Altenmann, Ams80, Andre Engels, Andrea105, Andreas Kaufmann, Andris, Ankitbhatt, Anna Lincoln, Anonymous Dissident,
Anthonynow12, Aquae, Arbor, Arvindn, Astrophil, AxelBoldt, Ayda D, Bact, Bananastalktome, Bazuz, Beda42, Bender235, Bereziny, Berteun, Bg9989, Bkell, Blair Azzopardi, BlckKnght,
Boleslav Bobcik, Booyabazooka, Brad7777, Brent Gulanowski, Brick Thrower, Brona, Bumbulski, C S, CRGreathouse, Camembert, CanadianLinuxUser, Caramdir, Cerber, CesarB, Chalst,
Charles Matthews, Chas zzz brown, Chmod007, ChrisGualtieri, Conversion script, Corpx, Csl77, Cyborgbadger, D.Lazard, D75304, DFRussia, Dafyddg, Damien Karras, Daniel Quinlan, David
Eppstein, Davidfstr, Dawn Bard, Dbenbenn, Delaszk, DerHexer, Dgrant, Dicklyon, Diego UFCG, Dina, Disavian, Discospinster, Dittaeva, Doctorbozzball, Domesticenginerd, Doradus,
Dr.S.Ramachandran, Duncharris, Dycedarg, Dysprosia, Dze27, ElBenevolente, Eleuther, Elf, Eric.weigle, Eugeneiiim, Faizan, Favonian, FayssalF, Fchristo, Feeeshboy, Fivelittlemonkeys, Fred
Bradstadt, Fredrik, FvdP, GGordonWorleyIII, GTBacchus, Gaius Cornelius, Gandalf61, Garyzx, Geometry guy, George Burgess, Gianfranco, Giftlite, GiveAFishABone, Glinos, Gmelli,
GoShow, Gold4444, Goochelaar, Gragragra, Graham87, GraphTheoryPwns, GregorB, Grog, Gutza, Gyan, HMSSolent, Hannes Eder, Hans Adler, Hazmat2, Hbruhn, Headbomb, Henning
Makholm, Hirzel, Idiosyncratic-bumblebee, Ignatzmice, Igodard, Ino5hiro, Jakob Voss, Jalpar75, Jarble, Jaxl, JeLuF, Jean.julius, Jeronimo, Jheuristic, Jinma, Jmencisom, Joel B. Lewis, Joespiff,
JohnBlackburne, Johnsopc, Jojit fb, Jon Awbrey, Jon har, Jonsafari, Jorge Stolfi, Jwissick, Kalogeropoulos, Karl E. V. Palmen, KellyCoinGuy, Kilom691, King Bee, Knutux, Kope, Kpjas,
Kruusamägi, LC, LOL, Lanem, Ldecola, Liao, Linas, Looxix, Low-frequency internal, Lpgeffen, Lupin, Madewokherd, Magister Mathematicae, Magmi, Mahlerite, Mailer diablo, Mani1,
Marek69, Marianocecowski, Mark Foskey, Mark Renier, MathMartin, Matthiaspaul, Matusz, Maurobio, Maxime.Debosschere, Maxwell bernard, McKay, Meekohi, MelbourneStar, Melchoir,
Michael Hardy, Michael Slone, Miguel, Mild Bill Hiccup, Miym, Mlpkr, Morphh, Msh210, MusikAnimal, Mxn, Myanw, Myasuda, MynameisJayden, Naerbnic, Nanshu, Nasnema, Netha
Hussain, Nichtich, Ntsimp, OMurgo, Obankston, Obradovic Goran, Ohnoitsjamie, Oleg Alexandrov, Oli Filth, Omgigotanaccount, Onorem, Optim, OrangeDog, Oroso, Oskar Flordal, Outraged
duck, Pashute, Patrick, Paul August, Paul Murray, PaulHoadley, PaulTanenbaum, Pcb21, Peter Kwok, Photonique, Piano non troppo, PierreAbbat, Policron, Poor Yorick, Populus,
Powerthirst123, Protonk, Pstanton, Puckly, Pugget, Quaeler, Qutezuce, R'n'B, RUVARD, Radagast3, RandomAct, Ratiocinate, Renice, Requestion, Restname, Rev3nant, RexNL, Rjwilmsi,
Rmiesen, Roachmeister, Robert Merkel, Robin klein, RobinK, Ronz, Ruud Koot, SCEhardt, Sacredmint, SakeUPenn, Sangak, Shanes, Shd, Shepazu, Shikhar1986, Shizhao, Sibian,
SixWingedSeraph, Slawekb, SlumdogAramis, Smoke73, Sofia karampataki, Solitude, Sonia, Spacefarer, Spitfire8520, StaticElectricity, Stochata, Sundar, Sverdrup, TakuyaMurata, Tangi-tamma,
Tarotcards, Taw, Taxipom, Tckma, Template namespace initialisation script, The Cave Troll, The Isiah, Thesilverbail, Thv, Titanic4000, Tmusgrove, Tobias Bergemann, Tolly4bolly, Tomo,
Tompw, Trinitrix, Tyir, Tyler McHenry, Uncle Dick, Usien6, Vacio, Vonkje, Watersmeetfreak, Whiteknox, Whyfish, Wikiisgreat123, Womiller99, Wshun, Wsu-dm-jb, Wsu-f, XJaM, Xdenizen,
Xiong, Xnn, XxjwuxX, Yecril, Ylloh, Yloreander, Youngster68, Zaslav, Zero0000, Zoicon5, Zsoftua, Zundark, Александър, Канеюку, 502 anonymous edits
Article Sources and Contributors 153

Glossary of graph theory  Source: https://en.wikipedia.org/w/index.php?oldid=584927450  Contributors: 3mta3, A1kmm, Aarond144, Achab, Alexey Muranov, Algebraist, Altenmann, Altoid,
Andrey.gric, Anonymous Dissident, Archelon, ArnoldReinhold, Bender235, Bethnim, Bkell, Booyabazooka, Brick Thrower, Brona, Buenasdiaz, Charles Matthews, ChrisGualtieri, Citrus538,
Closedmouth, Csaracho, D6, DVanDyck, Damian Yerrick, David Eppstein, DavidRideout, Dbenbenn, Dcljr, Dcoetzee, Denisarona, Doc honcho, Doradus, Dysprosia, Edward, Eggwadi, El C,
Elwikipedista, EmanueleMinotto, Eric119, Ferris37, GGordonWorleyIII, GTBacchus, Gaius Cornelius, Giftlite, Grubber, Happynomad, Headbomb, HorsePunchKid, Hyacinth, Ivan Štambuk,
JLeander, Jalal0, Jfitzell, Jmerm, John of Reading, Jokes Free4Me, Joriki, Justin W Smith, Jw489kent, Jwanders, Jérôme, Kjoonlee, Kope, Lansey, Lasunncty, Linas, Markhurd, Mastergreg82,
MathMartin, MattGiuca, Maximus Rex, Mcld, Me and, MentorMentorum, Mgreenbe, Michael Hardy, Michael@nosivad.com, Mikhail Dvorkin, Miym, Morgoth106, Mzamora2, N8wilson,
Nonenmac, Oleg Alexandrov, Ott2, Patrick, Paul August, PaulTanenbaum, Peter Kwok, Pmq20, Pojo, Populus, Prunesqualer, Quaeler, Quintopia, Quuxplusone, R'n'B, Ratfox, Rdvdijk, Reina
riemann, RekishiEJ, Rich Farmbrough, Rick Norwood, Ricky81682, Rsdetsch, Ruud Koot, Salgueiro, Salix alba, SanitySolipsism, Scarpy, Shadowjams, SixWingedSeraph, Skaraoke,
Spanningtree, Starcrab, Stux, Sunayana, Sundar, Szebenisz, TakuyaMurata, The Transliterator, TheSolomon, Thechao, Thehotelambush, Tizio, Tomo, Twri, Vonkje, Warumwarum, Whorush,
Wshun, XJaM, Xiong, Yitzhak, Zaslav, 153 anonymous edits

Directed graph  Source: https://en.wikipedia.org/w/index.php?oldid=585775125  Contributors: Alesiom, Altenmann, Andreas Kaufmann, Anne Bauval, BiT, Bkell, Booyabazooka,
Bryan.burgers, Calle, Catgut, Corruptcopper, David Eppstein, Delcypher, Dzied Bulbash, Einkil, Giftlite, Grue, Hamaryns, Headbomb, Henry Delforn (old), HughD, JP.Martin-Flatin, Jarble,
Justin W Smith, Linas, M-le-mot-dit, Marcuse7, Mark Renier, Mark viking, Meno25, Miym, Ms.wiki.us, Nbarth, NuclearWarfare, PaulTanenbaum, Pcap, R'n'B, Ricardo Ferreira de Oliveira,
Rinix, Saranavan, Sinuhe20, SixWingedSeraph, Stepa, Twri, Vadmium, Werddemer, WookieInHeat, Zaslav, ‫ﺁﺭﻣﺎﻥ‬, 42 anonymous edits

Adjacency matrix  Source: https://en.wikipedia.org/w/index.php?oldid=583227725  Contributors: Abdull, Abha Jain, Aleph4, Arthouse, AxelBoldt, Beetstra, BenFrantzDale, Bender235,
Bender2k14, Bitsianshash, Bkell, Booyabazooka, Burn, Calle, Cburnett, Chenopodiaceous, David Eppstein, Dcoetzee, Debresser, Dreadstar, Dysprosia, ElBenevolente, Felix Hoffmann,
Fgnievinski, Fredrik, Garyzx, Gauge, Giftlite, Headbomb, Hu12, JackSchmidt, JamesBWatson, Jean Honorio, Jogers, John of Reading, Jpbowen, Juffi, Kneufeld, Kompot 3, MarkSweep,
Mate2code, MathMartin, Matěj Grabovský, Mbogelund, Mdrine, Michael Hardy, Miym, Morre, Natelewis, Oleg Alexandrov, Olenielsen, Only2sea, Patmorin, Paulish, Periergeia, Phils, Reina
riemann, Rgdboer, Rhetth, Rich Farmbrough, RobinK, SPTWriter, Salgueiro, Schneelocke, Senfo, Shilpi4560, Sneftel, Snowcream, Squids and Chips, Sławomir Biały, TakuyaMurata, Tamfang,
Tbackstr, Tim Q. Wells, Timendum, Tomo, Tomruen, Ttzz, Twri, X7q, Yoav HaCohen, YuryKirienko, Zaslav, ‫ﺳﻌﯽ‬, 85 anonymous edits

Floyd–Warshall algorithm  Source: https://en.wikipedia.org/w/index.php?oldid=586564052  Contributors: 16@r, Aenima23, AlanUS, AlexandreZ, Algebra123230, Altenmann, Anharrington,
Barfooz, Beardybloke, Buaagg, C. Siebert, CBM, Cachedio, CiaPan, Closedmouth, Cquimper, Daveagp, Davekaminski, David Eppstein, Dcoetzee, Dfrankow, Dittymathew, Donhalcon,
DrAndrewColes, Exercisephys, Fatal1955, Fintler, Frankrod44, Gaius Cornelius, Giftlite, Greenleaf, Greenmatter, GregorB, Gutworth, Harrigan, Hemant19cse, Hjfreyer, Intgr, J. Finkelstein,
JLaTondre, Jarble, Jaredwf, Jellyworld, Jerryobject, Jixani, Joy, Julian Mendez, Justin W Smith, Kanitani, Kenyon, Kesla, KitMarlow, Kletos, Leycec, LiDaobing, Luv2run, Maco1421,
Magioladitis, MarkSweep, Martinkunev, Md.aftabuddin, Mellum, Michael Hardy, Minghong, Mini-Geek, Minority Report, Mqchen, Mwk soul, Nanobear, Netvor, Nicapicella, Nishantjr,
Nneonneo, Obradovic Goran, Oliver, Opium, Pandemias, Phil Boswell, Pilotguy, Pjrm, Polymerbringer, Poor Yorick, Pvza85, Pxtreme75, Quuxplusone, Rabarberski, Raknarf44, RobinK, Roman
Munich, Ropez, Roseperrone, Ruud Koot, Sakanarm, SchreiberBike, Shadowjams, Shmor, Shnowflake, Shyamal, Simoneau, Smurfix, Soumya92, Specs112, Sr3d, Stargazer7121, SteveJothen,
Strainu, Svick, Taejo, Teles, Thái Nhi, Treyshonuff, Two Bananas, Volkan YAZICI, W3bbo, Wickethewok, Xyzzy n, Yekaixiong, 245 anonymous edits

Shortest path problem  Source: https://en.wikipedia.org/w/index.php?oldid=586998220  Contributors: Aaron Rotenberg, Alcidesfonseca, Alexander Anoprienko, Alfredo J. Herrera Lago,
Altenmann, Amine.marref, Andreas Kaufmann, Andris, Anomie, Artyom Kalinin, AxelBoldt, B4hand, BenFrantzDale, Booyabazooka, Brian0918, C. lorenz, Caesura, Cairomax, Camw, Choess,
Chris Capoccia, Cipher1024, Cthe, Daveagp, David Eppstein, Dcoetzee, Deanphd, Delaszk, Denisarona, Devis, Dmforcier, Download, Dysprosia, ESkog, Edepot, Essess, Fgnievinski, Fæ, Gaius
Cornelius, General Wesc, Giftlite, Glrx, Graph Theory page blanker, Happyuk, Hari6389, Hariva, Headbomb, Jarble, Jason.surratt, JeLuF, Justin W Smith, Kbrose, Kope, LC, Lourakis, Lpgeffen,
Lqs, Lzur, MadScientistVX, Marc van Leeuwen, Marcelkcs, Mathiastck, Mcld, Metaprimer, Michael Hardy, Mindmatrix, MiroBrada, MisterSheik, Mkroeger, Nethgirb, Nullzero, Nuno Tavares,
Ohnoitsjamie, Oliphaunt, OverlordQ, Phelanpt, Pigorsch, Pmlineditor, Qwertyus, Rasmusdf, Rich Farmbrough, Rjwilmsi, Robert Geisberger, RobinK, Robmccoll, RomanSpa, Ruud Koot, Shd,
Sho Uemura, Squire55, Suzhouwuyue, Taemyr, Templatetypedef, TerraFrost, The Anome, Tommyjb, ToneDaBass, Treesmill, Wolfkeeper, Xperimental, Yacs, 123 anonymous edits

Breadth-first search  Source: https://en.wikipedia.org/w/index.php?oldid=584926744  Contributors: 28bytes, A5b, Adam Zivner, Akmenon, AllenDowney, Amelio Vázquez, Andre Engels,
Antkro, Asp-GentooLinux, Bazonka, BenFrantzDale, Bender05, Bkkbrad, BlueNovember, Bmatheny, Bonadea, Braindrain0000, CBM, CesarB, Cheethdj, Clacker, Corti, Cscott, CyberShadow,
Cybercobra, DHN, Davepape, David Eppstein, DavidCary, Davidyeotb, Dbeatty, Dcoetzee, Dedicatedecoy, Diannaa, Dileep98490, Dysprosia, Earobinson, Ecb29, Edaelon, Elyazalmahfouz,
Erdogany, EricTalevich, ErikvanB, Ffaarr, Fkodama, Folletto, Frankrod44, Frizzil, Galaxiaad, Gdr, GeeksHaveFeelings, Gelwood, GeorgeBills, Giftlite, Gilliam, Headbomb, Hmwith, IRP, J Di,
Jacobkwitkoski, Jan Winnicki, Jbellessa87, Jerf, Jludwig, Jogloran, Justin W Smith, Kdau, Kdnewton, Kenyon, Kesla, Kiand, Kostmo, LOL, Lemontea, LittleDan, Liuofficial, Lixinso, Loading,
Lvsmart, M.aznaveh, Madmardigan53, Mahue, MarkSweep, Martynas Patasius, Matt.smart, Mcld, Mentifisto, Michael Hardy, Michael Slone, Miia, Miym, Mmtux, Mrwojo, Musiphil, Mxn,
Night Gyr, Nisargtanna, NotAnonymous0, Nuno Tavares, Nvrmnd, Oblatenhaller, Ott2, P b1999, Pcap, Philip Trueman, Phoe6, Polveroj, Pomte, Poor Yorick, Qrancik, Qwertyus, Ragzouken,
Regnaron, RobinK, Robost, Saurabhnbanerji, Secfan, Smallman12q, Smjg, Sonett72, Spoon!, Staszek Lem, Steverapaport, Szabolcs Nagy, TWiStErRob, TakuyaMurata, Thesilverbail, Timwi,
Tooto, Tracytheta, Valery.vv, Velella, VictorAnyakin, Viebel, W3bbo, Wtmitchell, Xodarap00, Zad68, Тиверополник, 285 anonymous edits

Depth-first search  Source: https://en.wikipedia.org/w/index.php?oldid=586571627  Contributors: A5b, ABCD, Aadnan.tufail, Andre Engels, Antiuser, Apalsola, Apanag, Batkins, Bbi5291,
Beetstra, BenRG, Braddjwinter, Bryan Derksen, Bubba73, CesarB, Citrus538, Clacker, Craig Barkhouse, Cst17, Curtmack, DHN, David Eppstein, Davidyeotb, Dcoetzee, Dllu, Dreske, Duncan
Hope, Dysprosia, EdiTor, Edlee, ErikvanB, Fkodama, Fraggle81, Frecklefoot, Frizzil, G8moon, Giftlite, Gscshoyru, Gurch, Gurpreetkaur88, Gustavb, Hansamurai, Hemanshu, Hermel, Itai,
Ixfd64, Jaredwf, Jef41341, Jeltz, Jerf, Jnlin, Jonon, Jtle515, Justin Mauger, Justin W Smith, Kate, Kdau, Kesla, Koertefa, Kromped, LittleDan, Mark viking, MarkSweep, Marked, Martynas
Patasius, Materialscientist, McKay, Mctpyt, Mentifisto, Michael Hardy, Miknight, Mild Bill Hiccup, Miles, Mipadi, Miym, Moosebumps, MusicScience, Musiphil, Nizonstolz, Nuno Tavares,
Nvrmnd, Pmussler, Poor Yorick, Pukeye, Qrancik, Qwertyus, Qx2020, Readytohelp, Regnaron, Rich Farmbrough, Rick Norwood, Rror, Rtcasey, RxS, SPTWriter, Shuroo, Smallman12q,
Snowolf, Srrrgei, Staszek Lem, Stefan, Steverapaport, Stimpy, Subh83, Svick, Tauwasser, Taxipom, Thegeneralguy, Thesilverbail, Thumperward, TimBentley, Vroo, Vsh3r, Waldir, Wavelength,
Yonestar, Yuide, Z10x, 253 anonymous edits

Backtracking  Source: https://en.wikipedia.org/w/index.php?oldid=583237109  Contributors: 10metreh, A333, Adavidb, Alansohn, Andre.holzner, Andrei Stroe, Angela, Anna Lincoln, Arjayay,
Beland, Brentsmith101, Bubba73, Camw, CarlHewitt, Crockspot, Darklilac, Darkov, Dcoetzee, DerHexer, Dougher, Edward, Enochlau, Error -128, Falcon8765, FatalError, Frikle, Gfonsecabr,
Gioto, Gobbleswoggler, Goochelaar, Happyrabbit, Hariva, Harmil, Helen D., Heron, Ida Shaw, JHMM13, JRSpriggs, Jaredwf, Jll, Jokes Free4Me, JonHarder, Jorge Stolfi, Khanishu, Kku,
Lacrymology, LarryLACa, Lcarscad, Light current, Lights, Marc van Leeuwen, Michael Hardy, Mikael Häggström, Milad621, Monger, Mxn, Nivix, Ojw, OlEnglish, Oliphaunt, Pac72, Paulcmnt,
Philip Trueman, Pmanderson, Project2501a, Pvshnik, Rich Farmbrough, Robertd, Scientizzle, Shaun F, SimonP, Simpsons contributor, TAz69x, Themania, Welsh, WikiDao, Xprotocol,
YUL89YYZ, Zatin, ZxxZxxZ, Île flottante, 140 anonymous edits

Topological sorting  Source: https://en.wikipedia.org/w/index.php?oldid=586746444  Contributors: A3 nm, Abaumgar, Aeons, AlainD, Apanag, Armine badalyan, Arvindn, Atlantia, BACbKA,
Baby112342, Bjarneh, Bluegrass, Bosmon, Branonm, CJLL Wright, Captainfranz, Cburnett, Cfeet77, Charles Matthews, Chenopodiaceous, Chub, Csl77, Cybercobra, David Eppstein, Dcoetzee,
Delaszk, DmitTrix, Dmitry Dzhus, DomQ, Esmond.pitt, Fresheneesz, Gmaxwell, Greenrd, GregorB, Grimboy, Guray9000, Gurt Posh, Hannes Eder, Headbomb, Imnotminkus, Jarble, Jim
Huggins, John of Reading, Johnwon, Jokes Free4Me, Jonsafari, Joy, Karada, Kenyon, Kingpin13, Kristjan.Jonasson, Leland McInnes, Maneeshsinha, Mark viking, Matthew Woodcraft,
Mgreenbe, Michael Hardy, Mikofski, Miym, Mordomo, Nthns43, Obradovic Goran, Oliphaunt, Oskar Sigvardsson, Pascal666, Pasixxxx, Pekinensis, Planetscape, Quarl, Qwertyus, Rattatosk,
RazorICE, RobinK, Slaniel, Stimpy, Sundar, Surturz, TSornalingam, Taxipom, Tobias Bergemann, TomasRiker, Unbitwise, Vonbrand, Vromascanu, Workaphobia, Zundark, ‫ﻣﺎﻧﻲ‬, 101 anonymous
edits

Dijkstra's algorithm  Source: https://en.wikipedia.org/w/index.php?oldid=587129530  Contributors: 4v4l0n42, 90 Auto, AJim, Abu adam, Adamarnesen, Adamianash, AgadaUrbanit, Agthorr,
Ahy1, Airalcorn2, Aladdin.chettouh, AlanUS, Alanb, AlcoholVat, Alex.mccarthy, Allan speck, Alquantor, Altenmann, Alyssaq, Amenel, Andreasneumann, Angus Lepper, Anog, Apalamarchuk,
Aragorn2, Arjun G. Menon, Arrenlex, Arsstyleh, AxelBoldt, Aydee, B3virq3b, B6s, BACbKA, B^4, Bcnof, Beatle Fab Four, Behco, BenFrantzDale, BernardZ, Bgwhite, Bkell, Blueshifting,
Boemanneke, Borgx, Brona, CGamesPlay, CambridgeBayWeather, Charles Matthews, Chehabz, Choess, Christopher Parham, Cicconetti, Cincoutprabu, Clementi, Coralmizu, Crazy george,
Crefrog, Css, Csurguine, Ctxppc, Cyde, Danmaz74, Daveagp, Davekaminski, David Eppstein, Davub, Dcoetzee, Decrypt3, Deflective, Diego UFCG, Digwuren, Dionyziz, Dmforcier, Dmitri666,
DoriSmith, Dosman, Dougher, Dreske, Drostie, Dudzcom, Dysprosia, Edemaine, ElonNarai, Erel Segal, Eric Burnett, Esrogs, Ewedistrict, Ezrakilty, Ezubaric, Faure.thomas, Foobaz, Foxj,
FrankTobia, Frankrod44, Frap, Fresheneesz, GRuban, Gaiacarra, Galoubet, Gauravxpress, Gerel, Geron82, Gerrit, Giftlite, Gordonnovak, GraemeL, Graham87, Grantstevens, GregorB, Guanaco,
Gutza, Hadal, Haham hanuka, Hao2lian, Happyuk, Hell112342, HereToHelp, Herry12, Huazheng, I am One of Many, Ibmua, IgushevEdward, IkamusumeFan, Illnab1024, Iridescent, Itai,
JBocco, JForget, Jacobolus, Jarble, Jaredwf, Jason.Rafe.Miller, Jellyworld, Jeltz, Jewillco, Jheiv, Jim1138, Jochen Burghardt, Joelimlimit, JohnBlackburne, Jongman.koo, Joniscool98, Jorvis,
Julesd, Juliusz Gonera, Justin W Smith, K3rb, Kbkorb, Keilana, Kesla, Kh naba, King of Hearts, Kku, Kndiaye, Kooo, Kostmo, LC, LOL, Laurinkus, Lavaka, Lawpjc, Leonard G., Lone boatman,
LordArtemis, LunaticFringe, Mahanga, Mameisam, MarkSweep, Martynas Patasius, Materialscientist, MathMartin, Mathiastck, MattGiuca, Matusz, Mccraig, Mcculley, Megharajv,
MementoVivere, Merlion444, Mgreenbe, Michael Hardy, Michael Slone, Mikeo, Mikrosam Akademija 2, Milcke, MindAfterMath, Mkw813, Mr.TAMER.Shlash, MrOllie, MusicScience,
Mwarren us, Nanobear, NetRolller 3D, Nethgirb, Nixdorf, Noogz, Norm mit, Obradovic Goran, Obscurans, Oleg Alexandrov, Oliphaunt, Olivernina, Optikos, Owen, Peatar, PesoSwe, Piojo,
Pol098, PoliticalJunkie, Possum, ProBoj!, Pseudomonas, Pshanka, Pskjs, Pxtreme75, Quidquam, RISHARTHA, Radim Baca, Rami R, RamiWissa, Recognizance, Reliableforever, Rhanekom,
Rjwilmsi, Robert Southworth, RodrigoCamargo, RoyBoy, Ruud Koot, Ryan Roos, Ryangerard, Ryanli, SQGibbon, Sambayless, Sarkar112, Seanhalle, Sephiroth storm, Shd, Sheepeatgrass,
Shizny, Shriram, Shuroo, Sidonath, SiobhanHansa, Sk2613, Slakr, Sniedo, Sokari, Someone else, Soytuny, Spencer, Sprhodes, Stdazi, SteveJothen, Subh83, Sundar, Svick, T0ljan,
Tehwikipwnerer, Tesse, Thayts, The Arbiter, TheRingess, Thijswijs, Thom2729, ThomasGHenry, Timwi, Tobei, Tomisti, Torla42, Trunks175, Turketwh, VTBassMatt, Vecrumba, Velella,
Article Sources and Contributors 154

Vevek, Watcher, Wierdy1024, WikiSlasher, Wikipelli, Wildcat dunny, Wphamilton, X7q, Xerox 5B, Ycl6, Yujianzhao, Yutsi, Yworo, ZeroOne, Zhaladshar, Zr2d2, 656 anonymous edits

Greedy algorithm  Source: https://en.wikipedia.org/w/index.php?oldid=579602776  Contributors: A8UDI, AManWithNoPlan, Albany NY, Andreas Kaufmann, Andycjp, ArzelaAscoli,
Avicennasis, AxelBoldt, Bernard Teo, BernardZ, Brianyoumans, CKlunck, CatherineMunro, Chamale, Charles Matthews, Chire, Cjohnzen, Clan-destine, CloudNine, Cruccone, Darrel francis,
David Eppstein, Dcoetzee, Denisarona, Diomidis Spinellis, Eirik the Viking, Eleschinski2000, Enmc, Enochlau, Fraggle81, Giftlite, Haham hanuka, Hairy Dude, HairyFotr, Hammertime,
Hfastedge, Hhulzk, Hobartimus, Jaredwf, Jason Quinn, Jddriessen, Jibbist, John of Reading, JumpDiscont, Kiefer.Wolfowitz, Kim Bruning, LOL, Lesser Cartographies, Maghnus, Magioladitis,
Malcohol, Mange01, Marcosw, Mcstrother, Mechonbarsa, Meduz, Meekywiki, MichaelBillington, Mindmatrix, Mlpkr, Monty845, Nabla, Nandhp, Nethgirb, New Thought, NickShaforostoff,
Nkarthiks, Notheruser, Obradovic Goran, Omgoleus, Optimering, Pavel Kotrc, PeterBrooks, Pgan002, Polariseke, Que, Ralphy, Rickproser, Ruud Koot, Ryanmcdaniel, Salgueiro, Sango123,
Shrishaster, Skakkle, Smjg, Suanshsinghal, Sverdrup, Swapspace, Swfung8, SynergyBlades, TheMandarin, TomRitchford, Trezatium, Unyoyega, Uselesswarrior, Wavelength, Wikid77,
Wleizero, Wlievens, X7q, Zangkannt, ZeroOne, Ziradkar, ‫ﭘﻮﻭﯾﺎ‬, 125 anonymous edits

Travelling salesman problem  Source: https://en.wikipedia.org/w/index.php?oldid=586641489  Contributors: 130.233.251.xxx, 1exec1, 28421u2232nfenfcenc, 4ndyD, 62.202.117.xxx,
ANONYMOUS COWARD0xC0DE, Aaronbrick, Adammathias, Aftermath1983, Ahoerstemeier, Akokskis, Alan.ca, AlanUS, Aldie, Altenmann, Anaxial, Andreas Kaufmann, Andreasr2d2,
Andris, Angus Lepper, Apanag, ArglebargleIV, ArnoldReinhold, Aronisstav, Astral, AstroNomer, AustinBuchanan, Azotlichid, B4hand, Bathysphere, Beatkist, Bender2k14, BenjaminTsai,
Bensin, Bernard Teo, Bjornson81, Bmears11, Bo Jacoby, Bongwarrior, Boothinator, Brian Gunderson, Brucevdk, Brw12, Bubba73, C. lorenz, CRGreathouse, Can't sleep, clown will eat me,
Capricorn42, ChangChienFu, Chris-gore, Chris55, ChrisCork, ChrisGualtieri, Classicalecon, Cngoulimis, Coconut7594, Colonies Chris, Conversion script, CountingPine, DVdm, Daniel
Karapetyan, David Eppstein, David.Mestel, David.Monniaux, David.hillshafer, DavidBiesack, Davidhorman, Dbfirs, Dcoetzee, Devis, Dino, Disavian, Dishantpandya777, Donarreiskoffer,
Doradus, Downtown dan seattle, DragonflySixtyseven, DreamGuy, Duoduoduo, Dwhdwh, Dysprosia, Edward, El C, Ellywa, ErnestSDavis, EzequielBalmori, Fanis84, Ferris37, Fioravante
Patrone, Flapitrr, Fmccown, Fmorstatter, Fredrik, Gaeddal, Galoubet, Gdessy, Gdr, Geofftech, Gerhardvalentin, Giftlite, Gnomz007, Gogo Dodo, Graham87, Greenmatter, GregorB, H, Haeinous,
Hairy Dude, Hans Adler, Happyuk, Haterade111, Hawk777, Herbee, Hike395, Honnza, Hyperneural, Ilya.gazman, Ironholds, Irrevenant, Isaac, IstvanWolf, IvR, Ixfd64, J.delanoy, JackH,
Jackbars, Jamesd9007, Jasmjc2, Jasonb05, Jasonyo, Jeffhoy, Jim.Callahan,Orlando, John of Reading, Johngouf85, Johnleach, Jok2000, JonathanFreed, Jonhkr, Jsamarziya, Jugander, Justin W
Smith, KGV, Kane5187, Karada, Kenneth M Burke, Kenyon, Kf4bdy, Kiefer.Wolfowitz, Kjells, Klausikm, Kotasik, Kri, Ksana, Kvamsi82, Kyokpae, LFaraone, LOL, Laleppa, Lambiam,
Lanthanum-138, Laudaka, Lesser Cartographies, Lingwanjae, Lone boatman, MSGJ, MagicMatt1021, Magioladitis, Male1979, Mantipula, MarSch, Marj Tiefert, Martynas Patasius,
Materialscientist, MathMartin, McGeddon, Mcld, Mdd, Mellum, Melsaran, Mhahsler, Michael Hardy, Michael Slone, Mikeedla, Mild Bill Hiccup, Miym, Mojoworker, Monstergurkan,
MoraSique, Mormegil, Musiphil, Mzamora2, Naff89, Nethgirb, Nguyen Thanh Quang, Ninjagecko, Nobbie, Nr9, Obradovic Goran, Orfest, Ozziev, Paul Silverman, Pauli133, Pegasusbupt,
PeterC, Petrus, Pgr94, Phcho8, Piano non troppo, PierreSelim, Pleasantville, Pmdboi, Prosfilaes, Pschaus, Qaramazov, Qorilla, Quadell, Quebec99, R3m0t, RDBury, Random contributor, Ratfox,
Raul654, Reconsider the static, RedLyons, Requestion, Rheun, Richmeister, Ricksmt, Rjwilmsi, RobinK, Rocarvaj, Ronaldo, Ross Fraser, Rror, Ruakh, Ruud Koot, Ryan Roos, STGM,
Saeed.Veradi, Sahuagin, Samusoft1994, Sarkar112, Saurabh.harsh, Scravy, Seet82, Seraphimblade, Sergey539, Shadowjams, Shafaet, Sharcho, ShelfSkewed, Shoujun, Shubhrasankar, Siddhant,
Simetrical, Sladen, Slartibartfastibast, Smmurphy, Smremde, Smyth, Snow Blizzard, Some standardized rigour, Soupz, South Texas Waterboy, SpNeo, Spock of Vulcan, SpuriousQ, Stemonitis,
Stevertigo, Stimpy, Stochastix, StradivariusTV, Superm401, Superninja, Tamfang, Teamtheo, Tedder, That Guy, From That Show!, The Anome, The Thing That Should Not Be, The stuart,
Theodore Kloba, Thisisbossi, Thore Husfeldt, Tigerqin, Tinman, Tobias Bergemann, Tom Duff, Tom3118, Tomgally, Tomhubbard, Tommy2010, Touriste, Trlkly, Tsplog, Twas Now, Vasiľ,
Vgy7ujm, Wavelength, WhatisFeelings?, Wizard191, Wumpus3000, Wwwwolf, Xiaojeng, Xnn, Yixin.cao, Ynhockey, Zaphraud, Zeno Gantner, ZeroOne, Zyqqh, 628 anonymous edits
Image Sources, Licenses and Contributors 155

Image Sources, Licenses and Contributors


Image:Tree graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Tree_graph.svg  License: Public Domain  Contributors: Dcoetzee, Tacsipacsi, Tizio
Image:binary tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Binary_tree.svg  License: Public Domain  Contributors: User:Dcoetzee
File:BinaryTreeRotations.svg  Source: https://en.wikipedia.org/w/index.php?title=File:BinaryTreeRotations.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors:
User:Josell7
File:Waldburg Ahnentafel.jpg  Source: https://en.wikipedia.org/w/index.php?title=File:Waldburg_Ahnentafel.jpg  License: Public Domain  Contributors: AndreasPraefcke, Elwikipedista,
FA2010, LosHawlos, Mdd, Shakko, Skim, Wuselig, 1 anonymous edits
File:Insertion of binary tree node.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Insertion_of_binary_tree_node.svg  License: Creative Commons Zero  Contributors: Hazmat2
File:Deletion of internal binary tree node.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Deletion_of_internal_binary_tree_node.svg  License: Creative Commons Zero
 Contributors: Hazmat2
Image:Binary tree in array.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Binary_tree_in_array.svg  License: Public Domain  Contributors: User:Dcoetzee
Image:N-ary to binary.svg  Source: https://en.wikipedia.org/w/index.php?title=File:N-ary_to_binary.svg  License: Public Domain  Contributors: CyHawk
File:Binary search tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Binary_search_tree.svg  License: Public Domain  Contributors: User:Booyabazooka, User:Dcoetzee
File:binary search tree delete.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Binary_search_tree_delete.svg  License: Public Domain  Contributors: User:Dcoetzee
File:Binary tree sort(2).png  Source: https://en.wikipedia.org/w/index.php?title=File:Binary_tree_sort(2).png  License: Public Domain  Contributors: Mathgorges
File:Infix-dia.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Infix-dia.svg  License: Public Domain  Contributors: Infix-dia.png: blahedo derivative work: Beao
Image:Complete graph K7.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K7.svg  License: Public Domain  Contributors: User:Dbenbenn
Image:Complete graph K1.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K1.svg  License: Public Domain  Contributors: David Benbennick wrote this file.
Image:Complete graph K2.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K2.svg  License: Public Domain  Contributors: David Benbennick wrote this file.
Image:Complete graph K3.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K3.svg  License: Public Domain  Contributors: David Benbennick wrote this file.
Image:3-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:3-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:4-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:4-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:5-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:5-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:6-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:6-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:7-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:7-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:8-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:8-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:9-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:9-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:10-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:10-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
Image:11-simplex graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:11-simplex_graph.svg  License: Creative Commons Attribution-Sharealike 3.0,2.5,2.0,1.0  Contributors:
Koko90
File:Prefix-dia.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Prefix-dia.svg  License: Public Domain  Contributors: Prefix-dia.png: blahedo derivative work: Beao
File:Postfix-dia.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Postfix-dia.svg  License: Public Domain  Contributors: Postfix-dia.png: blahedo derivative work: Beao
File:HP hat.jpg  Source: https://en.wikipedia.org/w/index.php?title=File:HP_hat.jpg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: User:Egbert35
Image:Unbalanced binary tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Unbalanced_binary_tree.svg  License: Public Domain  Contributors: Me (Intgr)
Image:AVLtreef.svg  Source: https://en.wikipedia.org/w/index.php?title=File:AVLtreef.svg  License: Public Domain  Contributors: User:Mikm
File:AVL Tree Rebalancing.svg  Source: https://en.wikipedia.org/w/index.php?title=File:AVL_Tree_Rebalancing.svg  License: Creative Commons Attribution-ShareAlike 3.0 Unported
 Contributors: CyHawk
File:B-tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:B-tree.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: CyHawk
Image:B tree insertion example.png  Source: https://en.wikipedia.org/w/index.php?title=File:B_tree_insertion_example.png  License: Public Domain  Contributors: User:Maxtremus
Image:Max-Heap.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Max-Heap.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: Ermishin
Image:Fibonacci heap.png  Source: https://en.wikipedia.org/w/index.php?title=File:Fibonacci_heap.png  License: GNU Free Documentation License  Contributors: User:Brona,
User:Brona/Images/fibonacci_heap.tex
Image:Fibonacci heap extractmin1.png  Source: https://en.wikipedia.org/w/index.php?title=File:Fibonacci_heap_extractmin1.png  License: GNU Free Documentation License  Contributors:
User:Brona, User:Brona/Images/fibonacci_heap.tex
Image:Fibonacci heap extractmin2.png  Source: https://en.wikipedia.org/w/index.php?title=File:Fibonacci_heap_extractmin2.png  License: GNU Free Documentation License  Contributors:
User:Brona, User:Brona/Images/fibonacci_heap.tex
Image:Fibonacci heap-decreasekey.png  Source: https://en.wikipedia.org/w/index.php?title=File:Fibonacci_heap-decreasekey.png  License: GNU Free Documentation License  Contributors:
User:Brona, User:Brona/Images/fibonacci_heap.tex
File:4x4 grid spanning tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:4x4_grid_spanning_tree.svg  License: Public Domain  Contributors: David Eppstein
Image:Cayley's formula 2-4.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Cayley's_formula_2-4.svg  License: unknown  Contributors: User:Rocchini
File:6n-graf.svg  Source: https://en.wikipedia.org/w/index.php?title=File:6n-graf.svg  License: Public Domain  Contributors: User:AzaToth
file:Directed.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Directed.svg  License: Public Domain  Contributors: Grafite, Jcb, Josette, 2 anonymous edits
file:Undirected.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Undirected.svg  License: Public Domain  Contributors: JMCC1, Josette, Kilom691, 2 anonymous edits
File:Complete graph K5.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K5.svg  License: Public Domain  Contributors: David Benbennick wrote this file.
Image:6n-graf.svg  Source: https://en.wikipedia.org/w/index.php?title=File:6n-graf.svg  License: Public Domain  Contributors: User:AzaToth
Image:Konigsberg bridges.png  Source: https://en.wikipedia.org/w/index.php?title=File:Konigsberg_bridges.png  License: GNU Free Documentation License  Contributors: Bogdan Giuşcă
File:Multi-pseudograph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Multi-pseudograph.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: 0x24a537r9
Image:Directed cycle.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Directed_cycle.svg  License: Public domain  Contributors: en:User:Dcoetzee, User:Stannered
Image:Complete graph K5.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Complete_graph_K5.svg  License: Public Domain  Contributors: David Benbennick wrote this file.
File:4-critical graph.png  Source: https://en.wikipedia.org/w/index.php?title=File:4-critical_graph.png  License: Creative Commons Zero  Contributors: Jmerm
File:Directed.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Directed.svg  License: Public Domain  Contributors: Grafite, Jcb, Josette, 2 anonymous edits
File:DirectedDegrees.svg  Source: https://en.wikipedia.org/w/index.php?title=File:DirectedDegrees.svg  License: GNU Free Documentation License  Contributors: Melchoir
File:Directed acyclic graph 2.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Directed_acyclic_graph_2.svg  License: Public Domain  Contributors: Johannes Rössel (talk)
File:4-tournament.svg  Source: https://en.wikipedia.org/w/index.php?title=File:4-tournament.svg  License: Public Domain  Contributors: Booyabazooka
Image:6n-graph2.svg  Source: https://en.wikipedia.org/w/index.php?title=File:6n-graph2.svg  License: Public Domain  Contributors: Booyabazooka, Dcoetzee, 1 anonymous edits
Image Sources, Licenses and Contributors 156

File:Symmetric group 4; Cayley graph 1,5,21 (Nauru Petersen); numbers.svg  Source:


https://en.wikipedia.org/w/index.php?title=File:Symmetric_group_4;_Cayley_graph_1,5,21_(Nauru_Petersen);_numbers.svg  License: Public Domain  Contributors: Lipedia
File:Symmetric group 4; Cayley graph 1,5,21 (adjacency matrix).svg  Source:
https://en.wikipedia.org/w/index.php?title=File:Symmetric_group_4;_Cayley_graph_1,5,21_(adjacency_matrix).svg  License: Public Domain  Contributors: Lipedia
File:Symmetric group 4; Cayley graph 4,9; numbers.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Symmetric_group_4;_Cayley_graph_4,9;_numbers.svg  License: Public
Domain  Contributors: GrapheCayley-S4-Plan.svg: Fool (talk) derivative work: Lipedia (talk)
File:Symmetric group 4; Cayley graph 4,9 (adjacency matrix).svg  Source: https://en.wikipedia.org/w/index.php?title=File:Symmetric_group_4;_Cayley_graph_4,9_(adjacency_matrix).svg
 License: Public Domain  Contributors: Lipedia
File:Floyd-Warshall example.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Floyd-Warshall_example.svg  License: Creative Commons Zero  Contributors: User:Dcoetzee
Image:Shortest path with direct weights.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Shortest_path_with_direct_weights.svg  License: Creative Commons
Attribution-Sharealike 3.0  Contributors: User:Artyom Kalinin
Image:Breadth-first-tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Breadth-first-tree.svg  License: Creative Commons Attribution 3.0  Contributors: Alexander Drichel
Image:Animated BFS.gif  Source: https://en.wikipedia.org/w/index.php?title=File:Animated_BFS.gif  License: GNU Free Documentation License  Contributors: Blake Matheny. Original
uploader was Bmatheny at en.wikipedia
Image:MapGermanyGraph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:MapGermanyGraph.svg  License: Public Domain  Contributors: AndreasPraefcke, Mapmarks,
MistWiz, Regnaron
Image:GermanyBFS.svg  Source: https://en.wikipedia.org/w/index.php?title=File:GermanyBFS.svg  License: Public Domain  Contributors: Regnaron
Image:Depth-first-tree.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Depth-first-tree.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: Alexander
Drichel
Image:graph.traversal.example.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Graph.traversal.example.svg  License: GNU Free Documentation License  Contributors: Miles
Image:Tree edges.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Tree_edges.svg  License: Public domain  Contributors: Geraki
Image:If-then-else-control-flow-graph.svg  Source: https://en.wikipedia.org/w/index.php?title=File:If-then-else-control-flow-graph.svg  License: Public domain  Contributors: Geraki
File:MAZE 30x20 DFS.ogv  Source: https://en.wikipedia.org/w/index.php?title=File:MAZE_30x20_DFS.ogv  License: Creative Commons Attribution-Sharealike 3.0  Contributors: Purpy
Pupple
File:Sudoku solved by bactracking.gif  Source: https://en.wikipedia.org/w/index.php?title=File:Sudoku_solved_by_bactracking.gif  License: Creative Commons Attribution-Sharealike 3.0
 Contributors: Simpsons contributor
Image:Directed acyclic graph.png  Source: https://en.wikipedia.org/w/index.php?title=File:Directed_acyclic_graph.png  License: Public Domain  Contributors: Anarkman, Dcoetzee, Ddxc,
EugeneZelenko, Fæ, Joey-das-WBF
Image:Dijkstra Animation.gif  Source: https://en.wikipedia.org/w/index.php?title=File:Dijkstra_Animation.gif  License: Public Domain  Contributors: Ibmua
Image:Dijkstras progress animation.gif  Source: https://en.wikipedia.org/w/index.php?title=File:Dijkstras_progress_animation.gif  License: Creative Commons Attribution 3.0  Contributors:
Subh83
Image:Greedy algorithm 36 cents.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Greedy_algorithm_36_cents.svg  License: Public Domain  Contributors: Nandhp
file:Greedy Glouton.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Greedy_Glouton.svg  License: Creative Commons Attribution-Sharealike 3.0  Contributors: User:Tos
file:Greedy-search-path-example.gif  Source: https://en.wikipedia.org/w/index.php?title=File:Greedy-search-path-example.gif  License: Creative Commons Attribution-Sharealike 3.0
 Contributors: Swfung8
Image:William Rowan Hamilton painting.jpg  Source: https://en.wikipedia.org/w/index.php?title=File:William_Rowan_Hamilton_painting.jpg  License: Public Domain  Contributors: Quibik
Image:Weighted K4.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Weighted_K4.svg  License: Creative Commons Attribution-Sharealike 2.5  Contributors: Sdo
Image:Aco TSP.svg  Source: https://en.wikipedia.org/w/index.php?title=File:Aco_TSP.svg  License: Creative Commons Attribution-ShareAlike 3.0 Unported  Contributors: User:Nojhan,
User:Nojhan
File:AntColony.gif  Source: https://en.wikipedia.org/w/index.php?title=File:AntColony.gif  License: Creative Commons Attribution-Sharealike 3.0  Contributors: User:Saurabh.harsh
License 157

License
Creative Commons Attribution-Share Alike 3.0
//creativecommons.org/licenses/by-sa/3.0/

View publication stats

You might also like