Package: postman_problems
Note to those reading this on PyPI: For a better reading experience, checkout the README on GitHubhere.GitHub and PyPI are not
cooperating on rendering SVGs.Table of Contents:1Contents2Install2.1Basic2.2Viz3Usage3.1CLI3.2Python4Examples4.1CPP: Seven Bridges of Konigsberg4.2RPP: Star4.3RPP: Sleeping Giant5Developers6Release Notes7License1ContentsThis package contains implementations to solve the suite ofPostman Problemsfrom graph theory.Algorithms currently implemented:Chinese PostmanRural Postman (partial)The Rural Postman implementation is currently restricted to a subset of problems.  Specifically to graphs where
the required edges form a single connected component when the optional edges are removed.2Install2.1BasicOption 1. Install from PyPI:Stable release.pipinstallpostman_problemsOption 2. Install from GitHub:As this project develops, GitHub will have the most recent features, but no
guarantees they’ll be stable.Clone the repo.git clone https://github.com/brooksandrew/postman_problems.git
cd postman_problemsInstall with pip.  Builds are tested on Python 2.7, 3.3, 3.4, 3.5, 3.6.pip install .2.2Vizpostman_problemsleveragesGraphvizfor visualization which unlocks more robust visualizations than just NetworkX
and matplotlib.  However, this also comes with several dependencies.  These are managed separately from the
base package, so users can optimize graphs to their heart’s content unencumbered from the weight and hassle of
installing viz dependencies, if they so choose.Install optional Python visualization libraries.pip install postman_problems[viz]Install Graphviz.  You need this underlying software application in addition to thegraphviz python packagewhich
wraps around it.  Checkout theGraphviz Download pagefor the best way to install on your OS.For Mac, this should be as easy as:brew install graphvizFor Linux,sudo apt-get install graphvizThese are the installs I’m currently using on my builds for thetests on TravisCI.  YMMV.  For Windows users and
for those where these methods fail, I defer to the Graphviz download docs.3Usage3.1CLIThe easiest way to start is with the command line using the entry points installed with this package:chinese_postmanandrural_postman.3.1.1Arguments: edgelistThere are several optional command line arguments, but the only one required is--edgelist:Description:Filename of edgelist.  Expected to be comma delimited text file readable with pandas.read_csv.  The first two columns
should be the “from” and “to” node names.  Additional columns can be provided for edge attributes.  The first row
should be the edge attribute names.A note on some edge attributes:required: must be provided for the RPP.  0 is used for optional edges, 1 for required.distance: default edge attribute used for shortest path computations.  Can be overridden withedge_weight.id: recommended to not include, but will be used if provided.  This will be generated automatically to assist with
computation of parallel edges.  If provided, it should be unique to ensure stable computation.3.1.2Arguments: othersFor the complete list of optional arguments run one of the following:chinese_postman --help
rural_postman --helpThe big ones are--vizand--animation, which when present will create the static (single visualization) and
animation of the postman problem solution.  Most of the other arguments modify the default values used for the
visualizations.3.1.3Simple exampleBelow we solve the CPP on theSeven Bridges of Konigsbergnetwork.  The edgelist is provided in this repo, but you
can swap this out for any comma delimited text file where the first two columns represent the node pairs in your network.chinese_postman --edgelist postman_problems/examples/seven_bridges/edgelist_seven_bridges.csvIf thechinese_postmanentry point is not working for whatever reason, you can run the script directly with:python postman_problems/chinese_postman.py --edgelist postman_problems/examples/seven_bridges/edgelist_seven_bridges.csvYou should see output that describes the CPP route solution (Eulerian circuit through each edge).  Something like this:('A', 'C', 1, {'trail': 'd', 'distance': 10, 'id': 3})
('C', 'D', 0, {'trail': 'g', 'distance': 3, 'id': 6, 'augmented': True})
('D', 'C', 0, {'trail': 'g', 'distance': 3, 'id': 6})
('C', 'A', 0, {'trail': 'c', 'distance': 2, 'id': 2})
('A', 'D', 0, {'trail': 'e', 'distance': 1, 'id': 4})
('D', 'B', 0, {'trail': 'f', 'distance': 9, 'id': 5})
('B', 'A', 0, {'trail': 'a', 'distance': 3, 'id': 0, 'augmented': True})
('A', 'B', 1, {'trail': 'b', 'distance': 5, 'id': 1})
('B', 'A', 0, {'trail': 'a', 'distance': 3, 'id': 0})The first two values of each tuple are the “from” and the “to” node respectively for each edge in the circuit.The third value indicates the key of the edge on the MultiGraph.  These will be 0 unless there are parallel edges.The fourth value contains the edge attributes for each edge walked.  These are mostly grabbed from the starting graph,
with two exceptions:augmentedis added to edges after their first walk (double backing… the thing we want to minimize)idis generated to aid computation in the case of parallel edges.  This can generally be ignored.A summary report of the solution should be printed.  Something like this:Solution summary stats:
distance_walked : 39
distance_doublebacked : 6
distance_walked_once : 33
distance_walked_optional : 0
distance_walked_required : 39
edges_walked : 9
edges_doublebacked : 2
edges_walked_once : 7
edges_walked_optional : 0
edges_walked_required : 93.2PythonThe postman solvers are modules that can also be imported and run within a Python environment.  This might interest you
if solving the CPP/RPP is just one step in your problem, you’d like to poke and prod at the output, or you’d like to tweak
the visualization or optimization parameters beyond what’s provided from the CLI.The snippet below should produce exactly the same output as printed above inCLI.frompostman_problems.solverimportcppfrompostman_problems.statsimportcalculate_postman_solution_stats# find CPP solutioncircuit,graph=cpp(edgelist_filename='postman_problems/examples/seven_bridges/edgelist_seven_bridges.csv',start_node='D')# print solution routeforeincircuit:print(e)# print solution summary statsfork,vincalculate_postman_solution_stats(circuit).items():print(k,v)4ExamplesThree examples are included inpostman_problemswhich demonstrate end-to-end usage: raw edgelist & nodelist =>
optimization and visualization.Examples are added as entry points and pre-configured arguments, so they can be executed with the single commands below.Note, the visualization step will write a GIF and a series of PNGs to your filesystem.  The paths are locked intopostman_problems/examples/[example_name]/output/.An expected exception will be thrown if you don’t have the visualization dependencies.  But no worries,
the output is prepackaged into/examplesand images are embedded below.Each example will produce the following file types:cpp_graph: representation ofcpp_graph.svgin theDOTgraph description language.  This is provided mostly
for reference, or for tweaking.cpp_graph.svg: static image with edge attributes annotating the walk sequence.cpp_graph.gif: animation highlighting each edge in the Euler circuit (solution route) as it’s walked.png/img*.png: PNGs generated for each frame of the GIF (omitted from package, but will hit your filesystem when
you run the examples).4.1CPP: Seven Bridges of KonigsbergThe Seven Bridges of Konigsberg is rather simple network with just 4 nodes and 7 edges.  Although small, it does contain
2 parallel edges which introduce some complexity to the CPP computation.This was the graph with which Leonhard Euler famously postulated in 1736 that there exists a path which visits each edge
exactly once if all nodes have even degree. Although this wasn’t proven until the 1870s by Carl Hierholzer, Euler was
right and this property is a key part of solving the Postman Problems.This contrived example has been bundled and parameterized into a script that can be run with:chinese_postman_seven_bridgesThe example can also be run using the verbose method below which allows you to parameterize more pieces.
Many of the options provided below are defaults and can be excluded in practice. They are included here simply to convey
what the possibilities are:chinese_postman --edgelist postman_problems/examples/seven_bridges/edgelist_seven_bridges.csv \
--viz \
--viz_filename 'postman_problems/examples/seven_bridges/output/cpp_graph.svg' \
--viz_engine 'dot' \
--animation \
--animation_filename 'postman_problems/examples/seven_bridges/output/cpp_graph.gif' \
--animation_images_dir 'postman_problems/examples/seven_bridges/output/img' \
--animation_engine 'dot' \
--animation_format 'png' \
--fps 2base_cpp_graph.svg: This is the starting graph.  Edges are annotated by distance.cpp_graph.svg: Edges are annotated with the order in which they are walked, starting at 0.  Edges walked more than
once are annotated by a sequence of numbers (walk order) andbolded.cpp_graph.gif: The nodes and edges in red indicate the current direction.cpp_graph: dot representation of the graph is also provided.  This is mostly for reference, but in rare cases you may
want to tweak graphviz parameters directly here.graph {
    graph [forcelabels=true "strict"=false]
    C [label=C]
    D [label=D]
    A [label=A]
    B [label=B]
            C -- D [label=9 decorate=true distance=3 id=6 penwidth=1 trail=g]
            C -- A [label="6, 8" augmented=True decorate=true distance=2 id=2 penwidth=4 trail=c]
            C -- A [label=7 decorate=true distance=10 id=3 penwidth=1 trail=d]
            D -- A [label="0, 3" augmented=True decorate=true distance=1 id=4 penwidth=4 trail=e]
            D -- B [label=4 decorate=true distance=9 id=5 penwidth=1 trail=f]
            A -- B [label="2, 5" augmented=True decorate=true distance=3 id=0 penwidth=4 trail=a]
            A -- B [label=1 decorate=true distance=5 id=1 penwidth=1 trail=b]
}4.2RPP: StarThis is a simple example that demonstrates the power of the RPP vs CPP.Run with:rural_postman_starbase_rpp_graph.svg: Required edges are solid. Optional edges are dotted.  Simply showing the edge distances here.cpp_graph_req.svg: If we solve this with the CPP and only only consider the required edges, we get the pretty inefficient solution below
doubling back on every single edge.cpp_graph_opt.svg: If we recognize the optional edges, and apply the the CPP again (where the optional edges are treated
as required edges), we get a slightly better solution:rpp_graph.svg: When we recognize the optional edges as truly optional and employ the RPP, we get the optimal solution
where we walk all required edges exactly once and only use a subset of optional edges:rpp_graph.gif: Same information as above, but in an animation… because flashy moving pictures are fun.4.3RPP: Sleeping GiantThis example is near and dear to my heart and the motivation for this project in the first place.Sleeping Giantis a state park near my hometown in Hamden CT with a little challenge called theGiant Master Program.
Those who hike every trail (seetrail map) are awarded the honor of Giantmaster Marathoner and earn themselves a
spot on theGiantmaster rosterand the glory of a red highlight on their name.That’s all I’ll say here.  I wrote more about the personal motivation and Sleeping Giant specific data/problem in aDataCamp tutorialwhich also helped motivate this project.Run this example with:rural_postman_sleeping_giantpostman_problems/examples/sleeping_giant/rpp_graph.svg:The optional edges are marked with a dotted line.  You’ll notice that not all of them are utilized (no edge label
annotating their order in the route).postman_problems/examples/sleeping_giant/rpp_graph.gif(omitted from package due to size):  Can be viewedhere.Here are the solution summary stats.RPP Solution summary stats:

Solution summary stats:
distance_walked : 32.119999999999976
distance_doublebacked : 6.11
distance_walked_once : 26.009999999999977
distance_walked_optional : 0.68
distance_walked_required : 31.439999999999976
edges_walked : 151
edges_doublebacked : 30
edges_walked_once : 121
edges_walked_optional : 2
edges_walked_required : 149A CPP example is also provided with entry pointchinese_postman_sleeping_giant.  The solution is very similar,
so it is omitted here.For a base of comparison on RPP vs CPP, selected stats are printed below for the CPP.  the RPP shortens the CPP solution
route by about 1 mile.CPP Solution summary stats:

distance_walked : 33.24999999999998
distance_doublebacked : 7.240000000000001
distance_walked_once : 26.009999999999977
edges_walked : 155
edges_doublebacked : 34
edges_walked_once : 1215DevelopersIf you’d like to fork or contribute directly to this project (PRs welcome), or simply want run the tests, here’s how:Clone/Fork repoFull install with test and viz dependencies.pip install .[test,viz]Or do an editable install from the beginning.  This is my typical approach when developing.pip install -e .[test,viz]Run testspython -m pytest
pytest --covSome tests take quite a while to run.  Namely the examples that write visualizations to the filesystem for large networks.As I have limited patience while developing, but am too cautious to drop them completely, I’ve kept and marked them with the@slowand@longdecorators.conftest.pyis configured to exclude them by default with a simple run ofpytestorpython-mpytest, but the full test suite can be run by:python -m pytest --runslow
pytest --cov --runslow6Release NotesCheckout the release notes in Gitubhere.If I’m doing a good job of keeping PyPI updated, each release should also be availablehere.7LicenseReleased under the MIT License (seeLICENSE.txt).Copyright (C) 2017 Andrew Brooks.