data structures & algorithms

19
Lec#1 Data Structures and Algorithms

Upload: ain-ul-moiz-khawaja

Post on 30-Nov-2014

658 views

Category:

Education


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Data Structures & Algorithms

Lec#1

Data Structures andAlgorithms

Page 2: Data Structures & Algorithms

What is a data structure?

Data structures are used to store data in a computer in an organized fashion.

The scheme of organizing related information that considers not only the items stored, but also their relationship to each other .

It is a group of data elements grouped together under one name. - These data elements are called members. They can have different types and different lengths.- Some of them store the data of same type while others store different types of data.

In programming the term data structure refers to a scheme for organizing related piece of information.

Data Structure = Organized Data + Allowed Operations.2

Page 3: Data Structures & Algorithms

Goals of this Course

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

2. Learn the commonly used data structures. These form a programmer's basic data structure

``toolkit.'‘

3. Understand how to measure the cost of a data structure or program. These techniques also allow you to judge the merits

of new data structures that you or others might invent.

Page 4: Data Structures & Algorithms

Areas in which data structures are applied

Compiler Design Operating System Database Management System Statistical analysis package Numerical Analysis Graphics Artificial Intelligence

4

Page 5: Data Structures & Algorithms

The Need for Data StructuresData structures organize data

more efficient programs.

More powerful computers

more complex applications.

More complex applications demand more calculations.

Complex computing tasks are unlike our everyday experience.

Page 6: Data Structures & Algorithms

Organizing DataAny organization for a collection of

records can be searched, processed in any order, or modified.

The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.

Page 7: Data Structures & Algorithms

EfficiencyA solution is said to be efficient if it solves the

problem within its resource constraints.SpaceTime

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

Page 8: Data Structures & Algorithms

Selecting a Data StructureSelect a data structure as follows:

1. Analyze the problem to determine the basic operations that must be supported.

2. Quantify the resource constraints for each operation.

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

Page 9: Data Structures & Algorithms

Costs and BenefitsEach data structure has costs and benefits.Rarely is one data structure better than another in all

situations.Any data structure requires:

space for each data item it stores,time to perform each basic operation,programming effort.

Page 10: Data Structures & Algorithms

Costs and Benefits (cont)Each problem has constraints on available

space and time.Only after a careful analysis of problem

characteristics we can know the best data structure for the task.

Bank example:Start account: a few minutesTransactions: a few secondsClose account: overnight

Page 11: Data Structures & Algorithms

Example 1.2Problem: Create a database containing information

about cities and towns.Tasks: Find by name or attribute or locationExact match, range query, spatial queryResource requirements: Times can be from a few

seconds for simple queries to a minute or two for complex queries

Page 12: Data Structures & Algorithms

Abstract Data TypesAbstract Data Type (ADT): defining a data type

in terms of a set of values and a set of operations on that data type.

Each ADT operation is defined by its inputs and outputs.

Encapsulation: Hide implementation details.

Page 13: Data Structures & Algorithms

Data StructureA data structure is the physical implementation of an

ADT.Each operation associated with the ADT is

implemented by one or more subroutines in the implementation.

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

File structure: an organization for data on peripheral storage, such as a disk drive.

Page 14: Data Structures & Algorithms

MetaphorsAn ADT manages complexity through abstraction:

metaphor.Hierarchies of labels

Ex: transistors gates CPU.

In a program, implement an ADT, then think only about the ADT, not its implementation.

Page 15: Data Structures & Algorithms

Logical vs. Physical FormData items have both a logical and a physical form.

Logical form: definition of the data item within an ADT.Ex: Integers in mathematical sense: +, -

Physical form: implementation of the data item within a data structure.Ex: 16/32 bit integers, overflow.

Page 16: Data Structures & Algorithms

Data Type

ADT:TypeOperations

Data Items: Logical Form

Data Items: Physical Form

Data Structure:Storage SpaceSubroutines

Page 17: Data Structures & Algorithms

17

Define a linear and non linear data structure

• Linear data structure: A linear data structure traverses the data elements sequentially

Only one data element can directly be reached. Ex: Arrays, Linked Lists• Non-Linear data structure: Every data item is

attached to several other data items in a way that is specific for reflecting relationships.

• The data items are not arranged in a sequential structure.

• Ex: Trees, Graphs 

Page 18: Data Structures & Algorithms

What are the types of data structures?

The types of data structure are:Arrays: A set of homogeneous values.Lists: A group of similar items with connectivity to the previous or/and next data items.Matrix: A way to store data in an organized form in the form of rows and columns. Linked list: Stored data in a linear fashion.Stack: Works in first in last out order. The element inserted first in stack is removed last

18

Page 19: Data Structures & Algorithms

Queues: First in First out order. The element inserted first is removed first.

Records: A set of fields, where each field consists of data belongs to one data type.

Trees: A data structure where the data is organized in a hierarchical structure. This type of data structure follows the sorted order of insertion, deletion and modification of data items.

Tables: Data is persisted in the form of rows and columns. These are similar to records, where the result or manipulation of data is reflected for the whole table

19