## 2019年2月12日火曜日

### Algorithm - Python - 6次の隔たり - 集合演算、集合を使ったグラフの幅優先探索(辞書、無向グラフ、対称性のチェック)

コード

Python 3

```#!/usr/bin/env python3
def is_symmetry(graph: dict) -> bool:
'''
>>> is_symmetry({})
True
>>> is_symmetry({'a': ['b']})
False
>>> is_symmetry({'a': ['b'], 'b': {'a'}})
True
>>> is_symmetry({'a':['b', 'c'], 'b': ['a', 'c'], 'c': ['a', 'b'], 'd': ['e', 'f'], 'e': ['d', 'f'], 'f':['d', 'e']})
True
>>> is_symmetry({'a':['b', 'c'], 'b': ['a'], 'c': ['a', 'b'], 'd': ['e', 'f'], 'e': ['d', 'f'], 'f':['d', 'e']})
False
'''
for k, v in graph.items():
for t in v:
if t not in graph or k not in graph[t]:
return False
return True

if __name__ == '__main__':
import doctest
doctest.testmod()
```

```C:\Users\...>py -3 sample2.py -v
Trying:
is_symmetry({})
Expecting:
True
ok
Trying:
is_symmetry({'a': ['b']})
Expecting:
False
ok
Trying:
is_symmetry({'a': ['b'], 'b': {'a'}})
Expecting:
True
ok
Trying:
is_symmetry({'a':['b', 'c'], 'b': ['a', 'c'], 'c': ['a', 'b'], 'd': ['e', 'f'], 'e': ['d', 'f'], 'f':['d', 'e']})
Expecting:
True
ok
Trying:
is_symmetry({'a':['b', 'c'], 'b': ['a'], 'c': ['a', 'b'], 'd': ['e', 'f'], 'e': ['d', 'f'], 'f':['d', 'e']})
Expecting:
False
ok
__main__
1 items passed all tests:
5 tests in __main__.is_symmetry
5 tests in 2 items.
5 passed and 0 failed.
Test passed.

C:\Users\...>
```