Skip to content

Commit b6b01a3

Browse files
Panquesito7github-actions[bot]
andauthored
feat: add dynamic_programming to CMake lists (#1266)
* feat: add `dynamic_programming` to CMake lists * updating DIRECTORY.md * chore: fix minor doc. issues and indentation --------- Co-authored-by: github-actions[bot] <[email protected]>
1 parent 2e8374e commit b6b01a3

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ add_subdirectory(process_scheduling_algorithms)
6565
add_subdirectory(numerical_methods)
6666
add_subdirectory(math)
6767
add_subdirectory(cipher)
68+
add_subdirectory(dynamic_programming)
6869

6970
## Configure Doxygen documentation system
7071
cmake_policy(SET CMP0054 NEW)

dynamic_programming/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. The RELATIVE flag makes it easier to extract an executable's name
3+
# automatically.
4+
5+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
6+
foreach( testsourcefile ${APP_SOURCES} )
7+
string( REPLACE ".c" "" testname ${testsourcefile} ) # File type. Example: `.c`
8+
add_executable( ${testname} ${testsourcefile} )
9+
10+
if(OpenMP_C_FOUND)
11+
target_link_libraries(${testname} OpenMP::OpenMP_C)
12+
endif()
13+
if(MATH_LIBRARY)
14+
target_link_libraries(${testname} ${MATH_LIBRARY})
15+
endif()
16+
install(TARGETS ${testname} DESTINATION "bin/dynamic_programming") # Folder name. Do NOT include `<>`
17+
18+
endforeach( testsourcefile ${APP_SOURCES} )

dynamic_programming/lcs.c

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/**
22
* @file
3-
* @brief [Longest Common Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) algorithm
3+
* @brief [Longest Common
4+
* Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)
5+
* algorithm
46
* @details
57
* From Wikipedia: The longest common subsequence (LCS) problem is the problem
6-
* of finding the longest subsequence common to all sequences in a set of sequences
7-
* (often just two sequences).
8+
* of finding the longest subsequence common to all sequences in a set of
9+
* sequences (often just two sequences).
810
* @author [Kurtz](https://github.com/itskurtz)
911
*/
12+
1013
#include <stdio.h> /* for io operations */
1114
#include <stdlib.h> /* for memory management & exit */
1215
#include <string.h> /* for string manipulation & ooperations */
@@ -15,13 +18,13 @@
1518
enum {LEFT, UP, DIAG};
1619

1720
/**
18-
* @breif Computes LCS between s1 and s2 using a dynamic-programming approach
19-
* @param1 s1 first null-terminated string
20-
* @param2 s2 second null-terminated string
21-
* @param3 l1 length of s1
22-
* @param4 l2 length of s2
23-
* @param5 L matrix of size l1 x l2
24-
* @param6 B matrix of size l1 x l2
21+
* @brief Computes LCS between s1 and s2 using a dynamic-programming approach
22+
* @param s1 first null-terminated string
23+
* @param s2 second null-terminated string
24+
* @param l1 length of s1
25+
* @param l2 length of s2
26+
* @param L matrix of size l1 x l2
27+
* @param B matrix of size l1 x l2
2528
* @returns void
2629
*/
2730
void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
@@ -31,8 +34,8 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
3134

3235
/* loop over the simbols in my sequences
3336
save the directions according to the LCS */
34-
for (i = 1; i <= l1; ++i)
35-
for (j = 1; j <= l2; ++j)
37+
for (i = 1; i <= l1; ++i) {
38+
for (j = 1; j <= l2; ++j) {
3639
if (s1[i-1] == s2[j-1]) {
3740
L[i][j] = 1 + L[i-1][j-1];
3841
B[i][j] = DIAG;
@@ -44,16 +47,18 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
4447
else {
4548
L[i][j] = L[i-1][j];
4649
B[i][j] = UP;
47-
}
50+
}
51+
}
52+
}
4853
}
4954

5055
/**
51-
* @breif Builds the LCS according to B using a traceback approach
52-
* @param1 s1 first null-terminated string
53-
* @param2 l1 length of s1
54-
* @param3 l2 length of s2
55-
* @param4 L matrix of size l1 x l2
56-
* @param5 B matrix of size l1 x l2
56+
* @brief Builds the LCS according to B using a traceback approach
57+
* @param s1 first null-terminated string
58+
* @param l1 length of s1
59+
* @param l2 length of s2
60+
* @param L matrix of size l1 x l2
61+
* @param B matrix of size l1 x l2
5762
* @returns lcs longest common subsequence
5863
*/
5964
char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) {
@@ -76,13 +81,18 @@ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) {
7681
i = i - 1;
7782
j = j - 1;
7883
}
79-
else if (B[i][j] == LEFT)
80-
j = j - 1;
81-
else
82-
i = i - 1;
84+
else if (B[i][j] == LEFT)
85+
{
86+
j = j - 1;
87+
}
88+
else
89+
{
90+
i = i - 1;
91+
}
8392
}
8493
return lcs;
8594
}
95+
8696
/**
8797
* @brief Self-test implementations
8898
* @returns void
@@ -132,9 +142,11 @@ static void test() {
132142
printf("LCS len:%3d\n", L[l1][l2]);
133143
printf("LCS: %s\n", lcs);
134144

135-
free(lcs);
136-
for (j = 0; j <= l1; j++)
137-
free(L[j]), free(B[j]);
145+
free(lcs);
146+
for (j = 0; j <= l1; j++)
147+
{
148+
free(L[j]), free(B[j]);
149+
}
138150
free(L);
139151
free(B);
140152

0 commit comments

Comments
 (0)