Small functions for discovering graph topology
authordeba
Mon, 03 Oct 2005 10:18:38 +0000
changeset 1698755cdc461ddd
parent 1697 4c593a4096da
child 1699 29428f7b8b66
Small functions for discovering graph topology
lemon/topology.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/topology.h	Mon Oct 03 10:18:38 2005 +0000
     1.3 @@ -0,0 +1,219 @@
     1.4 +/* -*- C++ -*-
     1.5 + * lemon/topology.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_TOPOLOGY_H
    1.21 +#define LEMON_TOPOLOGY_H
    1.22 +
    1.23 +#include <lemon/dfs.h>
    1.24 +#include <lemon/graph_utils.h>
    1.25 +
    1.26 +#include <lemon/concept/graph.h>
    1.27 +#include <lemon/concept/undir_graph.h>
    1.28 +#include <lemon/concept_check.h>
    1.29 +
    1.30 +/// \ingroup flowalgs
    1.31 +/// \file
    1.32 +/// \brief Topology related algorithms
    1.33 +///
    1.34 +/// Topology related algorithms
    1.35 +
    1.36 +namespace lemon {
    1.37 +
    1.38 +  namespace _topology_bits {
    1.39 +    
    1.40 +    template <typename NodeMap>
    1.41 +    class BackCounterMap {
    1.42 +    public:
    1.43 +      BackCounterMap(NodeMap& _nodeMap, int _counter)
    1.44 +	: nodeMap(_nodeMap), counter(_counter) {}
    1.45 +
    1.46 +      void set(typename NodeMap::Key key, bool val) {
    1.47 +	if (val) {
    1.48 +	  nodeMap.set(key, --counter);
    1.49 +	} else {
    1.50 +	  nodeMap.set(key, -1);
    1.51 +	}
    1.52 +      }
    1.53 +
    1.54 +      bool operator[](typename NodeMap::Key key) const {
    1.55 +	return nodeMap[key] != -1;
    1.56 +      }
    1.57 +
    1.58 +    private:
    1.59 +      NodeMap& nodeMap;
    1.60 +      int counter;
    1.61 +    };
    1.62 +  }
    1.63 +
    1.64 +  // \todo Its to special output // ReadWriteMap
    1.65 +  template <typename Graph, typename NodeMap>
    1.66 +  bool topological_sort(const Graph& graph, NodeMap& nodeMap) {
    1.67 +    using namespace _topology_bits;
    1.68 +
    1.69 +    checkConcept<concept::StaticGraph, Graph>();
    1.70 +    checkConcept<concept::ReadWriteMap<typename Graph::Node, int>, NodeMap>();
    1.71 +
    1.72 +    typedef typename Graph::Node Node;
    1.73 +    typedef typename Graph::NodeIt NodeIt;
    1.74 +    typedef typename Graph::Edge Edge;
    1.75 +
    1.76 +    typedef BackCounterMap<NodeMap> ProcessedMap;
    1.77 +
    1.78 +    typename Dfs<Graph>::template DefProcessedMap<ProcessedMap>::
    1.79 +      Dfs dfs(graph);
    1.80 +
    1.81 +    ProcessedMap processed(nodeMap, countNodes(graph));
    1.82 +
    1.83 +    dfs.processedMap(processed);
    1.84 +    dfs.init();
    1.85 +    for (NodeIt it(graph); it != INVALID; ++it) {
    1.86 +      if (!dfs.reached(it)) {
    1.87 +	dfs.addSource(it);
    1.88 +	while (!dfs.emptyQueue()) {
    1.89 +	  Edge edge = dfs.nextEdge();
    1.90 +	  Node target = graph.target(edge);
    1.91 +	  if (dfs.reached(target) && !processed[target]) {
    1.92 +	    return false;
    1.93 +	  }
    1.94 +	  dfs.processNextEdge();
    1.95 +	}
    1.96 +      }
    1.97 +    }    
    1.98 +    return true;
    1.99 +  }
   1.100 +
   1.101 +  /// \brief Check that the given graph is a DAG.
   1.102 +  ///
   1.103 +  /// Check that the given graph is a DAG. The DAG is
   1.104 +  /// an Directed Acyclic Graph.
   1.105 +  template <typename Graph>
   1.106 +  bool dag(const Graph& graph) {
   1.107 +
   1.108 +    checkConcept<concept::StaticGraph, Graph>();
   1.109 +
   1.110 +    typedef typename Graph::Node Node;
   1.111 +    typedef typename Graph::NodeIt NodeIt;
   1.112 +    typedef typename Graph::Edge Edge;
   1.113 +
   1.114 +    typedef typename Graph::template NodeMap<bool> ProcessedMap;
   1.115 +
   1.116 +    typename Dfs<Graph>::template DefProcessedMap<ProcessedMap>::
   1.117 +      Dfs dfs(graph);
   1.118 +
   1.119 +    ProcessedMap processed(graph);
   1.120 +    dfs.processedMap(processed);
   1.121 +
   1.122 +    dfs.init();
   1.123 +    for (NodeIt it(graph); it != INVALID; ++it) {
   1.124 +      if (!dfs.reached(it)) {
   1.125 +	dfs.addSource(it);
   1.126 +	while (!dfs.emptyQueue()) {
   1.127 +	  Edge edge = dfs.nextEdge();
   1.128 +	  Node target = graph.target(edge);
   1.129 +	  if (dfs.reached(target) && !processed[target]) {
   1.130 +	    return false;
   1.131 +	  }
   1.132 +	  dfs.processNextEdge();
   1.133 +	}
   1.134 +      }
   1.135 +    }    
   1.136 +    return true;
   1.137 +  }
   1.138 +
   1.139 +  // UndirGraph algorithms
   1.140 +
   1.141 +  /// \brief Check that the given undirected graph is connected.
   1.142 +  ///
   1.143 +  /// Check that the given undirected graph connected.
   1.144 +  template <typename UndirGraph>
   1.145 +  bool connected(const UndirGraph& graph) {
   1.146 +    checkConcept<concept::UndirGraph, UndirGraph>();
   1.147 +    typedef typename UndirGraph::NodeIt NodeIt;
   1.148 +    if (NodeIt(graph) == INVALID) return false;
   1.149 +    Dfs<UndirGraph> dfs(graph);
   1.150 +    dfs.run(NodeIt(graph));
   1.151 +    for (NodeIt it(graph); it != INVALID; ++it) {
   1.152 +      if (!dfs.reached(it)) {
   1.153 +	return false;
   1.154 +      }
   1.155 +    }
   1.156 +    return true;
   1.157 +  }
   1.158 +
   1.159 +  /// \brief Check that the given undirected graph is acyclic.
   1.160 +  ///
   1.161 +  /// Check that the given undirected graph acyclic.
   1.162 +  template <typename UndirGraph>
   1.163 +  bool acyclic(const UndirGraph& graph) {
   1.164 +    checkConcept<concept::UndirGraph, UndirGraph>();
   1.165 +    typedef typename UndirGraph::Node Node;
   1.166 +    typedef typename UndirGraph::NodeIt NodeIt;
   1.167 +    typedef typename UndirGraph::Edge Edge;
   1.168 +    Dfs<UndirGraph> dfs(graph);
   1.169 +    dfs.init();
   1.170 +    for (NodeIt it(graph); it != INVALID; ++it) {
   1.171 +      if (!dfs.reached(it)) {
   1.172 +	dfs.addSource(it);
   1.173 +	while (!dfs.emptyQueue()) {
   1.174 +	  Edge edge = dfs.nextEdge();
   1.175 +	  Node source = graph.source(edge);
   1.176 +	  Node target = graph.target(edge);
   1.177 +	  if (dfs.reached(target) && 
   1.178 +	      dfs.pred(source) != graph.oppositeEdge(edge)) {
   1.179 +	    return false;
   1.180 +	  }
   1.181 +	  dfs.processNextEdge();
   1.182 +	}
   1.183 +      }
   1.184 +    }
   1.185 +    return true;
   1.186 +  }
   1.187 +
   1.188 +  /// \brief Check that the given undirected graph is tree.
   1.189 +  ///
   1.190 +  /// Check that the given undirected graph is tree.
   1.191 +  template <typename UndirGraph>
   1.192 +  bool tree(const UndirGraph& graph) {
   1.193 +    checkConcept<concept::UndirGraph, UndirGraph>();
   1.194 +    typedef typename UndirGraph::Node Node;
   1.195 +    typedef typename UndirGraph::NodeIt NodeIt;
   1.196 +    typedef typename UndirGraph::Edge Edge;
   1.197 +    if (NodeIt(graph) == INVALID) return false;
   1.198 +    Dfs<UndirGraph> dfs(graph);
   1.199 +    dfs.init();
   1.200 +    dfs.addSource(NodeIt(graph));
   1.201 +    while (!dfs.emptyQueue()) {
   1.202 +      Edge edge = dfs.nextEdge();
   1.203 +      Node source = graph.source(edge);
   1.204 +      Node target = graph.target(edge);
   1.205 +      if (dfs.reached(target) && 
   1.206 +	  dfs.pred(source) != graph.oppositeEdge(edge)) {
   1.207 +	return false;
   1.208 +      }
   1.209 +      dfs.processNextEdge();
   1.210 +    }
   1.211 +    for (NodeIt it(graph); it != INVALID; ++it) {
   1.212 +      if (!dfs.reached(it)) {
   1.213 +	return false;
   1.214 +      }
   1.215 +    }    
   1.216 +    return true;
   1.217 +  }
   1.218 + 
   1.219 +
   1.220 +} //namespace lemon
   1.221 +
   1.222 +#endif //LEMON_TOPOLOGY_H