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 pathembedding_state.cpp
More file actions
116 lines (100 loc) · 3.5 KB
/
Copy pathembedding_state.cpp
File metadata and controls
116 lines (100 loc) · 3.5 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "embedding_state.hpp"
#include "common/utils.hpp"
using namespace majorminer;
EmbeddingState::EmbeddingState(const graph_t& sourceGraph, const graph_t& targetGraph, EmbeddingVisualizer* vis)
: m_sourceGraph(&sourceGraph), m_targetGraph(&targetGraph), m_visualizer(vis)
{
initialize();
}
void EmbeddingState::initialize()
{
convertToAdjacencyList(m_source, *m_sourceGraph);
convertToAdjacencyList(m_target, *m_targetGraph);
for (const auto& arc : *m_targetGraph)
{
m_targetNodesRemaining.insert(arc.first);
m_targetNodesRemaining.insert(arc.second);
}
for (const auto& arc : *m_sourceGraph)
{
m_nodesRemaining[arc.first] = 0;
m_nodesRemaining[arc.second] = 0;
m_sourceNeededNeighbors[arc.first]++;
m_sourceNeededNeighbors[arc.second]++;
}
m_numberSourceVertices = m_nodesRemaining.size();
}
fuint32_t EmbeddingState::getTrivialNode()
{ // TODO: assert
auto node = *m_nodesRemaining.begin();
m_nodesRemaining.unsafe_erase(m_nodesRemaining.begin());
return node.first;
}
bool EmbeddingState::removeRemainingNode(fuint32_t node)
{
if (!m_nodesRemaining.contains(node)) return false;
m_nodesRemaining.unsafe_erase(node);
return true;
}
void EmbeddingState::unmapNode(vertex_t sourceVertex)
{
auto range = m_mapping.equal_range(sourceVertex);
for (auto mappedIt = range.first; mappedIt != range.second; ++mappedIt)
{
eraseSinglePair(m_reverseMapping, mappedIt->second, mappedIt->first);
}
m_mapping.unsafe_erase(sourceVertex);
}
void EmbeddingState::updateNeededNeighbors(fuint32_t node)
{
fuint32_t nbNodes = 0;
iterateSourceGraphAdjacent(node, [&, this](fuint32_t adjacentSource){
if (isNodeMapped(adjacentSource))
{
nbNodes++; m_sourceNeededNeighbors[adjacentSource]--;
}
});
m_sourceNeededNeighbors[node] -= nbNodes;
}
void EmbeddingState::updateConnections(fuint32_t node, PrioNodeQueue& nodesToProcess)
{
iterateSourceGraphAdjacent(node, [&](fuint32_t adjacent){
auto findIt = m_nodesRemaining.find(adjacent);
if (findIt != m_nodesRemaining.end())
{
findIt->second += 1; // one of its neighbors is now embedded
nodesToProcess.push(PrioNode{findIt->first, findIt->second});
}
});
}
int EmbeddingState::numberFreeNeighborsNeeded(fuint32_t sourceNode) const
{ // TODO: rework
// std::cout << "Source node " << sourceNode << " needs " << m_sourceNeededNeighbors[sourceNode].load() << " neighbors and has " << m_sourceFreeNeighbors[sourceNode].load() << std::endl;
auto it = m_sourceNeededNeighbors.find(sourceNode);
return 2 * (it == m_sourceNeededNeighbors.end() ? 0 : it->second.load())
- std::max(getSourceNbFreeNeighbors(sourceNode), 0);
}
int EmbeddingState::getSourceNbFreeNeighbors(fuint32_t sourceNode) const
{
auto it = m_sourceFreeNeighbors.find(sourceNode);
return it == m_sourceFreeNeighbors.end() ? 0 : it->second.load();
}
void EmbeddingState::mapNode(fuint32_t source, fuint32_t targetNode)
{
m_nodesOccupied.insert(targetNode);
m_mapping.insert(std::make_pair(source, targetNode));
m_reverseMapping.insert(std::make_pair(targetNode, source));
m_targetNodesRemaining.unsafe_extract(targetNode);
removeRemainingNode(source);
}
void EmbeddingState::mapNode(fuint32_t source, const nodeset_t& targets)
{
for (auto targetNode : targets)
{
m_nodesOccupied.insert(targetNode);
m_mapping.insert(std::make_pair(source, targetNode));
m_reverseMapping.insert(std::make_pair(targetNode, source));
m_targetNodesRemaining.unsafe_extract(targetNode);
}
removeRemainingNode(source);
}