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
|
#include <iostream>
#include <vector>
void displayElements(const std::vector<std::vector<int>>& elements, std::ostream& out);
int getMaxElement(const std::vector<std::vector<int>>& elements);
void sizeAdjacencyMatrix(std::vector<std::vector<int>>& adjacencies, int maxElement);
void findAdjacencies(std::vector<std::vector<int>> adjacencies, const std::vector<std::vector<int>>& elements);
void displayAdjacencies(const std::vector<std::vector<int>>& adjacencies, std::ostream& out);
int main()
{
std::vector<std::vector<int>> elements{ {1, 2, 4}, {1, 4, 5}, {2, 3, 4}, {5, 4, 6} };
std::vector<std::vector<int>> adjacencies;
displayElements(elements, std::cout);
std::cout << std::endl;
int maxElement = getMaxElement(elements);
sizeAdjacencyMatrix(adjacencies, maxElement);
findAdjacencies(adjacencies, elements);
displayAdjacencies(adjacencies, std::cout);
}
void displayElements(const std::vector<std::vector<int>>& elements, std::ostream& out)
{
for (size_t i = 0; i < elements.size(); ++i)
{
out << "element " << (i + 1) << ":";
for (int e : elements[i])
{
out << " " << e;
}
out << std::endl;
}
}
int getMaxElement(const std::vector<std::vector<int>>& elements)
{
int maxElement = 0;
for (auto element : elements)
{
for (int e : element)
{
if (e > maxElement)
{
maxElement = e;
}
}
}
return maxElement;
}
void sizeAdjacencyMatrix(std::vector<std::vector<int>>& adjacencies, int maxElement)
{
adjacencies.resize(maxElement);
for (auto& adjacency : adjacencies)
{
adjacency.resize(maxElement);
}
}
void findAdjacencies(std::vector<std::vector<int>> adjacencies, const std::vector<std::vector<int>>& elements)
{
for (auto element : elements)
{
for (size_t idx = 0; idx < element.size(); ++idx)
{
for (size_t other = idx; other < element.size(); ++other)
{
int node1 = element[idx];
int node2 = element[other];
int node1Idx = node1 - 1;
int node2Idx = node2 - 1;
adjacencies[node1Idx][node2Idx] = node2;
adjacencies[node2Idx][node1Idx] = node1;
}
}
}
}
void displayAdjacencies(const std::vector<std::vector<int>>& adjacencies, std::ostream& out)
{
for (size_t i = 0; i < adjacencies.size(); ++i)
{
out << "node " << (i + 1) << ":";
for (int n : adjacencies[i])
{
out << " " << n;
}
out << std::endl;
}
}
|