Data Structures Chapter 1- Introduction Mohamed Mustaq.A.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 2: Algorithm Discovery and Design
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Fundamentals of Python: From First Programs Through Data Structures
BY MISS FARAH ADIBAH ADNAN IMK
Edgardo Molina, CSC212 Data Structures - Section AB Lecture 1: Introduction Instructor: Edgardo Molina Department of Computer Science City College.
BIT Presentation 4.  An algorithm is a method for solving a class of problems.  While computer scientists think a lot about algorithms, the term.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CS 101: “If” and Recursion Abhiram Ranade. This week’ lab assignment: Write a program that takes as input an integer n and a sequence of numbers w1,w2,...,wn.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data Structures and Algorithms -- Chapter 3 Abstract Data Types Mohamed Mustaq.
What does a computer program look like: a general overview.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
How to Read Code Benfeard Williams 6/11/2015 Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
Numerical Methods.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Computer Science 112 Fundamentals of Programming II.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Introduction to Algorithms
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 1 The Phases of Software Development
Welcome to CSIS 10B Overview of Course Software Design (from Nyhoff)
Chapter 15 Recursion.
Chapter 15 Recursion.
Chapter 1 The Phases of Software Development
GC211Data Structure Lecture2 Sara Alhajjam.
Revision Lecture
Control Structures – Selection
Preconditions, Postconditions & Assertions
Preconditions and Postconditions
Computer programming Lecture 3.
Introduction to Algorithms
Introduction to Data Structure
Preconditions and Postconditions
Presentation transcript:

Data Structures Chapter 1- Introduction Mohamed Mustaq.A

1- 2 Introduction Overview History of algorithms. Examples of algorithms. Algorithms vs. programs. Data structures. Abstract data types.

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

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.

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.

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 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 A B A B A B C D A B C D A B C D E A B C D E

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.

1- 8 Example: GCDs (2) Animation: 77 m 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. 21 n 77 m 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. 21 n 77 p 21 q 77 m 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. 21 n 77 p 21 q 77 m 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. 21 n p 14 q 77 m 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. 21 n p 14 q 77 m 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. 21 n 14 p 7 q 77 m 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. 21 n 14 p 7 q 77 m 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. 21 n 14 p 7 q

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

1- 10 Example: square roots (1) The positive square root of a positive number a is the positive number r such that r 2 = 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.1Set 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.

1- 11 Example: square roots (2) Animation: 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r. 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r. 1.5 r 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r. 1.5 r 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r r 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r r 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r r 2.0 a To compute approximately the positive square root of a positive number a: 1.Set r to the mean of 1 and a. 2.Until r 2 is approximately equal to a, repeat: 2.1Set r to the mean of r and a/r. 3.Terminate with answer r r 2.0 a 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.1Set r to the mean of r and a/r. 3.Terminate with answer r r

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.cppSQRT2.cpp

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.

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.

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

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.

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’ ‘\n’ 4

1- 18 Example: representing sets Possible data structures to represent the set of words {bat, cat, mat, rat, sat}: (Sorted) linked list: batcatmatratsat (Sorted) array: 0 batcatmatratsat Binary search tree: bat cat mat rat sat

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.

1- 20 An important topic: preconditions and postconditions. They are a method of specifying what a function accomplishes. Preconditions and Postconditions

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.

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 THE FUNCTION USES, AS LONG AS THESE REQUIREMENTS ARE MET ( doesn’t care how a function works)

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.

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

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.

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.

1- 27 Example write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 ); Which of these function calls meet the precondition ?

1- 28 Example Which of these function calls meet the precondition ? The second and third calls are fine, since the argument is greater than or equal to zero. write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 );

1- 29 Example Which of these function calls meet the precondition ? But the first call violates the precondition, since the argument is less than zero. write_sqrt( -10 ); write_sqrt( 0 ); write_sqrt( 5.6 );

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.

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

1- 32 Another Example is_vowel( 'A' ); is_vowel( 'Z' ); is_vowel( '?' ); What values will be returned by these function calls ?

1- 33 Another Example is_vowel( 'A' ); is_vowel( 'Z' ); is_vowel( '?' ); What values will be returned by these function calls ? true false Nobody knows, because the precondition has been violated. ( (might return true, or it might return false)

1- 34 Another Example is_vowel( '?' ); What values will be returned by these function calls ? Violating the precondition might even crash the computer.

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.

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.

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.