This repository was archived by the owner on Dec 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_mapping.py
More file actions
65 lines (51 loc) · 2.25 KB
/
Copy pathgraph_mapping.py
File metadata and controls
65 lines (51 loc) · 2.25 KB
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
from typing import Optional
class GraphMapping():
"""
Invertable unique dict using sets as values to encode the mapping between
nodes in the input graph H to embed into the hardware graph G.
"""
def __init__(self, size_H: int):
self.H_to_G = dict() # values are ints
self.G_to_H = dict() # values are sets
self.max_supernodes_count = size_H
def get_nodes_G(self, node_H: int) -> set[int]:
# Note that we do not handle KeyErrors here as they should never occur
# Might want to use method: get_supernode_create_if_not_available()
return self.H_to_G[node_H].copy()
def get_node_H(self, node_G: int) -> int:
# Note that we do not handle KeyErrors here as they should never occur
return self.G_to_H[node_G]
def set_mapping(self, node_H: int, node_G: int):
self.H_to_G[node_H] = set([node_G])
self.G_to_H[node_G] = node_H
def extend_mapping(self, node_H: int, node_G: int):
"""Places ``node_G`` into the super node representing ``node_H``."""
try:
self.H_to_G[node_H].add(node_G)
except KeyError:
self.H_to_G[node_H] = set([node_G])
self.G_to_H[node_G] = node_H
def remove_mapping(self, node_H: int, node_G: int):
"""Removes ``node_G`` from the super node represents ``node_H``."""
self.H_to_G[node_H].remove(node_G)
del self.G_to_H[node_G]
def get_supernode_create_if_not_available(self, node_G: int) -> int:
"""Returns the supernode for ``node_G``.
If there is no supernode of ``node_G``, create a new supernode and return it.
"""
try:
supernode = self.G_to_H[node_G]
except KeyError:
supernode = self._get_new_supernode()
self.set_mapping(supernode, node_G)
return supernode
def _get_new_supernode(self) -> int:
supernode = len(self.H_to_G.keys())
if supernode >= self.max_supernodes_count:
raise KeyError(f'Maximum of possible embeddings reached: '
f'{self.max_supernodes_count}')
return supernode
def get_mapping_H_to_G(self):
return self.H_to_G.copy()
def get_mapping_G_to_H(self):
return self.G_to_H.copy()