Skip to content

Commit b23de09

Browse files
committed
Optimal BST realization
1 parent 4530892 commit b23de09

File tree

11 files changed

+282
-11
lines changed

11 files changed

+282
-11
lines changed

General/General.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
<ClInclude Include="Knapsack.h" />
119119
<ClInclude Include="Matrix.h" />
120120
<ClInclude Include="MaxWIS.h" />
121+
<ClInclude Include="OptimalBST.h" />
121122
<ClInclude Include="SequenceAlignment.h" />
122123
</ItemGroup>
123124
<ItemGroup>
@@ -126,6 +127,7 @@
126127
<FileType>CppCode</FileType>
127128
</ClInclude>
128129
<ClCompile Include="MaxWIS.cpp" />
130+
<ClCompile Include="OptimalBST.cpp" />
129131
<ClCompile Include="SequenceAlignment.cpp" />
130132
</ItemGroup>
131133
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

General/General.vcxproj.filters

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<ClInclude Include="Matrix.h">
2828
<Filter>Header Files</Filter>
2929
</ClInclude>
30+
<ClInclude Include="Matrix.hpp">
31+
<Filter>Header Files</Filter>
32+
</ClInclude>
33+
<ClInclude Include="OptimalBST.h">
34+
<Filter>Header Files</Filter>
35+
</ClInclude>
3036
</ItemGroup>
3137
<ItemGroup>
3238
<ClCompile Include="MaxWIS.cpp">
@@ -38,8 +44,8 @@
3844
<ClCompile Include="SequenceAlignment.cpp">
3945
<Filter>Source Files</Filter>
4046
</ClCompile>
41-
<ClCompile Include="Matrix.hpp">
42-
<Filter>Header Files</Filter>
47+
<ClCompile Include="OptimalBST.cpp">
48+
<Filter>Source Files</Filter>
4349
</ClCompile>
4450
</ItemGroup>
4551
</Project>

General/Matrix.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ class Matrix
1919

2020
public:
2121
Matrix();
22-
Matrix(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc);
23-
Matrix(const Matrix<ValueType>& matrix) throw (std::bad_alloc);
22+
Matrix(uint64_t rowsCount, uint64_t colsCount);
23+
Matrix(const Matrix<ValueType>& matrix);
2424
Matrix(Matrix<ValueType>&& matrix);
2525
~Matrix();
2626

2727
ValueType* operator[] (uint64_t i);
2828

2929
void reset();
30-
void resize(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc);
30+
void resize(uint64_t rowsCount, uint64_t colsCount);
3131

3232
const uint64_t getRowsCount() const;
3333
const uint64_t getColsCount() const;
34+
35+
template <typename U>
36+
friend std::ostream& operator << (std::ostream& out, Matrix<U>& matrix);
3437
};
3538

3639
template<typename T>

General/Matrix.hpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Matrix<T>::Matrix():
77
}
88

99
template<typename T>
10-
Matrix<T>::Matrix(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc) :
10+
Matrix<T>::Matrix(uint64_t rowsCount, uint64_t colsCount) :
1111
m_rowsCount(rowsCount),
1212
m_colsCount(colsCount),
1313
m_matrix()
@@ -16,7 +16,7 @@ Matrix<T>::Matrix(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc)
1616
}
1717

1818
template<typename T>
19-
Matrix<T>::Matrix(const Matrix<T>& matrix) throw (std::bad_alloc) :
19+
Matrix<T>::Matrix(const Matrix<T>& matrix) :
2020
m_rowsCount(matrix.m_rowsCount),
2121
m_colsCount(matrix.m_colsCount),
2222
m_matrix()
@@ -56,7 +56,7 @@ void Matrix<T>::reset()
5656
}
5757

5858
template<typename T>
59-
void Matrix<T>::resize(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_alloc)
59+
void Matrix<T>::resize(uint64_t rowsCount, uint64_t colsCount)
6060
{
6161
std::unique_ptr<ValueType> matrix(new ValueType[rowsCount * colsCount]);
6262
uint64_t minColsCount = std::min(m_colsCount, colsCount);
@@ -69,4 +69,22 @@ void Matrix<T>::resize(uint64_t rowsCount, uint64_t colsCount) throw (std::bad_a
6969
m_rowsCount = rowsCount;
7070
m_colsCount = colsCount;
7171
m_matrix = std::move(matrix);
72+
}
73+
74+
template <typename U>
75+
std::ostream& operator << (std::ostream& out, Matrix<U>& matrix)
76+
{
77+
for (uint64_t i = 0; i < matrix.m_rowsCount; ++i)
78+
{
79+
for (uint64_t j = 0; j < matrix.m_colsCount; ++j)
80+
{
81+
if (j != 0)
82+
{
83+
out << ' ';
84+
}
85+
out << matrix[i][j];
86+
}
87+
out << std::endl;
88+
}
89+
return out;
7290
}

General/OptimalBST.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <algorithm>
2+
3+
#include "OptimalBST.h"
4+
5+
6+
OptimalBST::Node::Node():
7+
m_left(),
8+
m_right(),
9+
m_idx(0)
10+
{}
11+
12+
OptimalBST::Node::~Node()
13+
{
14+
m_left.reset();
15+
m_right.reset();
16+
}
17+
18+
OptimalBST::MatrixEntity::MatrixEntity() :
19+
m_val(0),
20+
m_rootPos(0)
21+
{}
22+
23+
OptimalBST::MatrixEntity::MatrixEntity(MatrixEntity&& matrixEntity)
24+
{
25+
m_val = matrixEntity.m_val;
26+
m_rootPos = matrixEntity.m_rootPos;
27+
matrixEntity.m_val = 0;
28+
matrixEntity.m_rootPos = 0;
29+
}
30+
31+
std::ostream& operator<< (std::ostream& out, OptimalBST::MatrixEntity& matrixEntity)
32+
{
33+
out << "[rootPos : " << matrixEntity.m_rootPos << " ; val : " << matrixEntity.m_val << ']';
34+
return out;
35+
}
36+
37+
OptimalBST::OptimalBST():
38+
m_probabilities()
39+
{
40+
}
41+
42+
OptimalBST::~OptimalBST()
43+
{
44+
}
45+
46+
void OptimalBST::clear()
47+
{
48+
m_probabilities.clear();
49+
}
50+
51+
bool OptimalBST::read(std::istream& in)
52+
{
53+
clear();
54+
55+
uint32_t numElements = 0;
56+
in >> numElements;
57+
m_probabilities.resize(numElements);
58+
59+
for (auto& probability : m_probabilities)
60+
{
61+
in >> probability;
62+
}
63+
64+
return true;
65+
}
66+
67+
bool OptimalBST::run(double &c)
68+
{
69+
70+
uint32_t numProbabilities = static_cast<uint32_t>(m_probabilities.size());
71+
m_matrix.resize(numProbabilities, numProbabilities);
72+
73+
// utility structures
74+
MatrixEntity minVal;
75+
double tmpVal = 0;
76+
for (uint32_t s = 0; s < numProbabilities; ++s)
77+
{
78+
for (uint32_t i = 0; i < numProbabilities - s; ++i)
79+
{
80+
double probSum = 0;
81+
for (uint32_t k = i; k <= i + s; ++k)
82+
{
83+
probSum += m_probabilities[k];
84+
}
85+
86+
// r = i
87+
minVal.m_rootPos = i;
88+
minVal.m_val = ((i + 1) > (i + s)) ? 0 : m_matrix[i + 1][i + s].m_val;
89+
for (uint32_t r = i + 1; r <= i + s; ++r)
90+
{
91+
tmpVal = ((i + 1 > r) ? 0 : m_matrix[i][r - 1].m_val) +
92+
(((r + 1) > (i + s)) ? 0 : m_matrix[r + 1][i + s].m_val);
93+
if (tmpVal < minVal.m_val)
94+
{
95+
minVal.m_val = tmpVal;
96+
minVal.m_rootPos = r;
97+
}
98+
}
99+
minVal.m_val += probSum;
100+
m_matrix[i][i + s] = std::move(minVal);
101+
}
102+
}
103+
104+
c = m_matrix[0][numProbabilities - 1].m_val;
105+
106+
formTree(m_tree, m_matrix, 0, numProbabilities - 1);
107+
108+
return true;
109+
}
110+
111+
void OptimalBST::formTree(
112+
std::unique_ptr<Node>& tree,
113+
Matrix<MatrixEntity>& matrix,
114+
const uint32_t begin,
115+
const uint32_t end)
116+
{
117+
if (begin > end)
118+
{
119+
return;
120+
}
121+
else if (begin == end)
122+
{
123+
tree.reset(new Node);
124+
tree->m_idx = begin;
125+
return;
126+
}
127+
tree.reset(new Node);
128+
tree->m_idx = matrix[begin][end].m_rootPos;
129+
formTree(tree->m_left, matrix, begin, (matrix[begin][end].m_rootPos >= 1) ? (matrix[begin][end].m_rootPos - 1) : 0);
130+
formTree(tree->m_right, matrix, matrix[begin][end].m_rootPos + 1, end);
131+
}
132+
133+
void OptimalBST::writeTree(
134+
const std::unique_ptr<Node>& tree,
135+
std::ostream& out,
136+
const std::string& indent)
137+
{
138+
if (!tree)
139+
{
140+
return;
141+
}
142+
writeTree(tree->m_right, out, indent + " ");
143+
out << indent << tree->m_idx << " : " << m_probabilities[tree->m_idx] << std::endl;
144+
writeTree(tree->m_left, out, indent + " ");
145+
}
146+
147+
bool OptimalBST::write(std::ostream& out)
148+
{
149+
out << m_matrix;
150+
151+
uint32_t numProbabilities = static_cast<uint32_t> (m_probabilities.size());
152+
for (uint32_t i = 0; i < numProbabilities; ++i)
153+
{
154+
out << i << " : " << m_probabilities[i] << std::endl;
155+
}
156+
157+
// write tree
158+
writeTree(m_tree, out);
159+
return true;
160+
}

General/OptimalBST.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef OPTIMAL_BST_H
2+
#define OPTIMAL_BST_H
3+
4+
#include <memory>
5+
#include <vector>
6+
#include <fstream>
7+
#include <string>
8+
#include <cstdint>
9+
10+
#include "Matrix.h"
11+
12+
class OptimalBST
13+
{
14+
private:
15+
struct Node
16+
{
17+
std::unique_ptr<Node> m_left;
18+
std::unique_ptr<Node> m_right;
19+
uint32_t m_idx;
20+
Node();
21+
~Node();
22+
};
23+
struct MatrixEntity
24+
{
25+
double m_val;
26+
uint32_t m_rootPos;
27+
MatrixEntity();
28+
MatrixEntity(MatrixEntity&& matrixEntity);
29+
};
30+
friend std::ostream& operator<< (std::ostream& out, MatrixEntity& matrixEntity);
31+
32+
std::vector<double> m_probabilities;
33+
Matrix<MatrixEntity> m_matrix;
34+
std::unique_ptr<Node> m_tree;
35+
36+
private:
37+
void formTree(
38+
std::unique_ptr<Node>& tree,
39+
Matrix<MatrixEntity>& matrix,
40+
const uint32_t begin,
41+
const uint32_t end);
42+
void writeTree(
43+
const std::unique_ptr<Node>& tree,
44+
std::ostream& out,
45+
const std::string& indent = "");
46+
void clear();
47+
48+
public:
49+
OptimalBST();
50+
~OptimalBST();
51+
52+
bool read(std::istream& in);
53+
bool write(std::ostream& out);
54+
bool run(double &c);
55+
};
56+
57+
#endif // OPTIMAL_BST_H

General/SequenceAlignment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ bool SequenceAlignment::run(
5959
Matrix<uint32_t> nwScore(size[0], size[1]);
6060
for (uint64_t i = 0; i < size[0]; ++i)
6161
{
62-
nwScore[i][0] = (i + 1) * m_gapScore;
62+
nwScore[i][0] = static_cast<uint32_t>((i + 1) * m_gapScore);
6363
}
6464
for (uint64_t i = 0; i < size[1]; ++i)
6565
{
66-
nwScore[0][i] = (i + 1) * m_gapScore;
66+
nwScore[0][i] = static_cast<uint32_t>((i + 1) * m_gapScore);
6767
}
6868

6969
uint32_t tmpScore[3];

test/main.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "graphs/GraphMatrix.h"
1515
#include "General/Knapsack.h"
1616
#include "General/SequenceAlignment.h"
17+
#include "General/OptimalBST.h"
1718

1819
void knapsackFunc()
1920
{
@@ -50,9 +51,27 @@ void sequenceAlignmentFunc()
5051
std::cin.get();
5152
}
5253

54+
void optimalBSTFunc()
55+
{
56+
OptimalBST optimalBST;
57+
std::ifstream fin("optimal_bst.in");
58+
if (!optimalBST.read(fin))
59+
{
60+
std::cout << "Cannot read OptimalBST" << std::endl;
61+
exit(2);
62+
}
63+
double val = 0;
64+
optimalBST.run(val);
65+
std::cout << "Value : " << val << std::endl;
66+
optimalBST.write(std::cout);
67+
68+
std::cin.get();
69+
}
70+
71+
5372
int main(int argc, const char * argv[])
5473
{
55-
sequenceAlignmentFunc();
74+
optimalBSTFunc();
5675
#if 0
5776
// insert code here...
5877
std::cout << "Creating graph.\n";

test/optimal_bst.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
7
2+
0.01 0.20 0.14 0.15 0.17 0.25 0.05

0 commit comments

Comments
 (0)