Uploaded by Ryan Paul Roy

Module 01 - Fundamentals of Algorithms

advertisement
2020
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
COSC90: DESIGN AND
ANALYSIS OF
ALGORITHM
RYAN E. ROY, LPT
INSTRUCTORS
COURSE DESCRIPTION
INSY55: SYSTEM ANALYSIS AND DESIGN
The student will be given the general idea of what is
involved in developing an organization to achieve its
vision and mission, its philosophies and culture to
ensure its competitiveness in a globalized economy.
Republic of the Philippines
CAVITE STATE
UNIVERSITY
Bacoor City Campus
Development of applications using web, mobile, and
emerging technologies with emphasis on
requirements management, interface design,
usability, testing, deployment, including ethical and
legal considerations.
SHIV, Molino VI, City of Bacoor
MISSION
Cavite State University shall
provide excellent, equitable and
relevant
educational
opportunities in the arts, science
and technology through quality
instruction
and
relevant
research and development
activities.
It shall produce professional,
skilled, and morally upright
individuals
for
global
competitiveness.
VISION
The premier university in historic
Cavite recognized for excellence in
the development of globally
competitive and morally upright
individuals.
Ryan Paul Roy
Instructors, Design and Analysis of
Algorithm
PROGRAM OUTCOMES ADDRESSED
BY THE COURSE. AFTER
COMPLETING THIS COURSE, THE
STUDENTS MUST BE ABLE TO:
1. Attain the vision, mission, goals and
objectives of the university, campus and
department,
2. Deliver a gender fair and gender
sensitive instruction to students aligned
with university goals and objectives,
3. Familiarize with the different algorithmic
strategies, principles, and theories.
4. Describe the complexity, accuracy and
efficiency of every algorithm design
technique discussed; and
5. Utilize the most appropriate algorithm design
technique in solving different computing
problems.
COURSE REQUIREMENTS
1. Quizzes / Activities
2. Project
3. Major Examinations
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
INTENDED LEARNING OUTCOMES – PRE-TEST
After the completion of the unit, students will be able to:
1. Discuss what is algorithms.
2. Learn and describe the types of algorithms.
PRE-TEST
I.
DIRECTION: Fill in the blanks: From the data bank below, fill in the missing word(s)
which best describes the following statements. (20 pts)
.
1. ____________ is a type of structured English that is used to specify an algorithm.
2. ____________ is a step-by-step problem-solving procedure, specifically an established,
recursive computational procedure for solving a problem in a finite number of steps.
3. _____________ is concerned in measuring algorithm’s running time. If a program takes a
lot of time, you can still run it, and just wait longer for the result.
4. _____________ is a way of organizing data which considers not only the items stored but
also the relationship among data.
5. ______________ is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing
6. ______________ is consists of a finite set of elements called nodes and a finite set of
directed lines called branches.
7. ______________ is a graph which has no direction (arrow head) in any of the lines (edges).
8. ______________ is a type of linked list in which the last node’s link points to the first node
of the list.
9. ______________ is an ordered collection of data in which each element contains the
location of the next element.
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
MODULE 01: REVIEW OF ALGORITHMS
After the completion of the unit, students will be able to:
1. Understand the importance of algorithm in computing,
2. Identify the different algorithm design techniques and
3. Define fundamental data structures.
ALGORITHM
According to Bill Bryson, “A computer is a stupid machine with the ability to do incredibly smart
things, while computer programmers are smart people with the ability to do incredibly stupid things.”
They are, in short, a perfect match. A computer is a “dumb” machine (cannot think on its own) but
very good at following (a specific set of) instructions. To give it orders, computer programmers
creates programs (sequences of instructions) for computers to follow to complete the tasks.
A set of finite and detailed step-by-step instructions performed to complete a specific task is what
we call an algorithm.
Did you know that the word algorithm comes from Persian scientist, mathematician, and author Abu
Abdullah Muhammad bin Musa al-Khwarizmi, also called Muhammad ibn-Musa al-Khwarizmi,
Muhammad ibn- Musa al-Khowarizmi, and Mohammad Bin Musa Al- Khawarizmi (flourished early
9th century)? He is sometimes given the title of “grandfather of computer science.” The words
“algorithm” and “algorism” derive ultimately from his name.
Algorithmics, on the other hand, is the design and analysis of computer algorithms.
The design part pertains to the method or mathematical process behind the conception of
an algorithm while the analysis part deals with the determination of the computational
complexity of an algorithm.
Notion of Algorithm
The design of an algorithm starts from defining the problem cautiously by describing the set of
instances it must work on, and then, conceptualizing solutions in terms of discrete steps in a
procedure. The algorithm will often be divided into sections;
● the parts/components (inputs) required in accomplishing the task
● actions/steps/methods (processing) in producing the required outcome (output)
Real-world Examples of Algorithms
Installing a Motherboard
1. Install standoffs on the back plate of the computer case according to the screw holes on
the motherboard.
2. Align the I/O connectors at the back of the motherboard with the openings at the back
of the case.
3. Align the screw holes of the motherboard with the standoffs.
4. Insert the screws through the screw holes and tighten.
Making a Sandwich
1. Get 2 slices of bread.
2. Spread butter on one side of each slice of bread.
3. Put the desired filling on the buttered side of one of the slices of bread.
Put the other slice of bread over the other and cut in half.
Performing Long Division
To perform a long division, the method (algorithm) learned is to iterate through the digits of
the dividend (number to be divided) from left to right. For each digit of the dividend, one
divides, multiplies, subtracts, and brings down the next digit.
CHARACTERISTICS OF ALGORITHMS
Characteristics of Algorithm
For an algorithm to be useful, it must help find a solution to a specific problem. For that to happen,
an algorithm must satisfy five (5) properties.
1. Input Specification: The inputs must be clearly identified. An algorithm can have zero or
more inputs, taken from a specified set of objects. (Zero-input algorithms include the
calculation of irrational and transcendental numbers, and the generation of lists, e.g., of
prime numbers, etc.)
For example, if we are to write an algorithm to calculate the area of a square, our input
will be the length of one side, which, for this example, can be from the set of positive real
numbers.
2. Output Specification: The output, as well as its relationship to the input/s, must be clearly
identified. An algorithm must have at least one.
For example, if the inputs are the height and width of a rectangle, and the desired output is
its area, then the output is the product of the height and the width.
Definiteness: The steps in the algorithm must be precisely defined; the actions to be
carried out must be rigorously and unambiguously specified for each case.
4. Finiteness: The algorithm must always come to an end after a finite number of steps and
using a finite amount of resources. Usually, the time to termination is desired to be
reasonably short. However, it is also common to have time-space tradeoffs, e. g., in
embedded systems where memory space is as much, if not more, a commodity as
performance speed.
Effectiveness: All operations to be performed must be sufficiently basic that they can be
done exactly and in finite time. "Basic" implies that the instructions need to be welldefined and can be performed exactly (but not necessarily "simple").
For example, in the instruction, “X = 1 + 1”, the
Y
Z
value of X can be determined in a finite time. This
is called effectiveness. On the other hand, the
instruction, “X =
1
Y
+
1
Z
1
. . . . . . . . . . n � �”, is still
n
effective. The behavior is exactly known even if the value of X changes in a loop up to N
number of times.
"Can be done exactly" refers to how the instruction is to be performed and its ensuing
result, i.e., the outcome of the instruction will always be "as specified". What it is
imprecise. However, knowing the restrictions, systems and algorithms are designed to
reduce the impact of the imprecision.
REPRESENTATION OF ALGORITHMS
Representation of Algorithm
An algorithm is represented using two (2) common techniques: pseudocode and flowchart.
●
Pseudocode (pronounced as soo-dow-kowd) is a combination of the prefix pseudo and the
word code. “Pseudo” means imitation while “code” refers to instructions written in a
programming language. Pseudocode is a generic way of
describing an algorithm without using any specific programming language-related notations.
●
Flowchart is a method of expressing an algorithm by a collection of connected geometric
shapes containing descriptions of the algorithm’s steps. It is drawn using a set of symbols
linked by connecting lines and directional arrows that indicates the order in which activities
will occur.
With proper design and construction, flowchart communicates the steps in a process very
effectively for once it is drawn, it is easy to visually
follow the process from beginning to end.
Common Pseudocode Notation
There is no strict set of standard notations for pseudocode, but the most widely recognized
are:
o Input - indicates a user will be inputting something.
o Output - indicates that an output will appear on the screen.
o If-Then-Else - a decision (selection) in which a choice is made any instructions that
occur inside a selection or iteration are usually indented.
o For - a counting loop (iteration)
o Repeat-Until - a loop (iteration) that has a condition at the end.
While - a loop (iteration) that has a condition at the beginning.
Example: Use pseudocode and draw a flowchart to specify and outline the algorithm for
ordering food at a fast food restaurant. At the food counter, the person can either order or
not order the following items: a burger, fries, and a drink. After placing the order, the
person then goes to the cashier.
ALGORITHM DESIGN AND ANALYSIS PROCESS
The design and analysis of algorithm process consists of six (6) phases as follows:
1. Understanding the problem completely.
The first thing you need to do before designing an algorithm is to understand completely
the problem given. Understanding the problem completely involves reading the
problem’s description carefully and knowing the given data, needs, and boundaries of
the problem. The following questions below could be your guide in understanding the
problem:
● What input data are available?
● Where is the data?
● What formulas pertain to the problem?
● What rules exist for working with the data?
● What relationships exist among the data values?
● What output information should I produce?
● How do I get the input from the output? Do I have everything that I need?
● How should the output look like … text, a picture, a graph …?
If there are any doubts about the problem, do a few small examples by hand, think about
special cases, and ask questions again if
needed.
2. Deciding on the computational device, data structures, and algorithm design technique.
Choosing Between Exact and Approximate Problem Solving
There are problems that require certain solutions that require choosing problem solving:
● Exact problem solving – These are procedures that will have quantifiable and
definite end result.
● Approximate problem solving – These are procedures that will have near result
to the actual value because of instances that cannot be solved exactly.
In choosing from these two (2) problem solving will depend on the given and the things
needed in a specific problem.
Why would one opt for an approximation algorithm?
●
There are important problems that simply cannot be solved exactly for most of
their instances.
Available algorithms for solving a problem exactly can be unacceptably slow
because of the problem’s intrinsic complexity, or require excessive amounts of
resources.
Algorithm Design Techniques (or "strategy" or "paradigm") is a general approach by
which a variety of problems from different areas of computing can be solved
algorithmically (e.g., brute force, divide and conquer, dynamic programming, greedy
technique and so on.). This implies that the solution may differ in one problem because
there would be different strategies that can be implemented in one problem.
Different Areas of Computing:
1. Brute force
It is a straightforward approach to solve a problem based on the problem’s
statement and definitions of the concepts involved. It is considered as one of the
easiest approach to apply and is useful for
solving small–size instances of a problem.
2. Divide and conquer
Essence of Divide and Conquer
▪ Divide/Break a problem into several smaller subproblem. Normally, the
subproblem are similar to the original problem.
▪ Conquer the subproblems by recursively solving the subproblems. If they
are small enough, solve the subproblems by brute force.
Combine the solutions to get an answer to the subproblems and, finally, a solution
to the original problem.
3. Designing an Algorithm and Data Structure
Though algorithms and data structures are independent, they are combined together to
develop a program. Hence, the choice of proper data structures is required for better
performance. Data structure is a way of organizing the data in a computer. If the developer
chooses bad data structure, the system does not perform well.
Three (3) Common Data Structures Used in Algorithm Design
Linear Data Structure – It is represented by a list in which each element has a unique
successor. The two (2) most common linear data structures are the following:
Array is a data structure that may contain a fixed number of components all of the
same data type. It can be use in the following use cases.
●
●
●
●
Need access the elements using the index.
Know the size of the array before defining the memory.
Speed when iterating through all the elements in the sequence.
Array takes less memory compare than linked list.
Individual variables in the array are called “elements of the array” that can be accessed
through an index. An index is also called a “subscript of the array” that references the
location of the variable relative to the first element of the array. In most cases, the
index is an integer from 0 to 𝑛𝑛-1, where 𝑛𝑛 is the size of the array.
Arrays are used for implementing a variety of other data structures. One of its useful
applications is the manipulation of string. Strings are important for processing textual
data, defining computer languages and compiling programs, and studying abstract
computational models. Even though array is a very useful data structure, it also has the
following limitations:
▪
▪
The size has to be known at compilation time.
The data in the array are separated in computer memory by the same
distance; this means that inserting an item inside the array requires
shifting other data.
Linked list is a data structure that allows programmers to create a new place
automatically to store data whenever necessary. The element in a linked list called
“node” can be linked to another by using pointers. Each node of the linked list consists
of the following parts:
▪
▪
Data holds useful information and the data to be processed.
Link (pointer) is used to chain the data to be processed. It contains a
pointer that identifies the next element in the list.
Nodes in a linked list are called “self-referential structures” because each instance of
the structure contains a pointer to another instance of the same structure type.
Three (3) Types of Linked List
− Singly linked list is where each node, except the last node, contains
a single pointer to the next element. Simple operations of this linked
list are insertion, deletion, traversal, and search of a node in a given
list. It can be use in the following use cases.
● When the developer needs constant time for insertion and
deletion.
● When the data dynamically grows.
● Do not access random elements from the linked list.
● Insert the element in any position of the list.
− Doubly linked list is where each node, except for the first and
last, contains pointers to both its successor and its predecessor.
−
It can be use in the following uses cases:
● Easier to delete the node from doubly linked list.
● Can be iterated in reverse order without recursion
implementation.
● Insert or remove from double-linked lists faster.
Circularly linked list is where the last node’s link points to the
first node of the list. It can be use in the following use cases:
● Develop the buffer memory.
●
●
●
●
Represent a deck of cards in a game.
Browser cache which allows to hit the BACK button.
Implement Most Recently Used (MRU) list.
Undo functionality in Photoshop or Word.
Graph Data Structure – It is a collection of nodes (vertices) and line segments (lines). The
graph is an abstract data type that is meant to implement the graph and directed graph
concepts from mathematics and it can be use in the following use cases.
● Networks have many uses in the practical side of graph theory.
● Finding the shortest path between the cities.
● Solve maze game.
● Find the optimized route between the cities.
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
INTENDED LEARNING OUTCOMES – POST-TEST
After the completion of the unit, students will be able to:
1. Discuss what is algorithms; and
2. Learn and describe the types of algorithms.
POST-TEST
I.
DIRECTION: Fill in the blanks: From the data bank below, fill in the missing word(s)
which best describes the following statements. (20 pts)
.
1. ____________ is a type of structured English that is used to specify an algorithm.
2. ____________ is a step-by-step problem-solving procedure, specifically an established,
recursive computational procedure for solving a problem in a finite number of steps.
3. _____________ is concerned in measuring algorithm’s running time. If a program takes a
lot of time, you can still run it, and just wait longer for the result.
4. _____________ is a way of organizing data which considers not only the items stored but
also the relationship among data.
5. ______________ is a general approach to solving problems algorithmically that is
applicable to a variety of problems from different areas of computing
6. ______________ is consists of a finite set of elements called nodes and a finite set of
directed lines called branches.
7. ______________ is a graph which has no direction (arrow head) in any of the lines (edges).
8. ______________ is a type of linked list in which the last node’s link points to the first node
of the list.
9. ______________ is an ordered collection of data in which each element contains the
location of the next element.
Republic of the Philippines
CAVITE STATE UNIVERSITY
Bacoor City Campus
SHIV, Molino VI, City of Bacoor
KEY TO CORRECTION
1. Pseudocode
2. Algorithm
3. Time efficiency
4. Data Structure
5. Algorithm design technique
6. Tree data structure
7. Undirected graph
8. Circularly linked list
9. Linked list
Download