From 2c226753ab56ab3f2d66686ff689f89c79f458b4 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Fri, 6 May 2016 06:35:11 -0700 Subject: [PATCH] Hadrons: comments on graph theory algorithm complexity --- programs/Hadrons/Graph.hpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/programs/Hadrons/Graph.hpp b/programs/Hadrons/Graph.hpp index c7591350..e43f98d1 100644 --- a/programs/Hadrons/Graph.hpp +++ b/programs/Hadrons/Graph.hpp @@ -116,14 +116,20 @@ makeDependencyMatrix(const std::vector> &topSort); /****************************************************************************** * template implementation * - ******************************************************************************/ + ****************************************************************************** + * in all the following V is the number of vertex and E is the number of edge + * in the worst case E = V^2 + */ + // access ////////////////////////////////////////////////////////////////////// +// complexity: log(V) template void Graph::addVertex(const T &value) { isMarked_[value] = false; } +// complexity: O(log(V)) template void Graph::addEdge(const Edge &e) { @@ -132,12 +138,14 @@ void Graph::addEdge(const Edge &e) edgeSet_.insert(e); } +// complexity: O(log(V)) template void Graph::addEdge(const T &start, const T &end) { addEdge(Edge(start, end)); } +// complexity: O(V*log(V)) template void Graph::removeVertex(const T &value) { @@ -167,6 +175,7 @@ void Graph::removeVertex(const T &value) } } +// complexity: O(log(V)) template void Graph::removeEdge(const Edge &e) { @@ -182,12 +191,14 @@ void Graph::removeEdge(const Edge &e) } } +// complexity: O(log(V)) template void Graph::removeEdge(const T &start, const T &end) { removeEdge(Edge(start, end)); } +// complexity: O(1) template unsigned int Graph::size(void) const { @@ -195,6 +206,7 @@ unsigned int Graph::size(void) const } // tests /////////////////////////////////////////////////////////////////////// +// complexity: O(log(V)) template bool Graph::gotValue(const T &value) const { @@ -211,6 +223,7 @@ bool Graph::gotValue(const T &value) const } // vertex marking ////////////////////////////////////////////////////////////// +// complexity: O(log(V)) template void Graph::mark(const T &value, const bool doMark) { @@ -224,6 +237,7 @@ void Graph::mark(const T &value, const bool doMark) } } +// complexity: O(V*log(V)) template void Graph::markAll(const bool doMark) { @@ -233,18 +247,21 @@ void Graph::markAll(const bool doMark) } } +// complexity: O(log(V)) template void Graph::unmark(const T &value) { mark(value, false); } +// complexity: O(V*log(V)) template void Graph::unmarkAll(void) { markAll(false); } +// complexity: O(log(V)) template bool Graph::isMarked(const T &value) const { @@ -260,6 +277,7 @@ bool Graph::isMarked(const T &value) const } } +// complexity: O(log(V)) template const T * Graph::getFirstMarked(const bool isMarked) const { @@ -279,6 +297,7 @@ const T * Graph::getFirstMarked(const bool isMarked) const } } +// complexity: O(log(V)) template const T * Graph::getFirstUnmarked(void) const { @@ -286,6 +305,7 @@ const T * Graph::getFirstUnmarked(void) const } // prune marked/unmarked vertices ////////////////////////////////////////////// +// complexity: O(V^2*log(V)) template void Graph::removeMarked(const bool isMarked) { @@ -300,6 +320,7 @@ void Graph::removeMarked(const bool isMarked) } } +// complexity: O(V^2*log(V)) template void Graph::removeUnmarked(void) { @@ -307,12 +328,14 @@ void Graph::removeUnmarked(void) } // depth-first search marking ////////////////////////////////////////////////// +// complexity: O(V*log(V)) template void Graph::depthFirstSearch(void) { depthFirstSearch(isMarked_.begin()->first); } +// complexity: O(V*log(V)) template void Graph::depthFirstSearch(const T &root) { @@ -330,6 +353,7 @@ void Graph::depthFirstSearch(const T &root) } // graph topological manipulations ///////////////////////////////////////////// +// complexity: O(V*log(V)) template std::vector Graph::getAdjacentVertices(const T &value) const { @@ -357,6 +381,7 @@ std::vector Graph::getAdjacentVertices(const T &value) const return adjacentVertex; } +// complexity: O(V*log(V)) template std::vector Graph::getChildren(const T &value) const { @@ -377,6 +402,7 @@ std::vector Graph::getChildren(const T &value) const return child; } +// complexity: O(V*log(V)) template std::vector Graph::getParents(const T &value) const { @@ -397,6 +423,7 @@ std::vector Graph::getParents(const T &value) const return parent; } +// complexity: O(V^2*log(V)) template std::vector Graph::getRoots(void) const { @@ -415,6 +442,7 @@ std::vector Graph::getRoots(void) const return root; } +// complexity: O(V^2*log(V)) template std::vector> Graph::getConnectedComponents(void) const { @@ -435,6 +463,7 @@ std::vector> Graph::getConnectedComponents(void) const } // topological sort using Tarjan's algorithm +// complexity: O(V*log(V)) template std::vector Graph::topoSort(void) { @@ -493,6 +522,7 @@ std::vector Graph::topoSort(void) // generate all possible topological sorts // Y. L. Varol & D. Rotem, Comput. J. 24(1), pp. 83–84, 1981 // http://comjnl.oupjournals.org/cgi/doi/10.1093/comjnl/24.1.83 +// complexity: O(V*log(V)) template std::vector> Graph::allTopoSort(void) { @@ -570,6 +600,7 @@ std::vector> Graph::allTopoSort(void) } // build depedency matrix from topological sorts /////////////////////////////// +// complexity: can be V! template std::map> makeDependencyMatrix(const std::vector> &topSort)