Published on

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • If you are willing to pay enough in time delay. Example: Simple unordered array of records.
  • Alternate definition: Better than known alternatives (“relatively efficient”). Space and time are typical constraints for programs. This does not mean always strive for the most efficient program. If the program operates well within resource constraints, there is no benefit to making it faster or smaller.
  • Typically want the “simplest” data structure that will meet the requirements.
  • These questions often help to narrow the possibilities. If data can be deleted, a more complex representation is typically required.
  • The space required includes data and overhead. Some data structures/algorithms are more complicated than others.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The concept of an ADT is one instance of an important principle that must be understood By any successful computer specialist: managing complexity through abstraction.
  • In this class, we frequently move above and below “the line” separating logical and physical forms.
  • But NO constraints on HOW the problem is solved
  • “ Correct” means computes the proper function. “ Concrete steps” are executable by the machine in question. We frequently interchange use of “algorithm” and “program” though they are actually different concepts.
  • Lec1

    1. 1. Data Structures and Algorithm Analysis Dr. Nagwa Badr
    2. 2. The Need for Data Structures <ul><li>Data structures organize data </li></ul><ul><li> more efficient programs. </li></ul><ul><li>More powerful computers  more complex applications. </li></ul><ul><li>More complex applications demand more calculations. </li></ul><ul><li>Complex computing tasks are unlike our everyday experience. </li></ul>
    3. 3. What is a data structure? <ul><li>In a general sense, any data representation is a data structure. Example: An integer </li></ul><ul><li>More typically, a data structure is meant to be an organization for a collection of data items . </li></ul>
    4. 4. Organizing Data <ul><li>Any organization for a collection of records can be searched, processed in any order, or modified. </li></ul><ul><li>The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. </li></ul>
    5. 5. Efficiency <ul><li>A solution is said to be efficient if it solves the problem within its resource constraints . </li></ul><ul><ul><li>Space </li></ul></ul><ul><ul><li>Time </li></ul></ul><ul><li>The cost of a solution is the amount of resources that the solution consumes. </li></ul>
    6. 6. Costs and Benefits <ul><li>A data structure requires a certain amount of: </li></ul><ul><li>space for each data item it stores </li></ul><ul><li>time to perform a single basic operation </li></ul><ul><li>programming effort. </li></ul>
    7. 7. Example: Banking Application <ul><li>Operations are (typically): </li></ul><ul><ul><li>Open accounts </li></ul></ul><ul><ul><li>Close accounts </li></ul></ul><ul><ul><li>Access account to Add money </li></ul></ul><ul><ul><li>Access account to Withdraw money </li></ul></ul>
    8. 8. Example: Banking Application <ul><li>Teller and ATM transactions are expected to take little time. </li></ul><ul><li>Opening or closing an account can take much longer (perhaps up to an hour). </li></ul>
    9. 9. Example: Banking Application <ul><li>When considering the choice of data structure to use in the database system that manages the accounts, we are looking for a data structure that: </li></ul><ul><ul><li>Is inefficient for deletion </li></ul></ul><ul><ul><li>Highly efficient for search </li></ul></ul><ul><ul><li>Moderately efficient for insertion </li></ul></ul>
    10. 10. Example: Banking Application <ul><li>One data structure that meets these requirements is the hash table (chapter 9). </li></ul><ul><li>Records are accessible by account number (called an exact-match query ) </li></ul><ul><li>Hash tables allow for extremely fast exact-match search. </li></ul><ul><li>Hash tables also support efficient insertion of new records. </li></ul><ul><li>Deletions can also be supported efficiently (but too many deletions lead to some degradation in performance – requiring the hash table to be reorganized). </li></ul>
    11. 11. Example: City Database <ul><li>Database system for cities and towns. </li></ul><ul><li>Users find information about a particular place by name (exact-match query) </li></ul><ul><li>Users also find all places that match a particular value (or range of values), such as location or population size (called a range query ). </li></ul>
    12. 12. Example: City Database <ul><li>The database must answer queries quickly enough to satisfy the patience of a typical user. </li></ul><ul><li>For an exact-match query, a few seconds is satisfactory </li></ul><ul><li>For a range queries , the entire operation may be allowed to take longer, perhaps on the order of a minute . </li></ul>
    13. 13. Example: City Database <ul><li>The hash table is inappropriate for implementing the city database because: </li></ul><ul><ul><li>It cannot perform efficient range queries </li></ul></ul><ul><li>The B+ tree (section 10) supports large databases: </li></ul><ul><ul><li>Insertion </li></ul></ul><ul><ul><li>Deletion </li></ul></ul><ul><ul><li>Range queries </li></ul></ul><ul><li>If the database is created once and then never changed, a simple linear index would be more appropriate. </li></ul>
    14. 14. Selecting a Data Structure <ul><li>Select a data structure as follows: </li></ul><ul><li>Analyze the problem to determine the resource constraints a solution must meet. </li></ul><ul><li>Determine the basic operations that must be supported. Quantify the resource constraints for each operation. </li></ul><ul><li>Select the data structure that best meets these requirements. </li></ul>
    15. 15. Some Questions to Ask <ul><li>Are all data inserted into the data structure at the beginning, or are insertions intersparsed with other operations ? </li></ul><ul><li>Can data be deleted ? </li></ul><ul><li>Are all data processed in some well-defined order, or is random access allowed ? </li></ul>
    16. 16. Data Structure Philosophy <ul><li>Each data structure has costs and benefits. </li></ul><ul><li>Rarely is one data structure better than another in all situations. </li></ul><ul><li>A data structure requires: </li></ul><ul><ul><li>space for each data item it stores, </li></ul></ul><ul><ul><li>time to perform each basic operation, </li></ul></ul><ul><ul><li>programming effort. </li></ul></ul>
    17. 17. Data Structure Philosophy <ul><li>Each problem has constraints on available space and time. </li></ul><ul><li>Only after a careful analysis of problem characteristics can we know the best data structure for the task. </li></ul><ul><li>Bank example: </li></ul><ul><ul><li>Start account: a few minutes </li></ul></ul><ul><ul><li>Transactions: a few seconds </li></ul></ul><ul><ul><li>Close account: overnight </li></ul></ul>continued
    18. 18. Goals of this Course <ul><li>Reinforce the concept that costs and benefits exist for every data structure. </li></ul><ul><li>Learn the commonly used data structures. </li></ul><ul><ul><li>These form a programmer's basic data structure ``toolkit.'‘ </li></ul></ul><ul><li>Understand how to measure the cost of a data structure or program. </li></ul><ul><ul><li>These techniques also allow you to judge the merits of new data structures that you or others might invent. </li></ul></ul>
    19. 19. Abstract Data Types <ul><li>Abstract Data Type (ADT): a definition for a data type solely in terms of a set of values and a set of operations on that data type. </li></ul><ul><li>Each ADT operation is defined by its inputs and outputs. </li></ul><ul><li>Encapsulation : Hide implementation details. </li></ul>
    20. 20. Data Structure <ul><li>A data structure is the physical implementation of an ADT. </li></ul><ul><ul><li>Each operation associated with the ADT is implemented by one or more subroutines in the implementation. </li></ul></ul><ul><li>In a OO language such as C++, an ADT and its implementation together make up a class. </li></ul><ul><li>Data structure usually refers to an organization for data in main memory. </li></ul><ul><li>File structure : an organization for data on peripheral storage, such as a disk drive. </li></ul>
    21. 21. Labeling collections of objects <ul><li>Humans deal with complexity by assigning a label to an assembly of objects. An ADT manages complexity through abstraction. </li></ul><ul><ul><li>Hierarchies of labels </li></ul></ul><ul><li>Ex1: transistors  gates  CPU. </li></ul><ul><li>In a program, implement an ADT, then think only about the ADT, not its implementation. </li></ul>
    22. 22. Logical vs. Physical Form <ul><li>Data items have both a logical and a physical form. </li></ul><ul><li>Logical form : definition of the data item within an ADT. </li></ul><ul><ul><li>Ex: Integers in mathematical sense: +, - </li></ul></ul><ul><li>Physical form : implementation of the data item within a data structure. </li></ul><ul><ul><li>Ex: 16/32 bit integers, overflow. </li></ul></ul>
    23. 23. Data Type <ul><li>ADT: </li></ul><ul><ul><li>Type </li></ul></ul><ul><ul><li>Operations </li></ul></ul>Data Items: Logical Form Data Items: Physical Form <ul><li>Data Structure: </li></ul><ul><ul><li>Storage Space </li></ul></ul><ul><ul><li>Subroutines </li></ul></ul>
    24. 24. Problems, Algorithms and Programs <ul><li>Programmers deal with: </li></ul><ul><ul><li>problems, </li></ul></ul><ul><ul><li>algorithms and </li></ul></ul><ul><ul><li>computer programs. </li></ul></ul><ul><li>These are distinct concepts… </li></ul>
    25. 25. Problems <ul><li>Problem : a task to be performed . </li></ul><ul><ul><li>Best thought of as inputs and matching outputs. </li></ul></ul><ul><ul><li>Problem definition should include constraints on the resources that may be consumed by any acceptable solution. </li></ul></ul>
    26. 26. Problems (cont) <ul><li>Problems  mathematical functions </li></ul><ul><ul><li>A function is a matching between inputs (the domain ) and outputs (the range ). </li></ul></ul><ul><ul><li>An input to a function may be single number, or a collection of information. </li></ul></ul><ul><ul><li>The values making up an input are called the parameters of the function. </li></ul></ul><ul><ul><li>A particular input must always result in the same output every time the function is computed. </li></ul></ul>
    27. 27. Algorithms and Programs <ul><li>Algorithm : a method or a process followed to solve a problem. </li></ul><ul><ul><li>A recipe: The algorithm gives us a “recipe” for solving the problem by performing a series of steps, where each step is completely understood and doable. </li></ul></ul><ul><li>An algorithm takes the input to a problem (function) and transforms it to the output. </li></ul><ul><ul><li>A mapping of input to output. </li></ul></ul><ul><li>A problem can be solved by many algorithms . </li></ul>
    28. 28. A problem can have many algorithms <ul><li>For example, the problem of sorting can be solved by the following algorithms: </li></ul><ul><li>Insertion sort </li></ul><ul><li>Bubble sort </li></ul><ul><li>Selection sort </li></ul><ul><li>Shellsort </li></ul><ul><li>Mergesort </li></ul><ul><li>Others </li></ul>
    29. 29. Algorithm Properties <ul><li>An algorithm possesses the following properties: </li></ul><ul><ul><li>It must be correct . </li></ul></ul><ul><ul><li>It must be composed of a series of concrete steps . </li></ul></ul><ul><ul><li>There can be no ambiguity as to which step will be performed next. </li></ul></ul><ul><ul><li>It must be composed of a finite number of steps. </li></ul></ul><ul><ul><li>It must terminate . </li></ul></ul><ul><li>A computer program is an instance, or concrete representation, for an algorithm in some programming language. </li></ul>
    30. 30. Programs <ul><li>A computer program is a concrete representation of an algorithm in some programming language. </li></ul><ul><li>Naturally, there are many programs that are instances of the same algorithms, since any modern programming language can be used to implement any algorithm. </li></ul>
    31. 31. To Summarize: <ul><li>A problem is a function or a mapping of inputs to outputs. </li></ul><ul><li>An algorithm is a recipe for solving a problem whose steps are concrete and ambiguous. </li></ul><ul><li>A program is an instantiation of an algorithm in a computer programming language. </li></ul>
    32. 32. Example <ul><li>Problem: find y = x to the power of 2 </li></ul><ul><li>Algorithm1: Multiply X by X </li></ul><ul><li>Algorithm2: Add X to itself X times </li></ul><ul><li>Program1: for (int i = 0; i<x; i++) </li></ul><ul><li>y +=x; </li></ul>
    33. 33. Example (cont.) <ul><li>Program2: (Assembly Intel 8086) </li></ul><ul><li>mov bl,x // read x </li></ul><ul><li>mov al,bl // store x </li></ul><ul><li>mov cl,bl // int counter </li></ul><ul><li>loop: add al,bl // al = al + x </li></ul><ul><li>dec cl // decrement loop ctr </li></ul><ul><li>jnz loop </li></ul>
    34. 34. In class exercises <ul><li>Think of a program you have used that is unacceptably slow. Identify other basic operations that the program performs quickly enough. </li></ul><ul><li>Imagine that you are a shipping clerk for a large company. You have just been handed about 1000 invoices, each of which is a single sheet of paper with a large number in the upper right corner. The invoices must be sorted by this number, in order from lowest to highest. Write down as many different approaches to sorting the invoices as you can think of. </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.