Algorithms and Data Structures TEACH THAT Algorithms and Data Structures An algorithm does...

Preview:

Citation preview

Algorithms and Data Structures

TEACH THAT

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 dataso 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 and Algorithmsa Computer Scientist’s building blocks

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

Our website, link from moodle

Lecture

14

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•

ADS2 Lecture 1

Lectures … Aye, right.

Algorithms&

Data Structures

Algorithms

&

Data Structures

Great!

Now and then I will take an attendance register

Lassie, what’s in this ADS2 course ? Will it be interesting?

woof!

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

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• quick sort• bucket sort (pigeon hole sort)• bead sort (maybe)

• Dictionaries• maps and hash tables

• open and closed hashing

Continued

ContentContinued

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

Algorithms&

Data Structures

Algorithms

&

Data Structures

Great!

our ADS2 website under construction

(permanently)

Home

News

The lectures

The lectures

code & api

Lots of code in there

demos

Links to interesting & useful stuff

Links to interesting & useful stuff

exercises

exercises

exercises

Questions & Answers

… and now for something completely different!!!

Lassie, why are algorithms and data structures important?

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

I just hope I get feedback, continuously

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 our programming?

Will we use BIG data sets?

woof!

1st lecture … “Who cares?”

algorithms are ancient

Many algorithms predate computers

But first … what is an algorithm?

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

Abu Jafar Mohammed ibm Musa Al Khwarizmi

800AD

Before I was born

Euclid of Alexandria

325BC to 265BC

GCD

Data Structures … what’s that then?

ways to organise data/information efficiently

where will I (not) see data structures?

Data structure

Data structure

Data structure

Not a Data structure – ADT (see next slide)

abstract data types?

Definitions contd.

• Basically an ADT is an “abstract” entity• But a data structure has a fixed implementation (as well as

“structural” properties).

60ADS2 Lecture 2

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

An Example: from problem to program

Sequencing traffic lightsA Formal Model

A-left

A-right

A-left

A-right

B-right

B-leftB-straight

A-left

A-right

B-right

B-leftB-straight

C-straight

AL

AR

BR

BS

BL

CS

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Draw a line (edge) between turns that CANNOT occur atthe same time

Draw a line (edge) between turns that CANNOT occur atthe same time

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Label turns that have an edge between them (adjacent) differently.The minimum number of labels used Is the minimum (maximum) number of concurrent turns disallowed (allowed)

For “label” let’s use “colour”

AL

AR

BR

BS

BL

CS

Therefore we could allow same colouredturns to go at the same time: blue, green, pink and it will be collision-free

AL

AR

BR

BS

BL

CS

We have 3 time steps

AL

AR

BR

BS

BL

CS

Could we do it in less than 3 time steps?Is this solution unique?What is the “underlying” problem?What is its complexity?Are there any algorithms that can be used?

AL

AR

BR

BS

BL

CS

Is Dobie’s Loan actually scheduled optimally?

AL

AR

BR

BS

BL

CS

Might this be an obvious solution?

B

C

A

E

D

Try This

One way

JoJo’s intersection, Princeton

B

C

A

E

D

JoJo’s intersection, Princeton

ABACADBABCBDDADBDCEAEBECED

turns

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

JoJo’s intersection, Princeton, USA (allowed a right turn against the lights)

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

ED

EA

BC

EC

EB

DC

DA

BA

AD

AC

DB

AB

BD

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

ED

EA

BC

EC

EB

DC

DA

BA

AD

AC

DB

AB

BD

JoJo’s intersection, Princeton, USA (allowed a right turn against the lights)

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

ED

EA

BC

EC

EB

DC

DA

BA

AD

AC

DB

AB

BD

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

ED

EA

BC

EC

EB

DC

DA

BA

AD

AC

DB

AB

BD

4 colours4 steps

B

C

A

E

D

JoJo’s intersection, Princeton

ED

EC

EB

EA

DC

DB

DA

BD

BC

BA

AD

AC

AB

EDECEBEADCDBDABDBCBAADACAB

1111

11111

111

111

11111

11111

11

111

11111

1111

ED

EA

BC

EC

EB

DC

DA

BA

AD

AC

DB

AB

BD

4 colours4 steps

4-clique!

Recommended