Algorithms.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Chapter 1 – Basic Concepts
Introduction to Analysis of 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. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate.
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
CMPT 438 Algorithms.
Introduction to Algorithms
Chapter 15 Recursion.
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Analysis of Algorithms
Introduction Algorithms Order Analysis of Algorithm
Recursion and Logarithms
COMP 53 – Week Seven Big O Sorting.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS 2210 Discrete Structures Algorithms and Complexity
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Algorithm Analysis (not included in any exams!)
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Quick Sort (11.2) CSE 2011 Winter November 2018.
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.
Module #7: Algorithmic Complexity
Algorithm Efficiency and Sorting
Analysis of Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Introduction to Algorithms
Analysis of Algorithms
Discrete Mathematics and its Applications
Module #7: Algorithmic Complexity
CSC 427: Data Structures and Algorithm Analysis
Ch. 2: Getting Started.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Analysis of Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Cryptography Lecture 16.
Analysis of Algorithms
Analysis of Algorithms
Algorithms and data structures: basic definitions
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

Algorithms

Problems, Algorithms, Programs Problem - a well defined task. Sort a list of numbers. Find a particular item in a list. Find a winning chess move.

Algorithms A series of precise steps, known to stop eventually, that solve a problem. NOT necessarily tied to computers. There can be many algorithms to solve the same problem.

Characteristics of an Algorithm Precise steps. Effective steps. Has an output. Terminates eventually.

Trivial Algorithm Computing an average: Sum up all of the values. Divide the sum by the number of values.

Problems vs. Algorithms vs. Programs There can be many algorithms that solve the same problem. There can be many programs that implement the same algorithm. We are concerned with: Analyzing the difficulty of problems. Finding good algorithms. Analyzing the efficiency of algorithms.

Example: Search Search through a list of items for a particular value. Search through an array of student records for the student with ID 12345. Search through an array of address records for the address of the person with last name Doe.

Linear Search If we are searching in a list, start at the beginning and check each element until we find the one we want or reach the end. Best case? Worst case? Average case?

Binary Search If we are searching in a sorted list, we look at the middle item and then choose which half to continue looking in. We continue to cut the area we are searching in half until we find the value, or there are no more values to check. Best case? Worst case? Average case? (A little tricky)

Binary Search: Worst Case Let’s say the list has1024 items and the item is the last one we check. Check midpoint of 1024 items. Check midpoint of upper or lower half (512). Check midpoint of a half of that half (128). Successive ranges we are checking have lengths 64, 32, 16, 8, 4, 2, 1. How many checks was that? 10 (log 1024 = 10)

Binary Search Aside: Note that binary search only works if the data in the list are sorted by the field on which we’re searching!

Classifying Problems Problems fall into two categories. Computable problems can be solved using an algorithm. Non-computable problems have no algorithm to solve them. Historical note: Hilbert’s questions in 1900: complete? Consistent? Decidable?

Classifying Problems Historical note: Hilbert posed the following questions in 1900: Is mathematics complete? Is mathematics consistent? Is every statement in mathematics decidable? In 1930, he thought the all 3 answers would be “yes.” Almost immediately, Gödel showed that no closed system can be both complete & consistent. By the mid-1930’s, Turing showed that the answer to the 3rd question is “no.”

Classifying Problems Two categories of problems: Computable Non-computable Wouldn’t it be nice to know which category a problem falls into? (Topic for later in class: this problem itself is non-computable.)

Classifying Computable Problems Tractable There is an efficient algorithm to solve the problem. Intractable There is an algorithm to solve the problem but there is no efficient algorithm. (This is difficult to prove.)

Examples Sorting: tractable. The traveling salesperson problem: intractable. (we think…) Halting Problem: non-computable. (More on this in a minute.)

Measuring Efficiency We are (usually) concerned with the time an algorithm takes to complete. We often count the number of times blocks of code are executed, as a function of the size of the input. Why not measure time directly? Why not count the number of instructions executed?

Example Code: def aFunction(array): statementA; statementB; statementC; for x in array: statementD; statementE; return someValue; If the array has N elements, this function executes 4 + (2 * N) statements (i.e., 2N + 4).

Example: Computing an Average def average(array): sum = 0 for x in array: sum += x return sum / len(array) The statement inside the for loop gets executed len(array) times. If the length is n, we say this algorithm is “on the order of n”, or, O(n). O(n)??? What’s this?

Big O The worst case running time, discounting constants and lower order terms. Example: n3 + 2n is O(n3)

Exchange Sort Let’s work out the big O running time… def exchangeSort(array): for indx1 in range(len(array)): for indx2 in range(indx1, len(array)): if (array[indx1] > array[indx2]): swap(array, indx1, indx2) Let’s work out the big O running time…

Merge Sort Given a list, split it into 2 equal piles. Then split each of these piles in half. Continue to do this until you are left with 1 element in each pile. Now merge piles back together in order.

Merge Sort An example of how the merge works: Suppose the first half and second half of an array are sorted: 5 9 10 12 17 1 8 11 20 32 Merge these by taking a new element from either the first or second subarray, choosing the smallest of the remaining elements. Big O running time?

Big O Can Be Misleading Big O analysis is concerned with worst case behavior. Sometimes we know that we are not dealing with the worst case.

Searching an Array Worst case? Best case? def search(array, key): for x in array: if x == key: return key Worst case? Best case?

Algorithms Exercise…

Problem - Finding the Greatest Common Denominator Examples: gcd(12, 2) = 2 gcd(100, 95) = 5 gcd(100, 75) = 25 gcd(39, 26) = 13 gcd(17, 8) = 1

Possible Algorithm #1 Assumption: A > B >= 0 If A is a multiple of B, then gcd(A, B) = B. Otherwise, return an error. Works for gcd(12,2) = 2 But what about gcd(100, 95)???

Possible Algorithm #2 Works, but is there a better way? Start with 1 and go up to B. If a number if a common divisor of both A and B, remember it. When we get to B, stop. The last number we remembered is the gcd. Works, but is there a better way? Think about gcd(100, 95)

Euclid’s Algorithm Make use of the fact that: gcd(A, B) = gcd(B, A rem B) Note: A rem B refers to the remainder left when A is divided by B. Examples: 12 rem 2 = 0 100 rem 95 = 5

Euclid’s Algorithm Note – this algorithm is recursive. Examples: If B = 0, then gcd(A, B) = A. Otherwise, gcd(A, B) = gcd (B, A rem B). Note – this algorithm is recursive. Examples: gcd(12, 2) = gcd(2, 0) = 2 gcd(100, 95) = gcd(95, 5) = gcd(5, 0) = 5

Why do we care? Let’s say we want the gcd of 1,120,020,043,575,432 and 1,111,363,822,624,856 Assume we can do 100,000,000 divisions per second. Algorithm #2 will take about three years. Euclid’s Algorithm will take less than a second.

Programs vs. Algorithms Program: “A set of computer instructions written in a programming language” We write Programs that implement Algorithms

Algorithm vs. Program If B = 0, then gcd(A, B) = A. Otherwise, gcd(A, B) = gcd (B, A rem B). def gcd(A, B): if B == 0: return A else: return gcd(B, A % B)

Tractable vs. Intractable Problems Problems with polynomial time algorithms are considered tractable. Problems without polynomial time algorithms are considered intractable. Eg. Exponential time algorithms. (More on Friday)