# 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
• 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)
• 1-connected components
• 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.