Computer Science: An Overview Eleventh Edition

Slides:



Advertisements
Similar presentations
Algorithms.
Advertisements

Introduction to Algorithms Quicksort
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.
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.
Chapter 2: Algorithm Discovery and Design
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
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
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
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.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Introduction to Computer Systems
Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting 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.
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
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
Chapter 5 Algorithms © 2007 Pearson Addison-Wesley. All rights reserved.
Computer Science: An Overview Eleventh Edition
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Topic 5.
Algorithms and Problem Solving
Chapter 15 Recursion.
Q & A.
Chapter 15 Recursion.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Teach A level Computing: Algorithms and Data Structures
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 5: Algorithms 5.1 The Concept of an Algorithm
Algorithm design and Analysis
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 5: Algorithms Computer Science: An Overview Tenth Edition
Computer Science — An Overview J. Glenn Brookshear
Algorithm Discovery and Design
Search,Sort,Recursion.
Algorithms and Problem Solving
Algorithms: the big picture
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
ITEC324 Principle of CS III
Computer Science: An Overview Tenth Edition
Presentation transcript:

Computer Science: An Overview Eleventh Edition Chapter 5: Algorithms Computer Science: An Overview Eleventh Edition

Chapter 5: Algorithms The Concept of an Algorithm Algorithm Representation Algorithm Discovery Iterative Structures Recursive Structures Efficiency and Correctness

Definition of Algorithm An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

Algorithm Representation Requires well-defined primitives A collection of primitives constitutes a programming language.

Folding a bird from a square piece of paper

Origami primitives

Pseudocode Primitives Assignment name = expression Example RemainingFunds = CheckingBalance + SavingsBalance

Pseudocode Primitives (continued) Conditional selection if (condition): activity Example if (sales have decreased): lower the price by 5%

Pseudocode Primitives (continued) Conditional selection if (condition): activity else: Example if (year is leap year): daily total = total / 366 daily total = total / 365

Pseudocode Primitives (continued) Repeated execution while (condition): body Example while (tickets remain to be sold): sell a ticket

Pseudocode Primitives (continued) Indentation shows nested conditions if (not raining): if (temperature == hot): go swimming else: play golf watch television

Pseudocode Primitives (continued) Define a function def name(): Example def ProcessLoan(): Executing a function if (. . .): ProcessLoan() else: RejectApplication()

The procedure Greetings in pseudocode def Greetings(): Count = 3 while (Count > 0): print('Hello') Count = Count - 1

Pseudocode Primitives (continued) Using parameters def Sort(List): . Executing Sort on different lists Sort(the membership list) Sort(the wedding guest list)

Polya’s Problem Solving Steps 1. Understand the problem. 2. Devise a plan for solving the problem. 3. Carry out the plan. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems.

Polya’s Steps in the Context of Program Development 1. Understand the problem. 2. Get an idea of how an algorithmic function might solve the problem. 3. Formulate the algorithm and represent it as a program. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems.

Getting a Foot in the Door Try working the problem backwards Solve an easier related problem Relax some of the problem constraints Solve pieces of the problem first (bottom up methodology) Stepwise refinement: Divide the problem into smaller problems (top-down methodology)

Here…

Ages of Children Problem Person A is charged with the task of determining the ages of B’s three children. B tells A that the product of the children’s ages is 36. A replies that another clue is required. B tells A the sum of the children’s ages. A replies that another clue is needed. B tells A that the oldest child plays the piano. A tells B the ages of the three children. How old are the three children?

The sequential search algorithm in pseudocode Search (List, TargetValue): if (List is empty): Declare search a failure else: Select the first entry in List to be TestEntry while (TargetValue > TestEntry and entries remain): Select the next entry in List as TestEntry if (TargetValue == TestEntry): Declare search a success

Components of repetitive control int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; }

Iterative Structures Pretest loop: while (condition): body Posttest loop: repeat: until(condition)

The while loop structure

The repeat loop structure

Sorting the list Fred, Alex, Diana, Byron, and Carol alphabetically https://www.cs.usfca.edu/~galles/visualization/Algorithms.html

The insertion sort algorithm expressed in pseudocode Sort(List): N = 2 while (N <= length of List): Pivot = Nth entry in List Remove Nth entry leaving a hole in List while (there is an Entry above the hole and Entry > Pivot): Move Entry down into the hole leaving a hole in the list above the Entry Move Pivot into the hole N = N + 1

Recursion The execution of a procedure leads to another execution of the procedure. Multiple activations of the procedure are formed, all but one of which are waiting for other activations to complete.

Applying our strategy to search a list for the entry John on a sorted data. Binary search

A first draft of the binary search technique if (List is empty): Report that the search failed else: TestEntry = middle entry in the List if (TargetValue == TestEntry): Report that the search succeeded if (TargetValue < TestEntry): Search the portion of List preceding TestEntry for TargetValue, and report the result of that search if (TargetValue > TestEntry): Search the portion of List following TestEntry for

The binary search algorithm in pseudocode Search(List, TargetValue): if (List is empty): Report that the search failed else: TestEntry = middle entry in the List if (TargetValue == TestEntry): Report that the search succeeded if (TargetValue < TestEntry): Sublist = portion of List preceding TestEntry Search(Sublist, TargetValue) Sublist = portion of List following TestEntry

int binarySearch(int[] array, int value, int left, int right) {       if (left > right)             return -1;       int middle = (left + right) / 2;       if (array[middle] == value)           return middle;       else if (array[middle] > value)           return binarySearch(array, value, left, middle - 1);       else           return binarySearch(array, value, middle + 1, right);            }

After calling the inner function

Figure 5.16

Figure 5.17

Algorithm Efficiency Measured as number of instructions executed Big-O notation O(n2) Big theta notation: Used to represent efficiency classes Example: Insertion sort is in Θ(n2) Best, worst, and average case analysis

Applying the insertion sort in a worst-case situation

Graph of the worst-case analysis of the insertion sort algorithm

Graph of the worst-case analysis of the binary search algorithm

Software Verification Proof of correctness Assertions Preconditions Loop invariants Testing

Chain Separating Problem A traveler has a gold chain of seven links. He must stay at 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?

Separating the chain using only three cuts

Solving the problem with only one cut

The assertions associated with a typical while structure

Hanoi tower problem https://www.youtube.com/watch?v=5_6nsViVM00

Design an algorithm that, given two strings of characters, tests whether the second string is same as the first string or not.

Identify the termination condition in the following recursive function. int foo(N) if (N == 5) foo(N + 1)

Design an algorithm to generate the sequence of positive integers (in increasing order) whose only prime divisors are 2 and 3; that is, your program should produce the sequence 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 27.

Design an algorithm that checks whether a given number is an Armstrong number or not. A positive integer is called an Armstrong number if the sum of the cubes of individual digits of the number is equal to that number itself. For example, the sum of cubes of individual digits of the number 153 is 1* 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 = 153. Hence, the number 153 is called an Armstrong number.