1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md....

13
1 Introduction to Data Structures

Transcript of 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md....

Page 1: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

1

Introduction to Data Structures

Page 2: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

2

Course Name : Data Structure (CSI 221)

Course Teacher : Md. Zakir Hossain

Lecturer, Dept. of Computer Science

Stamford University Bangladesh

Class Schedule : Tuesday : 11:45 am - 1:00 pm Thursday : 1:00 pm – 2.15 pm

Marks Distribution : Attendance - 10%

Assignment – (5%+5%)

Class Test - (10% +10%)

Midterm - 30%

Final - 30%

---------------------------------

Total - 100%

Page 3: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

3

Course Outline

1. Concepts and examples2. Elementary data objects3. Elementary data structures4. Arrays5. Lists6. Stacks7. Queues8. Graphs9. Trees10.Sorting and searching11.Hash techniques

Page 4: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

4

1. “Theory and Problems of Data Structures” by Seymour Lipschutz, McGraw-Hill, ISBN 0-07-038001-5.

Books

Page 5: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

Elementary Data Organization

• Data are simply values or sets of values.

• Collection of data are frequently organized into a hierarchy of fields, records and files.

• This organization of data may not complex enough to maintain and efficiently process certain collections of data.

• For this reason, data are organized into more complex type of structures called Data Structures.

Page 6: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

6

Data Structures

Data Structures

The logical or mathematical model of a particular organization of data is called a data

structure.

Types of Data Structure

1. Linear Data Structure

Example: Arrays, Linked Lists, Stacks, Queues

2. Nonlinear Data Structure

Example: Trees, Graphs A

B C

D E F

A B C D E F

Figure: Linear and nonlinear structures

Array Tree

Page 7: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

7

Choice of Data Structures

The choice of data structures depends on two considerations:

1. It must be rich enough in structure to mirror the actual relationships of data in the

real world.

2. The structure should be simple enough that one can effectively process data when

necessary.

1020304050607080

10

20 30

40 50 60 70

Figure 2: Array with 8 items Figure 3: Tree with 8 nodes

Page 8: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

8

Data Structure Operations

1. Traversing: Accessing each record exactly once so that certain items in

the record may be processed.

2. Searching: Finding the location of the record with a given key value.

3. Inserting: Adding a new record to the structure.

4. Deleting: Removing a record from the structure.

5. Sorting: Arranging the records in some logical order.

6. Merging: Combing the records in two different sorted files into a single

sorted file.

Page 9: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

9

Algorithms

It is a well-defined set of instructions used to solve a particular problem.

Example:

Write an algorithm for finding the location of the largest element of an array Data.

Largest-Item (Data, N, Loc)

1. set k:=1, Loc:=1 and Max:=Data[1]

2. while k<=N repeat steps 3, 4

3. If Max < Data[k] then Set Loc:=k and Max:=Data[k]

4. Set k:=k+1

5. write: Max and Loc

6. exit

Page 10: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

10

Complexity of Algorithms

• The complexity of an algorithm M is the function

f(n) which gives the running time and/or storage

space requirement of the algorithm in terms of the

size n of the input data.

• Two types of complexity

1. Time Complexity

2. Space Complexity

Page 11: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

11

O-notation

- A function f(n)=O(g(n)) if there are positive constants c and n0 such that

f(n)<=c.g(n) for all n>= n0.

- When f(n)=O(g(n)), it is guaranteed that f(n) grows at a rate no faster than g(n).

So g(n) is an upper bound on f(n).

Example:

(a) f(n) = 3n+2

Here f(n) <= 5n for n>=1

So, f(n) = O(n).

(b) f(n) = 3n2-2

Here f(n) < 3n2 for n>=1

So, f(n) = O(n2).

Page 12: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

12

Rule-1

If fa(n) = O(ga(n)) and fb(n) = O((gb(n)) then

(a) fa(n)+fb(n) = max(O(ga(n)),O(gb(n))

(b) fa(n) * fb(n) = O(ga(n) * gb(n))

Rule-2

If f(n) is a polynomial of degree k, then f(n) = Θ(nk).

Rule-3

Logkn = O(n) for any constant.

Some rules related to asymptotic notation

Page 13: 1 Introduction to Data Structures. 2 Course Name : Data Structure (CSI 221) Course Teacher : Md. Zakir Hossain Lecturer, Dept. of Computer Science Stamford.

Function Name

c Constant

logn Logarithmic

log2n Log-squared

n Linear

nlogn

n2 Quadratic

n3 Cubic

2n Exponential

13

Typical Growth Rates