- Code for weighted and unweighted graph and digraph problems
- No usage of owning raw pointers,
newetc. (usesstd::unique_ptr,std::vector, ... instead) - Unit tests in
test/subdirectories - This is a sample project and not a huge library. It is currently not split in headers and implementations.
DisjointSetsas an efficient union-find data structure with path compression and union-by-rank (see also here)IndexedPriorityQueueas a priority queue which supports changing keys in logarithmic time (see here), andPriorityQueueas a simpler version which does not
- Simple (undirected)
Graphinterface AdjacencyListGraph(https://en.wikipedia.org/wiki/Adjacency_list)- Basic utility functions (a few)
- Find paths starting at a vortex to all connected vertices
- with Depth-first Search (DFS) recursively (
graph::find_paths_to_all::fromVertexToAllDfs) - with DFS iteratively (
graph::find_paths_to_all::fromVertexToAllDfsNoRec) - with Breadth-first Search (BFS) iteratively (
graph::find_paths_to_all::fromVertexToAllBfs)
- with Depth-first Search (DFS) recursively (
- Find connected components (https://en.wikipedia.org/wiki/Component_(graph_theory)) with DFS (
graph::ConnectedComponents) - Check whether a graph is bipartite with DFS (
graph::isBipartite)
- Basic test of
Graph.hfunctionality - Reads graphs from standard input (
build/unweighted_graph/unweighted_graph_demo < unweighted_graph/tinyG.txt) or file given as program argument (build/unweighted_graph/unweighted_graph_demo unweighted_graph/tinyG.txt) - Sample graph from
tinyG.txt:
- Simple
Digraphinterface AdjacencyListDigraph(https://en.wikipedia.org/wiki/Adjacency_list)- check for cycles
- determine topological sort (https://en.wikipedia.org/wiki/Topological_sorting)
- determine strongly-connected components with Kosaraju-Sharir algorithm (https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm)
-
Basic test of
Digraph.hfunctionality -
Reads digraph from standard input (
build/unweighted_digraph/unweighted_digraph_demo < unweighted_digraph/tinyDG.txt) or file given as program argument (build/unweighted_digraph/unweighted_digraph_demo unweighted_digraph/tinyDG.txt) -
Sample digraph from
tinyDG.txt:
EdgeWeightedGraphinterface and an implementation with adjacency lists- Minimum Spanning Tree identification with Kruskal's algorithm
-
Reads a weighted graph from standard input or a file given as program argument
-
Prints the graph's edges and the Minimum Spanning Tree edges
-
Sample edge-weighted graph from
tinyEWG.txtand its Minimum Spanning Tree:
EdgeWeightedDigraphinterface and an implementation with adjacency listsSingleSourceShortestPathas an interface for finding shortest paths to all other nodes starting at a start vertex:SingleSourceAcyclicShortestPathfor acyclic weighted digraphs using the topological orderSingleSourceDijkstraShortestPathas an implementation of Dijkstra's algorithm for weighted digraphs without negative edge weightsSingleSourceBellmanFordShortestPathfor digraphs with negative edge weights (but without negative cycles)
-
Reads a weighted digraph from standard input or a file given as program argument
-
Determines the shortest paths starting at vertex 0 and prints the results
- Depending on whether the digraph is acyclic, contains no negative edge weights, or does contain negative edge weights, the DAG algorithm, Dijkstra's algorithm, or the Bellman-Ford algorithm is chosen respectively
-
Sample edge-weighted digraph from
tinyEWD.txt(without showing weights): -
Sample edge-weighted acyclic digraph from
tinyEWDAG.txt(without showing weights):
FlowEdgeas an edge with capacity and flow in a flow networkFlowNetworkandAdjancyListFlowNetworkas interface and implementation of a flow networkFordFulkersonas an implementation of the Ford-Fulkerson algorithm to solve the min-cut and max-flow problems in flow networks
- Download submodules (for unit tests):
git submodule update --init --recursive - Compile with
cmake:mkdir build cd build/ cmake .. make - Go to top-level folder again:
cd .. - Run all tests:
find build/ -name "*_gtest" -exec {} \; - Run demos:
build/unweighted_graph/unweighted_graph_demo unweighted_graph/tinyG.txtbuild/unweighted_digraph/unweighted_digraph_demo unweighted_digraph/tinyDG.txtbuild/weighted_graph/weighted_graph_demo weighted_graph/tinyEWG.txtbuild/weighted_digraph/weighted_digraph_demo weighted_digraph/tinyEWD.txtbuild/weighted_digraph/weighted_digraph_demo weighted_digraph/tinyEWDAG.txtbuild/flow_network/flow_network_demo flow_network/tinyFN.txt
- Introduction to Algorithms by Cormen et al.
- Algorithms, Part II by Princeton University (Robert Sedgewick, Kevin Wayne)




