1、演算法演算法(Algorithms)內容:本課程對象以對利用計算機來解決問題有興趣的同學為主。本課程主要是教授基本的演算法分析與設計技巧,並整理及比較目前最重要之演算法。Theme:What is the best algorithm for a given problem Three things you will learn:1.Design a good algorithm2.Analyze(and verify)it 3.Lower bounds:know when to stop.Grading1.作業,Term Project (20-25%)2.期中考(35-40%)3.期末考(
2、40%)Course Information Instructor:顏嗣鈞 E-mail:yenee.ntu.edu.tw Office:Rm.BL-525 Phone:3366 3581 http:/www.ee.ntu.edu.tw/yen Office Hours:by appointment Teaching Assistant(s):to be announced later Web site:http:/www.ee.ntu.edu.tw/yen/courses/algorithms06.htmlTextbook T.Cormen,C.Leiserson,R.Rivest,and
3、C.Stein Introduction to Algorithms,2nd Ed.MIT Press and McGraw-Hill Book Company,2001Reference Books Anany Levitin Introduction to The Design&Analysis of Algorithms.Addison-Wesley,2002Reference Books J.Kleinberg,E.Tardos.Algorithm Design.Addison-Wesley,2005.Reference Books G.Brassard and P.Bratley,F
4、undamentals of Algorithmics,Prentice-Hall,1996.Reference Books G.Rawlins,Compare to What?An Introduction to the Analysis of Algorithms,Computer Science Press,1992.Reference Books D.E.Knuth,The Art of Computer Programming,Vol.1 and 3,Third Edition,Addison Wesley,1997.Reference Books D.Harel,The Scien
5、ce of Computing,Addison Wesley,1987.Reference Books Robert Sedgewick,Algorithms(in C,C+,Pascal),Second Edition,Addison Wesley,1992Reference Books U.Manber,Introduction to Algorithms,Addison Wesley,1989.Reference Books J.Nievergelt,Algorithms and Data Structures(With Applications to Graphics and Geom
6、etry),Prentice-Hall,1993.Reference Books B.Moret and H.Shapiro,Algorithms from P to NP,Vol.1,The Benjamin/Cummings Publishing Company,Inc.,1991.Life Cycle of Software Development Problem Interpretation Formal DescriptionModeling and Analysis of the ProblemAlgorithm AnalysisAlgorithm Design Algorithm
7、 Implementation Program VerificationSpecificationappropriate assumptionsAbstractionProblem representation,properties of the problemIdeas,Data structuresNot O.K.DO NEO.K.Partial correctness,TerminationFaithful codingcomplexity;correctnessdesign techniquesSatisfiedNot satisfiedTopics Techniques for al
8、gorithm design Divide and conquer,partitioning.Dynamic programming.Greedy algorithms.Backtracking.Tree based algorithms.Recursion.Approximations.Problem Transformation,reduction.Integer programming.Probabilistic techniques.Topics Sorting and searching algorithms.Geometric algorithms Graph algorithms
9、.Pattern matching algorithms Cryptographic algorithms.Network flow algorithms.Intractable problems.Introduction to computational complexity.Reductions NP completeness.TopicsIf time permits,the following topics will be covered:Introduction to program verification.Introduction to approximation algorit
10、hms.Introduction to probabilistic algorithms.Introduction to parallel algorithms.Introduction to distributed algorithms.Some Algorithm Application AreasDesignApplyAnalyzeAn Example in the Design and Analysis of an Algorithm Demonstrates:-Recurrence Relations-Design Techniques-The fact that analysis
11、provide useful information Problem to be solved:BUNNY PROBLEMWhen a pair of rabbits is less than one month old,it cannot reproduce,but during the second and each succeeding month of life,each pair of rabbits gives rise to one new pair of rabbits.Given that you(Fibonacci)just purchased a pair of new
12、born rabbits,how many pairs will you have n months from now?IllustrationObservation MonthPairs of newborn rabbits at months end(S(n)Pairs of mature rabbits at months end(B(n)Total pairs of rabbits at months end(f(n)01234567101123580112358131123581321The Recurrence Relation i 0 1 2 3 4 5 6 7 8 9 .f(i
13、)1 1 2 3 5 8 13 21 34 55 .f(i)satisfies the following recurrence relationf(i)=f(i-1)+f(i-2)for I 2,f(0)=f(1)=1 B(n+1)=B(n)+S(n)=f(n)S(n+1)=B(n)=f(n-1)f(n+1)=B(n+1)+S(n+1)=f(n)+f(n-1)Solution#11.Function f(n:integer):integer;2.begin3.if n 1 then return(1)4.else return(f(n-1)+f(n-2)5.end.Analysis:Two
14、factors-#of function calls and#of additions.Let FC(n)=#of function calls needed to evaluate f(n)ADD(n)=#of additions needed Consider a tree of function calls(showing the value of n at each node).Analysis Thus,we have the recurrence relationsFC(n)=FC(n-1)+FC(n-2)+1,if n2 FC(0)=FC(1)=1ADD(n)=ADD(n-1)+
15、ADD(n-2)+1,if n2 ADD(0)=ADD(1)=0 Later,we will learn how to solve such equations.For nowf(n)=FC(n)=2f(n)-1,ADD(n)=f(n)-1Derivation125151125151nn Since Hence,f(n)=ADD(n)and FC(n)=O(1.618n)Exponential!Asymptotic Performance01251,1251n125151125151nn)618.1(125151nOnOAvoid Redundant Computations We can u
16、se the dynamic programming technique.Idea:Solving the problem(calculating f(n)consists of solving two smaller problems(calculating f(n-1)and f(n-2)and merging(i.e.,adding)the two smaller solutions to obtain a solution to the original problem.Also,we will have to solve the same problem more than once
17、.When a problem has these characteristics,build up a TABLE of SOLUTIONS to the smaller problems,starting with the smallest,and proceed upwards to the largest(original)problem.Solution#2 1.Program FIB2.var f:array0.n of integer;i:integer;3.begin4.f0:=1;5.f1:=1;6.for i:=2 to n do fi:=fi-1+fi-2;7.end.T
18、he answer is stored in fn.Analysis Clearly n-1 operations(additions)are required-time is O(n).Space required is also O(n).(Space can be reduce to O(1).)This method usually trades space for time(though not in this case).LINEAR!a vast improvement.Modified Solution#21.Program FIB2.var a,b,i:integer;3.b
19、egin4.a:=1;5.b:=1;6.for i:=2 to n do 7.begin b:=a+b;a:=b a end;8.end.The answer is stored in the variable“b”.Interlude This still doesnt seem very good.Do we need to calculate all the f(i)?Cant we skip over some?Try substituting recurrence into itself.f(n)=f(n-1)+f(n-2)=f(n-2)+f(n-3)+f(n-2)=2f(n-2)+
20、f(n-3)=3f(n-3)+2f(n-4)=5f(n-4)+3f(n-5)=8f(n-5)+5f(n-6)(these#s look familiar)Conjecture:f(n)=f(a)f(n-a)+f(a-1)f(n-a-1)where n2;n-1 a 1.1.Function f(x:integer):integer;2.var i,j:integer;3.begin4.if x 1 then return(1)5.else begin6.j:=x/2;7.i:=x/2;8.return(f(i)*f(j)+f(i-1)*f(j-1);9.end;10.end.Solution#
21、3 Analysis Why did we choose i and j x/2?Another design heuristic-BALANCING:divide problem sizes as evenly as possible.Divide and Conquer.Let n=100 Look at tree of function calls(notice that FC(n)=4FC(n/2)+1,FC(1)=1).Analysis What Happened?But obviously the above can be improved.f(100)calls f(50)twi
22、ce!Another important technique-(subtree isomorphism)Eliminate REDUNDANT Computation A Possible Improvement Consider 2 cases:x is even then i=j we want to return f(i)2+f(i-1)2.x is odd then i-1=j we want to return f(i-1)*(f(i)+f(i-2).What does the tree look like now?An even node has 1 even son and 1
23、odd son.Some odd nodes have 2 odd and 1 even sons(5,9,13,.,i.e.1 mod 4).The rest have 1 odd and 2 even sons(3 mod 4).The tree has between O(n1.271)and O(n1.385)nodes.Using the relation f(i)=f(i-1)+f(i-2),we have f(i-2)=f(i)-f(i-1),the odd return value can be further simplified:f(i-1)(f(i)+f(i-2)=f(i
24、-1)(2f(i)-f(i-1)Solution#4 1.Function f(x:integer):integer;2.var i,FI,FIM1:integer;3.begin4.if x 1 then return(1)5.else begin6.i:=x/2;7.FI:=f(i);8.FIM1:=f(i-1);9.if x is even 10.then return(FI*2+FIM1*2)11.else return(FIM1*(2*FI-FIM1)12.end;13.end.Analysis Tree of calls is now completely binary.Take
25、n=32,Height of tree is about log n,hence it has about n nodes,i.e.,FC(n)=O(n).Are You Satisfied?Obviously there still is some redundant computation.Can all the calls at one level be done only once?Then we would have:Solution#5 Procedure calc(x:integer;var fx,fxm1:integer);var i,fi,fim1,fim2:integer;
26、beginif x 1 then begin fx:=1;fxm1:=1 end else begin i:=x/2;calc(i,fi,fim1)if x is even then begin fx:=fi*2+fim1*2;fxm1:=fim1*(2*fi-fim1)end else begin fim2:=fi-fim1;fx:=fim1*(2*fi-fim1);fxm1:=fim1*2+fim2*2 end;end;end.Analysis:Tree obviously has Q(log n)nodes!Solution#6 Repeated SquaringX(n)=AX(n-1)
27、=A(A X(n-2)=An-1 X(1).Use this relation,we can design a Q(log n)algorithm for calculating Fibonacci numbers.Hint:Calculate A,A2,A4,A2log n,then compute An-1 using these items.Questions:Is there a better way for calculating An-1?Exercise Implement the above solutions 1,2 and 6,and compare their runni
28、ng time for calculating f(50),f(100),and f(150).Can your programs calculate f(500)?How about f(1000)?Now,you should realize that real implementation sometimes take a lot of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