Top Banner
CS321 Data Structures Jan 14 2019 Lecture 1 Introduction
28

CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Aug 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

CS321 Data Structures

Jan 14 2019

Lecture 1

Introduction

Page 2: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Topics

• Expectations

• Syllabus

• Goal – program design/system architecture

• Why Data Structures?

Page 3: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Expectations

• Know Java and can write basic Java programs.

• Are comfortable looking up Java API details.

• Will independently search for solutions to basic programing questions.

• Will correct my arithmetic errors in class – I make arithmetic errors.

• Will look up material independently.

Page 4: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Key Suggestions

• You get 0 points for a blank answer. Any answer, even a disparaging comment about the class, is better than a blank answer.

• If you are going to be late on a HW or programming assignment say so in advance.

Page 5: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

What is a Data Structure?

• Array

• Linked List

• HashTable

• Tree

Page 6: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Introduction to Data Structures

• Why do we have data structures?

• ----????

Page 7: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Introduction to Data Structures

• Why do we have data structures? – To look up data later!

• All data structures are designed to really do one thing: Let us find the data we want, later, quickly and efficiently.

• All data structures trade off between: – Space: memory use.

– Time: time to add or change data.

– Complexity: complexity of the algorithm.

Page 8: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Introduction to Data Structures

• This trade off is done for one reason:

– To optimize the speed at which a data item can be retrieved when an algorithm needs that data item to solve a problem.

• The best data structure for an algorithm is the one that provides the fastest retrieval of a specific data item exactly when it is needed. All other costs being equal.

Page 9: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Introduction to Data Structures

• Good programming is about:

– Writing good Data Structures that are reliable, robust, efficient, and provide quick access to needed data to the components of the program that need that data.

– If you want to write good code write good data structures and organize them carefully.

Page 10: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Oil Rig Story

• An Oil Company needed to stop all oil rigs simultaneously and recalculate their calibration.

• This took 24 hours of computation time.

• This amounts to many millions of dollars in lost revenue.

Story is Apocryphal = fancy way of saying can not confirm.

Page 11: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Oil Rig Story

• Hired a Computer Scientist to work on this.

• He spent 3 weeks studying the software.

• --company got rather impatient since he was just sitting around.

Page 12: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Oil Rig Story

• Hired a Computer Scientist to work on this.

• He spent 3 weeks studying the software.

• Discovered it was basically 3 nested for loops.

• In the center of the for loop was a call to a long computation function.

• This computation computed: A CONSTANT!

• So run time was O(n^3 * C).

Page 13: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Oil Rig Story

• The Fix!

• Compute the CONSTANT outside of the for loops and store it in a variable for later use.

• The variable is our data structure!

• This reduced the total run time from 24 hours to 8 hours!

• The Computer Scientist enjoyed a notable bonus!

Page 14: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

So what does this mean?

• Triangle of optimization Space, Time, Complexity.

Memory Use Number of Operations

Code Complexity

Increasing

Page 15: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Sorting Data Structures Compared Method/Structure Space Complexity Time

Insertion Sort/Array O(n) Simple O(n2)

Quicksort/Array O(2*n) Complex O(n2) (A(n*log(n))

Heapsort/Tree O(n) Complex O(n*log(n))

Mergesort/Array O(2*n) Simple O(n*log(n))

Counting Sort/Array O(k = range of n) Simple O(n)

Page 16: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Binary Search Trees Method/Structure Space Complexity Time

Binary Search Tree O(n) Simple O(n)

AVL Tree O(n) Complex O(log(n))

Abstract Data Type

Search Tree Object Variant #1 Atree = createTree(); insertKey (int key, Data *data); deleteKey(key); Data *findKey(key); deleteTree();

Search Tree Object Variant #2 Atree = createTree(); Node *insertKey (int key, Data *data); Node *deleteKey(key); Node *findKey(key); deleteTree();

Page 17: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Abstract Data Type

• An abstract data type (ADT) is the set of minimal methods necessary to define a data type without regard to how the underlying data structure is actually implemented.

Page 18: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Abstract Data Type: Examples

Stack

• Astack = newStack();

• Astack.push(int value);

• Astack.pop(int value);

• deleteStack(astack);

• The internals of the stack could be implemented as an array, tree, linked list but the ADT does not change.

Linked List

• Llist = newList();

• Llist.append(int value);

• Llist.delete(int value);

• Llist.find(int value);

• deleteList(astack);

• This is a linked list ADT but an extremely limited one.

Page 19: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Why Does this Matter?

• It defines a standard by which data structures can be compared and analyzed.

• You can switch different implementations of a linked list without worrying about compatibility if their ADT’s are the same.

• It lets you select the specific implementation of an ADT that best matches your applications requirements: speed versus size versus complexity.

• Java has lots of these.

Page 20: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Binary Search Trees

• Both have exactly the same ADT.

• However, a BST with no balancing will have very simple code.

• A Balanced BST (AVL) will have more complex code but better run time performance.

• Both have same memory consumption.

Page 21: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Linked List

• Linked List as an Array: wastes space but simple to code and fixed size!

• Liked List using pointers: space efficient, possible to corrupt memory, or lose pointers, not a fixed size.

Page 22: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Stacks?

• Stack as a linked list?

• Stack implemented using an array?

Page 23: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

How do we select a Data Structure?

• Does its ADT have the methods we need?

• How much space does it use?

• What is its worst and best case performance?

• How complex is the code to implement it?

Page 24: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Coming Up

• First assignment comes out on Wednesday.

• Slides and schedule will be posted by Wednesday’s class.

Page 25: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Expansion

• M1=(A12-A22)(B21-B22)=S1 * S2

• M2=(A11+A22)(B11+B22)=S3 * S4

• M3=(A11-A21)(B11+B12)=S5 * S6

• M4=(A11+A12)B22=S7 * B22

• M5=A11(B12-B22)=A11 * S8

• M6=A22(B21-B11)=A22 * S9

• M7=(A21+A22)B11=S10 * B11

• Now Have 10 Matrix Additions = O(N2)

• But now we do 7 Multiplications instead of 8.

• So for sufficiently large N Matrix Multiplication = O(Nlg7)

Page 26: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

What is Big O notation?

• A way to approximately count algorithm complexity.

• A way to describe the worst case running time of algorithms.

• A tool to help improve algorithm performance.

• Can be used to count operations and memory usage.

Page 27: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

Bounds on Operations

• An algorithm takes some number of steps to complete:

• a + b is a single operation, takes 1 op.

• Adding up N numbers takes N steps.

• O(1) means ‘on order of 1’ operation.

• O( c ) means ‘on order of constant’.

• O( n) means ‘ on order of N steps’.

• O( n2) means ‘ on order of N*N steps’.

Page 28: CS321 Intro to Data Structurescs.boisestate.edu/~scutchin/cs321/lectures/0114_lecture1_jan14.pdf · Introduction to Data Structures •This trade off is done for one reason: –To

O(n) times for sorting algorithms.

Technique O(n) operations O(n) memory use

Insertion Sort O(N2) O( 1 )

Bubble Sort O(N2) O(1)

Merge Sort N * log(N) O(N)

Heap Sort N * log(N) O(1)

Quicksort O(N2) O(N)