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 <abokovoy@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
Aleksei Slaikovskii authored and Tomas Krizek committed 2017-07-14 09:16:13 +02:00
1 parent f18ce01355
commit 52a435978b
1 file changed
+6 -4
+6 -4
View File
@@ -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)