cfl_graphfncts.h
Go to the documentation of this file.
1 /** @file cfl_graphfncts.h Operations on (directed) graphs. */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2009 Thomas Moor, Klaus Schmidt, Sebastian Perk
6  Exclusive copyright is granted to Klaus Schmidt
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21 
22 
23 #ifndef FAUDES_GRAPHFNCTS_H
24 #define FAUDES_GRAPHFNCTS_H
25 
26 #include "cfl_definitions.h"
27 #include "cfl_generator.h"
28 #include <stack>
29 
30 namespace faudes {
31 
32 
33 /**
34  * Filter for strictly connected components (SCC) search/compute routines.
35  *
36  * When ComputeScc()/HasScc()/NextScc() traverse a specified transition sytem in their
37  * search, a SccFilter parameter can mute certain transitions. The respective
38  * filter conditions are set by the constructor and consist of a flag word (mMode)
39  * and additional parameters (depending on the flags). The Flags can be combined
40  * from the following:
41  *
42  * - NoFilter: std textbook beheviour;
43  * - FindFirst: stop the search after one SCC has been found;
44  * - IgnoreTrivial: dont report singleton SCCs without selfloop;
45  * - StatesAvoid: mute specified states from trasition structure;
46  * - StatesRequire: dont report SCCs that fail to contain at least one state from the specified set;
47  * - EventsAvoid: mute any transitions with an event label from the specified set.
48  *
49  * Convenience modes set up the state related filter conditions from the set of marked states:
50  *
51  * - IgnoreLiveLocks: set StatesRequire to the marked states of a specified Generator;
52  * - LiveLocksOnly: set StatesAvoid to the marked states of a specified Generator;
53  * - IgnoreUnaccessible: initialise todo list with accessible states only.
54  *
55  * Technical note: SccFilters interally use references to condition
56  * parameters that are required to exist during the life time of the filter object.
57  *
58  * Technical note: currently, there is no EventsRequire filter because the implementation
59  * interprets the transition relation primarily from a directed graph perspective; while StatesRequire
60  * makes sense for marked states semantics, we are not aware of any applications for
61  * a corresponding version of EventsRequire; please let us know if you require such an extension.
62  *
63  * @ingroup GeneratorFunctions
64  */
66 
67  // Allow search function access
68  friend FAUDES_API void SearchScc(
69  const Idx vState,
70  int& vRcount,
71  const Generator& rGen,
72  const SccFilter& rFilter,
73  StateSet& rTodo,
74  std::stack<Idx>& rStack,
75  StateSet& rStackStates,
76  std::map<const Idx, int>& rDfn,
77  std::map<const Idx, int>& rLowLnk,
78  std::list<StateSet>& rSccList,
79  StateSet& rRoots);
80  friend FAUDES_API bool ComputeNextScc(
81  const Generator& rGen,
82  SccFilter& rFilter,
83  StateSet& rScc
84  );
85 
86 public:
87 
88  /** Typedef for filter modes */
89  typedef enum {
90  FmNoFilter=0x00, //// no filters at all (default)
91  FmFindFirst=0x01, //// only report first valid SCC
92  FmIgnoreTrivial=0x02, //// ignore non-cycles, aka singletons without selfloop
93  FmStatesAvoid=0x10, //// filter by avoiding specified states
94  FmStatesRequire=0x20, //// filter by requireing specified states
95  FmEventsAvoid=0x40, //// filter by avoiding specified events
96  FmIgnoreLiveLocks=0x1000, //// ignore cycles with unmarked states only
97  FmLiveLocksOnly =0x2000, //// ignore cycles with one or more marked states
98  FmIgnoreUnaccessible=0x4000 //// only inspect accessible part
99  } FMode;
100 
101  /** Constructor (no filter) */
102  SccFilter(void);
103 
104  /** Constructor (copy constructor) */
105  SccFilter(const SccFilter& rSrc);
106 
107  /** Constructor (from flags w.r.t. Generator) */
108  SccFilter(int mode, const Generator& rGen);
109 
110  /** Constructor (from flags and state set, either avoid or require) */
111  SccFilter(int mode, const StateSet& rStatesAvoidRequire);
112 
113  /** Constructor (from flags and state sets) */
114  SccFilter(int mode, const StateSet& rStatesAvoid, const StateSet& rStatesRequire);
115 
116  /** Constructor (from flags and event sets) */
117  SccFilter(int mode, const EventSet& rEventsAvoid);
118 
119  /** Constructor (from flags and sets) */
120  SccFilter(int mode, const StateSet& rStatesAvoid, const StateSet& rStatesRequire,
121  const EventSet& rEventsAvoid);
122 
123  /** Destructor */
124  ~SccFilter(void);
125 
126  /** Member access */
127  int Mode(void) const { return mMode;};
128 
129  /** Member access */
130  const StateSet& StatesAvoid(void) const { return *pStatesAvoid;};
131 
132  /** Member access */
133  const StateSet& StatesRequire(void) const { return *pStatesRequire;};
134 
135  /** Edit filter (RTI): no filter */
136  void Clear(void);
137 
138  /** Edit filter (RTI): set avoid states */
139  void StatesAvoid(const StateSet& rStatesAvoid);
140 
141  /** Edit filter (RTI): set required states */
142  void StatesRequire(const StateSet& rStatesRequire);
143 
144  /** Edit filter (RTI): set avoid events */
145  void EventsAvoid(const EventSet& rEventsAvoid);
146 
147  /** Edit filter (RTI): ignore trivial */
148  void IgnoreTrivial(bool flag);
149 
150  /** Edit filter (RTI): find first */
151  void FindFirst(bool flag);
152 
153 
154 protected:
155 
156  /** Edit filter (RTI): avoid states */
157  void MergeStatesAvoid(const StateSet& rStatesAvoid);
158 
159  /** Flag, combining bit masks from Mode */
160  int mMode;
161  /** States to avoid (if flag StatesAvoid is set) */
163  /** States to require (if flag StatesRequire is set) */
165  /** Events to avoid (if flag EventssAvoid is set) */
167 
168  /** Local sets (optional) */
172 
173  /** Static emptysets */
174  const static StateSet msEmptyStates;
175  const static EventSet msEmptyEvents;
176 };
177 
178 
179 /**
180  *
181  * Search for strongly connected components (SCC).
182  *
183  * This function partitions the stateset of a generator into equivalent
184  * classes such that states x1 and x2 are equivalent iff there is a path from x1
185  * to x2 AND a path from x2 to x1.
186  *
187  * This function implements the algorithm based on a recursive depth first search
188  * presented in:
189  *
190  * -- Aho, Hopcroft, Ullman: The Design and Analysis of Computer Algorithms --
191  *
192  * While the original algorithm works on a directed graph, this
193  * implementation adds some features that refer to transition systems and
194  * allow to filter SCCs on the run. The filter condition is specified by the
195  * SccFilter parameter rFilter.
196  *
197  * Note: this version is derived from earlier implementations used in
198  * various plug-ins; in due course, this version will replace earlier versions.
199  *
200  * Note: Due to the recursive implementation, this function requires a stack
201  * size proportional to the largest SCC. We have experienced typical default
202  * configurations to be good for a depth of about 80000 (Mac OSX 10.6, Debian 7.4).
203  * For SCCs exceeding the default stack size, you may adjust the operating system
204  * parameters accordingly. On Unix/Linux/MacOsX this is done by the shell command
205  * "ulimit -s hard". A future revision of SearchSCC() may be re-designed to
206  * circumvent this inconvenient issue.
207  *
208  * Note: for a convenience API see also ComputeScc()
209  *
210  * @param vState
211  * State, from which the current recursion is started.
212  * @param vRcount
213  * Denotes the current depth of the recursion.
214  * @param rGen
215  * Transition system to investigate
216  * @param rFilter
217  * Filter out specified transitions
218  * @param rTodo
219  * Set of states that up to now were not found by the
220  * depth first search.
221  * @param rStack
222  * Stack of states to represent current path.
223  * @param rStackStates
224  * Set of states that are in rStack
225  * @param rDfn
226  * Map assigning to each state idx its Depth-First Number.
227  * @param rLowLnk
228  * Map assigning to each state its LOWLINK Number.
229  * @param rSccList
230  * Set SCCs (accumulative result).
231  * @param rRoots
232  * Set of states that each are root of some SCC (accumulative result).
233  *
234  *
235  */
236 extern FAUDES_API void SearchScc(
237  const Idx vState,
238  int& vRcount,
239  const Generator& rGen,
240  const SccFilter& rFilter,
241  StateSet& rTodo,
242  std::stack<Idx>& rStack,
243  StateSet& rStackStates,
244  std::map<const Idx, int>& rDfn,
245  std::map<const Idx, int>& rLowLnk,
246  std::list<StateSet>& rSccList,
247  StateSet& rRoots);
248 
249 
250 /**
251  * Compute strongly connected components (SCC)
252  *
253  * This function is a API wrapper that calls the recursive implementation
254  * SearchScc().
255  *
256  *
257  * @param rGen
258  * Generator under investigation
259  * @param rFilter
260  * Filter specified transitions
261  * @param rSccList
262  * List of SCCs (result)
263  * @param rRoots
264  * Set of states that each are root of some SCC (result).
265  *
266  * @return
267  * True if SCCs have been found, false if not.
268  *
269  * @ingroup GeneratorFunctions
270  *
271  */
272 extern FAUDES_API bool ComputeScc(
273  const Generator& rGen,
274  const SccFilter& rFilter,
275  std::list<StateSet>& rSccList,
276  StateSet& rRoots);
277 
278 
279 /**
280  * Compute strongly connected components (SCC)
281  *
282  * This function is a API wrapper that calls the recursive implementation
283  * SearchScc().
284  *
285  * @param rGen
286  * Generator under investigation
287  * @param rSccList
288  * List of SCCs (result)
289  * @param rRoots
290  * Set of states that each are root of some SCC (result).
291  *
292  * @return
293  * True if SCCs have been found, false if not.
294  * Since there are no filters, true is returned iff the
295  * the state set is non-empty.
296  *
297  * @ingroup GeneratorFunctions
298  *
299  */
300 extern FAUDES_API bool ComputeScc(
301  const Generator& rGen,
302  std::list<StateSet>& rSccList,
303  StateSet& rRoots);
304 
305 
306 
307 /**
308  * Compute strongly connected component (SCC)
309  *
310  * This function is a API wrapper that calls the recursive implementation
311  * SearchScc(). It internally edits the filter to require the specified
312  * initial state and to stop on the first SCC found. In particular, any
313  * other state requirement will be ignored.
314  *
315  * @param rGen
316  * Generator under investigation
317  * @param rFilter
318  * Filter specified transitions
319  * @param q0
320  * Initial state for SCC.
321  * @param rScc
322  * SCC (result)
323  *
324  * @return
325  * True if an SCC has been found, false if not.
326  *
327  * @ingroup GeneratorFunctions
328  *
329  */
330 extern FAUDES_API bool ComputeScc(
331  const Generator& rGen,
332  const SccFilter& rFilter,
333  Idx q0,
334  StateSet& rScc
335 );
336 
337 
338 
339 /**
340  * Compute one strongly connected component (SCC)
341  *
342  * This functions searchs for the first SCC of the generator rGen
343  * while applying the filter rFilter; see SCCFilter for details.
344  *
345  * Technically, this function is a API wrapper that calls the recursive implementation
346  * SearchScc() as presented in
347  *
348  * -- Aho, Hopcroft, Ullman: The Design and Analysis of Computer Algorithms --
349  *
350  * @param rGen
351  * Generator under investigation
352  * @param rFilter
353  * Filter out specified transitions
354  * @param rScc
355  * First SCC that has been found, empty if no such.
356  *
357  * @return
358  * True if SCCs have been found, false if not.
359  *
360  * @ingroup GeneratorFunctions
361  *
362  */
363 
364 extern FAUDES_API bool ComputeScc(
365  const Generator& rGen,
366  const SccFilter& rFilter,
367  StateSet& rScc
368 );
369 
370 /**
371  * Test for strongly connected components (SCC)
372  *
373  * This functions searchs for the first SCC of the generator rGen
374  * while applying the filter rFilter; see SCCFilter for details.
375  *
376  * Technically, this function is an API wrapper that calls the recursive implementation
377  * SearchScc() as presented in
378  *
379  * -- Aho, Hopcroft, Ullman: The Design and Analysis of Computer Algorithms --
380  *
381  * @param rGen
382  * Generator under investigation
383  * @param rFilter
384  * Filter out specified transitions
385  *
386  * @return
387  * True if SCCs have been found, false if not.
388  *
389  * @ingroup GeneratorFunctions
390  *
391  */
392 extern FAUDES_API bool HasScc(
393  const Generator& rGen,
394  const SccFilter& rFilter
395 );
396 
397 
398 /**
399  * Compute next SCC
400  *
401  * This function provides an API for the iterative computation
402  * of SCCs. It invokes SearchScc() to find the next SCC and then
403  * adds the SCC to the StatesAvoid Filter. This approach is
404  * not computationally efficient but it allows for simple Lua wrappers.
405  *
406  * @param rGen
407  * Generator under investigation
408  * @param rFilter
409  * Filter out specified transitions
410  * @param rScc
411  * First SCC that has been found, empty if no such.
412  *
413  * @return
414  * True if an SCC has been found, false if not.
415  *
416  * @ingroup GeneratorFunctions
417  *
418  */
419 extern FAUDES_API bool ComputeNextScc(
420  const Generator& rGen,
421  SccFilter& rFilter,
422  StateSet& rScc
423 );
424 
425 } // namespace
426 #endif
Compiletime options.
Class vGenerator.
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
Set of indices.
Definition: cfl_indexset.h:78
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
Filter for strictly connected components (SCC) search/compute routines.
static const StateSet msEmptyStates
Static emptysets.
EventSet * mpEventsAvoid
const StateSet & StatesAvoid(void) const
Member access.
const StateSet & StatesRequire(void) const
Member access.
int Mode(void) const
Member access.
static const EventSet msEmptyEvents
SccFilter(int mode, const StateSet &rStatesAvoid, const StateSet &rStatesRequire, const EventSet &rEventsAvoid)
Constructor (from flags and sets)
StateSet * mpStatesAvoid
Local sets (optional)
const EventSet * pEventsAvoid
Events to avoid (if flag EventssAvoid is set)
int mMode
Flag, combining bit masks from Mode.
const StateSet * pStatesAvoid
States to avoid (if flag StatesAvoid is set)
const StateSet * pStatesRequire
States to require (if flag StatesRequire is set)
StateSet * mpStatesRequire
Base class of all FAUDES generators.
bool ComputeNextScc(const Generator &rGen, SccFilter &rFilter, StateSet &rScc)
Compute next SCC.
bool ComputeScc(const Generator &rGen, const SccFilter &rFilter, std::list< StateSet > &rSccList, StateSet &rRoots)
Compute strongly connected components (SCC)
bool HasScc(const Generator &rGen, const SccFilter &rFilter)
Test for strongly connected components (SCC)
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
void SearchScc(const Idx vState, int &vRcount, const Generator &rGen, const SccFilter &rFilter, StateSet &rTodo, std::stack< Idx > &rStack, StateSet &rStackStates, std::map< const Idx, int > &rDfn, std::map< const Idx, int > &rLowLnk, std::list< StateSet > &rSccList, StateSet &rRoots)
Search for strongly connected components (SCC).

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen