welcome to csl 201 – data structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf ·...

46
Welcome to CSL 201 – Data Structures Narayanan (CK) C Krishnan Department of Computer Science and Engineering IIT Ropar

Upload: others

Post on 04-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Welcome to CSL 201 – Data Structures

Narayanan (CK) C Krishnan Department of Computer Science and Engineering IIT Ropar

Page 2: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Administrative Trivia q  Course Structure

n  3-0-4 (5 credits)

q  Class Timings n  Monday 9.55-10.45 n  Tuesday 10.50-11.40 n  Wednesday 11.45-12.35

q  Lab hours n  Thursday 9.00-12.35

q  Teaching Assistant n  Yayati Gupta

([email protected])

q  Office Hours n  Instructor – M,T,W

immediately after class n  TA – during lab hours

q  Course Google group – [email protected]

q  Pre-registered students have already been added n  other students – send

email to the instructor

q  Pseudonym n  5 character n  July 31, 5.00pm

2

Page 3: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Reference Material

3

Data Structures and Algorithms in Java Goodrich, Tamasia and Goldwasser 6th edition

Introduction to Algorithms Cormen, Leiserson, Rivest, Stein 3rd edition

Fundamentals of Data Structures Horowitz Sahni

Page 4: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Learning every week

4

Page 5: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Tentative Course Schedule

5

Page 6: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Quizzes – 30% q  Almost every alternate

Thursday n  9.00-10.30am n  L2

q  Q3 and Q6 will also have programming problems

q  Cover material discussed from the previous quiz till the current week

q  Duration – 30-45m q  Rest of the time –

problem solving/tutorial 6

Quiz Number Date

Q1 Aug 6

Q2 Aug 20

Q3 Sep 3

Q4 Oct 1

Q5 Oct 15

Q6 Oct 29

Q7 Nov 12

Page 7: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Labs – 20% q  Due almost every

alternate Friday 11.55pm q  Programming

Assignments q  TA is available for any

assistance. q  Java – programming

language n  Introductory Tutorial -

July 30 9-10.30am in L2

Lab Number Date

L1 Aug 14

L2 Aug 28

L3 Sep 4

L4 Sep 24

L5 Oct 8

L6 Oct 22

L7 Nov 5

L8 Nov 19

7

Page 8: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Grading Scheme q  Tentative Breakup

n  Quizzes (6-7) 30% n  Labs (6-8) – 20% n  Mid-Semester Exam – 25% n  End-Semester Exam – 25%

8

Passing Criteria: A student has to secure an overall score of 40(out of 100) and a combined score of 60(out of 200) in the exams to pass the course

Page 9: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Attendance – Bonus 1% q  No mandatory attendance q  Encourage you to attend classes q  Attendance will be taken during every

class q  1% will count towards the final score

n  Borderline grades will have an edge

9

Page 10: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Honor Code q  Encourage to form study groups to review

class lectures/textbook material n  Group discussions are encouraged

q  Unless explicitly stated all quizzes and labs n  are to be done individually. n  web trawled code or code copied from classmates

is strictly forbidden.

q  Any infraction will be dealt in severest terms n  Direct ‘F’ grade in the course.

10

I reserve the right to question you, if I suspect any misconduct in any submission.

Page 11: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Course Website q  http://cse.iitrpr.ac.in/ckn/courses/f2015/

csl201/csl201.html

q  All class related material will be accessible from the course webpage.

11

Page 12: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Data Structures q  What are data structures?

12

Page 13: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Data Structures q  What are data structures?

n  a way to organize data n  easier access/manipulate to data

q  Topics for the next couple of weeks n  Concrete Data Structures

w Arrays and Linked Lists

n  Algorithmic Analysis n  Recursion

13

Page 14: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays 14

Arrays

© 2014 Goodrich, Tamassia, Goldwasser

Page 15: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Java Example: Game Entries q  A game entry stores the name of a player and her best score so far in a game

© 2014 Goodrich, Tamassia, Goldwasser Arrays 15

Page 16: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Java Example: Scoreboard

© 2014 Goodrich, Tamassia, Goldwasser Arrays 16

q  Keep track of players and their best scores in an array, board n  The elements of board are objects of class GameEntry

n  Array board is sorted by score

Page 17: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays 17

Array Definition q  An array is a sequenced collection of

variables all of the same type. Each variable, or cell, in an array has an index, which uniquely refers to the value stored in that cell. The cells of an array, A, are numbered 0, 1, 2, and so on.

q  Each value stored in an array is often called an element of that array.

A 0 1 2 n i

© 2014 Goodrich, Tamassia, Goldwasser

Page 18: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays 18

Array Length and Capacity q  Since the length of an array determines the

maximum number of things that can be stored in the array, we will sometimes refer to the length of an array as its capacity.

q  In Java, the length of an array named a can be accessed using the syntax a.length. Thus, the cells of an array, a, are numbered 0, 1, 2, and so on, up through a.length−1, and the cell with index k can be accessed with syntax a[k].

a 0 1 2 n k

© 2014 Goodrich, Tamassia, Goldwasser

Page 19: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Declaring Arrays (first way) q  The first way to create an array is to use an

assignment to a literal form when initially declaring the array, using a syntax as:

q  The elementType can be any Java base type or class name, and arrayName can be any valid Java identifier. The initial values must be of the same type as the array.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 19

Page 20: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Declaring Arrays (second way) q  The second way to create an array is to use

the new operator. n  However, because an array is not an instance of a

class, we do not use a typical constructor. Instead we use the syntax:

new elementType[length] q  length is a positive integer denoting the length

of the new array. q  The new operator returns a reference to the

new array, and typically this would be assigned to an array variable.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 20

Page 21: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays of Characters or Object References q  An array can store primitive elements, such as

characters.

q  An array can also store references to objects.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 21

Page 22: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays 22

Adding an Entry q  To add an entry e into array board at index i, we

need to make room for it by shifting forward the n - i entries board[i], …, board[n – 1]

board 0 1 2 n i

board 0 1 2 n i

0 1 2 n e i

© 2014 Goodrich, Tamassia, Goldwasser

board

Page 23: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 23

Page 24: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Arrays 24

Removing an Entry q  To remove the entry e at index i, we need to fill the hole

left by e by shifting backward the n - i - 1 elements board[i + 1], …, board[n – 1]

0 1 2 n i

0 1 2 n e i

0 1 2 n i

© 2014 Goodrich, Tamassia, Goldwasser

board

board

board

Page 25: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 25

Page 26: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Sorting an Array – Insertion Sort

26 Arrays

Code Fragment 3.6

Page 27: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Insertion Sort - Example

Arrays 27

insert

insert

insert

0

0

0

0

0

0

0

0

00

0 0

Done!

0

C E H G F

B C A E H G FD

B E H G FC D

A H G FB C D E

A FB C D E H

A G FB C D E H

E H G FDCBB E

C

F

F

G

H G FDC

A FB C D E H

C D E HG

BA

D

A B C D E G H A B C

A

G

H

E

A

D

D E HG

HGFED

A

C

B

B A

2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 7

1 2 3 4 5 6 71 2 3 4 5 6 7

1

1

2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7

cur

1 2 3 4 5 6 7

move movemove

no move

no move

no move

no move

move no move

move move no move

Figure 3.5

Page 28: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Drawbacks of Arrays q  Fixed capacity q  Insertions and Deletions involve many

shifting operations

Arrays 28

Page 29: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 29

Singly Linked Lists

Page 30: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 30

Singly Linked List q  A singly linked list is a

concrete data structure consisting of a sequence of nodes, starting from a head pointer

q  Each node stores n  element n  link to the next node

next

element node

A B C D

head

© 2014 Goodrich, Tamassia, Goldwasser

Page 31: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 31

A Nested Node Class

© 2014 Goodrich, Tamassia, Goldwasser

Page 32: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 32

Accessor Methods

© 2014 Goodrich, Tamassia, Goldwasser

Page 33: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 33

Inserting at the Head •  Allocate new

node •  Insert new

element •  Have new

node point to old head

•  Update head to point to new node

© 2014 Goodrich, Tamassia, Goldwasser

Page 34: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Inserting at the Tail •  Allocate a new node

•  Insert new element

•  Have new node point to null

•  Have old last node point to new node

•  Update tail to point to new node

Singly Linked Lists 34 © 2014 Goodrich, Tamassia, Goldwasser

Page 35: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 35

Java Methods

© 2014 Goodrich, Tamassia, Goldwasser

Page 36: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Removing at the Head

•  Update head to point to next node in the list

•  Allow garbage collector to reclaim the former first node

Singly Linked Lists 36 © 2014 Goodrich, Tamassia, Goldwasser

Page 37: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Singly Linked Lists 37

Java Method

© 2014 Goodrich, Tamassia, Goldwasser

Page 38: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Removing at the Tail

•  Removing at the tail of a singly linked list is not efficient!

•  There is no constant-time way to update the tail to point to the previous node

Singly Linked Lists 38 © 2014 Goodrich, Tamassia, Goldwasser

Page 39: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly Linked Lists 39

Doubly Linked Lists

Page 40: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly Linked Lists 40

Doubly Linked List q  A doubly linked list can be traversed

forward and backward q  Nodes store:

n  element n  link to the previous node n  link to the next node

q  Special trailer and header nodes

prev next

element

trailer header nodes/positions

elements

node

© 2014 Goodrich, Tamassia, Goldwasser

Page 41: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly Linked Lists 41

Insertion q  Insert a new node, q, between p and its successor.

A B X C

A B C

p

A B C

p

X

q

p q

© 2014 Goodrich, Tamassia, Goldwasser

Page 42: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly Linked Lists 42

Deletion q  Remove a node, p, from a doubly linked list.

A B C D

p

A B C

D

p

A B C © 2014 Goodrich, Tamassia, Goldwasser

Page 43: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly-Linked List in Java

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 43

Page 44: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly-Linked List in Java, 2

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 44

Page 45: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly-Linked List in Java, 3

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 45

Page 46: Welcome to CSL 201 – Data Structurescse.iitrpr.ac.in/ckn/courses/f2015/csl201/w1.pdf · 2017-03-21 · Data Structures ! What are data structures? " a way to organize data " easier

Doubly-Linked List in Java, 4

© 2014 Goodrich, Tamassia, Goldwasser Doubly Linked Lists 46