Data - Structure 1
Data - Structure 1
1
Introduction to Data Structures and
Algorithms
Software Engineering
• Is the use of engineering principles in order to develop good
software.
• Human creativity + Computer speed & Reliability = Program.
• There are a number of general principles that apply to many
areas, including aspects of software engineering.
Software Engineering Principles
a) Separation of Concern
– This is a very important principle that has many
applications
– in Software Engineering. Frequently, we have a large,
complex problem with many inter-related aspects.
– To deal with such problems, separate concerns and look
at each concern separately.
b) Modularity
– Every large system must be divided into modules so we
can understand it. 2
– Each module performs a set of tasks.
Software Engineering Principles....(Continued)
3
Software Engineering Principles....(Continued)
4
Software Engineering Principles....(Continued)
c) Abstraction
• It is sometimes best to concentrate on general aspects of th
e problem while carefully removing detailed aspects.
d) Anticipating Change
• General rule: write all documents and code under the assump
tion that they will subsequently be corrected, adapted, or cha
nged by somebody else.
e) Generality: A general solution is often:
• Not much harder to write than a special-purpose solution;
• More likely to be re-used; and
• Perhaps a little less efficient.
• Generality needs support from the programming language.
• Current languages do not support generality well; new OO lan
guages may be better (abstract classes, frameworks,. . .).
• Generalization is related to abstraction.
5
Software Engineering Principles....(Continued)
f) Incrementality
• It is easier to make small changes to a working system than t
o rebuild the system.
• Why? Because if the modified system does not work, the erro
rs must have been introduced by the small changes.
g) Rigor and Formality
• Software engineering is a creative design activity, BUT
• It must be practiced systematically
• Rigor is a necessary complement to creativity that increases
our confidence in our developments
• Formality is rigor at the highest degree
– Software process driven and evaluated by mathematical la
ws
• Rigor: Careful and precise reasoning.
6
Software Engineering Principles....(Continued)
Software Engineering
• Is the use of engineering principles in order to develop good
software.
Good software is:
• Correct
– The software performs according to the SRD (Software
Requirements Document).
– The SRD may be too vague (although it should not be) —
in this case, conformance to a specification is needed.
• Reliable
– This is a weaker requirement than “correct”. E-mail is
reliable — messages usually arrive — but probably
incorrect.
• Robust
– The software behaves well when exercised outside the
requirements.
– For example, software designed for 10 users should not
fall apart with 11 users.
8
Introduction....(Continued)
• Performance
– The software should have good space/time utilization, fast
response times.
– And the worst response time should not be too different fr
om the average response time.
• Friendly
– The software should be easy to use, should not irritate th
e user, and should be consistent.
– The screen always mirrors the state.
– One key — one effect. Example: F1 for help.
• Verifiable
– It should be possible to prove (verify) correctness of
the software.
– A common term that is not easily defined; it is easier to ver
ify a compiler than a word-processor.
• Flexibility
– Ability to accommodate new requirements (new
situations).
9
Introduction....(Continued)
• Maintainable
– Easy to correct or upgrade.
– Code traceable to design; design traceable to
requirements.
– Clear simple code.
– Good documentation.
– Simple interfaces between modules.
• Reusable
– We need abstract modules that can be used in many
situations.
– Sometimes, we can produce a sequence of products,
each using code from the previous one.
– Example: Accounting systems.
– Object-Oriented techniques aid reuse.
10
Introduction....(Continued)
• Portable
– The software should be easy to move to different
platforms.
– This implies few Operating System and hardware
dependencies.
– Recent developments in platform standards (PCs,
UNIX, . . .) have aided portability.
– Portability and efficiency are incompatible.
– Highly portable systems consist of many layers, each
layer hiding local details.
– Recent achievements in portability depend on fast
processors and large memories.
• Interoperable
– The software should be able to cooperate with other
software (word-processors, spread-sheets, graphics
packages, . . .).
11
Introduction....(Continued)
• Visible
– All steps must be documented.
A program
12
Introduction....(continued)
14
Introduction....(continued)
15
Introduction....(continued)
19
Example: ADT employees of an organization:
21
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.
– Life time: The time interval during execution of a program while the
variable exists.
22
Algorithm
24
• An algorithm transforms data structures from one
state to another state.
• What is the purpose of algorithms in programs?
– 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
25
• The quality of a data structure is related to its
ability to successfully model the characteristics
of the world (problem).
26
• However, the quality of data structure and algorithms
is determined by their ability to work together well.
27
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++;
} 28
Definiteness (Absence of ambiguity):
29
Sequential:
30
Feasibility:
It must be possible to perform each
instruction.
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.
}
31
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.
Completeness:
It must solve the problem completely.
32
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).
33
Example: Write a program that takes a number
and displays the square of the number.
1) int x;
cin>>x;
cout<<x*x*x;
2) int x,y;
cin>>x;
y=x*x*x;
cout<<y;
34
Example: Write a program that takes two numbers
and displays the sum of the two.
35
Input/output:
There must be a specified number of input
values, and one or more result values.
Zero or more inputs and one or more outputs.
Precision:
The result should always be the same if the
algorithm is given identical input.
36
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.
Levels of abstraction:
Used to organize the ideas expressed in algorithms.
• Used to hide the details of a given activity and refer to
just a name for those details.
• The simple (detailed) instructions are hidden inside
modules.
• Well-designed algorithms are organized in terms of
levels of abstraction. 37