- Code for weighted and unweighted graph and digraph problems
- No usage of owning raw pointers,
new
etc. (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.
DisjointSets
as an efficient union-find data structure with path compression and union-by-rank (see also here)IndexedPriorityQueue
as a priority queue which supports changing keys in logarithmic time (see here), andPriorityQueue
as a simpler version which does not
- Simple (undirected)
Graph
interface 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.h
functionality - 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
Digraph
interface 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.h
functionality -
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
:
EdgeWeightedGraph
interface 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.txt
and its Minimum Spanning Tree:
EdgeWeightedDigraph
interface and an implementation with adjacency listsSingleSourceShortestPath
as an interface for finding shortest paths to all other nodes starting at a start vertex:SingleSourceAcyclicShortestPath
for acyclic weighted digraphs using the topological orderSingleSourceDijkstraShortestPath
as an implementation of Dijkstra's algorithm for weighted digraphs without negative edge weightsSingleSourceBellmanFordShortestPath
for 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):
FlowEdge
as an edge with capacity and flow in a flow networkFlowNetwork
andAdjancyListFlowNetwork
as interface and implementation of a flow networkFordFulkerson
as 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.txt
build/unweighted_digraph/unweighted_digraph_demo unweighted_digraph/tinyDG.txt
build/weighted_graph/weighted_graph_demo weighted_graph/tinyEWG.txt
build/weighted_digraph/weighted_digraph_demo weighted_digraph/tinyEWD.txt
build/weighted_digraph/weighted_digraph_demo weighted_digraph/tinyEWDAG.txt
build/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)