From 52a435978b367fb94834f7c02debd06327c5e930 Mon Sep 17 00:00:00 2001 From: Aleksei Slaikovskii Date: Fri, 7 Jul 2017 15:50:06 +0200 Subject: [PATCH] ipapython/graph.py complexity optimization Hi! I've just read the code and I saw that graph bfs uses not optimal for Python solution. So I've edited it with more optimal one. https://pagure.io/freeipa/issue/7051 Reviewed-By: Alexander Bokovoy Reviewed-By: Martin Basti --- ipapython/graph.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ipapython/graph.py b/ipapython/graph.py index 6b31c915c..ed0b359f7 100644 --- a/ipapython/graph.py +++ b/ipapython/graph.py @@ -1,6 +1,7 @@ # -# Copyright (C) 2015 FreeIPA Contributors see COPYING for license +# Copyright (C) 2015-2017 FreeIPA Contributors see COPYING for license # +from collections import deque class Graph(object): @@ -69,11 +70,12 @@ class Graph(object): Return a set of all visited vertices """ if not start: - start = list(self.vertices)[0] + start = next(iter(self.vertices)) visited = set() - queue = [start] + queue = deque([start]) + while queue: - vertex = queue.pop(0) + vertex = queue.popleft() if vertex not in visited: visited.add(vertex) queue.extend(set(self._adj.get(vertex, [])) - visited)