28
CS333 Algorithms http://www.cs.binghamton.edu/~kang/teaching/cs333/

CS333 Algorithms kang/teaching/cs333

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS333 Algorithms kang/teaching/cs333

CS333 Algorithms

• http://www.cs.binghamton.edu/~kang/teaching/cs333/

Page 2: CS333 Algorithms kang/teaching/cs333

Course Objectives• Critical thinking for problem solving• Develop correct and efficient

algorithms • Analysis of time and space complexity• Learn key algorithms and learn how to

analyze and compare them• Learn how to design algorithms using

well known methods such as divide and conquer, greedy methods, and dynamic programming

• Theory of NP-completeness• Enjoy the beauty of algorithms!

Page 3: CS333 Algorithms kang/teaching/cs333

Chapter 1

Definition of algorithmsSample problems and algorithmsReview of basic math needed for this

course as necessaryAnalysis

Time complexityNotion of Order: big O, small o, Ω, Θ

Page 4: CS333 Algorithms kang/teaching/cs333

Basic ConceptsBasic Concepts

Algorithm: A step-by-step procedure for solving a problem.

Exemplar problems - Sort a list S of n numbers in non-decreasing order

- Determine whether a number x is in the list S of n numbers

- Matrix multiplication

Page 5: CS333 Algorithms kang/teaching/cs333

Importance of Algorithm Efficiency

Importance of Algorithm Efficiency

Time

Storage

Example - Sequential search vs. binary search Basic operation: comparison Number of comparisons grows at different rates

- nth Fibonacci sequence Recursive versus iterative solutions

Page 6: CS333 Algorithms kang/teaching/cs333

Example: search strategyExample: search strategy

Sequential search vs. binary search

Problem: determine whether x is in the

sorted array S of n keys

Inputs: key x, positive integer n, sorted (non-decreasing order) array of keys S indexed from 1 to n

Output: location of x in S (0 if x is not in S)

Page 7: CS333 Algorithms kang/teaching/cs333

Example: search strategyExample: search strategy

Sequential search: Basic operation: comparison

Void Seqsearch(int n, const keytype S[], keytype x, index& location)

location=1; while(location<=n && S[location] != x) location++; if(location > n) location = 0;

Page 8: CS333 Algorithms kang/teaching/cs333

Example: search strategyExample: search strategy Binary search: Basic operation: comparison

Void Binsearch(int n, const keytype S[], keytype x, index& location) index low, high, mid;

low = 1; high =n; location=0;

while(low<=high && location ==0) mid = floor((low+high)/2);

if(x==S[mid]) location = mid;

else if (x< S[mid]) high = mid -1;

else(low = mid +1);

Page 9: CS333 Algorithms kang/teaching/cs333

Example: number of comparisons

Example: number of comparisons

Sequential search:

n 32 128 1024 1,048,576

Binary search:

lg(n) + 1 6 8 11 21

Eg: S[1],…, S[16],…, S[24], S[28], S[30], S[31], S[32] (1st) (2nd) (3rd) (4th) (5th) (6th)

Page 10: CS333 Algorithms kang/teaching/cs333

Analysis of Time ComplexityAnalysis of Time Complexity Input size

Basic operation

Time complexity for the size of input, n - T(n) : Every-case time complexity - W(n): Worst-case time complexity - A(n): Average-case time complexity - B(n): Best-case time complexity

T(n) example - Add array members; Matrix multiplication; Exchange

sort T(n) = n-1; n*n*n n(n-1)/2

Page 11: CS333 Algorithms kang/teaching/cs333

Math preparationMath preparation

InductionLogarithmSetsPermutation and combinationLimitsSeriesAsymptotic growth functions and

recurrence Probability theory

Page 12: CS333 Algorithms kang/teaching/cs333

Programming preparationProgramming preparation

Data structure CC++

Page 13: CS333 Algorithms kang/teaching/cs333

Presenting Commonly Presenting Commonly used algorithmsused algorithms

• Search (sequential, binary)• Sort (mergesort, heapsort, quicksort,

etc.)• Traversal algorithms (breadth, depth,

etc.)• Shortest path (Floyd, Dijkstra)• Spanning tree (Prim, Kruskal)• Knapsack• Traveling salesman• Bin packing

Page 14: CS333 Algorithms kang/teaching/cs333

Well known problem

• Problem: Given a map of North America, find the best route from New York to Orlando?

• Many efficient algorithms• Choose appropriate one (e.g.,

Floyd’s algorithm for shortest paths using dynamic programming)

Page 15: CS333 Algorithms kang/teaching/cs333

Another well known problem

• Problem: You are supposed to deliver newspapers to n houses in your town. How can you find the shortest tour from your home to everybody on your list and return to your home?

• One solution to traveling salesperson problem: dynamic programming

Page 16: CS333 Algorithms kang/teaching/cs333

Another well known problem (continued)

• No efficient algorithm to general problem

• Many heuristic and approximation algorithms (e.g., greedy heuristic)

• Choose appropriate one

Page 17: CS333 Algorithms kang/teaching/cs333

Another well known problem (continued)

• Computational Geometry• Find a convex hull• Delaunay triangulation• Voronoi Diagram

Page 18: CS333 Algorithms kang/teaching/cs333

Design Methods or Design Methods or StrategiesStrategies

•Divide and conquer•Greedy•Dynamic programming•Backtrack•Branch and bound

Page 19: CS333 Algorithms kang/teaching/cs333

Not addressed (Advanced algorithms)

• Genetic algorithms• Neural net algorithms• Algebraic methods

Page 20: CS333 Algorithms kang/teaching/cs333

Theory of NP Theory of NP completenesscompleteness

• Many common problems are NP-complete – traveling salesperson, knapsack,... – NP: non-deterministic polynomial

• Fast algorithms for solving NP-complete problems probably don’t exist

• Approximation algorithms are used (e.g., minimum spanning tree derived by Prim’s algorithm using triangle inequality)

Page 21: CS333 Algorithms kang/teaching/cs333

Are algorithms useful?

• Hardware• Software• Economics• Biomedicine• Computational

geometry (graphics)

• Decision making• Scheduling …..

“Great algorithms

are the poetry of

computation”

Page 22: CS333 Algorithms kang/teaching/cs333

Hardware Design

• VLSI design• Multiplication • Search• Sort

networksSelection sort for A[1], …, A[n-1]

3

7

8

6

2

2

3

6

7

8

Page 23: CS333 Algorithms kang/teaching/cs333

Software

• Text processing– String matching, spell checking, and

pretty print,…• Networks

– Minimum spanning trees, routing algorithms, …

• Databases• Compilers

Page 24: CS333 Algorithms kang/teaching/cs333

Text processing – pretty print

I want ¶ this page ¶to look good ¶

I want this page ¶to look good ¶Method: Dynamic programming

Page 25: CS333 Algorithms kang/teaching/cs333

Engineering• Optimization problem (e.g., finite element,

energy minimization, dynamic simulation).• Best feature selection for object representation

and biometrics recognition (e.g., Genetic Algorithm)

• Mathematics: geometric simulation and approximation (e.g., algorithm approximation)

• Graphics visualization (e.g., area filling and scan conversion, 3D key framing).

• Signal analysis: FFT, Huffman coding…

Page 26: CS333 Algorithms kang/teaching/cs333

Economics

• Transportation problems– Shortest paths, traveling salesman,

knapsack, bin packing

• Scheduling problems• Location problems (e.g., Voronoi

Diagram)• Manufacturing decisions

Page 27: CS333 Algorithms kang/teaching/cs333

Social decisions

• Matching problem• Assigning residents to hospitals

– Matching residents to medical program

Page 28: CS333 Algorithms kang/teaching/cs333

Critical thinking for problem solving

• Consider different approaches to solving a problem such as dynamic programming and greedy approaches

• Analyze the merits of each• Consider different implementations

for a chosen approach • Analyzing the merit of the different

implementation