Skip to content

nspo/graphs-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

64f71c8 · Feb 7, 2021

History

18 Commits
Jan 29, 2021
Jan 13, 2021
Dec 30, 2020
Feb 7, 2021
Feb 7, 2021
Feb 7, 2021
Feb 7, 2021
Dec 30, 2020
Dec 30, 2020
Feb 7, 2021
Jan 29, 2021
Dec 29, 2020
Feb 7, 2021

Repository files navigation

Graphs and digraphs with modern C++

General

  • Code for weighted and unweighted graph and digraph problems
  • No usage of owning raw pointers, new etc. (uses std::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.

General headers (general/)

include/

  • 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), and PriorityQueue as a simpler version which does not

Unweighted graphs (unweighted_graph/)

include/

unweighted_graph_demo.cpp

  • 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:

Unweighted digraphs (unweighted_digraph/)

include/

unweighted_digraph_demo.cpp

  • 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:

Weighted graphs (weighted_graph/)

include/

weighted_graph_demo.cpp

  • 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:

Weighted digraphs (weighted_digraph/)

include/

  • EdgeWeightedDigraph interface and an implementation with adjacency lists
  • SingleSourceShortestPath as an interface for finding shortest paths to all other nodes starting at a start vertex:
    • SingleSourceAcyclicShortestPath for acyclic weighted digraphs using the topological order
    • SingleSourceDijkstraShortestPath as an implementation of Dijkstra's algorithm for weighted digraphs without negative edge weights
    • SingleSourceBellmanFordShortestPath for digraphs with negative edge weights (but without negative cycles)

weighted_digraph_demo.cpp

  • 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):

Flow networks (flow_network/)

include/

  • FlowEdge as an edge with capacity and flow in a flow network
  • FlowNetwork and AdjancyListFlowNetwork as interface and implementation of a flow network
  • FordFulkerson as an implementation of the Ford-Fulkerson algorithm to solve the min-cut and max-flow problems in flow networks

flow_network_demo.cpp

  • Reads flow network from file or standard input and prints it

  • Calculate max-flow and min-cut

Compilation and execution

  • 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

References

  • Introduction to Algorithms by Cormen et al.
  • Algorithms, Part II by Princeton University (Robert Sedgewick, Kevin Wayne)