forked from UnitTestBot/UTBotJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
42 lines (33 loc) · 916 Bytes
/
graph.py
File metadata and controls
42 lines (33 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from __future__ import annotations
from collections import deque
from typing import List
class Node:
def __init__(self, name: str, children: List[Node]):
self.name = name
self.children = children
def __repr__(self):
return f'<Node: {self.name}>'
def __eq__(self, other):
if isinstance(other, Node):
return self.name == other.name
else:
return False
def bfs(nodes):
if len(nodes) == 0:
return []
visited = []
queue = deque(nodes)
while len(queue) > 0:
node = queue.pop()
if node not in visited:
visited.append(node)
for child in node.children:
queue.append(child)
return visited
if __name__ == '__main__':
a = Node('a', [])
b = Node('b', [])
c = Node('c', [])
a.children.append(b)
b.children.append(c)
print(bfs([a, b, c]))