Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure and Algorithms

Similar presentations


Presentation on theme: "Data Structure and Algorithms"— Presentation transcript:

1 Data Structure and Algorithms
Topic 1 Introduction and Overview

2 Basic Terminology; Elementary Data Organization
Data – values/set of values. Data item – single unit of values. Group items – divided into subitems. Elementary items – not divided. E.g., employee name may be divided into three subitems – first name, middle initial and last name – social security number – single item.

3 Basic Terminology; Elementary Data Organization
Entity – something that has attributes/properties which may be assigned values. Values may be numeric/nonnumeric. Entities with similar attributes (e.g., all employees in organization) form entity set. Attributes: Name Age Sex Social Security Number Values: Rumaisya, Saifi 34 F

4 Basic Terminology; Elementary Data Organization
Each attribute of entity set has range of values – set of all possible values that could be assigned to the particular attribute. Term “information” sometimes used for data with given attributes, or, in other words, meaningful or processed data.

5 Basic Terminology; Elementary Data Organization
Field – single elementary unit of information representing entity attribute. Record – collection of field values of an entity. File – collection of entities records in an entity set.

6 Basic Terminology; Elementary Data Organization
Each record in file may contain many field items, but the value in a certain field may uniquely determine the record in the file. Such a field K is called a primary key, and values k1, k2, … in such a field are called keys or key values.

7 Basic Terminology; Elementary Data Organization
Example: Automobile dealership inventory file, each record contains: Serial Number, Type, Year, Price, Accessories Primary key? Since each automobile has unique serial number

8 Basic Terminology; Elementary Data Organization
Example: Organization membership file, each record contains: Name, Address, Telephone Number, Dues Owed Group items – Name, Address Primary key – Name Address and Telephone Number may not serve as primary key

9 Basic Terminology; Elementary Data Organization
Records may also be classified according to length. File can have: Fixed-length records – all records contain same data items with same amount of space Variable-length records – different lengths of records. E.g., student records since different students take different number of courses. Usually have minimum and maximum length.

10 Data Structures Data structure – logical or mathematical model of a particular organization of data. Choice of data model depends on: Richness in structure to mirror actual relationships of data in real world Simplicity, one can effectively process data when necessary

11 Classification of Data Structures

12 Arrays Linear (or dimensional) array
List of finite number n of similar data elements referenced by a set of n consecutive numbers, usually 1, 2, 3, …, n. Array name A, elements of A denoted by subscript a1, a2, a3, …, an A(1), A(2), A(3), …, A(N) A[1], A[2], A[3], …, A[N]

13 Arrays Number K in A[K] is called subscript
A[K] is called subscripted variable Linear arrays are called one-dimensional arrays because each element in array is referenced by one subscript.

14 Arrays List weekly sales can be stored in two-dimensional array, first subscript denotes store and second subscript the department. SALES is the name given to the array

15 Linked List Brokerage firm

16 Linked List Integer used as a pointer requires less space than a name

17 Linked List Suppose the firm wants the list of customers for a given salesperson Using Fig. 1.5, the firm would have to search through the entire customer file

18 Linked List Disadvantage: each salesperson have many pointers and the set of pointers change as customers are added and deleted.

19 Linked List

20 Trees Rooted tree graph/tree – data contain hierarchical relationship between elements

21 Trees Example Record Structure:
Employee personnel record contain data items: Social Security Number, Name, Address, Age, Salary, Dependents Group item Subitem Name Last, First, MI (middle initial) Address Street, Area Area City, State, ZIP code number

22

23 Trees Example Algebraic Expression: (2x + y)(a – 7b)3
Exponentiation – vertical arrow (↑) Multiplication – asterisk (*)

24 Trees Exponentiation take place after subtraction, multiplication at top of tree executed last.

25 Stack Also called last-in first-out (LIFO) system – linear list which insertions and deletions take place only at one end, called top New dishes inserted at top of stack and deleted only from top of stack

26 Queue Also called first-in first out (FIFO) system – linear list which deletions take place only at one end of list, “front” of list, and insertions take place only at other end of list, “rear” Line of people waiting at bus stop

27 Queue First person in line – first person to board the bus
Automobiles waiting to pass through an intersection – first car in line – first car through

28 Graph Data contain relationship between pairs of elements which is not hierarchical E.g., airline flies only between cities connected by lines

29 Data Structure “record” – files “node” – linked list, trees and graphs

30 Data Structure Operations
Data structure that one chooses depends on frequency with which specific operations are performed

31 Data Structure Operations
Four operations: Traversing: Accessing each record exactly once so that items in record may be processed (sometimes called “visiting”) Searching: Finding location of record with given key value/finding locations of all records which satisfy one/more conditions Inserting: adding new record Deleting: removing a record

32 Data Structure Operations
Two/more operations used; e.g., delete record with given key value, mean first need to search for location of record

33 Data Structure Operations
Two operations: Sorting: arranging records in logical order (e.g., alphabetically according to NAME key/numerical order according to NUMBER key, such as social security/account number) Merging: combining records in two different sorted files into single

34 Abstract Data Types A set of data values and associated operations that are specified accurately, independent of any particular implementation. Know what a specific data type can do, but how it actually does it is hidden. Consists of a set of definitions that allow us to use the functions while hiding the implementation.

35 Abstract Data Types List L consisting of data items – 1, 2, 3, 4, 5, 6, 7, 8, 9 as shown in (a). Can use any of four data structures to support L – a linear list, a matrix, a tree, or a graph, as given in (b), (c) and (d) respectively.

36 Abstract Data Types

37 Abstract Data Types The users should not be aware of the structure that we use, i.e., whether it is a tree, or graph or something else. As long as they are able to insert and retrieve data, it does not make a difference as to how we store the data.

38 Abstract Data Types A shop maintains the list of customers, sales assistants and the average transactions on a day as given in Fig 1.12. Need to write a program, which will determine the number of sales assistants required to serve customers efficiently. Need to simulate the waiting line in the shop.

39 Abstract Data Types This analysis will require the simulation of a queue. Need some basic queue operations such as enqueuing and dequeuing (insertion and deletion).

40 Abstract Data Types A data declaration packaged together with the operations that are meaningful for the data type. Encapsulate the data and the operations on the data, and then hide from the user. The user need not know the data structure to use the ADT.

41 Abstract Data Type Model

42 Abstract Data Type Model
Two different parts of ADT model – functions (public and private) and data structures. Do not come within scope of application program. Data structures are available to all ADTs functions, and function may call on any other function to accomplish its task.

43 Abstract Data Type Model
Data structures and functions are within scope of each other. Data are entered, accessed, modified and deleted through the external application programming interface. Can only access the public functions.

44 Abstract Data Type Model
For each ADT operation, there is an algorithm that performs its specific task. The operation name and parameters (the only interface) are available to the application. Two basic structures, namely array and linked list, can be used to implement an ADT list.

45 Algorithms: Complexity, Time-Space Tradeoff
Well-defined list of steps for solving a particular problem. One major purpose is to develop efficient algorithms for processing data. Two major measures are time and space.

46 Algorithms: Complexity, Time-Space Tradeoff
Complexity of an algorithm is the function which gives the running time and/or space in terms of input size. Increasing the amount of space for storing data, may be able to reduce the time needed for processing the data, or vice versa.

47 Searching Algorithms Consider a membership file, each record contains, among other data, the name and telephone number. Given the name of a member and want to find his or her telephone number.

48 Linear Search Search each record of file, one at a time, until finding the given Name and the corresponding telephone number. The time required to execute the algorithm is proportional to the number of comparisons.

49 Linear Search The average number of comparisons for a file with n records is equal to n/2; that is, the complexity of the linear search algorithm is given by C(n) = n/2. Impossible in practice if searching through a list consisting of thousands of names, as in a telephone book.

50 Binary Search Compare the given Name with the name in the middle of the list; this tells which half of the list contains Name. Then compare Name 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.

51 Binary Search Complexity of the binary search algorithm is given by
C(n) = log2 n Will not require more than 15 comparisons to find a given Name in a list containing names. Binary search algorithm has some drawbacks. The list must be stored in some type of array.

52 Binary Search Inserting an element in an array requires elements to be moved down the list, and deleting an element from an array requires element to be moved up the list. Telephone company printing a new directory (updates) every year while keeping a separate temporary file for new telephone customers.

53 Binary Search A bank may want to insert a new customer in its file almost instantaneously. A linearly sorted list may not be the best data structure for a bank.

54 Time-Space Tradeoff Given only the social security number of the person. Do a linear search for the record, which is extremely time-consuming for a very large number of records. How can we solve such a problem? One way is to have another file which is sorted numerically according to social security number.

55 Time-Space Tradeoff Would double the space required for storing data.
Another way, the main file sorted numerically by social security number and to have an auxiliary array with only two columns containing: Alphabetized list of the names Pointers which give the locations of the corresponding records in the main file.

56 Time-Space Tradeoff Since the additional space, containing only two columns, is minimal for the amount of extra information it provides.

57 Time-Space Tradeoff To minimize the movement of data is to have the social security number serve as the address of each record. No movement of data when records are inserted, instant access to any record. This method of storing data require one billion (109) memory locations for only hundreds or thousands of records.

58 Time-Space Tradeoff This tradeoff of space for time is not worth the expense. An alternative method is to define a function H from the set K of key values – social security numbers – into the set L of addresses of memory cells. Such a function H is called a hashing function.


Download ppt "Data Structure and Algorithms"

Similar presentations


Ads by Google