Algorithm & Data Structure I
Algorithm & Data Structure I
1
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Overview
▪ Characteristics of an Algorithm
▪ Algorithm Design and Analysis Process
▪ Important Problem Types
▪ Reasons Using Algorithm
▪ Algorithm Complexity
▪ Definitions
▪ Need for Algorithms and Data Structure
▪ General Approaches in Algorithm Design
➢ Randomized Algorithms
➢ Divide-and-conquer Algorithms
➢ Dynamic-programming solutions
➢ Greedy Algorithms
➢ Approximation Algorithms
▪ Analysis of Algorithms
▪ Expressing Algorithms
▪ Basic Terminology
2
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithms
▪ An algorithm: is a sequence of unambiguous
instructions for solving a problem, i.e., for obtaining a
required output for any legitimate input in a finite
amount of time.
Problem
Algorithm
3
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Characteristics of an Algorithm
▪ Unambiguous: Algorithm should be clear and unambiguous. Each
of its steps and their inputs/outputs should be clear and must lead
to only one meaning.
▪ Input: An algorithm should have 0 or more well-defined inputs.
▪ Output: An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
▪ Finiteness: Algorithms must terminate after a finite number of
steps.
▪ Independent: An algorithm should have step-by-step directions,
which should be independent of any programming code.
4
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithms
Algorithm Design and Analysis Process:
5
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithms
Algorithm Design and Analysis Process:
▪ Understanding the problem.
▪ Ascertain the capabilities of a computational device.
▪ Choose between exact and approximate problem solving.
▪ Choose a design technique and specify your algorithm.
▪ Prove the correctness of your algorithm.
▪ Analyse your algorithm.
▪ Code your algorithm.
6
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithms
Important Problem Types:
▪ The sorting problem asks us to rearrange the items of a given list.
e.g. in ascending or descending order.
▪ The searching problem deals with finding a given value, called a
search key, in a given set or a multi-set.
▪ Insert, Update or Delete : Algorithms to insert/ update or delete
item in a data structure.
▪ String Processing.
▪ Graph problems.
▪ Geometric algorithms.
▪ Numerical problems.
7
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
8
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Algorithm Complexity
▪ Suppose X is an algorithm and n is the size of input data,
the time and space used by the algorithm X are the two
main factors, which decide the efficiency of X.
▪ Time Complexity:
✓ Time is measured by counting the number of key operations
such as comparisons in the sorting algorithm.
▪ Space Complexity:
✓ Space is measured by counting the maximum memory space
required by the algorithm.
9
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Definitions
▪ An algorithm:
✓ Sequence of steps to be performed in order to solve a
problem by the computer.
▪ A data structure:
✓ Is a collection of data organized in some fashion.
▪ Program Design:
✓ Steps a programmer should do before they start coding the
program in a specific language.
☻in Proper program design helps other programmers to maintain the program
the future.
10
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
11
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
13
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
14
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Algorithms
Algorithm can be analyzed at two different stages, before
implementation and after implementation.
▪ A Priori Analysis: This is a theoretical analysis of an algorithm.
Efficiency of an algorithm is measured by assuming that all other
factors, for example, processor speed, are constant and have no
effect on the implementation.
▪ A Posterior Analysis: This is an empirical analysis of an
algorithm. The selected algorithm is implemented using
programming language. This is then executed on target
computer machine. In this analysis, actual statistics like running
time and space required, are collected.
15
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Algorithms
▪ Analysis of algorithms: is the theoretical study of computer
program performance and resource usage, without use of
specific programming language or implementation.
▪ The practical goal of algorithm analysis is to predict the
performance of different algorithms in order to guide program
design decisions.
▪ Whether we are designing an algorithm or applying one that is
widely accepted, it is important to understand how the
algorithm will perform.
▪ There are a number of ways we can look at an algorithm’s
performance, but usually the aspect of most interest is how fast
the algorithm will run, And storage area occupied by algorithm,
if we interested in its space requirement as well.
16
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Algorithms
Worst-Case Analysis:
▪ Most algorithms do not perform the same in all cases;
▪ Normally an algorithm’s performance varies with the data
passed to it.
▪ Typically, three cases are recognized: the best case, average
case and worst case.
▪ Understanding each of these cases is an important part of
analysis because performance can vary significantly between
them.
▪ A basic understanding of how an algorithm performs in all
cases is important, but usually we are most interested in how
an algorithm performs in the worst case.
17
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Analysis of Algorithms
O-notation:
▪ O-notation, also known as Big O-notation, is the most common
notation used to express an algorithm’s performance in a
formal manner.
▪ Formally, O-notation expresses the upper bound is a function
within a constant factor.
▪ Suppose we have an algorithm whose running time is described
by the function T(n) = 3n2 + 10n + 10. Using the rules of O-
notation, this function can be simplified to:
O(T(n)) = O(3n2 + 10n + 10) = O(3n2) = O(n2)
18
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Expressing Algorithms
We can describing algorithms in many different notations:
natural languages, pseudocode, flowcharts.
19
Algorithm & Data Structure I – AAU – Instructor: Albohtori Mohammed
Basic Terminology
▪ Data: Data are values or set of values.
▪ Data Item: Data item refers to single unit of values.
▪ Group Items: Data items that are divided into sub
items are called as Group Items.
▪ Field: Field is a single elementary unit of information
representing an attribute of an entity.
▪ Record: Record is a collection of field values of a given
entity.
20