• No results found

DSA 01.ppt

N/A
N/A
Protected

Academic year: 2020

Share "DSA 01.ppt"

Copied!
29
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structure and

Algorithm

(2)

This course

Prepares the students for (and is a prerequisite for) the more advanced material students will encounter in later courses.

Cover well-known data structures such as dynamic arrays, linked lists, stacks, queues, tree and graphs.

Implement data structures in C++

(3)

Books

(4)

Course Webpage

https://sites.google.com/site/mutishaikh

All lecture slides

Assignments

Homework's

Test

Others

(5)

Contents

Introduction and overview of Data Structure

and Algorithm

Classification of Data Structures

Arrays

Dynamic Arrays

List Data Structure

(6)

Need for Data Structures

Why we need data structure?

Data structures help us to organize the data in the computer, resulting in more efficient programs. An efficient program

executes faster and helps minimize the usage of resources like memory, disk.

Data structures organize data  more efficient programs.

More powerful computers  more complex applications.

More complex applications demand more calculations.

(7)

Organizing Data

What does organizing the data mean?

The data should be arranged in a way that it is easily accessible.

Data organization can also be for the collection of records that can

be searched, processed in any order, or modified.

Sometimes you may realize that the application is too slow and

taking more time. There are chances that it may be due to the data structure used, not due to the CPU speed and memory.

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

(8)

What is Data Structure?

Data may be organized in different ways: the logical or mathematical model for a particular organization of data

is called the data structure.

OR

A data structure is a particular way of organizing data in a computer so that it can used efficiently.

(9)

Algorithm

Algorithm is a clearly specified set of simple instructions to be followed to solve a problem.

Once an algorithm is given for a problem and decided

(somehow) to be correct, an important step is to determine how much in the way of resources, such as time or space, the algorithm will require.

An algorithm that solves a problem but requires a year is

hardly of any use. Likewise, an algorithm that requires thousands of gigabytes of main memory is not (currently) useful on most machines.

(10)

Efficiency

A solution is said to be efficient if it solves the problem within its resource constraints. A resource can be CPU, space or time.

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

(11)

Classification of Data Structure

(12)

Primitive and Non-primitive Data Types

The primitive data types are the basic data types that are available in most of the programming languages. The

primitive data types are used to represent single values.

The data types that are derived from primary data types

are known as non-Primitive data types. These data types are used to store group of values.

(13)

Linear and Non-linear Data Structure

A data structure is said to be linear if its elements form a

sequence or a linear list. In linear data structures, the data is arranged in a linear fashion through the way they are stored in memory may not be sequential.

A data structure is said to be non-linear if the data is not arranged in a sequence. The insertion and deletion of data is there not possible in a linear fashion

(14)

Selecting a Data Structure

Select a data structure as follows:

1.

Analyze the problem to determine the resource

constraints a solution must meet. For example, the available disk space.

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.

(15)

Data Structure Philosophy

Each data structure has costs and benefits.

Rarely is one data structure better than another in all situations.

A data structure requires:

space for each data item it stores,

time to perform each basic operation,programming effort.

(16)

Arrays

Elementary data structure that exists as built-in

in most programming languages.

main( int argc, char** argv ) {

int x[6]; int j;

for(j=0; j < 6; j++)

x[j] = 2*j; }

(17)

Arrays

Array declaration: int x[6];

An array is collection of cells of the same type.

The collection has the name

x

.

The cells are numbered with consecutive

integers.

To access a cell, use the array name and an

index:

x[0], x[1], x[2], x[3], x[4], x[5]

(18)

Array Layout

x[1] x[2] x[3] x[4] x[5] x[0]

Array cells are

contiguous in

computer memory

The memory can be

thought of as an

array

(19)

What is Array Name?

 ‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.

 For example, if we have the code

int a, b;

then we can write

b = 2; a = b; a = 5;

But we cannot write

2 = a;

(20)

Array Name

x

is not an lvalue

int x[6];

int n;

x[0] = 5;

x[1] = 2;

x = 3;

// not allowed

x = a + b;

// not allowed

x = &n;

// not allowed,

(21)

Dynamic Arrays

You would like to use an array data structure

but you do not know the size of the array at

compile time.

You find out when the program executes that

you need an integer array of size n=20.

Allocate an array using the new operator:

int* y = new int[20]; // or int* y = new int[n]

y[0] = 10;

y[1] = 15;

// use is the same

(22)

Dynamic Arrays

y

is a lvalue; it is a pointer that holds the address

of 20 consecutive cells in memory.

It can be assigned a value. The new operator

returns as address that is stored in y.

We can write:

y = &x[0];

y = x; // x can appear on the right

// y gets the address of the

// first cell of the x array

(23)

Dynamic Arrays

We must free the memory we got using the

new operator once we are done with the

y

array.

delete[ ] y;

We would not do this to the

x

array because we

did not use new to create it.

(24)

The LIST Data Structure

The List is among the most generic of data

structures.

Real life:

a.

shopping list,

b.

groceries list,

c.

list of people to invite to dinner

d.

List of presents to get

(25)

Lists

A list is collection of items that are all of the

same type (grocery items, integers, names)

The items, or elements of the list, are stored in

some particular

order

It is possible to

insert

new elements into

various positions in the list and

remove

any

element of the list

(26)

Lists

List is a set of elements in a linear order.

For example, data values a

1

, a

2

, a

3

, a

4

can be

arranged in a list:

(a

3

, a

1

, a

2

, a

4

)

In this list, a

3

, is the first element, a

1

is the

second element, and so on

The order is important here; this is not just a

random collection of elements, it is an

ordered

collection

(27)

List Operations

Useful operations

createList(): create a new list (presumably empty)copy(): set one list to be a copy of another

clear(); clear a list (remove all elements)

insert(X, ?): Insert element X at a particular position in the list

remove(?): Remove element at some position in the list

get(?): Get element at a given position

update(X, ?): replace the element at a given position with X

find(X): determine if the element X is in the listlength(): return the length of the list.

(28)

List Operations

We need to decide what is meant by

particular

position

; we have used

?

for this.

There are two possibilities:

1.

Use the actual index of element: insert after

element 3, get element number 6. This approach is taken by arrays

2.

Use a “current” marker or pointer to refer to a particular position in the list.

(29)

List Operations

If we use the

current

marker, the following four

methods would be useful:

start(): moves to “current” pointer to the very first

element.

tail(): moves to “current” pointer to the very last

element.

next(): move the current position forward one element

back(): move the current position backward one element

References

Related documents

Reachability -A node v is said to be reachable from node u, if and only if there exists a (directed) path from node u to node v. Strongly connected directed graph.. A digraph

6.7.2 The association between being bullied and perceived health status and school related factors: ...259 6.7.3 The association between being bullied and other risk behaviours:

Findings show that IT investment has a significant impact on the return on investment of the firms while there is no significant effect in terms firm size and industry.. The results

Specialization: Digital design(System design,RTL and Verification), Verilog Main business: Design, Research, Offshore projects. Description: ARF-Design works in two domains of

The two days conclave focussed on building partnership The two days conclave focussed on building partnership between State Agencies and Inter‐state agencies, it between State

Our main result is a sufficient condition on the network structure and available label information such that nLasso accurately learns a localized linear regression model from a

The Employ Florida Marketplace Document Management module features the ability for authorized staff users to upload documents and associate them with an individual or employer

Bulk Liquid - General Tanker Terminal Code Remarks.. 1 There is safe access between the tanker