Coverage for src/abcd_graph/graph/core/abcd_objects/utils.py: 100%

1 import random

2

3 from abcd_graph.graph.core.abcd_objects.edge import Edge

4

5

6 def build_recycle_list(adj_matrix: dict["Edge", int]) -> list["Edge"]:

7 return [edge for edge in adj_matrix.keys() if adj_matrix[edge] > 1 or edge.is_loop]

8

9

10 def choose_other_edge(adj_matrix: dict["Edge", int], edge: "Edge") -> "Edge":

11 edges = list(adj_matrix.keys())

12 other_edge = random.choice(edges)

13 while other_edge == edge:

14 other_edge = random.choice(edges)

15

16 return other_edge

17

18

19 def rewire_edge(adj_matrix: dict["Edge", int], edge: "Edge", other_edge: "Edge") -> None:

20 if edge not in adj_matrix:

21 return

22 adj_matrix[edge] -= 1

23

24 if adj_matrix[edge] == 0:

25 del adj_matrix[edge]

26

27 adj_matrix[other_edge] -= 1

28 if adj_matrix[other_edge] == 0:

29 del adj_matrix[other_edge]

30

31 new_edge = Edge(edge.v1, other_edge.v1)

32

33 if new_edge in adj_matrix:

34 adj_matrix[new_edge] += 1

35 else:

36 adj_matrix[new_edge] = 1

37

38 new_edge = Edge(edge.v2, other_edge.v2)

39

40 if new_edge in adj_matrix:

41 adj_matrix[new_edge] += 1

42 else:

43 adj_matrix[new_edge] = 1