1 / 92

Introduction to Data Structures

Introduction to Data Structures. Briana B. Morrison. Topics. What is a data structure? Defining Data Structures (APIs) Introduction to Software Engineering Big Oh Review Classes Templates Dynamic Memory Error Handling. Data Structures . What is a data structure?

vail
Télécharger la présentation

Introduction to Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Data Structures Briana B. Morrison

  2. Topics • What is a data structure? • Defining Data Structures (APIs) • Introduction to Software Engineering • Big Oh • Review • Classes • Templates • Dynamic Memory • Error Handling Intro to Data Structures

  3. Data Structures • What is a data structure? • Systematic way of organizing and accessing data. • Programmer-defined data structures bundle data with operations that manipulate the data. • The structures, called containers, have operations to access, insert, and remove items from the collection. • Usually implemented as an ADT • Data • Operations • No implementation details Intro to Data Structures

  4. ADTs • A model used to understand the design of a data structure. • Specify the type of data stored and the operations that support the data. • Viewing a data structure as an ADT allows a programmer to focus on an idealized model of the data and its operations. Intro to Data Structures

  5. Abstract Data Types ADT Operation Description • operationName: Action statement that specifies the input parameters, the type of operation on the elements of the data structure, and the output parameter • Preconditions: Necessary conditions that must apply to the input parameters and the current state of the object to allow successful execution of the operation. • Postconditions: Changes in the data of the structure caused by the operation. Intro to Data Structures

  6. Responsibilities • The programmer who calls the function is responsible for ensuring that the precondition is validwhen the function is called. • The programmer who writes the function counts on the precondition being valid, and ensures that the postcondition becomes trueat the function’s end. Intro to Data Structures

  7. Abstract Data Types ( time24 Class ) • duration(t): Time t is an input parameter. Measure the length of time from the current time to time t and return the result as a time24 value. • Precondition: Time t must not be earlier than the current time Intro to Data Structures

  8. Classes (Information Hiding)( Private/Public Sections) • Public members of a class are the interface of the object to the program. • Any statement in a program block that declares an object can access a public member of the object Intro to Data Structures

  9. Classes (Private/Public Sections) • The private section typically contains the data values of the object and utility functions that support class implementation. • Only member functions (or friends) of the class may access elements in the private section. Intro to Data Structures

  10. Application Programming Interface (API) • Allows other programmers to use the public interface of the class without having to view the technical details of the class declaration or implementation. • Instructions on how to use the data type. Intro to Data Structures

  11. CLASS className Constructors “<file>.h” className(<arguments>); Initializes the attributes of the object Postconditions: Initial status of the object API( Constructor ) Intro to Data Structures

  12. CLASS className Operations “<file>.h” returnType functionName(argument list); Description of the action of the function and any return value Preconditions: Necessary state of the object before executing the operation. Any exceptions that are thrown when an error is detected. Postconditions: State of the data items in the object after executing the operation …. API (Operations ) Intro to Data Structures

  13. Introducing Software Engineering SOFTWARE ENGINEERING: A SYSTEMATIC APPROACH TO THE DEVELOPMENT OF MEDIUM-TO-LARGE PROGRAMS Intro to Data Structures

  14. Software Design Overview • A computer program begins with a problem that the client wants solved. • The process of building the program starts with an analysis of the problem. • It proceeds through a series of stages to produce a product that is reliable and easy to maintain. Intro to Data Structures

  15. Software Design Overview • Prior to the standardization of the software development life cycle: • Programmers added code to software with little attention to its integration into the system. • Over time systems deteriorated and became so difficult to update that programmers had to develop new software to replace them. Intro to Data Structures

  16. Software Development Life Cycle Four Stages: I. Problem Analysis – determine what is to be done II. Program Design – What classes will I need? What methods will I need? What data will I need? III. Program Implementation IV. Program Maintenance – Modifications to a program that has been deployed Intro to Data Structures

  17. Problem Analysis • Request: (Requirements) • Client perceives a need for a software system to solve a problem. • A computer consultant undertakes a feasibility study for the project. • Analysis: • A systems analyst develops the requirements of the system and creates a functional specification that includes a list of needs and special requirements. Intro to Data Structures

  18. Analysis Deliverables • Functional Specifications • Explicitly describe the behavior (that is, input and output) of the program. • System Tests • Created to reinforce an understanding of the problem and to provide data for testing the completed program. Intro to Data Structures

  19. System Test • Define the purpose of the test case • Define inputs • Define expected outputs • Indicate pass/fail Test rollover of 24 hours Input: original time 21.30 add time 5.30 Expected output: 3.00 Intro to Data Structures

  20. PRINCIPLE OF ABSTRACTION WHEN TRYING TO SOLVE A PROBLEM, SEPARATE WHAT IS TO BE DONE FROM HOW IT WILL BE DONE. Intro to Data Structures

  21. Program Specification & Design • Specification - what a program or portion of a program is meant to accomplish, without regard to precisely how it accomplishes the work • Design - selection and creation of specific data structures and algorithms to meet the specification Intro to Data Structures

  22. Determination of Classes / Design • Suppose an existing class has everything needed. • Great! Use that class. • Suppose an existing class has almost everything needed. • Very good! Create a subclass of that class. • Suppose you have to start from scratch. Intro to Data Structures

  23. Classes from Scratch 1.  Determine responsibilities of the class: What the class will provide to users. 2. Refine responsibilities into method interfaces. 3. Decide what data to have. 4. After individual classes have been designed, determine CLASS-TO-CLASS RELATIONSHIP   and  OBJECT-DATA TO CLASS RELATIONSHIP Intro to Data Structures

  24. Object Composition • Object Composition: • refers to a condition that exists when a class contains one or more data members that are objects of class type. • Class included by composition == supplier class. • Class that includes an object by composition== client class. Intro to Data Structures

  25. Inheritance vs. Composition • Use inheritance if one class is a “specific kind” of another class. • Example: HourlyEmployee is a specific kind of Employee • “Is a” relationship • Use composition if one class has an instance of another class • Example: Employee has a Supervisor • “Has a” relationship Intro to Data Structures

  26. Program Implementation Define the methods, then check for correctness.      Method Validation • Use testing to increase confidence in the correctness of a method. • Check for efficiency. Intro to Data Structures

  27. Program Testing • Testing: • Check the program for logical and runtime errors and verify that the system meets the specifications of the client. Intro to Data Structures

  28. Methods for Program Testing • The black-box method: • Easy values • Typical, realistic values • Extreme values • Illegal values • The glass-box method: Trace all the paths through the program. Intro to Data Structures

  29. Testing & Debugging • Choosing Test Data • You must know what output a correct program should produce for each test input • The test inputs should include those inputs that are most likely to cause errors • Boundary Values • Path Coverage Intro to Data Structures

  30. Debugging Methods for debugging: • Structured walkthrough • Trace tools and snapshots • Scaffolding • Static analyzer • The quality of test data is more important than its quantity. • Program testing can be used to show the presence of bugs, but never their absence. Intro to Data Structures

  31. Intro to Data Structures

  32. Intro to Data Structures

  33. Program Maintenance • Maintenance: • Periodically update of the software to stay current and to respond to changing needs of the client. Intro to Data Structures

  34. Intro to Data Structures

  35. Programming Precept • For a large and important program, more than half the work comes in the maintenance phase, after it has been completely debugged, tested, and put into use. Intro to Data Structures

  36. Efficiency of Algorithms By comparing the efficiency of algorithms, we have one basis of comparison that can help us determine the best technique to use in a particular situation. Travel - by foot - by car - by train - by airplane Intro to Data Structures

  37. Analysis of Algorithms • The measure of the amount of work an algorithm performs (time) or • the space requirements of an implementation (space) Complexity Order of magnitude is a function of the number of data items. Intro to Data Structures

  38. Entire expression is called the "Big-O" measure for the algorithm. Big-O notation Big-O notation provides a machine independent means for determining the efficiency of an Algorithm. For the selection sort, the number of comparisons is T(n) = n2/2 - n/2. n = 100: T(100) = 1002/2 -100/2 = 10000/2 - 100/2 = 5,000 - 50 = 4,950 Intro to Data Structures

  39. Big O • Big-O notation measures the efficiency of an algorithm by estimating the number of certain operations that the algorithm must perform. • For searching and sorting algorithms, the operation is data comparison • Big-O measure is very useful for selecting among competing algorithms. Intro to Data Structures

  40. Big-O Notation • Algorithm A requires time proportional to a function F(N) given a reasonable implementation and computer. • big-o notation is then written as O(n) where n is proportional to the number of data items • You can ignore constants O(5n2) is O(n2) • You can ignore low-order terms O(n3 +n2 + n) is O(n3) Intro to Data Structures

  41. Growth Rates • Constant O(1) print first item • Linear O(n) print list of items • Polynomial O(n2) print table of items • Logarithmic O(log n) binary search • Exponential O(2n) listing all the subsets of a set • Factorial O(n!) traveling salesperson problem Intro to Data Structures

  42. Constant Time Algorithms An algorithm is O(1) when its running time is independent of the number of data items. The algorithm runs in constant time. The storing of the element involves a simple assignment statement and thus has efficiency O(1). Intro to Data Structures

  43. Linear Time Algorithms An algorithm is O(n) when its running time is proportional to the size of the list. When the number of elements doubles, the number of operations doubles. Intro to Data Structures

  44. Exponential Algorithms • Algorithms with running time O(n2) are quadratic. • practical only for relatively small values of n. • Whenever n doubles, the running time of the algorithm increases by a factor of 4. • Algorithms with running time O(n3)are cubic. • efficiency is generally poor; doubling the size of n increases the running time eight-fold. Intro to Data Structures

  45. Exponential Algorithms Intro to Data Structures

  46. Logarithmic Time Algorithms The logarithm of n, base 2, is commonly used when analyzing computer algorithms. Ex. log2(2) = 1 log2(75) = 6.2288 When compared to the functions n and n2, the function log2 n grows very slowly. Intro to Data Structures

  47. Intro to Data Structures

  48. Programming Precept • Do not optimize your code unless it is necessary to do so. • Do not start to optimize code until it is complete and correct. • Most programs spend 90 percent of their time doing 10 percent of their instructions. • Find this 10 percent, and concentrate your efforts for efficiency there. • Keep your algorithms as simple as you can. • When in doubt, choose the simple way. • Sometimes postponing problems simplifies their solution. Intro to Data Structures

  49. Review of Classes • A class collects data and the methods used to access or change the data. • Such a collection of data and methods is called an object belonging to the given class. • Every C++ class consists of members that represent either • variables (called data members) or • functions (called methods or member functions). • The member functions of a class are normally used to access or alter the data members. Intro to Data Structures

  50. Operator Overloading • Redefines an operator to accept operands of the class type. • Designing a class to take advantage of familiar operator notation makes the class easier to use and extends its flexibility. Intro to Data Structures

More Related