Professional Documents
Culture Documents
Chapter I-1
Chapter I-1
Chapter I-1
1
Objectives
• Introducing the basic definitions and principles of data
structure and algorithms
• To introduce commonly used data structures and their
application
• Describing the costs and benefits associated with data
structure.
– for each data structure, the amount of space and time
required for typical operations.
• Selecting the best data structure and algorithm for a given
problem.
• Introduce the basic principles of resource requirement
analysis technique
• Measure the effectiveness of a data structure and
algorithm.
Introduction
4
Introduction....(continued)
5
Abstraction
Problem is a process of classifying characteristics
as relevant and irrelevant for the
particular purpose at hand and ignoring
the irrelevant ones.
Abstraction
The model
Model
•The model defines an abstract view to
Data structure the problem.
•The model should only focus on
problem related stuff
Example: model students of BDU.
• Relevant:
Name
ID
Dept
Age, year;
• Non relevant
float hieght, weight;
7
• Using the model, a programmer tries to define the
properties of the problem.
8
Abstract Data Types
• Is logical description of a problem
• An abstract data type (ADT) is the realization of a data type as a software
component.
• Is a specification that describes a data set and the operation on that data.
• Think of an ADT as a picture of the data and the operations to manipulate and change
that data.
12
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must
meet.
2. Determine the basic operations that must
be supported. Quantify the resource
constraints for each operation.
3. Select the data structure that best meets
these requirements.
Algorithm
• An algorithm is a method or a process followed to solve a
problem
• If the problem is viewed as a function, then an algorithm is
an implementation for the function that transforms an
input to the corresponding output.
14
• Data structures model the static part of the world. They
are unchanging while the world is changing.
• In order to model the dynamic part of the world we
need to work with algorithms.
• The quality of a data structure is related to its ability to
successfully model the characteristics of the world
(problem).
• Similarly, the quality of an algorithm is related to its
ability to successfully simulate the changes in the world.
15
• However, the quality of data structure and
Algorithms is determined by their ability to work
together well(they are cooperative).
16
• How An algorithms transform data structure from one state
to another?.
– Take values as input. Example: cin>>age;
– Change the values held by data structures. Example:
age=age+1;
– Change the organization of the data structure:
Example:
• Sort students by name
– Produce outputs:
• Example: Display student’s information
17
Properties of Algorithms
Finiteness:
Algorithm must complete after a finite number
of steps.
Algorithm should have a finite number of steps.
19
Sequential:
Each step must have a uniquely defined preceding
and succeeding step.
The first step (start step) and last step (halt step)
must be clearly noted.
Correctness:
It must compute correct answer for all possible legal
inputs.
The output should be as expected and required and
correct.
Language Independence:
It must not depend on any one programming language
20
Feasibility:
Each instruction should have possibility to be
executed.
1) for(int i=0; i<0; i++){
cout<< i; // there is no possibility
} that this statement to
be executed.
2) if(5>7) {
cout<<“hello”; // not executed.
}
21
Effectiveness:
Doing the right thing. It should yield the correct result
all the time for all of the possible cases.
Efficiency:
It must solve with the least amount of computational
resources such as time and space.
Producing an output as per the requirement within the
given resources (constraints).
22
Example: Write a program that takes a number and
displays the square of the number.
1) int x;
cin>>x;
cout<<x*x;
2) int x,y;
cin>>x;
y=x*x;
cout<<y;
23
Example: Write a program that takes two numbers and
displays the sum of the two.
24
Precision:
The result should always be the same if the algorithm is
given identical input.
Simplicity:
A good general rule is that each step should carry out one logical
step.
• What is simple to one processor may not be simple to
another.
25
Algorithm Efficiency
There are often many approaches (algorithms)
to solve a problem.
So, How do we choose between them?
Two Goals of Designing an algorithm
1. To design an algorithm that is easy to
understand, code, debug(concern of SE)
2. To design an algorithm that makes efficient use
of the computer’s resources
• Is the concern of data structures and algorithm analysis
Algorithm Efficiency (cont)
An algorithm is said to be efficient if it solves
the problem within its resource constraints.
– Space
– Time
– Communication Band width
• The cost of a solution is the amount of
resources that the solution consumes.
How to Measure Efficiency?
1. Empirical comparison
based on the total running time of the program.
Uses actual system clock time.