Java Coding 6-extra David Davenport Computer Eng. Dept., Computational complexity David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr
IMPORTANT… Students… Instructors… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.
The Story so far… Searching Sorting O(N) – on unordered list O(log2N) – on ordered list! Sorting O(N2) O(N.log2N) For arrays (storing data in first N elements) Insertion & deletion O(1) - unordered O(N) - ordered Different implementations offer different complexity measures. Slide shows values for the common simple method of storing valid data in first N elements of array Note that, in practice, may also need to be concerned with initialisation. What is its complexity? Consider alternative implementations, eg. using –1 values to mark invalid array elements. Next semester we will look at other more complex data structures that offer different performance characteristics.
Powers of 2… log2N N N2 N times N2 times 10 103 106 20 1012 30 109 1,024 103 106 1/1000 sec 1 second 20 1,048,576 1012 ~12 days 30 1,073,741,824 109 1018 16 minutes ~30,000 years 40 1,099,511,627,776 1024 50 1,125,899,906,842,624 1015 1030 60 1,152,921,504,606,846,976 1036 70 1,180,591,620,717,411,303,424 1021 1042 80 1,208,925,819,614,629,174,706,176 1048 ~32 E 12 microseconds in a year! @ 1 E 6 operations / sec
A “simple” Decision Problem Does a 5 by 5 arrangement exist? Yes/No? Worst case: Need to check all possible arrangements No rotation. 25 possible pieces for first square, 24 left for next, then 23 for next, and so on. Gives 25 factorial. Big bang was ~15 billion years ago! Oops! How many arrangements? 25x24x23x … x3x2x1 = 25! How long @ 1 million/second? ~490 billion years
Values for various functions 10 50 100 300 1000 logN >4 6 7 9 N N.logN 33 282 665 2469 9966 N2 2500 10000 90000 1 million (7 digits) 2N 1024 16-digit number 31-digit number 91-digit number 302-digit number N! 3.6 million (7 digits) 65-digit number 161-digit number 623-digit number Unimaginably large NN 10 billion (11 digits) 85-digit number 201-digit number 744-digit number Polynomial For comparison: number of protons in known universe has 79 digits number of microseconds since big bang has 24 digits Polynomial time algorithms are said to be reasonable or tractable Exponential time algorithms are said to be unreasonable or intractable! Source: Algorithmics, David Harel. Exponential
Times (at one million instructions per second) 10 50 100 300 1000 logN 1/10000 second N 1/1000 second N.logN 1/100 second N2 1 second 2N 35.7 years 400 trillion centuries 75-digit # centuries N! 3.6 seconds 49-digit # centuries NN 2.8 hours 70-digit # centuries 185-digit # centuries 728-digit # centuries Polynomial For comparison: big bang was about 15 billion years ago. Tower of Hanoi problem 2 to the power N 64 rings @ 1 million per second = 0.5 million years! Tiling puzzles see next slide… Adapted from: Algorithmics, David Harel. Exponential
Worse still… Tractable (polynomial) Intractable (exponential) Uncomputable e.g. Halting problem! Should we be concerned?