COS 102 Module 2
COS 102 Module 2
Figure 1-1: Graphical representation of nodes (circles) and moves (lines) of Tower of Hanoi.
The abstraction above represents a computer. It shows the names of the components and how they
interact with each other but hides the complexity of each type of component.
Types of Abstraction
i. Representational Abstraction
Abstraction appears in many forms within computing, both in terms of techniques used to approach
problem-solving, and in the computational tools employed to develop solutions. The maps of many
metropolitan public transport systems worldwide is a classic example of a representational abstraction.
Many specific details about the lines are removed because they are not necessary for the purpose of the
map, which is to help plan a journey. What remains when unnecessary detail has been removed is a
representational abstraction, i.e. a simpler version directed at solving a particular problem.
ii Abstraction by Generalisation
When you group things in terms of a set of common characteristics, you are generalising. This is a
fundamental technique used in object-oriented programming (although it is not exclusive to OOP) when
you identify that some objects are 'kinds of' more generic objects. For example, you might say that a
cocker spaniel is a kind of dog. Dogs have common sets of characteristics, such as having four legs, a tail,
and ears. However, so do cats; a biologist may tell you that both animals belong to the order Carnivora (a
group that includes many other types of animal including bears, skunks, and badgers). Generalising in this
way allows code to be developed and shared between objects.
You can also apply the technique of generalisation to the problem itself. It is often helpful to be able to identify
a problem as an example of a more general set of problems. Sometimes, this will help you to understand
quickly that the problem is non-computable, or that it is intractable. Otherwise, if the problem can be solved,
you can benefit from a solution that already exists.
iii Procedural Abstraction
This type represents a computational method. One of the skills of a computer scientist is the ability to design a
well-abstracted procedure that is generalised as far as possible. For example, consider the problem of
calculating the surface area of a chopping board. You write the following subroutine:
PROCEDURE calculate_chopping_board_area()
side1_length = INPUT("Enter length of side 1: ")
side2_length = INPUT("Enter length of side 2: ")
side1_length = STR(side1_length)
side2_length = STR(side2_length)
area = side1_length * side2_length
PRINT(area)
ENDPROCEDURE
This is an example of a subroutine that is too specific. Firstly, it is bound to a specific (command line) user
interface. If you abstract away this detail, the subroutine will be independent of the user interface, and will
therefore be more general. Here is a more general version of the same subroutine:
FUNCTION calculate_chopping_board_area(side1_length, side2_length)
area = side1_length * side2_length
RETURN area
ENDFUNCTION
iv Functional Abstraction
In functional abstraction, the implementation detail of the computational method is hidden. You can think of a
function as a black box. The function will receive an input (or set of inputs), process the input(s), and return
the output. How the transformation is achieved is hidden from the user?
Most languages will provide a set of built-in functions that can be used by the programmer. In the previous
example of a subroutine that calculates the area of a regular polygon, a maths library is used to provide useful
mathematical functions. In the example, functions were used to provide the value of , to calculate the square
root of a number, and to calculate the tangent of a number.
In Python, for example, the built-in function to display a value to the console or command line interface is
print. If you code in Python, you will be familiar with typing a command such as print ("Hello World"). You will
know that the name of the function is print, and it must be followed by a value (to print) enclosed in
parentheses.
v Data Abstraction
Some data types, such as unsigned integers, are conceptually simple; others are more complex. Data
abstraction is a technique that allows you to separate the way a compound data object is used, from the
details of how it is constructed.
Studying data structures, will make you aware of the concept of a stack as an example of an abstract data
type (ADT). A stack is a last in, first out (LIFO) data structure that supports three standard operations: push
(add an item to the stack), pop (remove an item from a stack), and peek (look at the item at the top of the
stack). The abstract concept of a stack, and its operations, can be understood without any consideration of
how it is implemented. Sometimes, you will see a stack drawn as an upright container with a single opening at
the top. This abstraction helps you to understand the LIFO nature of the structure.
2. ALGORITHM
An algorithm is an effective step-by-step procedure for solving a problem in a finite number of steps. In other
words, it is a finite set of well-defined instructions or step-by-step description of the procedure written in
human readable language for solving a given problem. An algorithm itself is division of a problem into small
steps which are ordered in sequence and easily understandable. Algorithms are very important to the way
computers process information, because a computer program is basically an algorithm that tells computer
what specific tasks to perform in what specific order to accomplish a specific task. The same problem can be
solved with different methods. So, for solving the same problem, different algorithms can be designed. In
these algorithms, number of steps, time and efforts may vary more or less.
For example, we might need to sort a sequence of numbers into non-decreasing order. This problem arises
frequently in practice and provides fertile ground for introducing many standard design techniques and
analysis tools. Here is how we formally define the sorting problem: Input: A sequence of numbers 〈1, 2, ⋯,〉.
In computer science we give a special name to the sub-algorithms. They are sometimes called modules,
functions or procedures. In fact, it is not a good idea to simply number all the sub-algorithms but instead to
give them meaningful names. As standard convention, when naming a function or procedure, you should use
letters, numbers and underscore (i.e., _) characters but not any spaces or punctuation. Also, the first
character in the name should be a lower case letter. If multiple words are used as the name, each word
except the first should be capitalized. Lastly, we often use parentheses (i.e., ()) after the function or
procedure name to identify it as a sub-algorithm. You should choose meaningful names that are not too long
as will be evident in our discussion. When a sub-algorithm comes back with some kind of object or value such
as numerical result, we call the sub-algorithm a function. If the sub-algorithm does not return any particular
value, it is instead known as a procedure.
TYPES OF ALGORITHM
Algorithms can be generally divided into six fundamental types based on their function.
1. Recursive Algorithm
It refers to a way to solve problems by repeatedly breaking down the problem into sub-problems of the
same kind. The classic example of using a recursive algorithm to solve problems is the Tower of Hanoi.
2. Divide and Conquer Algorithm
Traditionally, the divide and conquer algorithm consists of two parts: 1. breaking down a problem into
some smaller independent sub-problems of the same type; 2. finding the final solution of the original
issues after solving these more minor problems separately. The key points of the divide and conquer
algorithm are:
If you can find the repeated sub-problems and the loop substructure of the original problem, you may
quickly turn the original problem into a small, simple issue.
Try to break down the whole solution into various steps (different steps need different solutions) to
make the process easier.
Are sub-problems easy to solve? If not, the original problem may cost lots of time.
3. Dynamic Programming Algorithm
Developed by Richard Bellman in the 1950s, the dynamic programming algorithm is generally used for
optimization problems. In this type of algorithm, past results are collected for future use. Like the divide
and conquer algorithm, a dynamic programming algorithm simplifies a complex problem by breaking it
down into some simple sub-problems. However, the most significant difference between them is that the
latter requires overlapping sub-problems, while the former doesn’t need to.
4. Greedy Algorithm
This is another way of solving optimization problems – greedy algorithm. It refers to always finding the
best solution in every step instead of considering the overall optimality. That is to say, what he has done is
just at a local optimum. Due to the limitations of the greedy algorithm, it has to be noted that the key to
choosing a greedy algorithm is whether to consider any consequences in the future.
5. Brute Force Algorithm
The brute force algorithm is a simple and straightforward solution to the problem, generally based on the
description of the problem and the definition of the concept involved. You can also use "just do it!" to
describe the strategy of brute force. In short, a brute force algorithm is considered as one of the simplest
algorithms, which iterates all possibilities and ends up with a satisfactory solution.
6. Backtracking Algorithm
Based on a depth-first recursive search, the backtracking algorithm focusing on finding the solution to the
problem during the enumeration-like searching process. When it cannot satisfy the condition, it will return
"backtracking" and tries another path. It is suitable for solving large and complicated problems, which
gains the reputation of the "general solution method". One of the most famous backtracking algorithm
example it the eight queens puzzle.
Advantages of Algorithm
Designing an algorithm has following advantages:
1. Effective Communication: Since algorithm is written in English like language, it is simple to
understand step-by-step solution of the problems.
2. Easy Debugging: Well-designed algorithm makes debugging easy so that we can identify logical error
in the program.
3. Easy and Efficient Coding: An algorithm acts as a blueprint of a program and helps during program
development.
4. Independent of Programming Language: An algorithm is independent of programming languages and
can be easily coded using any high level language.
Disadvantages of Algorithm
An algorithm has following disadvantages:
1. Developing algorithm for complex problems would be time consuming and difficult to understand.
2. Understanding complex logic through algorithms can be very difficult.
Representation of Algorithms
Using algorithmic thinking skills, software designers or programmers analyse the problem and identify the
logical steps that need to be followed to reach a solution. Once the steps are identified, the need is to write
down these steps along with the required input and desired output. There are two common methods of
representing an algorithm — flowchart and pseudocode.
Either of the methods can be used to represent an algorithm while keeping in mind the following:
It showcases the logic of the problem solution, excluding any implementation details
It clearly reveals the flow of control during execution of the program
3. MEANS-ENDS ANALYSIS
This was recognised as a general problem solving heuristic which involves a search for operations that will
reduce the difference between present state of knowledge and the goal state. In particular, means-end
analysis involves the following steps:
Set up a goal
Look for a difference between the current problem state and the goal state.
Look for a method to decrease or eliminate the difference between the two stages.
Set as a sub goal which is the application of that method.
If necessary apply means- ends analysis to apply to the sub goal.
Thus, the main heuristic used in GPS involves setting up goals and sub goals. In fact, this strategy can be
expressed very precisely as a production system, that is, as a set of if – then pairs stored in the computer’s
memory as production.
An illustrative geometric problem:
The problem is that ABCD is a rectangle; prove that AD and BC are same length
4. BRAINSTORMING
Brainstorming is a method used by individuals or groups to generate multiple inventive ideas or
solutions for a particular issue or subject. The whole concept is based on a cooperative effort and an
approach that prompts a person and a team member to think freely, articulating any thoughts you have
without restriction.
This process aims to build a creative and encouraging atmosphere, where everyone within a company can
expand each another's ideas to collaboratively come to creative solutions.
Brainstorming is a method used by individuals or groups to generate innovative ideas or solutions for a
specific issue. It encourages free thinking and unrestricted sharing of thoughts, promoting a creative and
collaborative atmosphere. Yet, at the end of the day, brainstorming is only part of problem-solving; they
aren’t the same thing and here are three reasons why:
1. Brainstorm focuses on generating ideas, while problem-solving involves analyzing and implementing
solutions.
2. Brainstorming is informal and spontaneous, encouraging creativity and diverse opinions, whereas
problem-solving follows a systematic and structured method whereby the outcome is a solution.
3. Brainstorming produces multiple ideas, while problem-solving simply seeks one practical and effective
solution.
Brainstorming techniques
Team Relay
Team Relay involves working together in small groups to share ideas. It’s just like a relay race; instead of
passing on the baton, you pass and build on ideas. Everyone takes turns to put their two cents in, and the
ideas continue to evolve within the group. This helps you to work with your team members and come up
with lots of creative ideas. Team Relay is best for teams of about 12 participants, and this method helps
you find new ideas by bouncing off what the team says. You can identify and initiate new projects by
working as a cohesive team, capitalizing on every idea collectively and elevating your thinking power to
new heights. If you want to kick things off with the Team Relay method.
Reverse Brainstorming
Reverse brainstorming is a technique where, instead of generating ideas to solve a problem, you focus
on creating ideas that will make the problem worse or that will cause the problem. In other words, you’ll
turn the problem upside down to define the worst-case scenario. So, how does this help solve the
problem?
This excellent brainstorming tool can be used with a team at the start of a project or when stucked at a
crossroads with a problem. It helps you think outside the box and unleash your imagination.
Focus group
A focus group is a small group of people (about 6 to 12 participants) who have been selected to meet up
and talk or share ideas about a specific problem (strategic development, marketing positioning, etc.).
This selected team works together to generate thoughts and suggestions to help solve the problem or
develop new ideas. You can either conduct a qualitative survey on a concept, product, or service.
Crazy 8
Crazy 8 is a fast-paced, dynamic technique. This is a unique strategy that allows brainstorming among a
team with a key element… speed! While some brainstorming techniques could simply waste time and drum
up an excessive volume of sub-par or irrelevant ideas, this method is all about eyes on the prize, full steam
ahead.
1-2-4-All
The 1-2-4-All method is like regular brainstorming but in several steps:
First, everyone thinks on their own;
Then, they share their ideas in bigger groups (in pairs, then in groups of 4, and then with
everyone).
The Rules of Brainstorming The following criteria are essential to the idea-generation phase of a
brainstorming session.
1. There is no criticism, evaluation, judgment, or defense of ideas during the brainstorming session.
The purpose of brainstorming is to generate as many ideas related to the topic as possible in the
time allowed. Evaluation, judgment, and selection of ideas are the purposes of subsequent
sessions.
2. Free wheeling and free association is encouraged. Group members are asked to voice any solutions
they can think of, no matter how outrageous or impractical they seem. There is no limit on “wild”
or “far-fetched” ideas. Every idea is to be expressed. It is easier to tone down an idea and to select
out later than it is to think up new and creative possibilities.
3. Quantity is more desired than quality. Group members are encouraged to contribute as many ideas
as they think of. The greater the number of ideas generated, the more likely it is that there will be
several useful ideas.
4. Building on ideas is encouraged. Combining, adding to, and “piggybacking” on ideas is part of the
creative process. Members can suggest improvements, variations, or combinations of previous
ideas.
5. HYPOTHESIS TESTING
Hypothesis testing or significance testing is a method for testing a claim or hypothesis about a parameter
in a population, using data measured in a sample. In this method, we test some hypothesis by determining
the likelihood that a sample statistic could have been selected, if the hypothesis regarding the population
parameter were true.
Hypothesis testing is a powerful tool for analytical thinking and problem-solving. It helps you to identify
and test possible explanations for a problem, using evidence and logic. Hypothesis testing to determine the
most likely cause of a problem can be carried out following these steps:
1 Define the problem
The first step is to clearly define the problem to be solved, its scope and impact. One should also gather
relevant data and information about the problem, such as its frequency, severity, duration, and location. A
well-defined problem will help to narrow down the potential causes and focus on hypothesis testing.
The first step in hypothesis testing is to define a problem in terms of the claim being made about a
population parameter. The claim is stated as the null hypothesis (H0), which is a specific value or a
particular mathematical statement about a population parameter, stated as if it were true, even though it
may not be.
2 Generate hypotheses
The next step is to generate hypotheses, or tentative assumptions, about the possible causes of the
problem. One can use different techniques to generate hypotheses, such as brainstorming, root cause
analysis, fishbone diagrams, or the 5 whys. As many hypotheses as possible should be generated, without
judging or rejecting them at this stage.
3 Prioritize hypotheses
The third step is to prioritize the hypotheses, based on their plausibility and testability, one should rank the
hypotheses according to how likely they are to explain the problem, and how easy they are to test with
data and evidence. Matrix or a scoring system could be use to prioritize the hypotheses, and eliminate the
ones that are too improbable or impractical.
4 Test hypotheses
The fourth step is to test the hypotheses, using data and evidence to support or reject them. One should
design and conduct experiments or analyses that can measure the effect of each hypothesis on the
problem. One should also define the criteria for accepting or rejecting a hypothesis, such as a significance
level, a confidence interval, or a p-value.
5 Evaluate results
The fifth step is to evaluate the results of the hypothesis testing, and draw conclusions about the most
likely cause of the problem. You should compare the results of the different hypotheses, and see which
one has the strongest or most consistent evidence. You should also consider the limitations and
assumptions of your hypothesis testing, and check for any errors or biases.
6 Communicate findings
The final step is to communicate your findings and recommendations, based on the hypothesis testing.
You should summarize the problem, the hypotheses, the methods, the results, and the conclusions in a
clear and concise way. You should also explain the implications and implications of your findings, and
suggest actions or solutions to address the problem
6. LITERAL THINKING
Literal thinking is the mental process of generating ideas and solving problems by looking at a situation or
problem from a unique perspective. It is the ability to think creatively or ”outside the box”
Literal thinking involves breaking away from traditional modes of thinking and discarding established
patterns and preconceived notions. This method of problems solving provides a deliberate, systematic
process that resulting in innovative thinking. By using these unconventional thinking techniques, it enables
one to find creative solution that may otherwise not be considered.
Techniques of literal thinking:
1. Alternatives: this is about using concepts as a breeding ground for new ideas. Concepts are general
theories or way of doing things. By thing of a variety of way to implement a concept is one way to
generate ideas. One can then further assessed each specific idea to generate additional concepts.
Establishing a new concept creates a whole new way of generating more ideas.
2. Focus: this is about learning when and how to change focus to improve creative efforts. Focusing on
areas that other people have not bothered to think about.
3. Challenge: is about breaking free from the limits of traditional thinking and the accepted ways of
doing things. It is based on the assumption that there may be a different and better ways to do
something even if there is no apparent problem with the current way.
4. Random Entry: is about using unconnected input to open up new lines of thinking. This technique
draws ones mind to find connections between seemingly unrelated things. With this technique one can
use a randomly chosen word, picture, sound or other stimulus to open new lines of thinking.
5. Provocation and Movement: is about provoking thoughts and using them to build new ideas. It is a
process that enables thinking outside the box in order to get a compelling list of innovative ideas to
consider.
6. Harvesting: it involves selecting specific ideas that seem practical and have the most value then
reshaping them into practical solutions. It is about turning starter ideas into workable ideas. The
technique is done towards the end of a thinking session in order to select ideas that may prove to be
valuable in the current situation or in the future. It helps in identifying ideas that could be
implemented right away as well as those that may need more work.
7. Treatment of ideas: involves shaping and strengthening ideas so the best fit a given situation. The
treatment techniques are bet for working with starter ideas to make them more specific and practical
for a given solution.
7. MORPHOLOGICAL ANALYSIS
Morphological analysis is breaking a seemingly complex thing into different fundamental units, eliminating
inconsistencies, and then creating a link between the remaining units to solve a problem.
Morphological analysis was invented by Fritz Zwicky, a Swedish astrophysicist who used morphological
analysis to solve a lot of incoherent aerospace and astronomical problems. He used it to develop jet and
rocket propulsion systems in classifying astrophysical objects and other complex aspects like the legal
aspect of colonizing space.
If you are someone who has dismantled toys in childhood, this method is for you. You will get a chance to
dismantle products/processes without physically dismantling them. And the fun is going to be almost the
same.
General Morphological Analysis over Mathematical Modeling
In crooked problems with multiple governing factors, a mathematical model that breaks the problem into
different components and drops trivial ones fails. The trivial components considered insignificant during
the analysis may become significant, and the model may collapse.
In contrast, General Morphological Analysis (GMA) is a sound method to deal with such non-quantifiable
problems. Every problem component gets considered and thoroughly investigated without putting it in the
bracket of insignificant. In essence, GMA turns a mess into structured problems.
For example, in our problem, we locked the paperback and asked Zwicky Box to help us find possible
combinations. The blue cells are corresponding outputs. You can even have multiple inputs. For example,
in Zwicky Box (2), we locked Paperback, white.
8. RESEARCH
Research is a process to discover new knowledge to find answers to a question. The word research has
two parts re (again) and search (find) which denote that we are taking up an activity to look into an aspect
once again or we want to look for some new information about something.
Clifford Woody states that research comprises defining and redefining problems, formulation of hypothesis;
collection, organizing and evaluation of data; and reaching conclusions. Here it is emphasized that
research has to be systematic and logical to arrive at expected outcome
It is systematic observation of processes to find better ways to do things and to reduce the effort being
put in to achieve an objective and identifying the validity of the targets.
Research is a powerful tool for problem solving, whether you are facing a personal, professional, or social
challenge.
Following are some standard algorithms that are of the Divide and Conquer algorithms variety.
i. Binary Search is a searching algorithm. In each step, the algorithm compares the input element (x)
with the value of the middle element in array. If the values match, return the index of middle.
Otherwise, if x is less than the middle element, then the algorithm recurs to the left side of the
middle element, else it recurs to the right side of the middle element.
ii. Quicksort is a sorting algorithm. The algorithm picks a pivot element, rearranges the array elements
in such a way that all elements smaller than the picked pivot element move to the left side of the
pivot, and all greater elements move to the right side. Finally, the algorithm recursively sorts the sub-
arrays on left and right of pivot element.
iii. Merge Sort is also a sorting algorithm. The algorithm divides the array into two halves, recursively
sorts them, and finally merges the two sorted halves. The time complexity of this algorithm
is O(nLogn), be it best case, average case or worst case. It's time complexity can be easily
understood from the recurrence equates to: T(n) = 2T(n/2) + n.
iv. Closest Pair of Points The problem is to find the closest pair of points in a set of points in x-y plane.
The problem can be solved in O(n^2) time by calculating distances of every pair of points and
comparing the distances to find the minimum. The Divide and Conquer algorithm solves the problem
in O(nLogn) time.
v. Strassen’s Algorithm is an efficient algorithm to multiply two matrices. A simple method to multiply
two matrices need 3 nested loops and is O(n^3). Strassen’s algorithm multiplies two matrices
in O(n^2.8974) time.
vi. Cooley–Tukey Fast Fourier Transform (FFT) algorithm is the most common algorithm for FFT. It is a
divide and conquer algorithm which works in O(nlogn) time.
vii. The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the
quadratic "grade school" algorithm. It reduces the multiplication of two n-digit numbers to at most to
n^1.585 (which is approximation of log of 3 in base 2) single digit products. It is therefore faster
than the classical algorithm, which requires n^2 single-digit products.