38 #include <type_traits> 44 namespace MaximalPlanarSubgraphSimpleInternal {
45 template<
typename TCost>
48 const EdgeArray<TCost> &m_weight;
50 explicit EdgeComparer(
const EdgeArray<TCost> &weight) : m_weight(weight) { }
52 bool less(
const edge &x,
const edge &y)
const {
53 return m_weight[x] < m_weight[y];
59 template<
typename TCost,
class Enable =
void>
60 class MaximalPlanarSubgraphSimple {};
72 template<
typename TCost>
73 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_integral<TCost>::value>
::type> :
public PlanarSubgraphModule<TCost> {
79 , m_deleteHeuristic(true) {}
86 : m_heuristic(heuristic)
87 , m_deleteHeuristic(false) { }
91 if(m_deleteHeuristic) {
97 virtual MaximalPlanarSubgraphSimple *
clone()
const override {
98 auto result =
new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()));
99 result->m_deleteHeuristic =
true;
120 bool preferredImplyPlanar)
override 125 if(pCost ==
nullptr) {
126 result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
128 result = m_heuristic.call(graph, *pCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
129 MaximalPlanarSubgraphSimpleInternal::EdgeComparer<TCost> cmp(*pCost);
134 for (
edge e : heuDelEdges) {
137 for (
edge e : heuDelEdges) {
157 template<
typename TCost>
158 class MaximalPlanarSubgraphSimple<TCost, typename std::enable_if<std::is_floating_point<TCost>::value>
::type> :
public PlanarSubgraphModule<TCost> {
169 , m_deleteHeuristic(true)
171 , m_randomGenerator()
181 : m_heuristic(heuristic)
182 , m_deleteHeuristic(false)
183 , m_randomness(randomness)
184 , m_randomGenerator()
189 if(m_deleteHeuristic) {
195 virtual MaximalPlanarSubgraphSimple *
clone()
const override {
196 auto result =
new MaximalPlanarSubgraphSimple(*(m_heuristic.clone()), m_randomness, m_runs);
197 result->m_deleteHeuristic =
true;
206 m_randomGenerator.seed(seed);
227 bool preferredImplyPlanar)
override 238 for (
auto i=0u; i < m_runs; i++) {
242 if(pCost ==
nullptr) {
243 result = m_heuristic.call(graph, preferredEdges, heuDelEdges, preferredImplyPlanar);
245 std::uniform_real_distribution<TCost> distribution (0.0,1.0);
246 TCost maxCost = (*pCost)[graph.
firstEdge()];
247 TCost minCost = (*pCost)[graph.
firstEdge()];
249 maxCost = max(maxCost, (*pCost)[e]);
250 minCost = min(minCost, (*pCost)[e]);
255 TCost normalized = ((*pCost)[e] - minCost) / (maxCost - minCost);
257 normalizedCost[e] = (1.0 - m_randomness)*normalized + m_randomness*distribution(m_randomGenerator);
259 result = m_heuristic.call(graph, normalizedCost, preferredEdges, heuDelEdges, preferredImplyPlanar);
265 if (pCost !=
nullptr) {
266 MaximalPlanarSubgraphSimpleInternal::EdgeComparer<TCost> cmp(normalizedCost);
270 for (
edge e : heuDelEdges) {
274 delEdgesCurrentBest.
clear();
275 for (
edge e : heuDelEdges) {
284 if (pCost ==
nullptr) {
285 if (i == 0 || delEdgesCurrentBest.
size() < delEdges.
size()) {
288 for (
auto e: delEdgesCurrentBest) { delEdges.
pushBack(e); };
291 if (i == 0 || weightOfList(delEdgesCurrentBest,normalizedCost) < weightOfList(delEdges,normalizedCost)) {
294 for (
auto e: delEdgesCurrentBest) { delEdges.
pushBack(e); };
318 result += weights[e];
void clear()
Removes all elements from the list.
EdgeElement * edge
The type of edges.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
virtual MaximalPlanarSubgraphSimple * clone() const override
Clone method.
virtual ~MaximalPlanarSubgraphSimple()
Destructor.
iterator pushBack(const E &x)
Adds element x at the end of the list.
Data type for general directed graphs (adjacency list representation).
unsigned int m_runs
number of runs when algorithms is randomized
virtual Module::ReturnType doCall(const Graph &graph, const List< edge > &preferredEdges, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferredImplyPlanar) override
Computes the set of edges delEdges which have to be deleted to obtain the planar subgraph.
PlanarSubgraphModule< TCost > & m_heuristic
user given heuristic
ReturnType
The return type of a module.
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic, double randomness=0.0, unsigned int runs=1)
Constructor.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
bool m_deleteHeuristic
flag to store we have to delete a self created heuristic
virtual ~MaximalPlanarSubgraphSimple()
Desctructor.
void quicksort()
Sorts the list using Quicksort.
Interface for planar subgraph algorithms.
edge firstEdge() const
Returns the first edge in the list of all edges.
static void copy(const T &from, T &to)
TCost weightOfList(const List< edge > &list, const EdgeArray< TCost > &weights)
bool isPlanar(const Graph &G)
Returns true, if G is planar, false otherwise.
Class for the representation of edges.
virtual MaximalPlanarSubgraphSimple * clone() const override
Clone method.
Copies of graphs supporting edge splitting.
std::default_random_engine m_randomGenerator
random generator to use with std::uniform_real_distribution
void setSeed(unsigned seed)
set seed of std::default_random_engine generator to use when randomness > 0
Declaration and Implementation of class PlanarSubgraphEmpty.
bool m_deleteHeuristic
flag to store we have to delete a self created heuristic
MaximalPlanarSubgraphSimple()
Constructor.
double m_randomness
randomness of the process: use 0 to compute everything based on the costs, use 1 for completely rando...
int size() const
Returns the number of elements in the list.
Declaration of simple graph algorithms.
Implementation of disjoint sets data structures (union-find functionality).
PlanarSubgraphModule< TCost > & m_heuristic
user given heuristic
static bool isSolution(ReturnType ret)
Returns true iff ret indicates that the module returned a feasible solution.
MaximalPlanarSubgraphSimple()
Constructor.
The namespace for all OGDF objects.
Declaration of extended graph algorithms.
virtual Module::ReturnType doCall(const Graph &graph, const List< edge > &preferredEdges, List< edge > &delEdges, const EdgeArray< TCost > *pCost, bool preferredImplyPlanar) override
Computes the set of edges delEdges which have to be deleted to obtain the planar subgraph.
MaximalPlanarSubgraphSimple(PlanarSubgraphModule< TCost > &heuristic)
Constructor with user given heuristic that is executed before extending the solution to maximality...
Dummy implementation for maximum planar subgraph that returns an empty graph.