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 2017-07-07 15:50:06 +02:00 committed by Tomas Krizek
parent f18ce01355
commit 52a435978b
No known key found for this signature in database
GPG Key ID: 22A2A94B5E49415A

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): class Graph(object):
@ -69,11 +70,12 @@ class Graph(object):
Return a set of all visited vertices Return a set of all visited vertices
""" """
if not start: if not start:
start = list(self.vertices)[0] start = next(iter(self.vertices))
visited = set() visited = set()
queue = [start] queue = deque([start])
while queue: while queue:
vertex = queue.pop(0) vertex = queue.popleft()
if vertex not in visited: if vertex not in visited:
visited.add(vertex) visited.add(vertex)
queue.extend(set(self._adj.get(vertex, [])) - visited) queue.extend(set(self._adj.get(vertex, [])) - visited)