As of Version 10, most of the functionality of the Combinatorica package is built into the Wolfram System. >>


Combinatorica extends the Wolfram Language by over 450 functions in combinatorics and graph theory. It includes functions for constructing graphs and other combinatorial objects, computing invariants of these objects, and finally displaying them. This documentation covers only a subset of these functions. The best guide to this package is the book Computational Discrete Mathematics: Combinatorics and Graph Theory with Mathematica, by Steven Skiena and Sriram Pemmaraju, published by Cambridge University Press, 2003. The new Combinatorica is a substantial rewrite of the original 1990 version. It is now much faster than before, and provides improved graphics and significant additional functionality.

You are encouraged to visit the website,, where you will find the latest release of the package, an editor for Combinatorica graphs, and additional files of interest.

This loads the package.
Click for copyable input

Permutations and Combinations

Permutations and subsets are the most basic combinatorial objects. Combinatorica provides functions for constructing objects both randomly and deterministically, to rank and unrank them, and to compute invariants on them. Here are examples of some of these functions in action.

These permutations are generated in minimum change order, where successive permutations differ by exactly one transposition. The builtin generator Permutations constructs permutations in lexicographic order.
Click for copyable input
The ranking function illustrates that the builtin function Permutations uses lexicographic sequencing.
Click for copyable input
With distinct permutations of three elements, within 20 random permutations you are likely to see all of them. Observe that it is unlikely for the first six permutations to all be distinct.
Click for copyable input
A fixed point of a permutation is an element in the same position in as in the inverse of . Thus, the only fixed point in this permutation is 7.
Click for copyable input
The identity permutation consists of singleton cycles or fixed points.
Click for copyable input
The classic problem in Polya theory is counting how many different ways necklaces can be made out of beads, when there are different types or colors of beads to choose from. When two necklaces are considered the same if they can be obtained only by shifting the beads (as opposed to turning the necklace over), the symmetries are defined by permutations, each of which is a cyclic shift of the identity permutation. When a variable is specified for the number of colors, a polynomial results.
Click for copyable input
The number of inversions in a permutation is equal to that of its inverse.
Click for copyable input
Generating subsets incrementally is efficient when the goal is to find the first subset with a given property, since every subset need not be constructed.
Click for copyable input
In a Gray code, each subset differs in exactly one element from its neighbors. Observe that the last eight subsets all contain Null, while none of the first eight do.
Click for copyable input
A subset is a subset with exactly elements in it. Since the lead element is placed in first, the subsets are given in lexicographic order.
Click for copyable input

Combinatorica functions for permutations.

Combinatorica functions for subsets.

Combinatorica functions for group theory.

Partitions, Compositions, and Young Tableaux

A partition of a positive integer is a set of strictly positive integers whose sum is . A composition of is a particular arrangement of non-negative integers whose sum is . A set partition of elements is a grouping of all the elements into nonempty, nonintersecting subsets. A Young tableau is a structure of integers where the number of elements in each row is defined by an integer partition of . Further, the elements of each row and column are in increasing order, and the rows are left justified. These four related combinatorial objects have a host of interesting applications and properties.

Here are the eleven partitions of 6. Observe that they are given in reverse lexicographic order.
Click for copyable input
Although the number of partitions grows exponentially, it does so more slowly than permutations or subsets, so complete tables can be generated for larger values of .
Click for copyable input
Ferrers diagrams represent partitions as patterns of dots. They provide a useful tool for visualizing partitions, because moving the dots around provides a mechanism for proving bijections between classes of partitions. This constructs a random partition of 100.
Click for copyable input
Here every composition of 5 into 3 parts is generated exactly once.
Click for copyable input
Set partitions are different than integer partitions, representing the ways you can partition distinct elements into subsets. They are useful for representing colorings and clusterings.
Click for copyable input
The list of tableaux of shape illustrates the amount of freedom available to tableau structures. The smallest element is always in the upper lefthand corner, but the largest element is free to be the rightmost position of the last row defined by the distinct parts of the partition.
Click for copyable input
By iterating through the different integer partitions as shapes, all tableaux of a particular size can be constructed.
Click for copyable input
The hook length formula can be used to count the number of tableaux for any shape. Using the hook length formula over all partitions of computes the number of tableaux on elements.
Click for copyable input
Each of the 117,123,756,750 tableaux of this shape will be selected with equal likelihood.
Click for copyable input
A pigeonhole result states that any sequence of distinct integers must contain either an increasing or a decreasing scattered subsequence of length .
Click for copyable input

Combinatorica functions for integer partitions.

Combinatorica functions for set partitions.

Combinatorica functions for Young tableaux.

Combinatorica functions for counting.

Representing Graphs

A graph is defined as a set of vertices with a set of edges, where an edge is defined as a pair of vertices. The representation of graphs takes on different requirements depending upon whether the intended consumer is a person or a machine. Computers digest graphs best as data structures such as adjacency matrices or lists. People prefer a visualization of the structure as a collection of points connected by lines, which implies adding geometric information to the graph.

In the complete graph on five vertices, denoted , each vertex is adjacent to all other vertices. CompleteGraph[n] constructs the complete graph on vertices.
Click for copyable input
The internals of the graph representation are not shown to the useronly a notation with the number of edges and vertices, followed by whether the graph is directed or undirected.
Click for copyable input
The adjacency matrix of shows that each vertex is adjacent to all other vertices. The main diagonal consists of zeros, since there are no selfloops in the complete graph, meaning edges from a vertex to itself.
Click for copyable input
The standard embedding of consists of five vertices equally spaced on a circle.
Click for copyable input
The number of vertices in a graph is termed the order of the graph.
Click for copyable input
M returns the number of edges in a graph.
Click for copyable input
Edge/vertex colors/styles can be globally modified, giving complete flexibility to change the appearance of a graph.
Click for copyable input
The colors, styles, labels, and weights of individual vertices and edges can also be changed individually, perhaps to highlight interesting features of the graph.
Click for copyable input
A star is a tree with one vertex of degree . Adding any new edge to a star produces a cycle of length 3.
Click for copyable input
Graphs with multiedges and selfloops are supported. Here there are two copies of each edge of a star.
Click for copyable input
The adjacency list representation of a graph consists of lists: one list for each vertex , , which records the vertices to which is adjacent. Each vertex in the complete graph is adjacent to all other vertices.
Click for copyable input
There are ordered pairs of edges defined by a complete graph of order .
Click for copyable input
An induced subgraph of a graph is a subset of the vertices of together with any edges whose endpoints are both in this subset. An induced subgraph that is complete is called a clique. Any subset of the vertices in a complete graph defines a clique.
Click for copyable input
The vertices of a bipartite graph have the property that they can be partitioned into two sets such that no edge connects two vertices of the same set. Contracting an edge in a bipartite graph can ruin its bipartiteness. Note the selfloop created by the contraction.
Click for copyable input
A breadthfirst search of a graph explores all the vertices adjacent to the current vertex before moving on. A breadthfirst traversal of a simple cycle alternates sides as it wraps around the cycle.
Click for copyable input
In a depthfirst search, the children of the first son of a vertex are explored before visiting his brothers. The depthfirst traversal differs from the breadthfirst traversal above in that it proceeds directly around the cycle.
Click for copyable input
Different drawings or embeddings of a graph can reveal different aspects of its structure. The default embedding for a grid graph is a ranked embedding from all the vertices on one side. Ranking from the center vertex yields a different but interesting drawing.
Click for copyable input
The radial embedding of a tree is guaranteed to be planar, but radial embeddings can be used with any graph. Here is a radial embedding of a random labeled tree.
Click for copyable input
By arbitrarily selecting a root, any tree can be represented as a rooted tree.
Click for copyable input
An interesting general heuristic for drawing graphs models the graph as a system of springs and lets Hooke's law space the vertices. Here it does a good job illustrating the join operation, where each vertex of is connected to each of two disconnected vertices. In achieving the minimum energy configuration, these two vertices end up on different sides of .
Click for copyable input

Combinatorica functions for modifying graphs.

Combinatorica functions for graph format translation.

Combinatorica options for graph functions.

Combinatorica functions for graph labels and weights.

Combinatorica functions for drawing graphs.

Generating Graphs

Many graphs consistently prove interesting, in the sense that they are models of important binary relations or have unique graph theoretic properties. Often these graphs can be parameterized, such as the complete graph on vertices , giving a concise notation for expressing an infinite class of graphs. Start off with several operations that act on graphs to give different graphs and which, together with parameterized graphs, give the means to construct essentially any interesting graph.

The union of two connected graphs has two connected components.
Click for copyable input
Graph products can be very interesting. The embedding of a product has been designed to show off its structure, and is formed by shrinking the first graph and translating it to the position of each vertex in the second graph.
Click for copyable input
The line graph of a graph has a vertex of associated with each edge of , and an edge of if and only if the two edges of share a common vertex.
Click for copyable input
Circulants are graphs whose adjacency matrix can be constructed by rotating a vector times, and include complete graphs and cycles as special cases. Even random circulant graphs have an interesting, regular structure.
Click for copyable input
Some graph generators create directed graphs with selfloops, such as this de Bruijn or shift register graph encoding all length5 substrings of a binary alphabet.
Click for copyable input
Hypercubes of dimension are the graph product of cubes of dimension and the complete graph . Here, a Hamiltonian cycle of the hypercube is highlighted. Colored highlighting and graph animations are also provided in the package.
Click for copyable input
Several of the builtin graph construction functions do not have parameters and only construct a single interesting graph. FiniteGraphs collects them together in one list for convenient reference. ShowGraphArray permits the display of multiple graphs in one window.
Click for copyable input

Combinatorica functions for graph constructors.

Properties of Graphs

Graph theory is the study of properties or invariants of graphs. Among the properties of interest are such things as connectivity, cycle structure, and chromatic number. Here is a demonstration of how to compute several different graph invariants.

An undirected graph is connected if a path exists between any pair of vertices. Deleting an edge from a connected graph can disconnect it. Such an edge is called a bridge.
Click for copyable input
GraphUnion can be used to create disconnected graphs.
Click for copyable input
An orientation of an undirected graph is an assignment of exactly one direction to each of the edges of . Note that arrows denoting the direction of each edge are automatically drawn in displaying directed graphs.
Click for copyable input
An articulation vertex of a graph is a vertex whose deletion disconnects . Any graph with no articulation vertices is said to be biconnected. A graph with a vertex of degree 1 cannot be biconnected, since deleting the other vertex that defines its only edge disconnects the graph.
Click for copyable input
The only articulation vertex of a star is its center, even though its deletion leaves connected components. Deleting a leaf leaves a connected tree.
Click for copyable input
Every edge in a tree is a bridge.
Click for copyable input
A graph is said to be connected if there does not exist a set of vertices whose removal disconnects the graph. The wheel is the basic triconnected graph.
Click for copyable input
A graph is edgeconnected if there does not exist a set of edges whose removal disconnects the graph. The edge connectivity of a graph is at most the minimum degree , since deleting those edges disconnects the graph. Complete bipartite graphs realize this bound.
Click for copyable input
These two complete bipartite graphs are isomorphic, since the order of the two stages is simply reversed. Here, all isomorphisms are returned.
Click for copyable input
A graph is selfcomplementary if it is isomorphic to its complement. The smallest nontrivial selfcomplementary graphs are the path on four vertices and the cycle on five.
Click for copyable input
A directed graph in which half the possible edges exist is almost certain to contain a cycle. Directed acyclic graphs are often called DAGs.
Click for copyable input
The girth of a graph is the length of its shortest cycle. A cage is the smallest possible regular graph (here degree 3) that has a prescribed girth.
Click for copyable input
An Eulerian cycle is a complete tour of all the edges of a graph. An Eulerian cycle of a bipartite graph bounces back and forth between the stages.
Click for copyable input

Combinatorica functions for graph predicates.

A Hamiltonian cycle of a graph is a cycle that visits every vertex in exactly once, as opposed to an Eulerian cycle that visits each edge exactly once. for are the only Hamiltonian complete bipartite graphs.
Click for copyable input
The divisibility relation between integers is reflexive, since each integer divides itself, and antisymmetric, since cannot divide if . Finally, it is transitive, as implies for some integer , so implies .
Click for copyable input
Since the divisibility relation is reflexive, transitive, and antisymmetric, it is a partial order.
Click for copyable input
A graph is transitive if any three vertices , , , such that edges , imply . The transitive reduction of a graph is the smallest graph such that . The transitive reduction eliminates all implied edges in the divisibility relation, such as , , , and .
Click for copyable input
The Hasse diagram clearly shows the lattice structure of the Boolean algebra, the partial order defined by inclusion on the set of subsets.
Click for copyable input
A topological sort is a permutation of the vertices of a graph such that an edge implies that appears before in . A complete directed acyclic graph defines a total order, so there is only one possible output from TopologicalSort.
Click for copyable input
Any labeled graph can be colored in a certain number of ways with exactly colors . This number is determined by the chromatic polynomial of the graph.
Click for copyable input

Combinatorica functions for graph invariants.

Algorithmic Graph Theory

Finally, there are several invariants of graphs that are of particular interest because of the algorithms that compute them.

The shortestpath spanning tree of a grid graph is defined in terms of Manhattan distance, where the distance between points with coordinates and is .
Click for copyable input
In an unweighted graph, there can be many different shortest paths between any pair of vertices. This path between two opposing corners goes all the way to the right, then all the way to the top.
Click for copyable input
A minimum spanning tree of a weighted graph is a set of edges of minimum total weight that form a spanning tree of the graph. Any spanning tree is a minimum spanning tree when the graphs are unweighted.
Click for copyable input
The number of spanning trees of a complete graph is , as was proved by Cayley.
Click for copyable input
The maximum flow through an unweighted complete bipartite graph is the minimum degree .
Click for copyable input
A matching, in a graph , is a set of edges of such that no two of them share a vertex in common. A perfect matching of an even cycle consists of alternating edges in the cycle.
Click for copyable input
Any maximal matching of a is a maximum matching, and perfect if is even.
Click for copyable input

Combinatorica functions for graph algorithms.