1 / 30

2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세

이산수학 (Discrete Mathematics)  알고리즘 (Algorithms). 2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세. Algorithms. Algorithms. The foundation of computer programming. ( 컴퓨터 프로그래밍의 기초 / 기반이다 .)

almira
Download Presentation

2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세

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. 이산수학(Discrete Mathematics) 알고리즘 (Algorithms) 2014년 봄학기 강원대학교 컴퓨터과학전공 문양세

  2. Algorithms 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.(컴퓨터 프로그램이란 정의된 알고리즘을 컴퓨터가 이해할 수 있는 언어로 정확하게 기술한 것에 불과하다.) We say that a program implements (or “is an implementation of”) its algorithm.(프로그램은 알고리즘을 “구현한 것이다”라고 이야기한다.)

  3. Programming Languages Algorithms • Some common programming languages: • Newer: Java, C, C++, Visual Basic, JavaScript, Perl, Tcl, Pascal • Older: Fortran, Cobol, Lisp, Basic • Assembly languages, for low-level coding. • In this class we will use an informal, Pascal-like “pseudo-code” language. • You should know at least 1 real language!

  4. Algorithm Example (in English) Algorithms • Task: Given a sequence {ai}=a1,…,an, aiN, say what its largest element is. (주어진 sequence에서 가장 큰 수는?) • Algorithm in English • Set the value of a temporary variablev to a1’s value. • Look at the next element ai in the sequence. • If ai>v, then re-assign v to the number ai. • Repeat previous 2 steps until there are no more elements in the sequence, & return v.

  5. Executing an Algorithm Algorithms 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!(2차 대전 이전에, 컴퓨터는 알고리즘을 수행하는 사람을 의미했다!)

  6. Execute the Max Algorithm Algorithms Let {ai}=7,12,3,15,8. Find its maximum… by hand. Set v = a1 = 7. Look at next element: a2 = 12. Is a2>v? Yes, so change v to 12. Look at next element: a3 = 3. Is 3>12? No, leave v alone…. Is 15>12? Yes, v=15…

  7. Algorithm Characteristics (알고리즘의 특성) (1/2) Algorithms 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.

  8. Algorithm Characteristics (알고리즘의 특성) (2/2) Algorithms Some important features of algorithms (계속): Effectiveness(효율성): Individual steps are all do-able. (각 단계는 유한한 양의 시간에 수행되어야 한다.) Generality(일반성): Works for many possible inputs. (특정한 입력이 아닌 모든 가능한 입력에 대해서 동작하여야 한다.)

  9. Declaration STATEMENTS PseudocodeLanguage Algorithms 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

  10. procedureprocname(arg: type) Algorithms • Declares that the following text defines • a procedure named procname that takes • inputs (arguments) named arg which are • data objects of the type type. • Example: proceduremaximum(L: list of integers) [statements defining maximum…]

  11. variable:=expression Algorithms • An assignment statement evaluates the expression, then reassigns the variable to the value that results. • Example: v := 3x+7 (If x is 2, changes v to 13.) • In pseudocode, the expression might be informal: • x := the largest integer in the list L

  12. Informal Statement Algorithms • Sometimes we may write an informal statement, if the meaning is still clear and precise: “swap x and y.” • Keep in mind that real programming languages never allow this. (궁극적으로는 알고리즘을 쓰고 이를 구현해야 한다.) • When we ask for an algorithm to do so-and-so, writing “Do so-and-so” isn’t enough! (“x를 찾는 알고리즘을 기술하라”했는데, “find x”라 하는 것은 충분치 않다!) • Break down algorithm into detailed steps.

  13. beginstatementsend Algorithms • Groups a sequence of statements together: beginstatement 1statement 2 …statement nend • Allows sequence to be used like a single statement. • Might be used: • After a procedure declaration. • In an if statement after then or else. • In the body of a for or while loop.

  14. { comment} Algorithms • Not executed (does nothing). • Natural-language text explaining some aspect of the procedure to human readers. (Reader의 이해 도모) • Also called a remark in some real programming languages. • Example: • {Note that v is the largest integer seen so far.}

  15. Ifconditionthenstatement Algorithms Evaluate the propositional expression condition. If the resulting truth value is true, then execute the statement; otherwise, just skip on ahead to the next statement. (조건이 true일 때만 문장을 수행한다.) Variant: ifcondthenstmt1elsestmt2 Like before, but iff truth value is false, executes stmt2.

  16. whileconditionstatement (1/2) Algorithms Evaluate the propositional expression condition. If the resulting value is true, then execute statement. Continue repeating the above two actions over and over until finally the condition evaluates to false; then go on to the next statement.(조건이 true인 한 문장을 반복하여 수행한다.)

  17. whilecommentstatement (2/2) Algorithms Also equivalent to infinite nested ifs, like so:(if를 무한히 써서 구현할 수도 있다…. 설마~) if conditionbeginstatement if conditionbeginstatement …(continue infinite nested if’s)endend

  18. forvar:= initial to final stmt Algorithms Initial is an integer expression. Final is another integer expression. Repeatedly execute stmt, first with variable var := initial, then with var := initial+1, then with var := initial+2, etc., then finally with var := final. What happens if stmt changes the value that initial or final evaluates to? For can be exactly defined in terms of while, like so: beginvar := initialwhilevar finalbeginstmtvar:=var + 1endend

  19. procedure(argument) Algorithms • A procedure call statement invokes the named procedure, giving it as its input the value of the argument expression. • Various real programming languages refer to procedures as • functions (since the procedure call notation works similarly to function application f(x)), or as • subroutines, subprograms, or methods.

  20. Max Procedure in Pseudocode Algorithms Rewrite “finding maximum number” 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

  21. Inventing an Algorithm Algorithms • Requires a lot of creativity and intuition. • Like writing proofs. • We can’t give you an algorithm for inventing algorithms. • Just look at lots of examples… [많은 예를 보세요…] • And practice (preferably, on a computer) [연습을 많이 하세요…] • And look at more examples… [좀 더 많은 예를 보세요…] • And practice some more… etc., etc. [좀 더 많은 연습을 하세요…]

  22. Searching Algorithm (탐색 알고리즘) Algorithms • 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. • Problem occurs often in many contexts. (여러 분이 Programming을 하는 한 수십 번 이상 마주치는 문제임!) • Let’s find an efficient algorithm!

  23. Linear Search (선형 탐색) Algorithms 리스트의 첫 번째 원소부터 차례대로 검색하는 방법 예: Find ‘12’ in {3, 6, 9, 12, 15, 18, 21, 24} 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} Linear search는 ordered list(정렬된 상태)뿐 아니라 unordered list(정렬되지 않은 상태)에서도 바르게 동작한다.

  24. Binary Search (이진 탐색) (1/2) Algorithms Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it. (리스트의 중간에 위치한 값과 비교하여, 작은 값들을 가지는 리스트 혹은 큰 값들을 가지는 리스트 중에서 한쪽 부분에 대해서만 검사를 진행한다.) 예: Find ‘18’ in { 3, 6, 9, 12, 15, 18, 21, 24 } <x <x <x >x

  25. Binary Search (이진 탐색) (2/2) Algorithms 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 := mend ifx = aithenlocation:=ielselocation:= 0returnlocation Binary search는 ordered list(정렬된 상태)에서만 바르게 동작할 뿐 unordered list(정렬되지 않은 상태)에서는 바르게 동작하지 않는다.

  26. Sorting Algorithm (정렬 알고리즘) Algorithms • Sorting is a common operation in many applications. (Programming을 하는 한 Search 다음으로 많이 마주치는 문제임!) • E.g. spreadsheets and databases • It is also widely used as a subroutine in other data-processing algorithms. • Two sorting algorithms shown in textbook: • Bubble sort • Insertion sort • There are more than 15 different sorting algorithms! (“The Art of Computer Programming” by Donald Knuth.) However, these are not very efficient, and you should not use them on large data sets!

  27. Bubble Sort – Example Algorithms Sort L = {3, 2, 4, 1, 5} 1st pass 2nd pass 3 2 4 1 5 2 3 4 1 5 2 3 4 1 5 2 3 1 4 5 2 3 1 4 5 2 3 1 4 5 2 1 3 4 5 3rd pass 4th pass 2 1 3 4 5 1 2 3 4 5 1 2 3 4 5

  28. Bubble Sort – Algorithm Algorithms procedurebubble sort(a1, a2, …, an: real numbers with n 2)for i := 1to n - 1 for j := 1to n - iifaj > aj+1then interchange aj and aj+1 {a1, a2, …, an is in increasing order.}

  29. Insertion Sort Algorithms 3 2 4 1 5 2 3 4 1 5 2 3 4 1 5 1 2 3 4 5 2 3 4 1 5 2 3 4 1 5 1 2 3 4 5 1 2 3 4 5 procedureinsertion sort(a1, a2, …, an: real numbers with n 2)for j := 2to ni := 1 while aj > ai {find a proper position of aj}i := i + 1 m := ajfork := 0 toj – i – 1 {insert aj into the proper position}aj-k := aj-k-1ai := m {a1, a2, …, an is in increasing order.}

  30. Greedy Algorithm Algorithms • 최적의 알고리즘(optimal algorithm)을 찾기가 어렵다?  알고리즘의 각 단계에서 최선을 선택을 수행하는 Greedy algorithm을 사용할 수 있다. • Greedy algorithm은 최적일 수도 있다. • 최적임을 보이기 위해서는 증명이 필요하고, 최적이 아님을 보이기 위해서는 반례(counterexample)를 든다. • 예제 • 25, 10, 5, 1센트의 동전이 있을 때, 동전의 수를 최소화하면서 67센트의 거스름돈을 만들어라. • 25센트 동전을 선택한다. (42센트 남음) • 25센트 동전을 선택한다. (17센트 남음) • 10센트 동전을 선택한다. (7센트 남음) • 5센트 동전을 선택한다. (2센트 남음) • 1센트 동전을 선택한다. (1센트 남음) • 1센트 동전을 선택한다. (1센트 남음)

More Related