-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicProg_Problems.cpp
More file actions
167 lines (154 loc) · 5.85 KB
/
Copy pathDynamicProg_Problems.cpp
File metadata and controls
167 lines (154 loc) · 5.85 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "DynamicProg_Problems.hh"
#include <iostream>
#include <limits>
/**
* @brief Find Fibonacci value for given integer
* Time complexity = O(2^n)
*
* @param n
* @return long int
*/
long int fibonacci(int n)
{
if (n <= 2)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
/**
* @brief Find Fibonacci value of given integer but optimised using memoisation
* Time complexity = O(n)
*
* @param n
* @param fibMap
* @return long int
*/
long int fibonacci_optimised(int n, std::unordered_map<int, long int> *fibMap)
{
if (fibMap == nullptr)
fibMap = new std::unordered_map<int, long int>();
if (n <= 2)
return 1;
if (fibMap->find(n) != fibMap->end())
return fibMap->at(n);
(*fibMap)[n] = fibonacci_optimised(n - 1, fibMap) + fibonacci_optimised(n - 2, fibMap);
return fibMap->at(n);
}
/**
* @brief Given a grid with rows and columns find the paths to travel from top left to bottom right
* Can move only to the right or bottom
* Time complexity = O(2^(m+n))
*
* @param rows
* @param columns
* @return long int
*/
long int gridTraveller(int rows, int columns)
{
if (rows == 0 || columns == 0)
return 0;
if (rows == 1 || columns == 1)
return 1;
return gridTraveller(rows - 1, columns) + gridTraveller(rows, columns - 1);
}
/**
* @brief Given a grid with rows and columns find the paths to travel from top left to bottom right
* Can move only to the right or bottom
* Time complexity = O(m*n)
*
* @param rows
* @param columns
* @param pathMap
* @return long int
*/
long int gridTraveller_optimised(int rows, int columns, std::unordered_map<std::string, long int> *pathMap)
{
if (pathMap == nullptr)
pathMap = new std::unordered_map<std::string, long int>();
if (rows == 0 || columns == 0)
return 0;
if (rows == 1 || columns == 1)
return 1;
std::string thisPair_1 = std::to_string(rows) + "," + std::to_string(columns);
if (pathMap->find(thisPair_1) != pathMap->end())
return pathMap->at(thisPair_1);
(*pathMap)[thisPair_1] = gridTraveller_optimised(rows - 1, columns, pathMap) + gridTraveller_optimised(rows, columns - 1, pathMap);
return pathMap->at(thisPair_1);
}
/**
* @brief grid traveller algo for grid with obstacles
* find all the paths that can reach end from start
* Time Complexity = O(m*n)
*
* @param input_matrix
* @param start_row
* @param start_column
* @param pathsFromPositionMap
* @return long int (total paths to target from current position)
*/
long int gridTraveller_withObstacles(std::vector<std::vector<char>> &input_matrix, int start_row, int start_column, std::unordered_map<std::string, long int> *pathsFromPositionMap)
{
//initialise memo
if (pathsFromPositionMap == nullptr)
pathsFromPositionMap = new std::unordered_map<std::string, long int>();
//construct key for the current position
std::string thisPos = std::to_string(start_row) + '_' + std::to_string(start_column);
//search the map if we have already calculated for the current pos
if (pathsFromPositionMap->count(thisPos) != 0)
return pathsFromPositionMap->at(thisPos);
//edge cases
if (start_column >= input_matrix[0].size() || start_row >= input_matrix.size())
return 0;
//base cases
if (input_matrix[start_row][start_column] == 'e')
return 1;
if (input_matrix[start_row][start_column] == 'x')
return 0;
//estimate for the current pos
(*pathsFromPositionMap)[thisPos] = gridTraveller_withObstacles(input_matrix, start_row + 1, start_column, pathsFromPositionMap) + gridTraveller_withObstacles(input_matrix, start_row, start_column + 1, pathsFromPositionMap);
//return the value
return pathsFromPositionMap->at(thisPos);
}
/**
* @brief grid traveller algo for grid with obstacles
* find the shortest path between start and end
* Time Complexity = O(m*n)
*
* @param input_matrix
* @param start_row
* @param start_column
* @param positionToLengthMap
* @return long int(shortest path from start to end)
*/
long int gridTraveller_shortestPathlength(std::vector<std::vector<char>> &input_matrix, int start_row, int start_column, std::unordered_map<std::string, long int> *positionToLengthMap)
{
//initialize memo
if (positionToLengthMap == nullptr)
positionToLengthMap = new std::unordered_map<std::string, long int>();
//construct key for the current position
std::string thisPos = std::to_string(start_row) + '_' + std::to_string(start_column);
//check the memo if we have already calculated for this pos
if (positionToLengthMap->count(thisPos) != 0)
return positionToLengthMap->at(thisPos);
//base case out of bounds
if (start_column >= input_matrix[0].size())
return std::numeric_limits<long int>::max();
if (start_row >= input_matrix.size())
return std::numeric_limits<long int>::max();
//base case reached the end
if (input_matrix[start_row][start_column] == 'e')
return 1;
//base case reached obstacle
if (input_matrix[start_row][start_column] == 'x')
return std::numeric_limits<long int>::max();
//calculate the distance of path on either directions and choose the shortest path
long int downPathLength = gridTraveller_shortestPathlength(input_matrix, start_row + 1, start_column, positionToLengthMap);
long int rightPathLength = gridTraveller_shortestPathlength(input_matrix, start_row, start_column + 1, positionToLengthMap);
long int shortestPathLength = downPathLength < rightPathLength ? downPathLength : rightPathLength;
//handlecase when both paths doesnt reach the end
if (shortestPathLength >= 0 && shortestPathLength < std::numeric_limits<long int>::max()){
shortestPathLength +=1;
}
(*positionToLengthMap)[thisPos] = shortestPathLength;
//return
return positionToLengthMap->at(thisPos);
}