Module 4 Algorithmic Thinking With Python (1)
Module 4 Algorithmic Thinking With Python (1)
1.0 Introduction
2.0 Intended Learning Outcome
3.0 Main Content
3.1 Brute-force Approach
3.2 Divide-and-conquer Approach
3.2.1 Example: The Merge Sort Algorithm
3.2.2 Advantages of Divide and Conquer Approach
3.2.3 Disadvantages of Divide and Conquer Approach
3.3 Dynamic Programming Approach
3.3.1 Example: Fibonacci series
3.3.2 Recursion vs Dynamic Programming
3.4 Greedy Algorithm Approach
3.4.1 Characteristics of the Greedy Algorithm
3.4.2 Motivations for Greedy Approach
3.4.3 Greedy Algorithms vs Dynamic Programming
3.5 Randomized Approach
4.0 Conclusion
5.0 Summary
6.0 Self-Assessment Exercise
7.0 References/Further Reading
1.0 INTRODUCTION
Example:
Imagine a small padlock with 4 digits, each from 0-9. You forgot your
combination, but you don't want to buy another padlock. Since you can't
remember any of the digits, you have to use a brute force method to
open the lock. So you set all the numbers back to 0 and try them one by
one: 0001, 0002, 0003, and so on until it opens. In the worst case
scenario, it would take 104, or 10,000 tries to find your combination.
25
CIT 108 PROBLEM SOLVING STRATEGIES
Divide
“Divide” is the first step of the divide and conquer strategy. In this step
the problem is divided into smaller sub-problems until it is small enough
to be solved. At this step, sub-problems become smaller but still
represent some part of the actual problem. As stated above, recursion is
used to implement the divide and conquer algorithm. A recursive
algorithm calls itself with smaller or simpler input values, known as the
recursive case. So, when the divide step is implemented, the recursive
case is determined which will divide the problem into smaller sub-
problems.
Combine
The merge sort algorithm closely follows the divide and conquer
paradigm. In the merge sort algorithm, we divide the n-element
sequence to be sorted into two subsequences of 𝑛 = 2 elements each.
Next, we sort the two subsequences recursively using merge sort.
Finally, we combine the two sorted subsequences to produce the sorted
answer.
26
CIT 108 MODULE 1
Again, divide each subpart recursively into two halves until you get
individual elements.
The first, and probably the most recognizable benefit of the divide and
conquer paradigm is the fact that it allows us to solve difficult problems.
Being given a difficult problem can often be discouraging if there is no
27
CIT 108 PROBLEM SOLVING STRATEGIES
idea how to go about solving it. However, with the divide and conquer
method, it reduces the degree of difficulty since it divides the
problem into easily solvable sub-problems.
In the divide and conquer strategy problems are divided into sub-
problems that can be executed independently from each other. Thus,
making this strategy suited for parallel execution.
One of the most common issues with this sort of algorithm is the fact
that the recursion is slow, which in some cases outweighs any
advantages of this divide and conquer process. Another concern with it
is the fact that sometimes it can become more complicated than a
basic iterative approach, especially in cases with a large n. In other
words, if someone wanted to add large numbers together, if they just
create a simple loop to add them together, it would turn out to be a much
simpler approach than it would be to divide the numbers up into two
groups, add these groups recursively, and then add the sums of the two
groups together.
Let's find the Fibonacci sequence up to the 5th term. A Fibonacci series
is the sequence of numbers in which each number is the sum of the two
preceding ones. For example, 0,1,1, 2, 3. Here, each number is the sum
of the two preceding numbers.
Algorithm
Hence, we have the sequence 0,1,1, 2, 3. Here, we have used the results
of the previous steps as shown below. This is called a dynamic
programming approach.
29
CIT 108 PROBLEM SOLVING STRATEGIES
F(0) = 0
F(1) = 1
F(2) = F(1) + F(0)
F(3) = F(2) + F(1)
F(4) = F(3) + F(2)
In a greedy algorithm, at each decision point the choice that has the
smallest immediate (i.e., local) cost is selected, without attempting to
look ahead to determine if this choice is part of our optimal solution to
the problem as a whole (i.e., a global solution). By locally optimal, we
mean a choice that is optimal with respect to some small portion of the
total information available about a problem.
The most appealing aspect of greedy algorithm is that they are simple
and efficient – typically very little effort is required to compute each
local decision. However, for general optimization problems, it is
obvious that this strategy will not always produce globally optimal
solutions. Nevertheless, there are certain optimization problems for
which a greedy strategy is, in fact, guaranteed to yield a globally optimal
solution.
30
CIT 108 MODULE 1
This approach is dependent not only on the input data, but also on the
values provided by a random number generator. If some portion of an
algorithm involves choosing between a number of alternatives, and it is
31
CIT 108 PROBLEM SOLVING STRATEGIES
4.0 CONCLUSION
5.0 SUMMARY
32
CIT 108 MODULE 1
Chevalier, M., Giang, C., Piatti, A., & Mondada, F. (2020). Fostering
computational thinking through educational robotics: a model for
creative computational problem solving. International Journal of
STEM Education, 7(1), 1-18.
Doleck, T., Bazelais, P., Lemay, D. J., Saxena, A., & Basnet, R. B.
(2017). Algorithmic thinking, cooperativity, creativity, critical
thinking, and problem solving: exploring the relationship between
computational thinking skills and academic performance. Journal
of Computers in Education, 4(4), 355-369.
de Ruffieu, F. L. (2016). Divide and Conquer Book 1: Fundamental
Dressage Techniques: Xenophon Press LLC.
Priemer, B., Eilerts, K., Filler, A., Pinkwart, N., Rösken-Winter, B.,
Tiemann, R., & Zu Belzen, A. U. (2020). A framework to foster
problem-solving in STEM and computing education. Research in
Science & Technological Education, 38(1), 105-130.