Skip to content

Commit

Permalink
Add cycle detection specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
franziska-wegner committed Dec 5, 2023
1 parent cac0650 commit 551ea70
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions include/Algorithms/GraphTraversal/CycleDetection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* CycleDetection.hpp
*
* Created on: Feb 17, 2019
* Author: Franziska Wegner
*/
#ifndef EGOA__ALGORITHMS__GRAPH_TRAVERSAL__CYCLE_DETECTION__HPP
#define EGOA__ALGORITHMS__GRAPH_TRAVERSAL__CYCLE_DETECTION__HPP

#include "Algorithms/GraphTraversal/DepthFirstSearch.hpp"

namespace egoa {

/**
* @brief Class to detect a cycle in the graph.
* @details Similar to DFS, but overriding the @p ProcessingEdgeWith
* method. Note that a cycle detection can only be if the target vertex
* was already visited, but not processed. This represent the second
* branch in the if condition of the @p breakable_for_all_edges_at.
*
* @tparam GraphType The graph should at least provide the same interface
* as the StaticGraph().
* @tparam IsDirected If @p true the graph is treated as a directed graph,
* if @p false the graph is treated as an undirected graph.
*/
template< typename GraphType = StaticGraph<Vertices::ElectricalProperties<>,Edges::ElectricalProperties>
, bool IsDirected = false >
class CycleDetection final : public DFS<GraphType, IsDirected> {
public:
#pragma mark CONSTRUCTOR_AND_DESTRUCTOR
CycleDetection ( TGraph const & graph, TVertex source )
: DFS ( graph, source ){}

#pragma mark FURTHER_PROCESSING
/**
* @brief Detect backward edge and thus, detect the first cycle found.
* @details Note that the DFS algorithm processes every edge
* exactly once using @p breakable_for_all_edges_at. Thus, there
* are no spurious two-vertex cycles in undirected graphs such as
* paths = (u,v) and (v,u).
*
* @param[in] source The source identifier.
* @param[in] target The target identifier.
*/
virtual inline void ProcessingEdgeWith ( TVertex const source
, TVertex const target ) override
{
if ( source != ParentOf ( target ) ) {
ExtractPath ( source, target );
SetTerminate();
}
}
};

#endif // EGOA__ALGORITHMS__GRAPH_TRAVERSAL__CYCLE_DETECTION__HPP

0 comments on commit 551ea70

Please sign in to comment.