25
1 CS 350 Data Structures Chaminade University of Honolulu

1 CS 350 Data Structures Chaminade University of Honolulu

Embed Size (px)

Citation preview

Page 1: 1 CS 350 Data Structures Chaminade University of Honolulu

1

CS 350Data Structures

Chaminade University of Honolulu

Page 2: 1 CS 350 Data Structures Chaminade University of Honolulu

2

Introduction Why Data Structures? What Is Data Structure? Program Efficiency Example: Banking Application Example: City Database Abstract Data Type

Page 3: 1 CS 350 Data Structures Chaminade University of Honolulu

3

Why Data Structures?

Main function of computers is to perform calculations—fast, and efficiently.

Complex jobs require intensive calculations How do you make computers work efficiently?

Hardware Software

Software solution Algorithms Algorithms depend on data structures

Page 4: 1 CS 350 Data Structures Chaminade University of Honolulu

4

What Is a Data Structure? In a general sense, any data

representation is a data structure. E.g., integer, string

Specifically, data structure is an organization for a collection of data items.

You choose appropriate data structure for efficient processing of data.

Page 5: 1 CS 350 Data Structures Chaminade University of Honolulu

5

Data Structure and Algorithm

Different data structures can be used for processing data—searching, sorting, modifying, etc.

But the choice of data structure, and accompanying algorithm can make the difference between a fast program (sec, min) and a long program (hrs, days).

Page 6: 1 CS 350 Data Structures Chaminade University of Honolulu

6

Program Efficiency

A program is said to be efficient if it solves a problem without wasting computing resources of: Space Time

The cost of a (program) solution is the amount of resources that the solution consumes.

Page 7: 1 CS 350 Data Structures Chaminade University of Honolulu

7

Example: Banking Application Typical Operations

Open accounts (far less often than access)

Close accounts (far less often than access)

Access account to Add money Access account to Withdraw money

Page 8: 1 CS 350 Data Structures Chaminade University of Honolulu

8

Banking Application (cont.) Teller and ATM transactions are

expected to take short time (minutes).

Opening or closing an account can take longer time (perhaps up to an hour).

Page 9: 1 CS 350 Data Structures Chaminade University of Honolulu

9

Banking Application (cont.) Kind of data structure for this

application: Must be highly efficient for

search of account May be less efficient for deletion

of account May be moderately efficient

for insertion of account

Page 10: 1 CS 350 Data Structures Chaminade University of Honolulu

10

Banking Application (cont.)

Hash Table Meets these requirements Allow for extremely fast records search. Also supports efficient insertion of new

records. Deletions can also be supported

efficiently. (But too many deletions lead to performance degradation–requiring the hash table to be reorganized.)

Page 11: 1 CS 350 Data Structures Chaminade University of Honolulu

11

Example: City Database Database system for city

information Search by particular value--e.g.,

ZIP code, street name, building name-- (exact match)

Search by range of values--e.g., population, income level—(range query)

Page 12: 1 CS 350 Data Structures Chaminade University of Honolulu

12

City Database (cont.) The database must answer queries

quickly For an exact-match query, fast operation

(seconds) For a range queries, the entire operation

may be allowed to take longer (minutes)

Page 13: 1 CS 350 Data Structures Chaminade University of Honolulu

13

City Database (cont.) The hash table is inappropriate because it

cannot perform efficient range queries The B+ tree supports large databases for:

Insertion Deletion Range queries

If the database is created once and then never changed, simple linear lists may be more appropriate.

Page 14: 1 CS 350 Data Structures Chaminade University of Honolulu

14

Selecting a Data Structure

Selection Criteria for a Data Structure

1. Analyze the problem to determine the resource constraints a solution must meet.

2. Determine the basic operations that must be supported. Quantify the resource constraints for each operation.

3. Select the data structure that best meets these requirements.

Page 15: 1 CS 350 Data Structures Chaminade University of Honolulu

15

Data Structure Philosophy

Each problem has constraints on available space and time.

Only after a careful analysis of problem characteristics can we know the best data structure for the task.

Bank example: Start account: a few minutes Transactions: a few seconds Close account: overnight

Page 16: 1 CS 350 Data Structures Chaminade University of Honolulu

16

Goals of this Course

Reinforce the concept that costs and benefits exist for every data structure

Choice of data structure is important for complex processing

Learn commonly used data structures Programmer's basic data structure “toolkit”

Understand how to measure the cost of a data structure or program.

Algorithm analysis

Page 17: 1 CS 350 Data Structures Chaminade University of Honolulu

17

Abstract Data Type

Abstract Data Type (ADT): specification for a data type solely in terms of a set of values a set of operations on that data type

Page 18: 1 CS 350 Data Structures Chaminade University of Honolulu

18

Abstract Data Type (cont.) Humans handle complexity in hierarchy

of abstraction E.g., dogruns, barks, eats

dogruns with legs barks with lungs, mouth eats with mouth, stomac

E.g., listinsert item, remove item, search listdata in array insert item into array remove item by overwriting use binary search algorith

Page 19: 1 CS 350 Data Structures Chaminade University of Honolulu

19

Abstract Data Type (cont.) ADT provides a set of (high-level)

operations as an interface to the outside world

Data Structure and algorithm provides the (low-level) implementation of the abstract operations

Page 20: 1 CS 350 Data Structures Chaminade University of Honolulu

20

Abstract Data Type (cont)

Information HidingPrinciple of hiding the design and implementation details of data structure

EncapsulationWay to achieve information hidng. Often used synonymously with information hiding itself.

Page 21: 1 CS 350 Data Structures Chaminade University of Honolulu

21

ADT and Data Structure

Data Structure the physical implementation of an ADT. Each operation in the ADT is implemented by one or

more subroutines in the implementation. In a OO language, an ADT and its implementation

together make up a class.

Data Structure: usually refers to an organization for data in main memory.

File Structure: an organization for data on external storage, such as a disk drive.

Page 22: 1 CS 350 Data Structures Chaminade University of Honolulu

22

Data Type

ADT:TypeOperations

Data Items: Logical Form

Data Items: Physical Form

Data Structure:Storage SpaceSubroutines

Page 23: 1 CS 350 Data Structures Chaminade University of Honolulu

23

Programs A computer program is a concrete

representation of an algorithm in some programming language.

Naturally, there are many programs that are instances of the same algorithms, since any modern programming language can be used to implement any algorithm.

Page 24: 1 CS 350 Data Structures Chaminade University of Honolulu

24

To Summarize: A problem is a function or a

mapping of inputs to outputs. An algorithm is a recipe for solving

a problem whose steps are concrete and ambiguous.

A program is an instantiation of an algorithm in a computer programming language.

Page 25: 1 CS 350 Data Structures Chaminade University of Honolulu

25

Your Turn

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

You can recruit your friend to help you.