Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
InfiniteLanguage.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <queue>
27#include <set>
28
29#include <automaton/FSM/NFA.h>
30#include <automaton/FSM/DFA.h>
31
33
34namespace automaton {
35
36namespace properties {
37
42public:
52 template < class T >
53 requires isDFA < T > || isNFA < T >
54 static bool infinite ( const T & fsm );
55};
56
57template < class T >
58requires isDFA < T > || isNFA < T >
59bool InfiniteLanguage::infinite ( const T & fsm ) {
60 using StateType = typename T::StateType;
61
63
64 std::queue < StateType > q;
65 std::set < StateType > visited { fsm.getInitialState ( ) };
66
67 q.push ( fsm.getInitialState ( ) );
68
69 while ( ! q.empty ( ) ) {
70 const StateType state = std::move ( q.front ( ) );
71 q.pop ( );
72
73 for ( const auto & transition : fsm.getTransitionsFromState ( state ) ) {
74 if ( visited.insert ( transition.second ).second ) {
75 q.push ( transition.second );
76 } else if ( usefulStates.contains ( transition.second ) ) {
77 return true;
78 }
79 }
80 }
81
82 return false;
83}
84
85} /* namespace properties */
86
87} /* namespace automaton */
Definition: InfiniteLanguage.h:41
static bool infinite(const T &fsm)
Definition: InfiniteLanguage.h:59
static ext::set< typename T::StateType > usefulStates(const T &fsm)
Definition: set.hpp:44
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
q
Definition: SingleInitialStateEpsilonTransition.h:85
ext::set< ext::tuple< StateType, StateType, SymbolType > > visited
Definition: Compaction.h:81
Definition: ToGrammar.h:31