diff options
| author | Tim Blazytko <tim.blazytko@rub.de> | 2015-09-03 08:21:49 +0200 |
|---|---|---|
| committer | Tim Blazytko <tim.blazytko@rub.de> | 2015-09-04 16:17:51 +0200 |
| commit | 54a6a2d915ea331a8505bd0dbcfdcacbf0991394 (patch) | |
| tree | 9886691a5c84377df3aea8796fc79cc35e1e2192 /miasm2/core/graph.py | |
| parent | e58c094050a1873d9eec4b609804f3e1d6cbb6bd (diff) | |
| download | miasm-54a6a2d915ea331a8505bd0dbcfdcacbf0991394.tar.gz miasm-54a6a2d915ea331a8505bd0dbcfdcacbf0991394.zip | |
DiGraph: added breadth and depth first traversals
Diffstat (limited to 'miasm2/core/graph.py')
| -rw-r--r-- | miasm2/core/graph.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/miasm2/core/graph.py b/miasm2/core/graph.py index 2565f3d1..c14a2f68 100644 --- a/miasm2/core/graph.py +++ b/miasm2/core/graph.py @@ -339,3 +339,42 @@ shape = "box" frontier[runner].add(node) runner = idoms[runner] return frontier + + def _walk_generic_first(self, head, flag, succ_cb): + """ + Generic algorithm to compute breadth or depth first search + for a node. + @head: the head of the graph + @flag: denotes if @todo is used as queue or stack + @succ_cb: returns a node's predecessors/successors + :return: next node + """ + todo = [head] + done = set() + + while todo: + node = todo.pop(flag) + if node in done: + continue + done.add(node) + + for succ in succ_cb(node): + todo.append(succ) + + yield node + + def walk_breadth_first_forward(self, head): + """Performs a breath first search on the graph from @head""" + return self._walk_generic_first(head, 0, self.successors_iter) + + def walk_depth_first_forward(self, head): + """Performs a depth first search on the graph from @head""" + return self._walk_generic_first(head, -1, self.successors_iter) + + def walk_breadth_first_backward(self, head): + """Performs a breath first search on the reversed graph from @head""" + return self._walk_generic_first(head, 0, self.predecessors_iter) + + def walk_depth_first_backward(self, head): + """Performs a depth first search on the reversed graph from @head""" + return self._walk_generic_first(head, -1, self.predecessors_iter) |