DAA(Lecture 2)
DAA(Lecture 2)
Algorithms (CS-306)
Lecture No. 2
2
Algorithm Representation
3
Pseudo Code
◼ Language that is typically used for writing algorithms
4
Flowchart
◼ A graphical representation of a process (e.g. an algorithm),
in which graphic objects are used to indicate the steps &
decisions that are taken as the process moves along from
start to finish
5
Start or stop
Process
Input or output
Flowchart
Elements Decision
Flow line
Connector
Off-page connector
6
Data Types & Data Structures
◼ Applications/programs read data, store data temporarily,
process it and finally output results.
◼ What is data? Numbers, Characters, etc.
7
Data Types & Data Structures
◼ Data is classified into data types. e.g. char, float, int,
etc.
◼ A data type is (i) a domain of allowed values and (ii) a
set of operations on these values.
◼ Compiler signals an error if wrong operation is
performed on data of a certain type. For example, char
x,y,z; z = x*y is not allowed.
8
Data Structures
9
Data Structures
10
Efficiency
11
Costs and Benefits
◼ A data structure requires a certain amount of:
◼ space for each data item it stores
◼ time to perform a single basic operation
◼ programming effort.
12
Selecting a Data Structure
13
Data Structures
14
Lists
15
Arrays
An array is a set of
variables that each
store an item.
16
Arrays and Lists
17
Arrays and Lists
18
Arrays and Lists
19
Example: A Queue
A queue is an example of commonly used simple data structure.
A queue has beginning and end, called the front and back of the
queue.
Data enters the queue at one end and leaves at the other.
Because of this, data exits the queue in the same order in which
it enters the queue, like people in a checkout line at a
supermarket.
20
Example: A Binary Tree
21
Example: A Binary Tree
Binary Tree
22
Choosing Data Structures
23
Choosing Data Structures
24
Choosing Data Structures
25
Choosing Data Structures
26
Choosing Data Structures
27
Choosing Data Structures
28
Choosing Data Structures
29
What kinds of problems are solved by
algorithms?
◼ Sorting is by no means the only computational problem for which algorithms have
been developed.
◼ The Human Genome Project has the goals of identifying all the 100,000 genes in
human DNA, determining the sequences of the 3 billion chemical base pairs that
make up human DNA, storing this information in databases, and developing tools
for data analysis. Each of these steps requires sophisticated algorithms.
◼ The Internet enables people all around the world to quickly access and retrieve
large amounts of information. In order to do so, clever algorithms are employed to
manage and manipulate this large volume of data.
◼ Electronic commerce enables goods and services to be negotiated and exchanged
electronically. The ability to keep information such as credit card numbers,
passwords, and bank statements private is essential. Public-key cryptography and
digital signatures are core technologies used and are based on numerical
algorithms and number theory
30
What kinds of problems are solved by
algorithms?
◼ Searching (Linear and Non-Linear)
◼ Sorting ( Elementary and Advanced)
◼ String Processing ( Pattern Matching, Parsing, Compression,
Cryptography)
◼ Optimization Problem ( Shortest routs, Minimum costs)
◼ Image Processing( Compression, Conversion, Matching)
◼ Data Mining Algorithms ( Clustering, Cleansing, Rules
Mining)
◼ Mathematical Algorithms ( Random Number generator,
Matrix Operation etc. )
31
What does “size of the input” mean?
◼ If we are searching an array, the “size” of the input
could be the size of the array
◼ If we are merging two arrays, the “size” could be the
sum of the two array sizes
◼ If we are computing the nth Fibonacci number, or the
nth factorial, the “size” is n
◼ We choose the “size” to be the parameter that most
influences the actual time/space required
◼ It is usually obvious what this parameter is
◼ Sometimes we need two or more parameters
32
Analysis of Algorithms
◼ An algorithm is a finite set of precise instructions for
performing a computation or for solving a problem.
◼ What is the goal of analysis of algorithms?
◼ To compare algorithms mainly in terms of running time but
also in terms of other factors (e.g., memory requirements,
programmer's effort etc.)
◼ What do we mean by running time analysis?
◼ Determine how running time increases as the size of the
problem increases.
33
Analysis of algorithms
◼ Issues:
◼ correctness
◼ time efficiency
◼ space efficiency
◼ optimality
◼ Approaches:
◼ theoretical analysis
◼ empirical analysis
Input Size
◼ Input size (number of elements in the input)
◼ size of an array
◼ polynomial degree
◼ # of elements in a matrix
35
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number
of repetitions of the basic operation as a function of
input size
◼ Basic operation: the operation that contributes the
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Types of Analysis
◼ Worst case
◼ Provides an upper bound on running time
◼ An absolute guarantee that the algorithm would not run longer, no
matter what the inputs are
◼ Best case
◼ Provides a lower bound on running time
◼ Input is the one for which the algorithm runs the fastest
38
Analyzing Complexity
The End
(for now)
41