Q & A.

Slides:



Advertisements
Similar presentations
Algorithms.
Advertisements

MATH 224 – Discrete Mathematics
Chapter 5 Algorithms. 2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Chapter 19: Searching and Sorting Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Chapter 2 The Algorithmic Foundations of Computer Science
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 2: Algorithm Discovery and Design
Describing Syntax and Semantics
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition by J.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
“The study of algorithms is the cornerstone of computer science.” Algorithms Fall 2011.
Chapter 5 Algorithms Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information Technology.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Algorithm: definition An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
Chapter 5 Algorithms. © 2005 Pearson Addison-Wesley. All rights reserved 5-2 Chapter 5: Algorithms 5.1 The Concept of an Algorithm 5.2 Algorithm Representation.
Chapter 5: 演算法 Algorithms 陳以德助理教授 : 高醫大舊二棟 轉 2586
Data Structure Introduction.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
Computer Science: An Overview Eleventh Edition
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CMPT 438 Algorithms.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion Topic 5.
Algorithms and Problem Solving
Analysis of Algorithms
Computer Science: An Overview Eleventh Edition
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction to Algorithms
Teach A level Computing: Algorithms and Data Structures
CS 240 – Lecture 11 Pseudocode.
Unit# 9: Computer Program Development
Chapter 5: Algorithms 5.1 The Concept of an Algorithm
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Objective of This Course
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Algorithm Discovery and Design
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm Discovery and Design
CSE 2010: Algorithms and Data Structures Algorithms
Algorithms and Problem Solving
This Lecture Substitution model
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Invitation to Computer Science 5th Edition
Computer Science: An Overview Tenth Edition
Presentation transcript:

Q & A

P96 1 C000 should be considered?

About Homework Some notices P52 6 P81 4, 5, 6, 7 P87 1, 2, 3, 4

Ch. 4 Algorithms The concept of an algorithm. Algorithm representation. Algorithm discovery. Iterative structures. Recursive structures. Efficiency and correctness.

An Informal Review Up to now, we have known: ● Converting numeric representation from one form to another; ● Detecting and correcting errors in data; ● Compressing and decompressing data files; ● Controlling timesharing in a multitasking environment; ● Algorithm can be expressed in machine language, and how it is executed.

Example of Algorithm Obtain a basket of unshelled peas and an empty bowl; As long as there are still peas in the basket continue to execute the following steps: a Take a pea from the basket b Break open the pea pod c Dump the peas form the pod into bowl d Discard the pod.

The definition of an algorithm A procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation; broadly: a step-by-step procedure for solving a problem or accomplishing some end especially by a computer. ---- From Webster

Class Review

Figure 4.1: The definition of an algorithm

The Concept of an Algorithm The steps of an algorithm are ordered; The steps of an algorithm are executable; Execution of an algorithm must led to an end. Make a list of all positive integers Is not executable.

Computer scientists use the term effective to capture the concept of being executable. That is, to say that a step is effective means that it is doable. Effective (有效的)

Algorithm VS Program Algorithm can be described through programs. One single algorithm can be described in various ways using different programs.

The Abstract nature of Algorithm The difference between algorithm and its representation is analogous to that between a story and a book. The story is abstract, conceptual, a book is the physical representation of the story, if the book has been rewritten in another language,that means the representation of that story has been changed, but the story itself remains the same.

4.2 Algorithm Representation Representing algorithm needs some form of languages. In the case of humans this may be a traditional language, language of pictures. Problem is that these language are ambiguous(不明确的)!

Primitives (原语) Primitive is a set of well-defined building blocks with which algorithm representations can be constructed. Primitive - graphical and texture. Primitive => programming language. Primitive - syntax and semantics.

Assigning precise definition to these primitives removes many problems of ambiguity, and requiring algorithms to be described in terms of these primitives establishes a uniform level of detail. A collection of primitives along with a collection of rules stating how the primitives can be combined to represent more complex ideas constitutes a programming language.

Each primitive consists of two parts: its syntax and its semantics. Syntax(语法) refers to the primitive's symbolic representation, and Semantics(语义) refers to the meaning of the primitive. e.g. The syntax of air consists of three symbols, whereas the semantics is a gaseous substance that surrounds the world

Figure 4.2: Folding a bird from a square piece of paper (continued)

Figure 4.2: Folding a bird from a square piece of paper

Figure 4.3: Origami primitives (continued)

Figure 4.3: Origami primitives

Pseudocode (伪码) Pseudocode - is a notational system in which ideas can be expressed informally during the algorithm development process. Pseudocode is to develop a consistent, concise notation for representing recurring semantic structures.

1. Assignment statement Assignment of values(赋值) to descriptive names: Format: name  expression Example RemainingFunds  CheckBalance+SavingBalance

2. if, then, else Select one of two possible activities depending on the truth or falseness of some condition. eg. If you have more than $10 buy a cake; otherwise buy nothing => if (cond) then (act1) else (act2) If ( year is leap year) then (daily total  total/366) else (daily total  total/365)

3. While do while there are tickets to sell, keep selling tickets. While(condition) do (activity) eg. While (tickets remain to be sold) do ( sell tickets)

Figure 4.8: The while loop structure

Indentation(缩进) is used to enhance the readability of a program if (item is taxable) then ( if (price>limit) then ( pay x) else (pay y) ) else (pay y) A little notice

Procedure( 过程) Form: Procedure name procedure: Program unit that contains activities that can be used in other applications. Form: Procedure name

Figure 4.4: The procedure Greetings in pseudocode -

Example of procedure calls(过程调用) If ( ….) then ( execute the procedure ProcessLoan) else ( execute the procedure rejectApplication)

Other Statement syntax (其它语句语法) While ( ….) do { … } end while If ( ) then {… } end if C 语言没有,某些脚本语言有

4.3 Algorithm Discovery The development of a program consists of two activities - discovering the underlying algorithm and representing that algorithm as a program.

The basic principles for problem-solving (G. Polya ,1945) 1. Understand the problem. 2. Get an idea as to how an algorithmic procedure might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the program for accuracy and for its potential as a tool for solving other problems.

Example: Person A is charged with the task of determining the ages of person B’s three children. B tells A that the product of three children’s ages is 36. After considering this clue, A replies that another clue is required. So B tells A the sum of the children’s ages again, A replies that another clue is needed,So B tells A that the oldest child plays piano. Then A gives the ages of three children.

Figure 4.5

Algorithm Discovery Conscious work Vs. inspiration. Stepwise refinement - a top-down methodology.

Getting a Foot in the Door One is to try working the problem backward. For instance, if the problem is to find a way of producing a particular output from a given input, One may start with that output , and attempt to back up to the given input. Another general problem solving approach is to look for a related problem that is either easier to solve or has been solved before and then try to apply its solution to the current problem.

Still another approach to getting a foot in the door is to apply stepwise refinement, which is essentially the technique of not trying to conquer an entire task (in all its gory detail) at once. Rather, stepwise refinement proposes that one first view the problem at hand in terms of several subproblems. The idea is that by breaking the original problem into subproblems, one is able to approach the overall solution in terms of steps, each of which is easier to solve than the entire original problem.

4.4 Iterative Structures Iterative structures - a collection of instructions is repeated in a looping manner. The while loop structure. The repeat loop structure. The insertion sort algorithm.

The sequential search(顺序搜索) algorithm Searching a list for the occurrence of a particular target value Select ( the first entry in the list as TestEntry) While (TargetValue > TestEntry and there remain data entries to be considered) do (select the next entry in the list as TestEntry) We assume that the list is sorted according to some rule for ordering its entries.

If ( TargetValue= TestEntry) then ( declare the search a success) Else ( declare the search a failure) If ( list is empty) then ( declare search a failure) else (…….)

Figure 4.6: The sequential search algorithm in pseudocode

Loop Control The repetitive use of an instruction or sequence of instructions is an important algorithmic concept.One method of implementing such repetition is the iterative structure known as loop . In which a collection of instructions,called the body of the loop, is executed in a repetitive fashion under the direction of some control processes.

1. while ( cond) do ( body) Loop structure’s execution traces the cyclic pattern: check the condition; execute the body; check the condition; execute the body: ….. …… ……

As a general rule, the use of a loop structure produces a higher degree of flexibility than would be obtained merely by explicitly writing the body several times.

While ( the PH level is greater than 4) do ( add a drop of sulfuric acid ) We can’t predict how many times the loop will be executed because we do not know in advance how many drops of acid will required.

Figure 4.8: The while loop structure

Figure 4.7: Components of repetitive control Loop control consists of three activities: Figure 4.7: Components of repetitive control

Termination condition may be in implicit expression; eg. In figure 4.6 the termination condition is ( TargetValue <=TestEntry) or ( there are no more data entries can be considered)

We should emphasize that the initialization and modification steps must lead to the appropriate termination condition. This characteristic is critical for proper loop control, and thus one should always double-check for its presence when designing a loop structure. Failure to make such an evaluation can lead to errors even in the simplest cases.

while(Number 6) do Number Number +2 It will not terminated. Pretest loop posttest loop Bad example

2. repeat ( activity ) until (condition) Eg. repeat (take a coin from your pocket) until (there are no coins in your pocket) This program assumes there is a coin in your pocket at the beginning. Different from while…do

Figure 4.9: The repeat loop structure

Figure 4.6: The sequential search algorithm in pseudocode

The Insertion Sort algorithm Let us consider the problem of sorting a name list in alphabetic order Constraint: Sorting the list within itself, ie. we sort the list by shuffling its entries as opposed to moving the list to other place. Shuffle 拖曳、搅乱

Figure 4.11: The insertion sort algorithm expressed in pseudocode

Note that the structure of Figure 4 Note that the structure of Figure 4.11 is that of a loop within a loop, the outer loop being expressed by the first while statement and the inner loop represented by the second while statement. Each execution of the body of the outer loop results in the inner loop being initialized and executed until its termination condition is obtained. Thus a single execution of the outer loop's body will result in several executions of the inner loop's body.

Figure 4.10: Sorting the list Fred, Alice, David, Bill, and Carol alphabetically

Figure 4.10: Sorting the list Fred, Alice, David, Bill, and Carol alphabetically (continued)

Figure 4.10: Sorting the list Fred, Alice, David, Bill, and Carol alphabetically (continued)

Another Example

4.5 Recursive Structures Recursive structures provide an alternative to the loop paradigm for repetitive structures (by invoking itself). The binary search algorithm. The quick sort algorithm.

The binary search algorithm (二分查找算法) Algorithm principle: Take the middle entry of the list as TestEntry, if the TargetValue =TestValue, We have done, otherwise,we can at least restrict our search process to the first or last half of the list depending on whether the TargetValue is less than or greater than the entry we have considered. We assume that the list is sorted according to some rule for ordering its entries.

Figure 4.12: Applying our strategy to search a list for the entry John

Another Example

Figure 4.13: A first draft of the binary search technique

Figure 4.14: The binary search algorithm in pseudocode

Notice: If a list has an even number of entries, let us agree that the middle entry refers to the first entry in the second half of the list.

Figure 4.15: TargetValue: Bill

Figure 4.16: TargetValue: David

Figure 4.17: (continued) TargetValue: David

Figure 4.17 TargetValue: David

4.6 Efficiency and Correctness You can develop a variety of algorithms to solve the same problem. However, the choice between efficient and inefficient algorithms can make the difference between a practical solution to a problem and an impractical one. Time and storage complexity of the algorithm.

Let us consider the problem of a university registrar faced with the task of retrieving and updating student records. Although the university has an actual enrollment of approximately 10,000 students during any one semester, its "current student file" contains the records of more than 30,000 students who are considered current students in the sense that they have registered for at least one course in the past few years but have not completed a degree. For now, let us assume that these records are stored in the registrar's computer as a list ordered by student identification numbers. To find any student record, the registrar would therefore search this list for a particular identification number.

Given a student identification number, the sequential search algorithm starts at the beginning of the list and compares the entries it finds to the number desired. Not knowing anything about the source of the target value, we cannot conclude how far into the list this search must go. We can say, though, that after many searches we expect the average depth of the searches to be halfway through the list, some will be shorter, but others will be longer. We conclude that over a period of time, the sequential search will investigate roughly 15,000 records per search. If retrieving and checking each record for its identification number requires 10 milliseconds (10 one-thousandths of a second), such a search would require an average of 150 seconds or 2.5 minutes-an unbearably long time for the registrar to wait for a student's record to appear on the screen.

In contrast, the binary search proceeds by comparing the target value to the middle entry in the list. If this is not the desired entry, then at least the remaining search is restricted to only half of the original list. Thus, after interrogating the middle entry in the list of 30,000 student records, the binary search has at most 15,000 records still to consider. After the second inquiry, at most 7500 remain, and after the third retrieval, the list in question has dropped to no more than 3750 entries. Continuing in this fashion, We see that the target record will be found after retrieving at most 15 entries from the list of 30,000 records. Thus if each of these retrievals can be performed in 10 milliseconds, the process of searching for a particular record requires only 0.15 seconds

when applied to a list with n entries the sequential search algorithm will interrogate an average of n/2 entries whereas the binary search algorithm will interrogate at most log n entries in its worst-case scenario. (log n represents the base two logarithm of n.)

The total number of comparisons when sorting a list of n entries using the insertion sort is:  In the worst- case l +2 + 3 + . . . + (n- l), which is equivalent to n(n - 1)/2  In the average case n(n - 1)/4

Figure 4.18: Applying the insertion sort in a worst-case situation Pivot 支点

Figure 4. 19: Graph of the worst-case analysis of Figure 4.19: Graph of the worst-case analysis of the insertion sort algorithm

Figure 4. 20: Graph of the worst-case analysis of Figure 4.20: Graph of the worst-case analysis of the binary search algorithm

The shape of the graph obtained by comparing the time required for an algorithm to perform its task to the size of the input data reflects the efficiency characteristics of the algorithm. Thus it is common to classify algorithms according to the shapes of these graphs normally based on the algorithm's worst-case analysis. The notation used to identify these classes is sometimes called "big-theta notation."

asymptotic [9AsImp`tCtik] adj. [数]渐近线的, 渐近的

The total number of comparisons when sorting a list of n entries using the insertion sort is:  In the worst- case l +2 + 3 + . . . + (n- l), which is equivalent to n(n - 1)/2 = n2/2- n/2  In the average case n(n - 1)/4

Figure 4.18: Applying the insertion sort in a worst-case situation

All algorithms whose graphs have the shape of a parabola, such as the insertion sort, are classified in the class represented by Θ(n2) (read "big theta of n squared")

Like binary search algorithm All algorithms whose graphs have then shape of a logarithmic expression, such as the binary search, fall into the class represented by Θ (log n) (read “big theta of log n”) Like binary search algorithm

Like sequential search algorithm All algorithms whose graphs have then shape of a linear expression, such as the sequential search, fall into the class represented by Θ (n) (read “big theta of log n”) Like sequential search algorithm

Software Verification Knowing the class in which a particular algorithm falls allows us to predict its performance and to compare it against other algorithms that solve the same problem. Software Verification

How to make sure the algorithm and program developed is correct? Difference between testing and verification. Precondition, assertions, loop invariant.

An Example: A traveler with a gold chain of seven links must stay in an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?

Figure 4.21: Separating the chain using only three cuts

Figure 4.22: Solving the problem with only one cut

One current line of research in this area attempts to apply the techniques of formal logic to prove the correctness of a program

 A proof of correctness begins with the assumption that certain conditions, called preconditions, are satisfied at the beginning of the program's execution.  The next step in a proof of correctness is to consider how the consequences of these preconditions propagate through the program.

A proof of correctness proceeds by identifying statements, called assertions, that can be established at various points in the program. The result is a collection of assertions, each being a consequence of the program's preconditions and the sequence of instructions that lead to the point in the program at which the assertion is established. If the assertion so established at the end of the program corresponds to the desired output specifications, we can conclude that the program is correct.

Figure 4.23: The assertions associated with a typical while structure

See you next time!