Locus No Pilotus
Project of four first grade MIPT DAFE/RSE students (for engineering practical work in the second semester) in Qt C++
Loading...
Searching...
No Matches
math::AdjacencyMatrix Class Reference

Матрица смежности для алгоритма Литтла More...

#include <adjacency_matrix.h>

Collaboration diagram for math::AdjacencyMatrix:

Public Types

enum  Mins { Rows , Columns }
 

Public Member Functions

 AdjacencyMatrix (const AdjacencyMatrix &other)=default
 
void CalculateData ()
 
void ExtendTo (std::size_t num_of_flyers)
 Расширяет матрицу для нескольких коммивояжеров
 
double GetBottomLineEvaluation () const
 
double GetMatrixValue (std::size_t i, std::size_t j) const
 
std::pair< std::size_t, std::size_t > GetSelectedEdge () const
 
std::pair< std::size_t, std::size_t > GetSelectedValue () const
 
std::size_t GetSize () const
 
AdjacencyMatrix Minor (std::size_t i, std::size_t j)
 Создает минор матрицы
 
AdjacencyMatrixoperator= (const AdjacencyMatrix &other)=default
 
AdjacencyMatrix Reducted ()
 Создает редуцированную матрицы
 
void SetMatrixValue (std::size_t i, std::size_t j, double num)
 Меняет элемент матрицы
 

Static Public Member Functions

static AdjacencyMatrix WithExtraRowCol (const std::vector< std::vector< double > > &nums)
 Создает новый экземпляр AdjacencyMatrix с добавлением строки и столбца номеров точек
 

Private Member Functions

 AdjacencyMatrix (const std::vector< std::vector< double > > &nums)
 Создает новый экземпляр AdjacencyMatrix.
 
double BottomLineEvaluation ()
 Находит нижнюю оценку для матрицы
 
Minimums FindTwoMinimums (Mins type, std::size_t index) const
 Находит 2 минимума в строке или столбце
 
std::pair< std::size_t, std::size_t > HighestPowerOfZero () const
 Находит позицию нуля с наибольшей степенью
 

Private Attributes

double evaluation_ = 0
 
std::vector< std::vector< double > > matrix_
 
std::vector< double > min_numbers_
 
std::vector< std::vector< double > > reducted_matrix_
 
std::pair< std::size_t, std::size_t > selected_edge_
 
std::pair< std::size_t, std::size_t > selected_value_
 
std::size_t size_
 

Detailed Description

Матрица смежности для алгоритма Литтла

Member Enumeration Documentation

◆ Mins

Enumerator
Rows 
Columns 
35{ Rows, Columns };
@ Rows
Definition adjacency_matrix.h:35
@ Columns
Definition adjacency_matrix.h:35

Constructor & Destructor Documentation

◆ AdjacencyMatrix() [1/2]

math::AdjacencyMatrix::AdjacencyMatrix ( const AdjacencyMatrix & other)
default

◆ AdjacencyMatrix() [2/2]

math::AdjacencyMatrix::AdjacencyMatrix ( const std::vector< std::vector< double > > & nums)
private

Создает новый экземпляр AdjacencyMatrix.

Parameters
numsматрица смежности графа
Returns
AdjacencyMatrix: экземпляр AdjacencyMatrix по данной матрице смежности
47 : size_{nums.size()}, matrix_{nums}, min_numbers_(size_ + size_) {}
std::size_t size_
Definition adjacency_matrix.h:99
std::vector< std::vector< double > > matrix_
Definition adjacency_matrix.h:102
std::vector< double > min_numbers_
Definition adjacency_matrix.h:108

Member Function Documentation

◆ BottomLineEvaluation()

double math::AdjacencyMatrix::BottomLineEvaluation ( )
private

Находит нижнюю оценку для матрицы

Returns
double: нижняя оценку матрицы
91 {
93 double mins_sum = 0;
94 for (std::size_t i = 0; i < size_; ++i) {
95 Minimums twoMins = FindTwoMinimums(Mins::Rows, i);
96 double first_min = twoMins.first;
97 double second_min = twoMins.second;
98 for (std::size_t j = 0; j < size_; ++j) {
99 reducted_matrix_[i][j] -= first_min;
100 }
101 second_min -= first_min;
102 min_numbers_[i] = second_min;
103 mins_sum += first_min;
104 }
105
106 for (std::size_t i = 0; i < size_; ++i) {
107 Minimums twoMins = FindTwoMinimums(Mins::Columns, i);
108 double first_min = twoMins.first;
109 double second_min = twoMins.second;
110 for (std::size_t j = 0; j < size_; ++j) {
111 if (std::abs(reducted_matrix_[j][i] - min_numbers_[j]) <= precision)
112 min_numbers_[j] -= first_min;
113 reducted_matrix_[j][i] -= first_min;
114 }
115 second_min -= first_min;
116 min_numbers_[size_ + i] = second_min;
117 mins_sum += first_min;
118 }
119
120 return mins_sum;
121}
Minimums FindTwoMinimums(Mins type, std::size_t index) const
Находит 2 минимума в строке или столбце
Definition adjacency_matrix.cpp:49
std::vector< std::vector< double > > reducted_matrix_
Definition adjacency_matrix.h:105
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CalculateData()

void math::AdjacencyMatrix::CalculateData ( )
141 {
144 selected_edge_ = std::make_pair(matrix_[selected_value_.first][size_],
145 matrix_[size_][selected_value_.second]);
146}
std::pair< std::size_t, std::size_t > selected_edge_
Definition adjacency_matrix.h:114
double evaluation_
Definition adjacency_matrix.h:111
std::pair< std::size_t, std::size_t > selected_value_
Definition adjacency_matrix.h:117
double BottomLineEvaluation()
Находит нижнюю оценку для матрицы
Definition adjacency_matrix.cpp:91
std::pair< std::size_t, std::size_t > HighestPowerOfZero() const
Находит позицию нуля с наибольшей степенью
Definition adjacency_matrix.cpp:123
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExtendTo()

void math::AdjacencyMatrix::ExtendTo ( std::size_t num_of_flyers)

Расширяет матрицу для нескольких коммивояжеров

Parameters
num_of_flyersколичество коммивояжеров
148 {
149 if (num_of_flyers < 2)
150 throw std::runtime_error("dev: num_of_flyers < 2 in ExtendTo");
151
152 for (std::size_t i = 1; i < num_of_flyers; ++i) {
153 matrix_.insert(matrix_.begin() + size_, matrix_[0]);
154 for (std::size_t j = 0; j < size_ + 1; ++j) {
155 matrix_[j].insert(matrix_[j].begin() + size_, matrix_[j][0]);
156 }
157 matrix_[size_ + 1][size_] = (double)size_;
158 matrix_[size_][size_ + 1] = (double)size_;
159 matrix_[size_ + 1].push_back(0);
160 ++size_;
161 min_numbers_.resize(size_ + size_);
162 }
163 for (std::size_t i = 1; i < num_of_flyers; ++i) {
164 for (std::size_t j = 1; j < num_of_flyers; ++j) {
165 if (i != j) matrix_[size_ - i][size_ - j] = 0;
166 }
167 matrix_[0][size_ - i] = 0;
168 matrix_[size_ - i][0] = 0;
169 }
171}
void CalculateData()
Definition adjacency_matrix.cpp:141
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindTwoMinimums()

Minimums math::AdjacencyMatrix::FindTwoMinimums ( Mins type,
std::size_t index ) const
private

Находит 2 минимума в строке или столбце

Parameters
typeтип поиска минимума(в строке или в столбце)
indexномер строки/столбца
Returns
Minimums: 2 найденных минимума
49 {
50 Minimums result;
51 double first_min = inf;
52 double second_min = inf;
53 switch (type) {
54 case Mins::Rows: {
55 for (std::size_t j = 0; j < size_; ++j) {
56 if (reducted_matrix_[index][j] < first_min) {
57 second_min = first_min;
58 first_min = reducted_matrix_[index][j];
59 } else if (reducted_matrix_[index][j] < second_min) {
60 second_min = reducted_matrix_[index][j];
61 }
62 }
63 result.first = first_min;
64 result.second = second_min;
65 break;
66 }
67
68 case Mins::Columns: {
69 for (std::size_t i = 0; i < size_; ++i) {
70 if (reducted_matrix_[i][index] < first_min) {
71 second_min = first_min;
72 first_min = reducted_matrix_[i][index];
73 } else if (reducted_matrix_[i][index] < second_min) {
74 second_min = reducted_matrix_[i][index];
75 }
76 }
77 result.first = first_min;
78 result.second = second_min;
79 break;
80 }
81
82 default: {
83 result.first = first_min;
84 result.second = second_min;
85 break;
86 }
87 }
88 return result;
89}
Here is the caller graph for this function:

◆ GetBottomLineEvaluation()

double math::AdjacencyMatrix::GetBottomLineEvaluation ( ) const
inline
60{ return evaluation_; }

◆ GetMatrixValue()

double math::AdjacencyMatrix::GetMatrixValue ( std::size_t i,
std::size_t j ) const
inline
55 {
56 return matrix_[i][j];
57 }
Here is the caller graph for this function:

◆ GetSelectedEdge()

std::pair< std::size_t, std::size_t > math::AdjacencyMatrix::GetSelectedEdge ( ) const
inline
64 {
65 return selected_edge_;
66 }

◆ GetSelectedValue()

std::pair< std::size_t, std::size_t > math::AdjacencyMatrix::GetSelectedValue ( ) const
inline
70 {
71 return selected_value_;
72 }

◆ GetSize()

std::size_t math::AdjacencyMatrix::GetSize ( ) const
inline
52{ return size_; }
Here is the caller graph for this function:

◆ HighestPowerOfZero()

std::pair< std::size_t, std::size_t > math::AdjacencyMatrix::HighestPowerOfZero ( ) const
private

Находит позицию нуля с наибольшей степенью

Returns
std::pair<std::size_t, std::size_t>: позицию нуля с наибольшей степенью
124 {
125 std::size_t row = 0, col = 0;
126 double max = -1;
127 for (std::size_t i = 0; i < size_; ++i) {
128 for (std::size_t j = 0; j < size_; ++j) {
129 if (std::abs(reducted_matrix_[i][j]) <= precision) {
130 if ((min_numbers_[i] + min_numbers_[size_ + j]) > max) {
131 max = min_numbers_[i] + min_numbers_[size_ + j];
132 row = i;
133 col = j;
134 }
135 }
136 }
137 }
138 return std::make_pair(row, col);
139}
Here is the caller graph for this function:

◆ Minor()

AdjacencyMatrix math::AdjacencyMatrix::Minor ( std::size_t i,
std::size_t j )

Создает минор матрицы

Parameters
iномер удаляемой строки
jномер удаляемого столбца
Returns
AdjacencyMatrix: минор матрицы
28 {
29 std::vector<std::vector<double>> minor_matrix = matrix_;
30 minor_matrix.erase(minor_matrix.begin() + i);
31 for (std::size_t k = 0; k < size_; ++k) {
32 minor_matrix[k].erase(minor_matrix[k].begin() + j);
33 }
34 AdjacencyMatrix minor(minor_matrix);
35 minor.size_--;
36 minor.CalculateData();
37 return minor;
38}
AdjacencyMatrix(const AdjacencyMatrix &other)=default
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

AdjacencyMatrix & math::AdjacencyMatrix::operator= ( const AdjacencyMatrix & other)
default

◆ Reducted()

AdjacencyMatrix math::AdjacencyMatrix::Reducted ( )

Создает редуцированную матрицы

Returns
AdjacencyMatrix: редуцированная матрица
40 {
41 AdjacencyMatrix reducted = *this;
42 reducted.matrix_ = reducted_matrix_;
43 return reducted;
44}

◆ SetMatrixValue()

void math::AdjacencyMatrix::SetMatrixValue ( std::size_t i,
std::size_t j,
double num )

Меняет элемент матрицы

Parameters
iномер строки элемента
jномер столбца элемента
numновое значение элемента
23 {
24 matrix_[i][j] = num;
26}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ WithExtraRowCol()

AdjacencyMatrix math::AdjacencyMatrix::WithExtraRowCol ( const std::vector< std::vector< double > > & nums)
static

Создает новый экземпляр AdjacencyMatrix с добавлением строки и столбца номеров точек

Parameters
numsматрица смежности графа
Returns
AdjacencyMatrix: экземпляр AdjacencyMatrix по данной матрице смежности
10 {
11 AdjacencyMatrix m(nums);
12 m.matrix_.resize(m.size_ + 1);
13 m.matrix_[m.size_].resize(m.size_ + 1);
14 for (std::size_t i = 0; i < m.size_; ++i) {
15 m.matrix_[i].resize(m.size_ + 1);
16 m.matrix_[i][m.size_] = (double)i;
17 m.matrix_[m.size_][i] = (double)i;
18 }
19 m.CalculateData();
20 return m;
21}
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ evaluation_

double math::AdjacencyMatrix::evaluation_ = 0
private

◆ matrix_

std::vector<std::vector<double> > math::AdjacencyMatrix::matrix_
private

◆ min_numbers_

std::vector<double> math::AdjacencyMatrix::min_numbers_
private

◆ reducted_matrix_

std::vector<std::vector<double> > math::AdjacencyMatrix::reducted_matrix_
private

◆ selected_edge_

std::pair<std::size_t, std::size_t> math::AdjacencyMatrix::selected_edge_
private

◆ selected_value_

std::pair<std::size_t, std::size_t> math::AdjacencyMatrix::selected_value_
private

◆ size_

std::size_t math::AdjacencyMatrix::size_
private

The documentation for this class was generated from the following files: