ds_lec_4_5 by kamrann123

VIEWS: 0 PAGES: 10

									Lecture                      DS & Algorithms:09




              DATA STRUCTURES &
                 ALGORITHMS



          1




                                              1
Lecture                                      DS & Algorithms:09


     THE NEED FOR DATA STRUCTURES
  • Goal: to organize data

  • Criteria: to facilitate efficient
     – storage of data
     – retrieval of data
     – manipulation of data

  • Design Issue:
     – select and design appropriate data types.
       (This is the real essence of OOP.)

                                                            2

                                                                2
Lecture                                      DS & Algorithms:09

          DATA STRUCTURE OPERATIONS
   • Data in the data structures are processed by means of
     certain operations.

   • Some of the most frequently used operations are:
     – Traversing
     – Searching
     – Inserting
     – Deleting
     – Sorting
     – Merging

   • Two or more operations may be used in a given situation
      – E.g., to delete a specific record
                                                             3

                                                                 3
Lecture                                       DS & Algorithms:09

      EXAMPLE: DATA STRUCTURE OPERATIONS

   • An organization contains a membership file in which each
     record contains the following data for a given member:
      – Name
      – Address
      – Telephone number
      – Age
      – Gender
   • Announce meeting through a mailing
      – Traverse the file to obtain name and address of each
        member
   • Find the names of all members living in a certain area
      – Again traverse to obtain the required data
   • Obtain address for a given name
      – Search the file for the record containing the name   4

                                                               4
Lecture                                      DS & Algorithms:09

                 EXAMPLE (CONT.)
   • New person joins the organization
     – Insert his/her record in the file

   • A member dies
     – Delete his/her record from the file

   • A member has moved to new location
     – Search for member’s record and perform update
       operation

   • Members with age >= 65
     – Traverse the file
                                                            5

                                                                5
Lecture                                         DS & Algorithms:09

          ALGORITHMS: COMPLEXITY, TIME-SPACE
                      TRADEOFF
   • Aim is to develop efficient algorithms for the processing
     of our data

   • Measure of efficiency
     – Time and space an algorithm uses

   • Choice of data structure involves many things including
     – Type of data
     – Frequency with which various data operations are
       applied

   • Sometimes the choice of data structure involves a time-
     space tradeoff                                          6

                                                                 6
Lecture                                                       DS & Algorithms:09

                          LINEAR SEARCH
   • One solution is linear search
      – Search each record of the file, one at a time, until finding the given Name
        and hence the corresponding telephone numbers
   • Searching an Array
       boolean find(int key, int min, int max)
     {
           for (int i=min; i<=max; i++)
                if (a[i]==key) // key found?
                      return true; // yes, done!
           return false; // key not found
     }

   • best-case:
   • worst-case:
   • average-case:
                                                                                 7

                                                                                      7
Lecture                                                       DS & Algorithms:09

                          LINEAR SEARCH
   • One solution is linear search
      – Search each record of the file, one at a time, until finding the given Name
        and hence the corresponding telephone numbers
   • Searching an Array
       boolean find(int key, int min, int max)
     {
           for (int i=min; i<=max; i++)
                if (a[i]==key) // key found?
                      return true; // yes, done!
           return false; // key not found
     }

   • best-case: 1 comparison
   • worst-case: n comparisons
   • average-case:        n/2 comparisons

   • Just think if we have millions of records in our file
                                                                                 8

                                                                                      8
Lecture                                        DS & Algorithms:09

                       EXAMPLE CONT..
   • However, if the names are sorted alphabetically,
     then we can use an efficient algorithm called
     binary search.

   • Binary search
      – Compare the given name with the name in the
        middle of the list; this tells which half of the list
        contains the name. then compare with the name in
        the middle of the correct half to determine which
        quarter of the list contains name. continue the
        process until finding name in the list
      – The complexity of binary search is ?
          • C(n) = log2n
                                                                9

                                                                    9
Lecture                                  DS & Algorithms:09

                        EXAMPLE CONT..


   • Suppose the list contains 25000 names
      – Linear search
          • C(25000) = 25000/2 = 12500




      – Binary search
          • C(25000) =~ 15




                                                        10

                                                         10

								
To top