The Role of Algorithms in Computing Chapter 1 1 CPTR 430 Algorithms The Role of Algorithms in Computing
What is an Algorithm? ■ Any well-defined computational procedure that takes some value (or set of values)—its input —and produces some value (or set of values)—its output ■ A sequence of computational steps that transforms its input into its output ■ A tool for solving a well-specified computational problem 2 CPTR 430 Algorithms The Role of Algorithms in Computing
✁ ☎ ✁ ☎ ✆ ✝ ✆ ✝ ✞ ✞ ✞ ✝ ✆ ✄ ✁ ✁ ✁ ✁ � ✁ ✁ ✁ ✁ � ✁ ☎ ✆ ✄ ✂ ✂ ✁ ☎ ✄ ✁ ✆ ✂ ✂ ✆ ✁ ✁ ✁ ✂ � ✂ � Example—Sorting ■ Input : a sequence of n numbers a 0 a 1 a n 1 ■ Output : a permutation (re-ordering) a a a 0 1 1 n such that a a a 0 1 1 n ■ Instance of problem : 31 41 59 26 41 58 Input: Output: 26 31 41 41 58 59 3 CPTR 430 Algorithms The Role of Algorithms in Computing
Algorithms for Sorting ■ Sorting is an important operation in computer science Many programs sort as an intermediate step ■ Thus, a large number of good sorting algorithms have been developed ■ Which is best ? 4 CPTR 430 Algorithms The Role of Algorithms in Computing
Algorithm Correctness ■ An algorithm is correct if, for every input instance, it halts with the correct output ■ A correct algorithm solves the given computational problem ■ An incorrect algorithm ❚ might not halt on some input instances, or ❚ might halt with incorrect output ■ Can an incorrect algorithm ever be useful? 5 CPTR 430 Algorithms The Role of Algorithms in Computing
Expressing Algorithms Algorithms can be expressed in ■ English (or any other natural language) ■ a computer program ■ a hardware design Requirement: The specification must provide a precise description of the computational procedure to be followed 6 CPTR 430 Algorithms The Role of Algorithms in Computing
Data Structures ■ A data structure is a way to store and organize data in order to facilitate access and modifications ■ No single data structure works well for all purposes ❚ It is advantageous to be familiar with a wide variety of data structures and be aware of their strengths and weaknesses 7 CPTR 430 Algorithms The Role of Algorithms in Computing
Hard Problems ■ We are most concerned about efficient algorithms An algorithm’s efficiency is often measured by its running time ■ Some problems have no known efficient solutions ❚ These are called NP-complete problems ❚ No one has proven that efficient algorithms for NP-complete problems do not exist ❚ If an efficient solution can be found for one NP-complete problem, then an efficient solution exists for all other NP-complete problems! ❚ NP-complete problems are very similar to problems that have an efficient solution A small change to the problem statement can cause a big change to the efficiency of the best known algorithm 8 CPTR 430 Algorithms The Role of Algorithms in Computing
Why study NP-Complete Problems? ■ They often arise in everyday situations ■ If you are asked to produce an efficient algorithm to to find an exact solution, . . . ■ If you can show that the problem is equivalent to a known NP-complete problem, then you can devote your efforts to developing an algorithm that gives a good approximation of the solution The traveling salesman problem (TSP) is one example of a common NP- complete problem 9 CPTR 430 Algorithms The Role of Algorithms in Computing
Algorithm Efficiency ■ Computing resources—time and space—are bounded resources ■ Efficient algorithms use computing resources wisely ■ Algorithms to solve the same problem often differ drastically in their efficiencies ■ Compare insertion sort and merge sort 10 CPTR 430 Algorithms The Role of Algorithms in Computing
☎ ✄ � ✄ ✄ � ✂ ✁ ✂ ✁ ✂ ✁ Insertion Sort vs. Merge Sort c 1 n 2 to sort n elements ■ Insertion sort takes time ■ Merge sort takes time n to sort n elements c 2 n ■ c 1 and c 2 are constants that do not depend on n ■ Usually c 1 c 2 ■ Comparing insertion sort vs. merge sort, note that n n ■ Given a large enough n , the n advantage of merge sort will n overwhelm the c 1 c 2 advantage of insertion sort 11 CPTR 430 Algorithms The Role of Algorithms in Computing
� � Concrete Example ■ The hardware: ❚ 1 GHz Pentium III vs. 10 MHz 386 ❚ In raw processing speed the Pentium is 100 times faster than the 386 ■ The algorithms: ❚ Insertion sort vs. merge sort ❚ Insertion sort implemented in assembly language by a team of 2 algorithm/assembler experts, so c 1 ❚ Merge sort implemented in C with a non-optimizing compiler by an 50 average programmer, so c 2 ■ The task: sort 1,000,000 integers 12 CPTR 430 Algorithms The Role of Algorithms in Computing
� � � ✞ � ✂ ✁ ✁ ✁ ✞ The Results 10 6 2 instructions 2 Pentium: 2000 seconds 10 9 instructions second 10 6 instructions 10 6 50 386: 100 seconds 10 7 instructions second The merge sort is 20 times faster than the insertion sort! For 10,000,000 numbers: 2.3 days vs. 20 minutes! 13 CPTR 430 Algorithms The Role of Algorithms in Computing
Recommend
More recommend