Algorithms and Data Structures Algorithms and Data Structures

  • Slides: 109
Download presentation

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures An algorithm does something, solves some problem, in a mechanical

Algorithms and Data Structures An algorithm does something, solves some problem, in a mechanical way. It is a bit like a “machine” or a specialised tool

Algorithms and Data Structures A data structure is some way to represent and store

Algorithms and Data Structures A data structure is some way to represent and store data so that we can process that data efficiently

Algorithms and Data Structures Data structures use algorithms Algorithms use data structures

Algorithms and Data Structures Data structures use algorithms Algorithms use data structures

Algorithms and Data Structures and Algorithms a Computer Scientist’s building blocks

Algorithms and Data Structures and Algorithms a Computer Scientist’s building blocks

Algorithms and Data Structures Computer Scientist invent and study algorithms and data structures and

Algorithms and Data Structures Computer Scientist invent and study algorithms and data structures and problems

Algorithms and Data Structures Computer Scientists do other things too

Algorithms and Data Structures Computer Scientists do other things too

Our website, link from moodle

Our website, link from moodle

Lecture

Lecture

Lectures … Aye, right. Why don’t I just go away and read the book

Lectures … Aye, right. Why don’t I just go away and read the book myself? Why come to lectures? • meet new exciting people • keep in touch with fashion, trends, … • be part of the “in” crowd • ADS 2 Lecture 1 12

Great! Algorit hms & tructur es Data S D ithms Algor & ures truct

Great! Algorit hms & tructur es Data S D ithms Algor & ures truct S a t a

Lassie, what’s in this ADS 2 course ? Will it be interesting? woof!

Lassie, what’s in this ADS 2 course ? Will it be interesting? woof!

Content • linked lists • single/doubly-linked, • insert, delete, find, min, max, … •

Content • linked lists • single/doubly-linked, • insert, delete, find, min, max, … • stacks • push/pop/size • bracket matching, RP calculator • queues • enqueue/dequeue/size • circular queue (in array) • recursion • see above • big O

Continued Content • trees • n-ary trees: representations & properties • binary trees •

Continued Content • trees • n-ary trees: representations & properties • binary trees • representations & properties & traversals • binary search trees • insert/find/delete/traverse/height … • AVL trees • heap • insert/delete/sort • splay trees (maybe) • red-black trees (maybe)

Continued Content • skip lists (maybe) • Sorting • bubble/insert/sift sort • merge sort

Continued Content • skip lists (maybe) • Sorting • bubble/insert/sift sort • merge sort • quick sort • bucket sort (pigeon hole sort) • bead sort (maybe) • Dictionaries • maps and hash tables • open and closed hashing

Content • 5 pieces of practical work • 2 of above are assessed (20%)

Content • 5 pieces of practical work • 2 of above are assessed (20%) • exam in May (80%) • lab every week • woof Continued

Great! Algorit hms & tructur es Data S D ithms Algor & ures truct

Great! Algorit hms & tructur es Data S D ithms Algor & ures truct S a t a

our ADS 2 website under construction (permanently)

our ADS 2 website under construction (permanently)

Home

Home

News

News

The lectures

The lectures

The lectures

The lectures

code & api

code & api

Lots of code in there

Lots of code in there

demos

demos

Links to interesting & useful stuff

Links to interesting & useful stuff

Links to interesting & useful stuff

Links to interesting & useful stuff

exercises

exercises

Questions & Answers

Questions & Answers

… and now for something completely different!!!

… and now for something completely different!!!

Lassie, why are algorithms and data structures important?

Lassie, why are algorithms and data structures important?

Lassie, will there be anything in the course that is challenging and exciting?

Lassie, will there be anything in the course that is challenging and exciting?

I just hope I get feedback, continuously

I just hope I get feedback, continuously

Lassie, will you teach us to “THINK LIKE A COMPUTER SCIENTIST”?

Lassie, will you teach us to “THINK LIKE A COMPUTER SCIENTIST”?

Lassie, honestly, I’m not that experienced as a programmer. Will you help us with

Lassie, honestly, I’m not that experienced as a programmer. Will you help us with our programming?

Will we use BIG data sets?

Will we use BIG data sets?

woof!

woof!

1 st lecture … “Who cares? ”

1 st lecture … “Who cares? ”

algorithms are ancient Many algorithms predate computers

algorithms are ancient Many algorithms predate computers

But first … what is an algorithm?

But first … what is an algorithm?

What is an algorithm? A finite sequence of instructions, each with a clear meaning,

What is an algorithm? A finite sequence of instructions, each with a clear meaning, and can be performed with a finite amount of effort in a finite length of time [Aho, Hopcroft, Ullman 1983]

Before some of you were born

Before some of you were born

Prim’s Algorithm ADS 2 Lecture 1 49

Prim’s Algorithm ADS 2 Lecture 1 49

Abu Jafar Mohammed ibm Musa Al Khwarizmi Before I was born 800 AD

Abu Jafar Mohammed ibm Musa Al Khwarizmi Before I was born 800 AD

325 BC to 265 BC GCD Euclid of Alexandria

325 BC to 265 BC GCD Euclid of Alexandria

Euclid’s algorithm ADS 2 Lecture 1 53

Euclid’s algorithm ADS 2 Lecture 1 53

Euclid’s algorithm ADS 2 Lecture 1 54

Euclid’s algorithm ADS 2 Lecture 1 54

Euclid’s algorithm n o i t ot n s it r lgo hm …

Euclid’s algorithm n o i t ot n s it r lgo hm … ma a e yb a s i al e r o g l na m h t ri a f o a n a Co i e d ADS 2 Lecture 1 55

Algorithms you might know/use • • Addition/subtraction Long division/multiplication Tying your shoe laces Changing

Algorithms you might know/use • • Addition/subtraction Long division/multiplication Tying your shoe laces Changing a tyre on your bike Baking a cake Opening a bottle of beer … ADS 2 Lecture 1 56

Algorithms you might know/use • • • Addition/subtraction Long division/multiplication Tying your shoe laces

Algorithms you might know/use • • • Addition/subtraction Long division/multiplication Tying your shoe laces Changing a tyre on your bike Baking a cake Opening a bottle of beer A finite sequence of instructions, each with a clear meaning, and can be performed with a finite amount of effort in a finite length of time [Aho, Hopcroft, Ullman 1983] ADS 2 Lecture 1 57

 • • • Addition/subtraction Long division/multiplication Tying your shoe laces Changing a tyre

• • • Addition/subtraction Long division/multiplication Tying your shoe laces Changing a tyre on your bike Baking a cake Opening a bottle of beer Algorithms you might know/use A finite sequence of instructions, each with a clear meaning, and can be performed with a finite amount of effort in a finite length of time [Aho, Hopcroft, Ullman 1983] • • It terminates It produces correct result It has a “complexity” (measure of performance) All steps can be performed without “magic” ADS 2 Lecture 1 58

Things I do not consider to be algorithms • Algorithmic trading • Rules to

Things I do not consider to be algorithms • Algorithmic trading • Rules to decide if you get a loan or a job or … ADS 2 Lecture 1 59

Things I do not consider to be algorithms http: //www. bbc. co. uk/programmes/b 085

Things I do not consider to be algorithms http: //www. bbc. co. uk/programmes/b 085 wj 18#play ADS 2 Lecture 1 60

Data Structures … what’s that then? ADS 2 Lecture 1 61

Data Structures … what’s that then? ADS 2 Lecture 1 61

Data Structures … what’s that then?

Data Structures … what’s that then?

Data structure Not a Data structure – ADT (see next slide) Data structure ways

Data structure Not a Data structure – ADT (see next slide) Data structure ways to organise data/information efficiently where will I (not) see data structures?

abstract data types?

abstract data types?

Definitions contd. • Basically an ADT is an “abstract” entity – Set, Queue, Priority

Definitions contd. • Basically an ADT is an “abstract” entity – Set, Queue, Priority Queue, Stack, … • But a data structure has a fixed implementation (as well as “structural” properties). ADS 2 Lecture 2 65

Who cares? Machines are getting bigger, faster, cheaper … do efficient data structures and

Who cares? Machines are getting bigger, faster, cheaper … do efficient data structures and algorithms really matter?

Bigger, faster, cheaper machines Bigger data sets, greater expectations

Bigger, faster, cheaper machines Bigger data sets, greater expectations

What data structures might we use and what algorithms? An example … • You

What data structures might we use and what algorithms? An example … • You have a file of names of people • We don’t know how many people • Read them in to memory and allow • Find • Add • delete ADS 2 Lecture 1 69

What data structures might we use and what algorithms? A quick look at f.

What data structures might we use and what algorithms? A quick look at f. Book We have a file of people members. txt and a file of friends with entries that are pairs of names of people who are friends. We want to be able to find people, find out who is friends with whom … basically, a toy facebook ADS 2 Lecture 1 70

What have we learned? • • What data structures might we use and what

What have we learned? • • What data structures might we use and what algorithms? Refreshed our knowledge of Array. List Consequences of add and remove How to compile and run on the command line What args means ADS 2 Lecture 1 71

ADS 2 Lecture 1 72

ADS 2 Lecture 1 72

ADS 2 Lecture 1 73

ADS 2 Lecture 1 73

Person ADS 2 Lecture 1 74

Person ADS 2 Lecture 1 74

Person ADS 2 Lecture 1 75

Person ADS 2 Lecture 1 75

Person ADS 2 Lecture 1 76

Person ADS 2 Lecture 1 76

I dream of Array. Lists

I dream of Array. Lists

So, Array. List created with space for 10 objects! That’s enormous

So, Array. List created with space for 10 objects! That’s enormous

And when I add an object into a given position it shuffle things up.

And when I add an object into a given position it shuffle things up. Is that for free? Does that take any time?

And what happens when I want to put in an 11 th object into

And what happens when I want to put in an 11 th object into my Array. List?

Back to Person ADS 2 Lecture 1 88

Back to Person ADS 2 Lecture 1 88

Person constructor ADS 2 Lecture 1 89

Person constructor ADS 2 Lecture 1 89

Person utilities ADS 2 Lecture 1 90

Person utilities ADS 2 Lecture 1 90

Person For printing ADS 2 Lecture 1 91

Person For printing ADS 2 Lecture 1 91

FBook ADS 2 Lecture 1 92

FBook ADS 2 Lecture 1 92

FBook ADS 2 Lecture 1 93

FBook ADS 2 Lecture 1 93

FBook ADS 2 Lecture 1 94

FBook ADS 2 Lecture 1 94

FBook Given a name as a String create a person with that name and

FBook Given a name as a String create a person with that name and make them a member of FBook ADS 2 Lecture 1 95

FBook Opening a file, reading it, creating persons and adding them as members ADS

FBook Opening a file, reading it, creating persons and adding them as members ADS 2 Lecture 1 96

Find a person with a given name … 1 st algorithm ADS 2 Lecture

Find a person with a given name … 1 st algorithm ADS 2 Lecture 1 FBook 97

Find a person with a given name … 1 st algorithm FBook But how

Find a person with a given name … 1 st algorithm FBook But how did we need to structure the data so that we can use that algorithm ADS 2 Lecture 1 98

Find a person with a given name … 1 st algorithm FBook But how

Find a person with a given name … 1 st algorithm FBook But how did we need to structure the data so that we can use that algorithm How efficient is that algorithm, and what could we have used instead? ADS 2 Lecture 1 99

FBook ADS 2 Lecture 1 100

FBook ADS 2 Lecture 1 100

Test ADS 2 Lecture 1 101

Test ADS 2 Lecture 1 101

ADS 2 Lecture 1 102

ADS 2 Lecture 1 102

Test What are args[]? ADS 2 Lecture 1 103

Test What are args[]? ADS 2 Lecture 1 103

Test HCI Fail? ADS 2 Lecture 1 104

Test HCI Fail? ADS 2 Lecture 1 104

I want to keep it simple ADS 2 Lecture 1 105

I want to keep it simple ADS 2 Lecture 1 105

So, let’s compile and run it … ADS 2 Lecture 1 107

So, let’s compile and run it … ADS 2 Lecture 1 107

So, what have we learned … • • The need for dynamic data structures

So, what have we learned … • • The need for dynamic data structures Refreshed our knowledge of Array. List Consequences of add and remove in Array. List Algorithms & data structures go hand in hand How to compile and run on the command line What args means Keeping it simple … ADS 2 Lecture 1 108