160 likes | 629 Views
Yonsei Univ. at Wonju Dept. of Mathematics 이산수학 Discrete Mathematics Prof. Gab-Byung Chae. Slides for a Course Based on the Text Discrete Mathematics & Its Applications (6 th Edition) by Kenneth H. Rosen. Module #5: Algorithms. Rosen 6 th ed., §2.1 ~31 slides, ~1 lecture.
E N D
Yonsei Univ. at Wonju Dept. of Mathematics이산수학Discrete MathematicsProf. Gab-Byung Chae Slides for a Course Based on the TextDiscrete Mathematics & Its Applications (6th Edition)by Kenneth H. Rosen 채갑병
Module #5:Algorithms Rosen 6th ed., §2.1 ~31 slides, ~1 lecture 채갑병
§2.1: Algorithms • The foundation of computer programming. • Most generally, an algorithm just means a definite procedure for performing some sort of task. • A computer program is simply a description of an algorithm in a language precise enough for a computer to understand, requiring only operations the computer already knows how to do. • We say that a program implements (수단, 방법, or “is an implementation of”) its algorithm. 채갑병
Algorithms You Already Know • Grade school(초등학교) arithmetic algorithms: • How to add any two natural numbers written in decimal on paper using carries(한자리 올리는것 ). • Similar: Subtraction using borrowing. • Multiplication & long division. • Your favorite cooking recipe. 채갑병
Programming Languages • Some common programming languages: • Newer: Java, C, C++, Visual Basic, JavaScript, Perl, Tcl, Pascal, Python • Older: Fortran, Cobol, Lisp, Basic • Assembly languages, for low-level coding. • In this class we will use an informal, Pascal-like “pseudo-code” language. 채갑병
Algorithm Example (English) • Task: Given a sequence {ai}=a1,…,an, aiN, say what its largest element is. Step 1. Set the value of a temporary variablev (largest element seen so far, 프로그램 진행중 임의의 시점에서 가장 큰 수를 저장 하기 위한 변수.) to a1’s value. Step 2. Look at the next element ai in the sequence. 채갑병
Step 3. If ai>v, then re-assign v to the number ai. Step 4. Repeat previous 2 steps until there are no more elements in the sequence, & return v. 채갑병
Executing an Algorithm • When you start up a piece of software, we say the program or its algorithm are being run or executed by the computer. • Given a description of an algorithm, you can also execute it by hand, by working through all of its steps on paper. • Before ~WWII, “computer” meant a person whose job was to run algorithms! 채갑병
Executing the Max algorithm • Let {ai}=7,12,3,15,8. Find its maximum… • Set v = a1 = 7. • Look at next element: a2 = 12. • Is a2>v? Yes, so change v to 12. • Look at next element: a2 = 3. • Is 3>12? No, leave v alone…. • Is 15>12? Yes, v=15… 채갑병
Algorithm Characteristics Some important features of algorithms: • Input. Information or data that comes in. • Output. Information or data that goes out. • Definiteness. Precisely defined.(용어정의) • Correctness. Outputs correctly relate to inputs. • Finiteness. Won’t take forever to describe or run. • Effectiveness. Individual steps are all do-able. • Generality. Works for many possible inputs. • Efficiency. Takes little time & memory to run. 채갑병
procedurename(argument: type) variable:=expression informal statement beginstatementsend {comment} ifcondition then statement [else statement] for variable:=initial value to final valuestatement whileconditionstatement procname(arguments) Not defined in book: returnexpression Our Pseudocode Language 채갑병
Max procedure in pseudocode proceduremax(a1, a2, …, an: integers) v:=a1 {largest element so far} fori:= 2 ton {go thru rest of elems} ifai > vthen v:=ai {found bigger?} {at this point v’s value is the same as the largest integer in the list} returnv 채갑병
Another example task • Problem of searching an ordered list. • Given a list L of n elements that are sorted into a definite order (e.g., numeric, alphabetical, 순서대로 나열된 리스트), • And given a particular element x, • Determine whether x appears in the list, • and if so, return its index (position) in the list. • Let’s find an efficient algorithm! 채갑병
Search alg. #1: Linear Search procedurelinear search(x: integer, a1, a2, …, an: distinct integers)i:= 1while (i n x ai)i:=i + 1ifi n then location:=ielselocation:= 0return location {index or 0 if not found} 채갑병
Search alg. #2: Binary Search • Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it, and quickly zero in(영점조준, on the desired element. <x <x <x >x 채갑병
Search alg. #2: Binary Search procedurebinary search(x:integer, a1, a2, …, an: distinct integers)i:= 1 {left endpoint of search interval}j:=n {right endpoint of search interval}while i<j begin {while interval has >1 item}m:=(i+j)/2 {midpoint}ifx>amtheni := m+1 else j := mendifx = aithenlocation:=ielselocation:= 0returnlocation 채갑병