37
Data Structures Chapter 1- Introduction Mohamed Mustaq.A

Data Structures Chapter 1- Introduction Mohamed Mustaq.A

Embed Size (px)

Citation preview

Page 1: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

Data Structures

Chapter 1-

Introduction

Mohamed Mustaq.A

Page 2: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 2

IntroductionOverview

• History of algorithms.

• Examples of algorithms.

• Algorithms vs. programs.

• Data structures.

• Abstract data types.

Page 3: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 3

What is an algorithm?

• An algorithm is a step-by-step procedure for solving a stated problem.

• For example, consider the problem of multiplying two numbers. There are many possible algorithms for solving this problem:– multiplication using a table (suitable only for small

numbers)– long multiplication– multiplication using logarithms– multiplication using a slide ruler– binary fixed-point or floating-point multiplication (in a

computer).

Page 4: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 4

Algorithms History

• Euclid (~ 300 BCE), Eratosthenes (~ 200 BCE)– arithmetic, geometric algorithms.

• al Khwarizmi (~ 800)– arithmetic, algebraic, geometric algorithms.

• Napier (~ 1600)– arithmetic using logarithms.

• Newton (~ 1700)– differentiation, integration.

• Turing (~ 1940)– code breaking, solvability.

Page 5: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 5

Example: finding a midpoint (1)

• Midpoint algorithm:To find the midpoint of a given straight-line segment AB:

1. Draw intersecting circles of equal radius, centered at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer E.

• Could be performed by a human equipped with drawing instruments.

Page 6: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 6

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

Example: finding a midpoint (2)

• Animation:

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

C

D

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

C

D

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

C

D

E

A

B

1. Draw intersecting circles

of equal radius, centered

at A and B respectively.2. Let C and D be the points where the circles intersect.3. Draw a straight line between C and D.4. Let E be the point where CD intersects AB.5. Terminate with answer

E.

C

D

E

Page 7: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 7

Example: GCDs (1)• The greatest common divisor (GCD) of two positive

integers is the largest integer that exactly divides both. E.g., the GCD of 77 and 21 is 7.

• Euclid’s GCD algorithm:

To compute the GCD of positive integers m and n:

1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

• Could be performed by a human, perhaps equipped with calculator.

Page 8: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 8

Example: GCDs (2)

• Animation:

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

77p 21q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

77p 21q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

21p 14q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

21p 14q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

14p 7q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

14p 7q

77m

To compute the GCD of positive integers m and n:1. Set p to m, and set q to n.2. Until q exactly divides p, repeat:

2.1. Set p to q, and set q to (p modulo q).3. Terminate with answer q.

21n

14p 7q

Page 9: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 9

Example: GCDs (3)• Implementation in C++:// gcd() Calculates the Great Common Divisor of two positive integers

int gcd(int m, int n)

{

int p = m, q = n;

while (p % q != 0) {

int r = p % q;

p = q;

q = r;

}

return q;

}

Example: GSD.cpp

Page 10: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 10

Example: square roots (1)• The positive square root of a positive number a is

the positive number r such that r2 = a.• Square-root algorithm:

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. For a few iterations (e.g. 10):

2.1 Set r to the mean of r and a/r (r = (r+a/r)/2).3. Terminate with answer r.

• Could be performed by a human, perhaps equipped with log tables or a calculator.

Page 11: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 11

Example: square roots (2)

• Animation:

2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.5r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.5r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.417r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.417r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.414r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. Until r2 is approximately equal to a, repeat:

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.414r2.0a

To compute approximately the positive square root of a positive number a:1. Set r to the mean of 1 and a.2. For a few iterations (e.g. 10):

2.1 Set r to the mean of r and a/r.3. Terminate with answer r.

1.414r

Page 12: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 12

Example: square roots (3)

• Implementation in C++:

float sqrt2 (float a) {// Compute approximately the positive square

// root of positive number a.float r = (1.0 + a)/2;for (int i = 0; i < 10; i++)

r = (r + a/r)/2;return r;

}

Example: SQRT2.cpp

Page 13: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 13

Algorithms vs. programs (1)

• Algorithms:– can be performed by humans or machines– can be expressed in any suitable language– may be as abstract as we like.

• Programs:– must be performed by machines– must be expressed in a programming language– must be detailed and specific.

Page 14: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 14

Algorithms vs. programs (2)

• Algorithms are expressed in (precise) English.• Steps may be numbered consecutively:

1. Do this.2. Do that.

• The extent of a conditional is indicated by indentation:

7. If …:7.1. Do this.7.2. Do that.

• The extent of a loop is indicated by indentation:

8. While …, repeat:8.1. Do this.8.2. Do that.

Do this and then do that.

Do this and then do that, but only if condition … is true.

Do this and then do that, as long as condition … is true.

Page 15: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 15

Algorithms vs. programs (3)

• If we wish to use the algorithm on a computer, we must first code it in a programming language.

• There may be many ways of coding the algorithm, and there is a wide choice of programming languages. But all the resulting programs are implementations of the same underlying algorithm.

• Here we express our implementations in C++.(Alternatives would be C, Pascal, Java, etc.)

Page 16: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 16

Data structures

• A data structure is a systematic way of organizing a collection of data.

• A static data structure is one whose capacity is fixed at creation. E.g.: array.

• A dynamic data structure is one whose capacity is variable, so it can expand or contract at any time. E.g.: linked list, binary tree.

• For each data structure we need algorithms for insertion, deletion, searching, etc.

Page 17: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 17

Example: representing strings

• Possible data structures to represent the string “Hany”:

‘H’ ‘a’ ‘n’ ‘y’Linked list:

‘H’ ‘a’ ‘n’Array:0

‘y’1 2 3

‘\n’4

Page 18: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 18

Example: representing sets

• Possible data structures to represent the set of words {bat, cat, mat, rat, sat}:

(Sorted) linked list:

bat cat mat rat sat

(Sorted) array:

0bat cat mat rat sat

1 2 3 4 5 6 7

Binary search tree:

bat

cat

mat

rat

sat

Page 19: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 19

Abstract data types• When we write application code, we don’t care how strings

are represented: we just declare variables of type String, and manipulate them using String operations.

• Similarly, we don’t care how sets are represented: we just declare variables of type Set, and manipulate them using Set operations.

• An abstract data type is a data type whose representation is of no concern to, and is hidden from the application code.Examples: String, Set.

• Abstract data types are an excellent way to design large programs.

Page 20: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 20

• An important topic: preconditions and postconditions.

• They are a method of specifying what a function accomplishes.

Preconditions and Postconditions

Page 21: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 21

Preconditions and Postconditions

Frequently a programmer must communicate precisely what a function accomplishes, without any indication of how the function does its work.

Page 22: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 22

Example

• A head of a programming team wants one of his programmers to write a function for part of a project.

THE HEAD OF THE TEAM PROVIDES THE

PROGRAMER THE FUNCTION

REQUIREMENTS TO

WRITE (what the function must accomplish)

THE HEAD OF THE TEAM DOESN'T CARE

WHAT METHOD THEFUNCTION USES,

AS LONG AS THESEREQUIREMENTS

ARE MET (doesn’t care how a function works)

Page 23: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 23

What are Preconditions and Postconditions?

• One way to specify such requirements is with a pair of statements about the function.

• The precondition statement indicates what must be true before the function is called.

• The postcondition statement indicates what will be true when the function finishes its work.

• The two statements work together.

Page 24: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 24

Example

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...

Page 25: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 25

Example

void write_sqrt( double x){// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

• The precondition and postcondition appear as comments in your program.

Page 26: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 26

Example

void write_sqrt( double x){// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

• In this example, the precondition requires that

x >= 0

be true whenever the function is called.

Page 27: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 27

Example

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

Page 28: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 28

Example

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

The second and third calls are fine, sinceThe second and third calls are fine, sincethe argument is greater than or equal to zero.the argument is greater than or equal to zero.

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Page 29: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 29

Example

Which of these function callsWhich of these function callsmeet the precondition ?meet the precondition ?

But the first call violates the precondition,But the first call violates the precondition,since the argument is less than zero.since the argument is less than zero.

write_sqrt( -10 );write_sqrt( 0 );write_sqrt( 5.6 );

Page 30: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 30

Example

void write_sqrt( double x)

// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.

...}

• The postcondition always indicates what work the function has accomplished, in this case, when the function returns the square root of x has been written.

Page 31: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 31

Another Example

bool is_vowel( char letter )// Precondition: letter is an uppercase or// lowercase letter (in the range 'A' ... 'Z' or 'a' ... 'z') .// Postcondition: The value returned by the// function is true if Letter is a vowel;// otherwise the value returned by the function is// false.

...

Page 32: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 32

Another Example

is_vowel( 'A' );is_vowel( 'Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

Page 33: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 33

Another Example

is_vowel( 'A' );is_vowel( 'Z' );is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

truetrue

falsefalse

Nobody knows, because theNobody knows, because theprecondition has been violated.precondition has been violated.((might return true, or it might return false)

Page 34: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 34

Another Example

is_vowel( '?' );

What values will be returnedWhat values will be returnedby these function calls ?by these function calls ?

Violating the preconditionViolating the preconditionmight even crash the computer.might even crash the computer.

Page 35: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 35

Careful programmers follow these rules:

• When you write a function, you should make every effort to detect when a precondition has been violated.

• If you detect that a precondition has been violated, then – print an error message, and – halt the program.

Page 36: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 36

Careful programmers follow these rules:

• When you write a function, you should make every effort to detect when a precondition has been violated.

• If you detect that a precondition has been violated, then – print an error message, and – halt the program...

• ...rather than causing a disaster.

Page 37: Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 37

Example

void write_sqrt( double x)// Precondition: x >= 0.// Postcondition: The square root of x has// been written to the standard output.{ assert(x >= 0);

...A function can be used to ensure that a precondition is not violated.

The C++ assert() function is useful for detecting violations of a precondition, prints a message, and then halts the program.