|
| 1 | +from pydatastructs.graphs import Graph |
| 2 | +from pydatastructs.utils import AdjacencyListGraphNode |
| 3 | +from pydatastructs.utils.raises_util import raises |
| 4 | + |
| 5 | +def test_adjacency_list(): |
| 6 | + v_1 = AdjacencyListGraphNode('v_1', 1) |
| 7 | + v_2 = AdjacencyListGraphNode('v_2', 2) |
| 8 | + g = Graph(v_1, v_2, implementation='adjacency_list') |
| 9 | + v_3 = AdjacencyListGraphNode('v_3', 3) |
| 10 | + g.add_vertex(v_2) |
| 11 | + g.add_vertex(v_3) |
| 12 | + g.add_edge('v_1', 'v_2') |
| 13 | + g.add_edge('v_2', 'v_3') |
| 14 | + g.add_edge('v_3', 'v_1') |
| 15 | + assert g.is_adjacent('v_1', 'v_2') is True |
| 16 | + assert g.is_adjacent('v_2', 'v_3') is True |
| 17 | + assert g.is_adjacent('v_3', 'v_1') is True |
| 18 | + assert g.is_adjacent('v_2', 'v_1') is False |
| 19 | + assert g.is_adjacent('v_3', 'v_2') is False |
| 20 | + assert g.is_adjacent('v_1', 'v_3') is False |
| 21 | + neighbors = g.neighbors('v_1') |
| 22 | + assert neighbors == [v_2] |
| 23 | + v = AdjacencyListGraphNode('v', 4) |
| 24 | + g.add_vertex(v) |
| 25 | + g.add_edge('v_1', 'v', 0) |
| 26 | + g.add_edge('v_2', 'v', 0) |
| 27 | + g.add_edge('v_3', 'v', 0) |
| 28 | + assert g.is_adjacent('v_1', 'v') is True |
| 29 | + assert g.is_adjacent('v_2', 'v') is True |
| 30 | + assert g.is_adjacent('v_3', 'v') is True |
| 31 | + e1 = g.get_edge('v_1', 'v') |
| 32 | + e2 = g.get_edge('v_2', 'v') |
| 33 | + e3 = g.get_edge('v_3', 'v') |
| 34 | + assert (e1.source.name, e1.target.name) == ('v_1', 'v') |
| 35 | + assert (e2.source.name, e2.target.name) == ('v_2', 'v') |
| 36 | + assert (e3.source.name, e3.target.name) == ('v_3', 'v') |
| 37 | + g.remove_edge('v_1', 'v') |
| 38 | + assert g.is_adjacent('v_1', 'v') is False |
| 39 | + g.remove_vertex('v') |
| 40 | + assert g.is_adjacent('v_2', 'v') is False |
| 41 | + assert g.is_adjacent('v_3', 'v') is False |
| 42 | + |
| 43 | + assert raises(ValueError, lambda: g.add_edge('u', 'v')) |
| 44 | + assert raises(ValueError, lambda: g.add_edge('v', 'x')) |
0 commit comments