5/15/2015cot 54071 cot 5407: introduction to algorithms tao li ecs 318; phone: x6036...

33
03/26/22 COT 5407 1 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 [email protected] http://www.cs.fiu.edu/~taoli/class/COT5407-F09/in dex.html

Upload: juniper-walsh

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 1

COT 5407: Introduction to Algorithms

Tao LiECS 318; Phone: x6036

[email protected]://www.cs.fiu.edu/~taoli/class/COT5407-F09/index.html

Page 2: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 2

Why should I care about Algorithms?Cartoon from Intractability by Garey and Johnson

Page 3: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 3

More questions you should ask• Who should know about Algorithms?• Is there a future in this field? • Would I ever need it if I want to be a software engineer

or work with databases?

Page 4: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 4

Why are theoretical results useful?

Cartoon from Intractability by Garey and Johnson

Page 5: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 5

Why are theoretical results useful?

Cartoon from Intractability by Garey and Johnson

Page 6: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 6

Self-Introduction

• Ph.D. in Computer Science from University of Rochester, 2004– Research Interests: data mining, machine learning, information

retrieval, bioinformatics and more ?

• Associate Professor in the School of Computer Science at Florida International University

• Industry Experience:– Summer internships at Xerox Research (summer 2001, 2002)

and IBM Research (Summer 2003, 2004)

Page 7: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 7

Student Self-Introduction

• Name– I will try to remember your names. But if you have a Long

name, please let me know how should I call you

• Major and Academic status

• Programming Skills– Java, C/C++, VB, Matlab, Scripts etc.

• Research Interest

• Anything you want us to know

Page 8: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 8

What this course is about• Introduction to Algorithms

• Analysis of Algorithms• How does one design programs and ascertain

their efficiency? • Divide-and-conquer techniques, string

processing, graph algorithms, mathematical algorithms. Advanced data structures such as balanced tree schemes.

Page 9: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 9

Course Logistics

• Meeting Time and Location: Tuesday and Thursday 17:00pm-18:15pm, ECS134

• Office Hours: Tuesday and Thursday 14:30pm-15:30pm

• TA: Yali Wu

• Textbook: Introduction to Algorithms,  (Third Edition) Thomas Cormen, Charles Leiserson, Ronald Rivest, and Clifford Stein. MIT Press.

Page 10: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 10

Evaluation• Class participation and Quizzes: 10% • Midterm Exam: 30%• Final Exam: 30%• Assignments:30%

• You may work with one other person on homeworks, but you must each write up your solutions separately. If you work with another person, indicate who you worked with on your solution.

• Please start a new page for each problem on your solutions, and include your name on each page, so the TA can choose the problems for grading.

• Exams are open/closed book ??

Page 11: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

Algorithm• A computational problem is a mathematical problem,

specified by an input/output relation.

• An algorithm is a computational procedure for solving a computational problem.

04/18/23 COT 5407 11

“computer”

problem

algorithm

input output

Page 12: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-12

Some Well-known Computational Problems

• Sorting• Searching• Shortest paths in a graph• Minimum spanning tree• Primality testing• Traveling salesman problem• Knapsack problem• Chess• Towers of Hanoi• Program termination

Page 13: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 13

History of Algorithms

The great thinkers of our field:• Euclid, 300 BC• Bhaskara, 6th century• Al Khwarizmi, 9th century• Fibonacci, 13th century• Babbage, 19th century• Turing, 20th century• von Neumann, Knuth, Karp, Tarjan, …

Page 14: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

8/28/07 COT 5407 14

Euclid’s Algorithm• GCD(12,8) = 4; GCD(49,35) = 7;• GCD(210,588) = ??• GCD(a,b) = ??• Observation: [a and b are integers and a b]

– GCD(a,b) = GCD(a-b,b)

• Euclid’s Rule: [a and b are integers and a b]– GCD(a,b) = GCD(a mod b, b)

• Euclid’s GCD Algorithm:– GCD(a,b)

If (b = 0) then return a;return GCD(a mod b, b)

Page 15: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-15

Basic Issues Related to Algorithms• How to design algorithms

• How to express algorithms

• Proving correctness

• Efficiency– Theoretical analysis

– Empirical analysis

• Optimality

Page 16: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-16

Algorithm design strategies• Brute force

• Divide and conquer

• Decrease and conquer

• Transform and conquer

• Greedy approach

• Dynamic programming

• Backtracking

• Branch and bound

• Space and time tradeoffs

Page 17: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-17

Analysis of Algorithms• How good is the algorithm?

– Correctness– Time efficiency: amount of work done– Space efficiency: amount of space used– Simplicity, clarity

• Does there exist a better algorithm?– Lower bounds– Optimality

Page 18: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 18

Correctness

Proving correctness is dreadful for large algorithms. A strategy that can be used is: divide the algorithm into smaller pieces, and then clarify what the preconditions and postconditions are and prove correct assuming everything else is correct.

Page 19: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 19

Amount of Work Done

Rather than counting the total number of instructions executed, we'll focus on a set of key instructions and count how many times they are executed. Use asymptotic notation and pay attention only to the largest growing factor in the formula of the running time.

Two major types of analysis: worst-case analysis and average-case analysis

Page 20: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 20

More• Amount of space used: The amount of space used can be

measured similarly. Consideration of this efficiency is often important.

• Simplicity, clarity: Sometimes, complicated and long algorithms can be simplified and shortened by the use of

recursive calls.

• Optimality: For some algorithms, you can argue that they are the best in terms of either amount of time used or

amount of space used. There are also problems for which you cannot hope to have efficient algorithms.

Page 21: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 21

Asymptotic Growth Rates of Functions

• Big O• Big Omega• Little O• Little Omega• Theta Notation

Page 22: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 22

Notations

Page 23: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 23

Other mathematical background• The ceiling function• The floor function• The exponentials and logarithms• Fibonacci number• Summations and Series

Page 24: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

Why study algorithms?• Theoretical importance

– the core of computer science

• Practical importance

– A practitioner’s toolkit of known algorithms

– Framework for designing and analyzing algorithms for new problems

Page 25: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

Two main issues related to algorithms

• How to design algorithms

• How to analyze algorithm efficiency

Page 26: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-26

Algorithm design strategies• Brute force

• Divide and conquer

• Decrease and conquer

• Transform and conquer

• Greedy approach

• Dynamic programming

• Backtracking

• Branch and bound

• Space and time tradeoffs

Page 27: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

Analysis of algorithms• How good is the algorithm?

– time efficiency– space efficiency

• Does there exist a better algorithm?– lower bounds– optimality

Page 28: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-28

Important problem types

• sorting

• searching

• string processing

• graph problems

• combinatorial problems

• geometric problems

• numerical problems

Page 29: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

1-29

Fundamental data structures• list

– array

– linked list

– string

• stack

• queue, priority queue

• Graph

• Tree

• set and dictionary

Page 30: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 30

Search

• You are asked to guess a number X that is known to be an integer lying between integers A and B. How many guesses do you need in the worst case?– Number of guesses = log2(B-A)

• You are asked to guess a positive integer X. How many guesses do you need in the worst case? – NOTE: No upper bound B is known for the number.

Page 31: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 31

Search

• You are asked to guess a number X that is known to be an integer lying between integers A and B. How many guesses do you need in the worst case?– Number of guesses = log2(B-A)

• You are asked to guess a positive integer X. How many guesses do you need in the worst case? – NOTE: No upper bound B is known for the number.– Algorithm:

• figure out B (by using Doubling Search) • perform binary search in the range B/2 through B.

– Number of guesses = log2B + log2(B – B/2)– Since X is between B/2 and B, we have: log2(B/2) < log2X, – Number of guesses < 2log2X - 1

Page 32: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 32

Polynomials

• Given a polynomial– p(x) = a0 + a1 x + a2 x2 + … + an-1 xn-1 + an xn

compute the value of the polynomial for a given value of x.

• How many additions and multiplications are needed?– Simple solution:

• Number of additions = n• Number of multiplications = 1 + 2 + … + n = n(n+1)/2

– Improved solution using Horner’s rule:

Page 33: 5/15/2015COT 54071 COT 5407: Introduction to Algorithms Tao Li ECS 318; Phone: x6036 taoli@cs.fiu.edu taoli/class/COT5407-F09/index.html

04/18/23 COT 5407 33

Polynomials

• Given a polynomial– p(x) = a0 + a1 x + a2 x2 + … + an-1 xn-1 + an xn

compute the value of the polynomial for a given value of x.

• How many additions and multiplications are needed?– Simple solution:

• Number of additions = n• Number of multiplications = 1 + 2 + … + n = n(n+1)/2

– Improved solution using Horner’s rule:• p(x) =p(x) = a0 + x(a1 + x(a2 + … x(an-1 + x an))…))

• Number of additions = n• Number of multiplications = n