# Graphs and Graph Algorithms in C++

## Synopsis

This C++ template library implements directed and undirected graphs with constant-time random access to incident vertices and edges. Vertices and edges can always be indexed by contiguous integers as well as by STL-compliant random access iterators. This simplifies the implementation of algorithms for static graphs. In dynamic graphs (where vertices or edges can be removed), their changing integer indices can be followed, if necessary, via callbacks. Subgraphs are defined by subgraph masks.

## Features

• Graph classes
• Graph
• Digraph
• Complete graph
• d-dimensional grid graph
• Constant-time random access to incident vertices and edges
• by contiguous integer indices
• by STL-compliant random access iterators
• Insertion and removal of vertices and edges in graph and digraphs
• Tracking of integer indices of vertices and edges by callbacks
• Multiple edges between the same pair of vertices (disabled by default)

## Time Complexity

 Operation Graph Digraph CompleteGraph GridGraph n = numberOfVertices() O(1) O(1) O(1) O(1) n = numberOfEdges() O(1) O(1) O(1) O(1) n = numberOfEdgesFromVertex(v) O(1) O(1) O(1) O(d) n = numberOfEdgesToVertex(v) O(1) O(1) O(1) O(d) v = vertexOfEdge(e, j) O(1) O(1) O(1) O(d) e = edgeFromVertex(v, j) O(1) O(1) O(1) O(d) e = edgeToVertex(v, j) O(1) O(1) O(1) O(d) v = vertexFromVertex(v, j) O(1) O(1) O(1) O(d) v = vertexToVertex(v, j) O(1) O(1) O(1) O(d) r = findEdge(v0, v1) O(log deg G) O(log deg G) O(1) O(d2) v = insertVertex() O(1) O(1) n/a n/a v = insertVertices(n) O(1) O(1) n/a n/a e = insertEdge(v0, v1) O(deg G) O(deg G) n/a n/a eraseEdge(e) O(deg G) O(deg G) n/a n/a eraseVertex(v) O((deg G)2) O((deg G)2) n/a n/a

## Algorithms

• Search and traversal, implemented generically, with callbacks
• depth first search (DFS)
• breadth first search (BFS)
• 1-connected components
• by breadth-first search
• by disjoint sets
• 2-connected components
• cut-vertices/articulation vertices (Hopcroft-Tarjan Algorithm )
• cut-edges/bridges (Tarjan's algorithm )
• Shortest paths in weighted and unweighted graphs, as sequences of edges or vertices
• Single source shortest path (SSSP)
• Single pair shortest path (SPSP)
• Minimum spanning trees and minimum spanning forests
• Prim's algorithm 
• Dynamic Programming
• Maximum st-flow
• Goldberg-Tarjan Algorithm/Push-Relabel Algorithm  with FIFO vertex selection rule
• Edmonds-Karp Algorithm 
• Bipartite Matching
• Extension of the Munkres algorithm by Bourgeois and Lasalle 
• Minimum Cost (Lifted) Multicut [2,3]
• A Branch-and-Cut Algorithm based on  and Gurobi
• A generalization  of the Kernighan-Lin Algorithm 
• Greedy additive edge contraction 
• Set Partition 
• by specialization of minimum cost multicut algorithms for complete graphs

## References

1. Levinkov E., Uhrig J., Tang S., Omran M., Insafutdinov E., Kirillov A., Rother C., Brox T., Schiele B. and Andres B. Joint Graph Decomposition and Node Labeling: Problem, Algorithms, Applications. CVPR 2017
2. Horňáková A., Lange J.-H. and Andres B. Analysis and Optimization of Graph Decompositions by Lifted Multicuts. ICML 2017
3. Chopra S. and Rao M. R. The partition problem. Mathematical Programming 59(1-3):87-115. 1993
4. Edmonds J. and Karp R. M. Theoretical improvements in algorithmic efficiency for network flow problems. Journal of the ACM 19(2):248-264. 1972
5. Goldberg A. V. and Tarjan R. E. A new approach to the maximum-flow problem. Journal of the ACM 35(4):921-940. 1988
6. Hopcroft J. and Tarjan R. E. Efficient algorithms for graph manipulation. Communications of the ACM 16(6):372-378. 1973
7. Kernighan B. W. and Lin S. An efficient heuristic procedure for partitioning graphs. Bell Systems Technical Journal. 49:291-307. 1970
8. Keuper M., Levinkov E., Bonneel N., Lavoué G., Brox T. and Andres B. Efficient Decomposition of Image and Mesh Graphs by Lifted Multicuts. Int. Conf. on Computer Vision (ICCV) 2015
9. Prim R. C. Shortest connection networks and some generalizations. Bell System Technical Journal 36:1389-1401. 1957
10. Tarjan R. E. A note on finding the bridges of a graph. Information Processing Letters 2(6):160-161. 1974
11. Bourgeois F. and Lasalle J.-C. An extension of the Munkres algorithm for the assignment problem to rectangular matrices. Communications of the ACM 14(12):802-804 1971.

## License

Copyright © by Bjoern Andres (bjoern@andres.sc).

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

• Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
• Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
• The name of the author must not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This is Bjoern Andres' personal website. © Copy freely.