CSU22011: Algorithms and Data Structures I - Lecture 1 ...

40
CSU22011: ALGORITHMS AND DATA STRUCTURES I Lecture 1: Module Overview & Introduction Vasileios Koutavas School of Computer Science and Statistics

Transcript of CSU22011: Algorithms and Data Structures I - Lecture 1 ...

Page 1: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

CSU22011: ALGORITHMS AND DATA STRUCTURES ILecture 1: Module Overview & Introduction

Vasileios Koutavas

School of Computer Science and Statistics

Page 2: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

CSU22011

→ Objective: learn to solve computational problems efficiently

“Algorithms + Data Structures = Programs”

— Niklaus Wirth

1

Page 3: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS & DATA STRUCTURES

→ Algorithm: The steps to correctly perform a task that answers ageneral1 computational problem

→ What is the median age of all people in Ireland?→ What is the quickest route from here to the airport?

→ Data Structures: The ways to store the information needed forthe algorithm.→ Array, Linked List, Hash Table, Binary Tree, etc.

1

Algorithms answering the above questions should work when the population ofIreland changes & for other destinations!

2

Page 4: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS & DATA STRUCTURES

→ Algorithm: The steps to correctly perform a task that answers ageneral1 computational problem→ What is the median age of all people in Ireland?

→ What is the quickest route from here to the airport?

→ Data Structures: The ways to store the information needed forthe algorithm.→ Array, Linked List, Hash Table, Binary Tree, etc.

1

Algorithms answering the above questions should work when the population ofIreland changes & for other destinations!

2

Page 5: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS & DATA STRUCTURES

→ Algorithm: The steps to correctly perform a task that answers ageneral1 computational problem→ What is the median age of all people in Ireland?→ What is the quickest route from here to the airport?

→ Data Structures: The ways to store the information needed forthe algorithm.→ Array, Linked List, Hash Table, Binary Tree, etc.

1

Algorithms answering the above questions should work when the population ofIreland changes & for other destinations!

2

Page 6: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS & DATA STRUCTURES

→ Algorithm: The steps to correctly perform a task that answers ageneral1 computational problem→ What is the median age of all people in Ireland?→ What is the quickest route from here to the airport?

→ Data Structures: The ways to store the information needed forthe algorithm.→ Array, Linked List, Hash Table, Binary Tree, etc.

1Algorithms answering the above questions should work when the population ofIreland changes & for other destinations!

2

Page 7: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS & DATA STRUCTURES

→ Algorithm: The steps to correctly perform a task that answers ageneral1 computational problem→ What is the median age of all people in Ireland?→ What is the quickest route from here to the airport?

→ Data Structures: The ways to store the information needed forthe algorithm.→ Array, Linked List, Hash Table, Binary Tree, etc.

1Algorithms answering the above questions should work when the population ofIreland changes & for other destinations!

2

Page 8: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS + DATA STRUCTURES = PROGRAMS

Programs

→ must be correct

→ Mathematical guarantee of correctness only though FormalVerification (see CSU44004)

→ Confidence of correctness though Testing→ in CSU22011 (and in the SW industry): Unit Testing

→ Other kinds of testing: integration testing, validation testing, usertesting, etc.

→ must be efficient

3

Page 9: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS + DATA STRUCTURES = PROGRAMS

Programs

→ must be correct→ Mathematical guarantee of correctness only though Formal

Verification (see CSU44004)

→ Confidence of correctness though Testing→ in CSU22011 (and in the SW industry): Unit Testing

→ Other kinds of testing: integration testing, validation testing, usertesting, etc.

→ must be efficient

3

Page 10: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS + DATA STRUCTURES = PROGRAMS

Programs

→ must be correct→ Mathematical guarantee of correctness only though Formal

Verification (see CSU44004)

→ Confidence of correctness though Testing→ in CSU22011 (and in the SW industry): Unit Testing

→ Other kinds of testing: integration testing, validation testing, usertesting, etc.

→ must be efficient

3

Page 11: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS + DATA STRUCTURES = PROGRAMS

Programs

→ must be correct→ Mathematical guarantee of correctness only though Formal

Verification (see CSU44004)

→ Confidence of correctness though Testing→ in CSU22011 (and in the SW industry): Unit Testing

→ Other kinds of testing: integration testing, validation testing, usertesting, etc.

→ must be efficient

3

Page 12: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

ALGORITHMS + DATA STRUCTURES = PROGRAMS

Programs

→ must be correct→ Mathematical guarantee of correctness only though Formal

Verification (see CSU44004)

→ Confidence of correctness though Testing→ in CSU22011 (and in the SW industry): Unit Testing

→ Other kinds of testing: integration testing, validation testing, usertesting, etc.

→ must be efficient

3

Page 13: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Is this a good measure of a program’s efficiency?

→ How long it takes the program to run on my laptop

→ How long it takes the program to run on the fastest computer

→ How long it takes the program to run on the slowest computer

→ How long it takes the program to run with the largest input

→ How long it takes the program to run with the smallest input

4

Page 14: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Is this a good measure of a program’s efficiency?

→ How long it takes the program to run on my laptop

→ How long it takes the program to run on the fastest computer

→ How long it takes the program to run on the slowest computer

→ How long it takes the program to run with the largest input

→ How long it takes the program to run with the smallest input

4

Page 15: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Is this a good measure of a program’s efficiency?

→ How long it takes the program to run on my laptop

→ How long it takes the program to run on the fastest computer

→ How long it takes the program to run on the slowest computer

→ How long it takes the program to run with the largest input

→ How long it takes the program to run with the smallest input

4

Page 16: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Is this a good measure of a program’s efficiency?

→ How long it takes the program to run on my laptop

→ How long it takes the program to run on the fastest computer

→ How long it takes the program to run on the slowest computer

→ How long it takes the program to run with the largest input

→ How long it takes the program to run with the smallest input

4

Page 17: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Is this a good measure of a program’s efficiency?

→ How long it takes the program to run on my laptop

→ How long it takes the program to run on the fastest computer

→ How long it takes the program to run on the slowest computer

→ How long it takes the program to run with the largest input

→ How long it takes the program to run with the smallest input

4

Page 18: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Established measure: how well the program scales to larger inputs

→ When I double the input size my program takes the same timeto run on the same computer (constant running time).

→ When I double the input size my program takes twice the time torun on the same computer (linear running time).

→ …

We also care about memory needed:

→ When I double the input size my program needs the sameamount of memory to run (constant memory space).

→ When I double the input size my program takes twice theamount of memory to run (linear memory space).

→ …

Scalability may some times seems crude but at least allows us tocompare algorithms

5

Page 19: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Established measure: how well the program scales to larger inputs

→ When I double the input size my program takes the same timeto run on the same computer (constant running time).

→ When I double the input size my program takes twice the time torun on the same computer (linear running time).

→ …

We also care about memory needed:

→ When I double the input size my program needs the sameamount of memory to run (constant memory space).

→ When I double the input size my program takes twice theamount of memory to run (linear memory space).

→ …

Scalability may some times seems crude but at least allows us tocompare algorithms

5

Page 20: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHAT IS EFFICIENCY?

Established measure: how well the program scales to larger inputs

→ When I double the input size my program takes the same timeto run on the same computer (constant running time).

→ When I double the input size my program takes twice the time torun on the same computer (linear running time).

→ …

We also care about memory needed:

→ When I double the input size my program needs the sameamount of memory to run (constant memory space).

→ When I double the input size my program takes twice theamount of memory to run (linear memory space).

→ …

Scalability may some times seems crude but at least allows us tocompare algorithms

5

Page 21: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

IN CSU22011 (1/2)

The Software Engineer’stoolbox

→ Learn the most common A&DS thatevery CS graduate must know.→ Example algorithms: Merge Sort,

Union-Find, Dijstra’s Shortest PathTree, …

→ Identify which knownalgorithms/data structures best fitspecific problems→ Example: What is the best

algorithm for finding the median?– It depends:a QuickSelect,MedianOfMedians, IntroSelect,using SoftHeaps

→ Learn Abstract Data Types:interfaces of A&DS

→ Practice implementing A&DS

ahttps://www.quora.com/What-is-the-most-efficient-algorithm-to-find-the-kth-smallest-element-in-an-array-having-n-unordered-elements

6

Page 22: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

IN CSU22011 (1/2)

The Software Engineer’stoolbox

→ Learn the most common A&DS thatevery CS graduate must know.→ Example algorithms: Merge Sort,

Union-Find, Dijstra’s Shortest PathTree, …

→ Identify which knownalgorithms/data structures best fitspecific problems→ Example: What is the best

algorithm for finding the median?– It depends:a QuickSelect,MedianOfMedians, IntroSelect,using SoftHeaps

→ Learn Abstract Data Types:interfaces of A&DS

→ Practice implementing A&DSahttps://www.quora.com/

What-is-the-most-efficient-algorithm-to-find-the-kth-smallest-element-in-an-array-having-n-unordered-elements

6

Page 23: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

IN CSU22011 (1/2)

The Software Engineer’stoolbox

→ Learn the most common A&DS thatevery CS graduate must know.→ Example algorithms: Merge Sort,

Union-Find, Dijstra’s Shortest PathTree, …

→ Identify which knownalgorithms/data structures best fitspecific problems→ Example: What is the best

algorithm for finding the median?– It depends:a QuickSelect,MedianOfMedians, IntroSelect,using SoftHeaps

→ Learn Abstract Data Types:interfaces of A&DS

→ Practice implementing A&DSahttps://www.quora.com/

What-is-the-most-efficient-algorithm-to-find-the-kth-smallest-element-in-an-array-having-n-unordered-elements

6

Page 24: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

IN CSU22011 (1/2)

The Software Engineer’stoolbox

→ Learn the most common A&DS thatevery CS graduate must know.→ Example algorithms: Merge Sort,

Union-Find, Dijstra’s Shortest PathTree, …

→ Identify which knownalgorithms/data structures best fitspecific problems→ Example: What is the best

algorithm for finding the median?– It depends:a QuickSelect,MedianOfMedians, IntroSelect,using SoftHeaps

→ Learn Abstract Data Types:interfaces of A&DS

→ Practice implementing A&DSahttps://www.quora.com/

What-is-the-most-efficient-algorithm-to-find-the-kth-smallest-element-in-an-array-having-n-unordered-elements6

Page 25: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

IN CSU22011 (2/2)

The ComputerScientist’s toolbox

→ Learn to evaluate new algorithms→ Efficiency: calculate the running time and

memory usage→ how well they scale→ Measuring aspects: Worst-case,

average-case, amortised, experimentalperformance

→ Measuring systems: big-O notation, tildenotation, cost models

→ Correctness: rigorous testing and someinformal correctness arguments (see UnitTesting, test coverage)

7

Page 26: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

CSU22011 LOGISTICS

Page 27: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

CSU22011 LOGISTICS

Primary website: https://mymodule.tcd.ie

9

Page 28: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

EXAMPLE PROBLEM

Page 29: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

A software engineer was asked to design an algorithm which willinput two unsorted arrays of integers, A (of size N) and B (also of sizeN), and will output true when all integers in A are present in B.

The engineer came up with two alternatives. The first is:

boolean isContained1(int[] A, int[] B) {boolean AInB = true;for (int i = 0; i < A.length; i++) {

boolean iInB = linearSearch(B, A[i]);AInB = AInB && iInB;

}return AinB;

}

11

Page 30: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

A software engineer was asked to design an algorithm which willinput two unsorted arrays of integers, A (of size N) and B (also of sizeN), and will output true when all integers in A are present in B.The engineer came up with two alternatives. The first is:

boolean isContained1(int[] A, int[] B) {boolean AInB = true;for (int i = 0; i < A.length; i++) {boolean iInB = linearSearch(B, A[i]);AInB = AInB && iInB;

}return AinB;

}

11

Page 31: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

A software engineer was asked to design an algorithm which willinput two unsorted arrays of integers, A (of size N) and B (also of sizeN), and will output true when all integers in A are present in B. Theengineer came up with two alternatives:

The second is:

boolean isContained2(int[] A, int[] B) {int[] C = new int[B.length];for (int i = 0; i < B.length; i++) { C[i] = B[i] }sort(C); // heapsortboolean AInC = true;for (int i = 0; i < A.length; i++) {boolean iInC = binarySearch(C, A[i]);AInC = AInC && iInC;

}return AinC;

}

12

Page 32: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

(a) Calculate the worst-case running time of each of the twoimplementations.

(b) For each implementation, how much extra memory space is itrequired to store copies of the elements in A and B? You shouldtake into account any copies made within the methods sort,linearSearch, and binarySearch.

(c) Find an implementation which is more efficient than both of theengineer’s implementation.

13

Page 33: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

Page 34: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

VIDEO

http://www.youtube.com/embed/vSi6YoTPWLw?rel=0&start=8&end=165

15

Page 35: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

→ To get a technology jobhttp://www.careercup.com

16

Page 36: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

→ To create the “New Google”http://en.wikipedia.org/wiki/PageRank

17

Page 38: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

→ To win big on the stock markethttp://www.theguardian.com/business/2012/oct/21/superstar-traders-lost-magic

19

Page 39: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

→ To rule the world!http://www.theguardian.com/science/2013/jul/01/how-algorithms-rule-world-nsa

20

Page 40: CSU22011: Algorithms and Data Structures I - Lecture 1 ...

WHY?

→ For fun!http://en.wikipedia.org/wiki/Tower_of_Hanoi

21