-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex_manipulations.py
81 lines (67 loc) · 2.38 KB
/
vertex_manipulations.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from random import randrange
def degree(node: int, edges: list[(int, int)]) -> int:
deg = 0
for (ea, eb) in edges:
if ea == node:
deg += 1
if eb == node:
deg += 1
return deg
def find_adjacent_nodes(node: int, edges: list[(int, int)]) -> list[int]:
nodes = []
for (ea, eb) in edges:
if ea == node:
nodes += [eb]
if eb == node:
nodes += [ea]
return nodes
def find_isolated_nodes(nodes: list[int], edges: list[(int, int)]) -> list[int]:
isolated = []
for node in nodes:
if degree(node, edges) == 0:
isolated += [node]
return isolated
def find_pendant_nodes(nodes: list[int], edges: list[(int, int)]) -> list[int]:
pendant = []
for node in nodes:
if degree(node, edges) == 1:
pendant += [node]
return pendant
def find_tops_nodes(nodes: list[int], edges: list[(int, int)], k: int) -> list[int]:
tops = []
for node in nodes:
if degree(node, edges) > k:
tops += [node]
k -= 1
return tops
def random_node(nodes: list[int], not_in: list[int] = []) -> int:
working = [n for n in nodes if n not in not_in]
return working[randrange(len(working))]
def remove_edges_for(node: int, edges: list[(int, int)]) -> list[(int, int)]:
return [(ea, eb) for (ea, eb) in edges if ea != node and eb != node]
def kernalize_vertices(nodes: list[int], edges: list[(int, int)], k) -> (list[int], list[(int, int)], list[int], int):
result_nodes = nodes[:]
result_edges = edges[:]
nodes_surely_in_cover = []
i = 0
len_nodes = len(nodes)
looping = True
while looping:
looping = False
for node in result_nodes:
deg = degree(node, result_edges)
if k > 0 and deg > k:
k -= 1
nodes_surely_in_cover += [node]
result_nodes.remove(node)
result_edges = remove_edges_for(node, result_edges)
looping = True
elif deg == 0:
result_nodes.remove(node)
looping = True
if looping == False and len(result_edges) > k ** 2:
result_nodes = result_edges = nodes_surely_in_cover = None
break
if result_nodes is None:
return (None, None, None, None)
return (result_nodes, result_edges, sorted(nodes_surely_in_cover), k)