Skip to content
Snippets Groups Projects
Commit ae146946 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

add Backbone length computation from dfa

parent ed7162bd
No related branches found
No related tags found
No related merge requests found
/*
* BackboneLength.cpp
*
* Created on: 17. 5. 2017
* Author: Jan Travnicek
*/
#include "BackboneLength.h"
namespace stringology {
namespace properties {
unsigned BackboneLength::length ( const automaton::Automaton & automaton ) {
return dispatch ( automaton.getData ( ) );
}
auto backboneLengthDFA = BackboneLength::RegistratorWrapper < unsigned, automaton::DFA < > > ( BackboneLength::length );
} /* namespace properties */
} /* namespace stringology */
/*
* BackboneLength.h
*
* Created on: 17. 5. 2017
* Author: Jan Travnicek
*/
#ifndef BACKBONE_LENGTH_H
#define BACKBONE_LENGTH_H
#include <automaton/Automaton.h>
#include <automaton/FSM/DFA.h>
#include <core/multipleDispatch.hpp>
#include <set>
#include <queue>
#include <algorithm>
#include <utility>
namespace stringology {
namespace properties {
class BackboneLength : public std::SingleDispatch < BackboneLength, unsigned, const automaton::AutomatonBase & > {
public:
/**
* Computes length of backbone for a suffix/factor/oracle automaton
* @param automaton the suffix/factor/oracle automaton
* @return backbone length
*/
static unsigned length ( const automaton::Automaton & automaton );
template < class SymbolType, class StateType >
static unsigned length ( const automaton::DFA < SymbolType, StateType > & automaton );
template < class StateType >
class BackboneLengthLess {
public:
bool operator ( ) ( const std::pair < StateType, unsigned > & first, const std::pair < StateType, unsigned > & second ) {
return first.second < second.second;
}
};
};
template < class SymbolType, class StateType >
unsigned BackboneLength::length ( const automaton::DFA < SymbolType, StateType > & automaton ) {
std::priority_queue < std::pair < StateType, unsigned >, std::vector < std::pair < StateType, unsigned > >, BackboneLengthLess < StateType > > open;
std::map < StateType, unsigned > closed;
unsigned max = 0;
open.push ( std::make_pair ( automaton.getInitialState ( ), max ) );
while ( ! open.empty ( ) ) {
std::pair < StateType, unsigned > current = std::move ( open.top ( ) );
open.pop ( );
unsigned & dist = closed [ current.first ];
if ( dist > current.second )
continue;
dist = current.second;
max = std::max ( max, current.second );
for ( const std::pair < const std::pair < StateType, SymbolType >, StateType > & target : automaton.getTransitionsFromState ( current.first ) )
open.push ( std::make_pair ( target.second, current.second + 1 ) );
}
return max;
}
} /* namespace properties */
} /* namespace stringology */
#endif /* BACKBONE_LENGTH_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment