Chapter I-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Chapter One

Introduction to Data Structures and


Algorithms

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

• The primary purpose of most computer


programs is not only to perform calculations.
• but to store and retrieve information usually as
fast as possible.
• So, Data Structure and Algorithm helping us to
understand how to structure information to
support efficient processing.
• For this reason, the study of data structures and
the algorithms that manipulate them is at the
heart of computer science.
Introduction....(Continued)

Program vs Data structure vs


Algorithm
A program

• A set of instruction which is written in order


to solve a problem.

 A solution to a problem actually consists of


two things:
 A way to organize the data
 Sequence of steps to solve the problem

4
Introduction....(continued)

• The way data are organized in a computers


memory is said to be Data Structure.
• The sequence of computational steps to solve a
problem is said to be an Algorithm.
• Therefore, a program is Data structures plus
Algorithm.
• The first step to solve the problem is obtaining ones
own abstract view, or model, of the problem.
• This process of modeling is called abstraction.

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.

• These properties include


 The data which are affected and
 The operations that are involved in the problem

• An entity with the properties just described is called


an abstract data type (ADT).

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.

• The ADT specifies:


 What data is stored.
 What operations can be done on the data.
• Does not specify how to store or how to implement the operation.
• Is independent of any programming language
9
Example: ADT employees of an organization:

 This ADT stores employees with their


relevant attributes and discarding irrelevant
attributes.
Relevant:- Name, ID, Sex, Age, Salary, Dept, Address
Non Relevant :- weight, color, height
Char name[20],char id[10],int age, int salary, char
dept[10], char adress[15]
 This ADT supports hiring, firing, retiring, …
operations. 10
Data Structure
• Is physical description of a problem
• A data structure is the implementations for an ADT
• It can be implemented and used within an algorithm.
• Data Structure is a way of collecting and organising data in such a way that we can
perform operations in an effective way.
• Anything that can store data can be called as a data structure.
– Hence Integer, Float, Boolean, Char etc, all are data structures. They are known as Primitive
Data Structures.
• Also some more complex Data Structures are :
– Linked List
– Tree
– Graph
– Stack, Queue etc.
• All these data structures allow us to perform different operations on data.
• We select these data structures based on which type of operation is required. 11
Data Structure
• Example:
struct Student_Record
{
char name[20];
char ID_NO[10];
char Department[10];
int age;
};
• Attributes of each variable:
– Name: Textual label.
– Address: Location in memory.
– Scope: Visibility in statements of a program.
– Type: Set of values that can be stored + set of operations that can be
performed.
– Size: The amount of storage required to represent the variable.

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.

• Inputs Algorithm Outputs

• A problem can be solved by many different algorithms


where as A given algorithm solves only one problem

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).

• Generally speaking, correct data structures lead to


simple and efficient algorithms and correct
algorithms lead to accurate and efficient data
structures.

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.

Finite  int i=0; Infinite while(true){


while(i>10){ cout<<“Hello”;
cout<< i; }
i++;
}
18
Definiteness (Absence of ambiguity):
There can be no ambiguity as to which step will be
performed next
 Each step must be clearly defined, having
one and only one interpretation.

 At each point in computation, one should be


able to tell exactly what happens next.

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.

Program I Program II Program II (the most efficient)


cin>>a; cin>>a; cin>>a;
cin>>b; cin>>b; cin>>b;
sum = a+b; a = a+b; cout<<a+b;
cout<<sum; cout<<a;

All are effective but with different efficiencies.

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.

2. Theoretical (Asymptotic Algorithm


Analysis)
Determining the quantity of resources required
using mathematical concept.
THE END

You might also like