Awali
Another Weighted Automata library
distance.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_DISTANCE_HH
18 # define AWALI_ALGOS_DISTANCE_HH
19 
20 # include <algorithm>
21 # include <iostream>
22 # include <queue>
23 # include <unordered_set>
24 # include <unordered_map>
25 # include <vector>
26 
27 #include <awali/sttc/algos/copy.hh>
29 #include <awali/sttc/misc/pair.hh>
30 
31 namespace awali { namespace sttc {
32 
33  /*
34  Reverse breadth first search from state set 'start'
35  */
36  template<typename Aut>
37  std::unordered_map<state_t,
38  std::pair<unsigned,
39  transition_t>>
40  paths_ibfs(const Aut& aut, std::vector<state_t> start)
41  {
42  // using context_t = context_t_of<Aut>;
43  // using automaton_t = mutable_automaton<context_t>;
44 
45  std::queue<state_t> todo;
46  std::unordered_set<state_t> marked;
47  std::unordered_map<state_t, std::pair<state_t, transition_t>> parent;
48 
49  for (auto s : start)
50  todo.push(s);
51 
52  while (!todo.empty())
53  {
54  state_t p = todo.front();
55  todo.pop();
56  marked.insert(p);
57  for (auto t : aut->in(p))
58  {
59  auto s = aut->src_of(t);
60  if (marked.find(s) == marked.end())
61  {
62  todo.push(s);
63  auto cur_p = parent.find(p);
64  unsigned cur_d;
65  if (cur_p == parent.end())
66  cur_d = 1;
67  else
68  cur_d = cur_p->second.first + 1;
69  parent[s] = {cur_d, t};
70  }
71  }
72  }
73  return parent;
74  }
75 
76  /*
77  Breadth first search from state set 'start'
78  */
79  template<typename Aut>
80  std::vector<transition_t>
81  path_bfs(const Aut& aut, state_t start,
82  state_t end)
83  {
84  // using context_t = context_t_of<Aut>;
85  // using automaton_t = mutable_automaton<context_t>;
86 
87  std::queue<state_t> todo;
88  std::unordered_set<state_t> marked;
89  std::unordered_map<state_t, std::pair<state_t, transition_t>> parent;
90 
91  todo.push(start);
92  while (!todo.empty())
93  {
94  state_t p = todo.front();
95  todo.pop();
96  marked.insert(p);
97  if (p == end)
98  {
99  std::vector<transition_t> rpath;
100  state_t bt_curr = end;
101  while (bt_curr != start)
102  {
103  transition_t t;
104  std::tie(bt_curr, t) = parent.find(bt_curr)->second;
105  rpath.push_back(t);
106  }
107  std::reverse(rpath.begin(), rpath.end());
108  return rpath;
109  }
110  else
111  for (auto t : aut->out(p))
112  {
113  auto s = aut->dst_of(t);
114  if (marked.find(s) == marked.end())
115  {
116  todo.push(s);
117  parent[s] = {p, t};
118  }
119  }
120  }
121  return std::vector<transition_t>();
122  }
123 
124 }}//end of ns awali::stc
125 
126 #endif // !AWALI_ALGOS_DISTANCE_HH
std::unordered_map< state_t, std::pair< unsigned, transition_t > > paths_ibfs(const Aut &aut, std::vector< state_t > start)
Definition: distance.hh:40
std::vector< transition_t > path_bfs(const Aut &aut, state_t start, state_t end)
Definition: distance.hh:81
pair_automaton< Aut > pair(const Aut &aut, bool keep_initials=false)
Definition: synchronizing_word.hh:266
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22