Chapter 1

11
Chapter 1 Chapter 1 Data Structures and Data Structures and Algorithms Algorithms

description

Chapter 1. Data Structures and Algorithms. Primary Goals. Present commonly used data structures Introduce the idea of tradeoffs; reinforce the concept of costs and benefits associated with every data structure Measure the effectiveness of a data structure or algorithm. - PowerPoint PPT Presentation

Transcript of Chapter 1

Page 1: Chapter 1

Chapter 1Chapter 1

Data Structures and Data Structures and AlgorithmsAlgorithms

Page 2: Chapter 1

Primary GoalsPrimary Goals

Present commonly used data Present commonly used data structuresstructures

Introduce the idea of tradeoffs; Introduce the idea of tradeoffs; reinforce the concept of costs and reinforce the concept of costs and benefits associated with every data benefits associated with every data structurestructure

Measure the effectiveness of a data Measure the effectiveness of a data structure or algorithmstructure or algorithm

Page 3: Chapter 1

Computer Programming Computer Programming GoalsGoals

There are two, sometime There are two, sometime conflicting:conflicting:

1.1. To design an algorithm that is easy to To design an algorithm that is easy to understand, code and debugunderstand, code and debug

2.2. To design an algorithm that make To design an algorithm that make efficient use of the computer’s efficient use of the computer’s resources.resources.

This book is mainly concerned with This book is mainly concerned with goal number 2.goal number 2.

Page 4: Chapter 1

What is a data structure?What is a data structure?

In the most general, a data structure In the most general, a data structure is any data representation and its is any data representation and its associated operations.associated operations.

More typically, a data structure is More typically, a data structure is meant to be an organization or meant to be an organization or structuring for a collection of data structuring for a collection of data items.items.

Page 5: Chapter 1

Efficiency and CostEfficiency and Cost

A solution to a problem is A solution to a problem is efficientefficient if if it solves the problem within the it solves the problem within the required resource constraintsrequired resource constraints Examples: total space to store data, time Examples: total space to store data, time

allowed to performallowed to perform The The costcost of a solution is the amount of of a solution is the amount of

resources that the solution consumes.resources that the solution consumes. Often the cost is measured in terms of one Often the cost is measured in terms of one

key resourcekey resource

Page 6: Chapter 1

Efficiency and CostEfficiency and Cost

A solution can also be efficient if it A solution can also be efficient if it requires fewer resources than other requires fewer resources than other know solutionsknow solutions

When solving problems, you will want When solving problems, you will want to analyze the different data structures to analyze the different data structures available and choose accordingly.available and choose accordingly.

You want to make sure you choose both You want to make sure you choose both efficient and cost effective solutionsefficient and cost effective solutions

Conversely, you don’t need a sledge Conversely, you don’t need a sledge hammer to put in a thumbtackhammer to put in a thumbtack

Page 7: Chapter 1

Choosing your Data Choosing your Data StructureStructure

You should follow these stepsYou should follow these steps1.1. Analyze your problem to determine Analyze your problem to determine

the resource constraints that any the resource constraints that any solution must meetsolution must meet

2.2. Determine the basic operations that Determine the basic operations that must be supported and quantify the must be supported and quantify the resource constraints for each resource constraints for each operationoperation

3.3. Select the data structure that best Select the data structure that best meets these requirementsmeets these requirements

Page 8: Chapter 1

Operational QuestionsOperational Questions

When choosing data structures, here When choosing data structures, here are some questions to ask.are some questions to ask. Are all data items inserted into the data Are all data items inserted into the data

structure at the beginning, or are the structure at the beginning, or are the insertions interspersed with other insertions interspersed with other operations?operations?

Can data items be deleted?Can data items be deleted? Are all data items process in some well-Are all data items process in some well-

defined order, or is search for specific defined order, or is search for specific data items allowed?data items allowed?

Page 9: Chapter 1

Abstract Data Types and Abstract Data Types and Data StructuresData Structures

TypeType a collection of values a collection of values A A simple typesimple type has no subparts has no subparts An An aggregate typeaggregate type or or composite typecomposite type

contains several pieces of informationcontains several pieces of information A A data itemdata item is a piece of information or a is a piece of information or a

record whose value is drawn from a type.record whose value is drawn from a type. It is said to be a It is said to be a membermember of a type of a type

A A data typedata type is a type together with a is a type together with a collection of operations to manipulate the collection of operations to manipulate the type.type.

Page 10: Chapter 1

Abstract Data Types and Abstract Data Types and Data StructuresData Structures

There is a distinction between the logical There is a distinction between the logical concept of a data type and its physical concept of a data type and its physical implementationimplementation Example: Trees can be implemented both in a Example: Trees can be implemented both in a

linked fashion or an array based fashionlinked fashion or an array based fashion An An Abstract Data Type (ADT)Abstract Data Type (ADT) is the is the

realization of a data type as a software realization of a data type as a software component.component.

An ADT does not specify how the data An ADT does not specify how the data type is implementedtype is implemented

Page 11: Chapter 1

Abstract Data Types and Abstract Data Types and Data StructuresData Structures

A A data structuredata structure is the implementation is the implementation for an ADTfor an ADT

ADT’s allow programmers to manage ADT’s allow programmers to manage complexity through abstractioncomplexity through abstraction

Data items have both a logical and a Data items have both a logical and a physical formphysical form The definition of the data item in terms of an The definition of the data item in terms of an

ADT is the logical formADT is the logical form The implementation of the data item within The implementation of the data item within

the data structure is its physical formthe data structure is its physical form