The Role of Algorithms in Computing Chapter 1 CPTR 430 Algorithms The Role of Algorithms in Computing 1
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 CPTR 430 Algorithms The Role of Algorithms in Computing 2
a1 41 26 Example Sorting Input: a sequence of n numbers a 0 an 1 Output: a permutation (re-ordering) a a 0 1 a n 1 such that a a 0 1 a n 1 Instance of problem: Input: 31 59 41 58 Output: 26 31 41 41 58 59 CPTR 430 Algorithms The Role of Algorithms in Computing 3
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? CPTR 430 Algorithms The Role of Algorithms in Computing 4
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? CPTR 430 Algorithms The Role of Algorithms in Computing 5
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 CPTR 430 Algorithms The Role of Algorithms in Computing 6
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 CPTR 430 Algorithms The Role of Algorithms in Computing 7
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 CPTR 430 Algorithms The Role of Algorithms in Computing 8
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 NPcomplete problem CPTR 430 Algorithms The Role of Algorithms in Computing 9
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 CPTR 430 Algorithms The Role of Algorithms in Computing 10
Insertion Sort vs. Merge Sort Insertion sort takes time c 1 n 2 to sort n elements Merge sort takes time c 2 n n to sort n elements c 1 and c 2 are constants that do not depend on n Usually c c2 1 Comparing insertion sort vs. merge sort, note that n n Given a large enough n, the overwhelm the c 1 c 2 advantage of insertion sort n n advantage of merge sort will CPTR 430 Algorithms The Role of Algorithms in Computing 11
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 algorithm/assembler experts, so c 1 2 Merge sort implemented in C with a non-optimizing compiler by an average programmer, so c 2 50 The task: sort 1,000,000 integers CPTR 430 Algorithms The Role of Algorithms in Computing 12
The Results Pentium: 2 106 2 instructions 10 9 instructions second 2000 seconds 386: 50 106 106 instructions 10 7 instructions second 100 seconds The merge sort is 20 times faster than the insertion sort! For 10,000,000 numbers: 2.3 days vs. 20 minutes! CPTR 430 Algorithms The Role of Algorithms in Computing 13