csci 125 introduction to computer science and programming ii …€¦ · introduction to computer...

21
27.07.20 10:47 CSCI 125 Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester (S2)

Upload: others

Post on 31-Jul-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

27.07.20 10:47CSCI 125 Introduction to Computer Science and

Programming II Lecture 7: Data Structure I

Jetic Gū2020 Summer Semester (S2)

Page 2: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Overview

• Focus: Data Structures

• Architecture: Linux/Unix OS

• Core Ideas:

1. Introduction to Data Structure

2. Lists

Page 3: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Why is Data Structure

Summary

P1 Introduction

Page 4: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

What is data structure• The way data is stored, utilised

• Data structures we’ve encountered so far

• Array

• String

• MyList

• The way our data is stored has direct impact on how our data can be used

Concep

t

P1 Introduction

Page 5: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Why does it matter?• Consider Accessing the -th entry in an array or linked list

• Array

• access using index array[k]

• Single step access: array + k -> fast

• Linked list

• must step through the first k – 1 nodes in a linked list

• k – 1 steps access -> slow

k

Example

P1 Introduction

Page 6: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Why does it matter?• Consider Inserting a new entry in an array or linked list

• Array: requires up to n copies, very slow

• Linked list: 1 step execution, very fast

Example

P0 XXXXXXX

P1 Introduction

before

after

0x0head

0x0head

Page 7: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Idea

• There is no perfect one-that-does all data structure

• Use the one that suits your need

• Courses to take

• CSCI 225: Data Structures and Programming

• SFU: CMPT 307, CMPT 405, advanced Data Structure and Algorithms

Concep

t

P1 Introduction

Page 8: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Idea

• This course

• List, Queue, Stack (This week)

• Basic algorithm analysis techniques and implementation (Next week)

Concep

t

P1 Introduction

Page 9: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

List

Summary

P2 List

Linked List, Array

Page 10: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Definition

• an Abstract ListLinearly ordered data entries

• Common Types

• Array

• Linked list

Concep

t

P2 List

Page 11: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Operations• Operations at the -th entry of the list include:

• Access to the object

• Erasing an object

• Insertion of a new object

• Replacement of the object

• Access to entry and

k

k − 1 k + 1

Concep

t

P2 List

Page 12: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Array Operations• Operations at the -th entry of a standard Array a include:

• Access to the object, e.g.: cout << a[k];

• Single step instruction, fast

• Replacement of the object, e.g.: a[k] = 10;

• Single step instruction, fast

• Access to entry and

• Single step instruction, fast

k

k − 1 k + 1

Concep

t

P2 List

Page 13: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Array Operations

• Erasing an object

for (int i=k; i<n; i++)

a[i] = a[i+1];

• Up to steps, slown

Concep

t

P2 List

• Insertion of a new object

for (int i=n; i>k; i--)

a[i] = a[i-1];

a[k] = newValue;

• Up to steps, slown

• Operations at the -th entry of a standard Array a include:k

Page 14: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Array Operations

• Erasing an object

for (int i=k; i<n; i++)

a[i] = a[i+1];

• Up to steps, slown

Concep

t

P2 List

• Insertion of a new object

for (int i=n; i>k; i--)

a[i] = a[i-1];

a[k] = newValue;

• Up to steps, slown

• How many steps will it take if: ?k = 0; k = n; k ≈n2

Page 15: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Linked lists• The linked list we’ve seen in class is singly linked

class MyList {

MyList *next;

MyList *last;

};

• Singly linked lists

• Doubly linked lists

Concep

t

P2 List

0x0head

0x0head

Page 16: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Linked lists Operations• Operations at the -th entry of a standard Array a include:

• Access to the object,

• k steps instruction, slower than array

• Replacement of the object

• k steps instruction, slower than array

• Access to entry and

• Single step instruction, fast

k

k − 1 k + 1

Concep

t

P2 List

Page 17: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Linked lists Operations

• Erasing an object

• k steps to find the object, slow

• 1 step to delete, fast

Concep

t

P2 List

• Insertion of a new object

• k steps to find the position, slow

• 1 step to insert, fast

• How many steps will it take if: ?k = 0; k = n; k ≈n2

Page 18: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Linked lists vs Array

• Erasing an object

• k steps to find the object, slow

• 1 step to delete, fast

Tech

nical

P0 XXXXXXX

P2 List

Accessing Erase Replace Insert

k=0 k=n k=n/2 k=0 k=n k=n/2 k=0 k=n k=n/2 k=0 k=n k=n/2

Array 1 stepfast

1 stepfast

1 stepfast

n stepslow

1 stepfast

n/2 stepslow

1 stepfast

1 stepfast

1 stepfast

n stepslow

1 stepfast

n/2 stepslow

List (doubly linked)

1 stepfast

1 stepfast

n/2 stepslow

1 stepfast

1 stepfast

n/2 stepslow

1 stepfast

1 stepfast

n/2 stepslow

1 stepfast

1 stepfast

n/2 stepslow

Page 19: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Other operations• Concatenating two lists:

• Linked list: can be done in 1 step

• Array: n steps

• Memory allocation

• Linked list: n steps

• Array: 1 step (allocation of an entire chunk of memory is very fast)

Concep

t

P2 List

Page 20: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

How to choose a DS?

• Application dependent

• e.g. always appending and prepending: linked list

• e.g. loads of random indexed access: array

• Interviewing for a job

• "Design an algorithm to do this..." = "Choose a data structure to do this..."

Concep

t

P2 List

Page 21: CSCI 125 Introduction to Computer Science and Programming II …€¦ · Introduction to Computer Science and Programming II Lecture 7: Data Structure I Jetic Gū 2020 Summer Semester

Review: List

• Basic Operations

• Linked list (singly linked; doubly linked), and Array

• Operations and Runtime

• Choosing DS for your application is important!

Future

P2 List