1 / 26

Optymalizacja-algorytmy i zastosowania ( English )

Optymalizacja-algorytmy i zastosowania ( English ). Computational complexity – theory by examples Mariusz Paradowski. General issues. Problem Algorithm – “a way to solve the problem” There can be many ways to solve the same problem Can an optimal solution for the problem be found?

Download Presentation

Optymalizacja-algorytmy i zastosowania ( English )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Optymalizacja-algorytmy i zastosowania (English) Computational complexity – theory by examples Mariusz Paradowski

  2. General issues • Problem • Algorithm – “a way to solve the problem” • There can be many ways to solve the same problem • Can an optimal solution for the problem be found? • How to choose the best way to solve the problem?

  3. Algorithms • How to measure algorithm „speed”? • How to compare algorithms? • How to pick the algorithm we need?

  4. What a computer can do? • Computer is a kind of complex calculator • Computer has a set of actions • Add (A+B=C) • Subtract (A-B=C) • Multiply (A*B=C) • Divide (A/B=C) • Compare numbers (A>B) • Many, many more • Algorithm as a set of computer actions

  5. On comparing algorithms 121 213 253 532 452 187 + + + ? ? ? 1) A+B=C 1) A+B=C 1) A+B=C 1 action needed in each case. For adding 2 numbers we always need 1 action. This is a rule: problem of adding 2 numbers can be solved using 1 action.

  6. On comparing algorithms 253 532 132 189 211 253 532 132 1) A+B=C 2) C+D=E 3) E+F=G 4) G+H=I 1) A+B=C 2) C+D=E + ? + ? For adding 3 numbers we need 2 actions. For adding 5 numbers we need 4 actions. Is there some kind of a rule? YES Problem of adding n numbers can be solved using n-1 actions.

  7. On comparing algorithms M[1]=253 M[2]=532 M[3]=132 1) R=M[1] 2) M[2]>R => R=M[2] 3) M[3]>R => R=M[3] Problem of finding maximum of n numbers can be solved using n actions. M[1]=253 M[2]=532 M[3]=132 M[4]=44 M[5]=543 1) R=M[1] 2) M[2]>R => R=M[2] 3) M[3]>R => R=M[3] 4) M[4]>R => R=M[4] 5) M[5]>R => R=M[5]

  8. On comparing algorithms Problem of finding maximum of n numbers sorted can be solved using 1 action. M[1]=17 M[2]=143 M[3]=276 1) R=M[1] M[1]=356 M[2]=698 M[3]=789 M[4]=987 M[5]=999 1) R=M[1] IDEA: Let’s first sort the numbers! NO! (answer on the next slides)

  9. M[1]=253 M[2]=132 M[3]=523 M[4]=143 M[5]=543 M[1]=253 M[2]=532 M[3]=132 M[4]=143 M[5]=543 M[1]=253 M[2]=532 M[3]=132 M[4]=143 M[5]=543 M[1]=132 M[2]=253 M[3]=523 M[4]=143 M[5]=543 M[1]=132 M[2]=253 M[3]=143 M[4]=523 M[5]=543 M[1]=132 M[2]=143 M[3]=253 M[4]=523 M[5]=543 M[1]=132 M[2]=143 M[3]=253 M[4]=523 M[5]=543 On comparing algorithms TOTAL: 7 comparisons 4 replacements

  10. On comparing algorithms M[1]=123 M[2]=356 M[3]=378 M[4]=894 M[5]=977 Best case total: 5 comparisons 0 replacements Will be no better than n actions M[1]=977 M[2]=894 M[3]=378 M[4]=356 M[5]=123 Worst case total: 10 comparisons 10 replacements Will be no worse than n*n - n actions

  11. On comparing algorithms ALGORITHM 1: Use first sorting, then maximum from sorted n*n – n actions from sorting 1 action from maximum selection Total worst case: n*n – n + 1 Total best case: n + 1 ALGORITHM 2: Use maximum from unsorted n actions from the algorithm Total: n actions, BETTER! We have just compared 2 algorithms!

  12. Big-O notation • There is a formal way to write down the number of operations • n is often referred as „the size of the problem” • Three major cases • Optimistic (we just got lucky) • Expected (usual case) • Pessimistic (we had bad luck)

  13. Big-O notation A function f(n) is O(g(n)) as n -> infinity if and only if there exists numbers n0 and c such that |f(n)| <= c|g(n)| for n > n0

  14. O(f(n))O(g(n)) = O(f(n)g(n)) O(f(n)) + O(g(n)) = O(max{f(n),g(n)}) T(1) = O(1) T(log n) = O(log n) T(n) = O(n) T(5n) = O(n) T(2n2) = O(n2) T(n6+n5+n4)=O(n6) T(1/2n) = O(n) T(2n) = O(2n) T(2n + n100) = O(2n) T(3n + 2n) = O(3n) Big-O notation

  15. Big-O notation

  16. Big-O notation • Find maximum from sorted • O(1) • Find maximum from unsorted • O(n) • Insert sort (similar to presented) • Pes: O(n2), Exp: O(n2), Opt: O(n) • Find maximum by sorting and then get from sorted • Pes: O(n2), Exp: O(n2), Opt: O(n) • Traveling salesman problem, exact solution algorithm • O(2n) • Genetic algorithm, not exact solution • Will be determined soon…

  17. A word about GA 01) FOR EVERY GENOTYPE 02) GENERATE RANDOM GENOTYPE 03) NEXT GENOTYPE 04) FOR EVERY GENERATION 05) FOR EVERY GENOTYPE 06) CONVERT GENOTYPE TO PHENOTYPE 07) CALCULATE FITNESS 08) NEXT GENOTYPE 09) CHECK STOP CONDITION, IF YES - STOP 10) MAKE SELECTION 11) MAKE CROSSOVER 12) MAKE MUTATION 13) NEXT GENERATION

  18. A word about GA 01) FOR EVERY GENOTYPE 02) GENERATE RANDOM GENOTYPE 03) NEXT GENOTYPE • Population of size p • Genotype of b bits • Random function – O(1) • T(p,b)=O(p)O(b)O(1)=O(pb)

  19. A word about GA 05) FOR EVERY GENOTYPE 06) CONVERT GENOTYPE TO PHENOTYPE 07) CALCULATE FITNESS 08) NEXT GENOTYPE • Population of size p • Genotype of b bits • Conversion of 1 bit to phenotype – O(1) • Fitness calculation, directly value of phenotype – O(1) • T(p,b)=O(p)O(b)O(1)=O(pb)

  20. A word about GA 10) MAKE SELECTION 11) MAKE CROSSOVER 12) MAKE MUTATION • Selection – pick p random numbers • Crossover – for every genotype (p), combine with another, length of genotype is b • Mutation – for every genotype (p), for every bit in genotype (b), perform mutation • Complexity • Selection – O(p) • Crossover – O(pb) • Mutation – O(pb)

  21. A word about GA 01) FOR EVERY GENOTYPE 02) GENERATE RANDOM GENOTYPE 03) NEXT GENOTYPE 04) FOR EVERY GENERATION (n generations) 05) FOR EVERY GENOTYPE 06) CONVERT GENOTYPE TO PHENOTYPE 07) CALCULATE FITNESS 08) NEXT GENOTYPE 09) CHECK STOP CONDITION, IF YES - STOP 10) MAKE SELECTION 11) MAKE CROSSOVER 12) MAKE MUTATION 13) NEXT GENERATION T(p,b,n) = O(pb) + O(n)[O(pb) + O(p) + O(p) + O(pb) + O(pb)] = O(pbn)

  22. P, NPC, NP NP (non polynomial), O(2n), O(3n), O(n!),… NPC(non polynomial complete), we do not know if NPC = P or NPC = NP Over 1.000.000$ reward P (polynomial) O(1), O(log n), O(n), O(n2), O(n3),…

  23. Genetic Algorithms and computational complexity • Our n is really big (e.g. 1000, 5000, 100000) • If we know a is in class P in most cases we should not use GA • If we know a solution in class P with big exponent (e.g. 20) we should use GA • If we know our problem is NPC or NP we should use GA

  24. Our n is small (e.g. 10, 20) We probably should not use GA in any case Determine O(f(n)) Determine possible values of n Determine if we need exact solution Determine how much time we have Pick an algorithm Genetic Algorithms and computational complexity

  25. Summary • Use GA only if you know that P class solution does not exists • Use GA only if your data is really large • Using GA lead to non optimal solutions • But… • Using GA in some cases is the only solution

  26. Computational complexity Thank you for your attention

More Related