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

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

MATH 224 – Discrete Mathematics
Analyzing Algorithms and Problems Prof. Sin-Min Lee Department of Computer Science.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
CMPS1371 Introduction to Computing for Engineers SORTING.
Chapter 1 – Basic Concepts
Introduction to Analysis of Algorithms
Eleg667/2001-f/Topic-1a 1 A Brief Review of Algorithm Design and Analysis.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Design and Analysis of Algorithms - Chapter 11 Algorithm An algorithm is a.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
COMP s1 Computing 2 Complexity
BIT Presentation 4.  An algorithm is a method for solving a class of problems.  While computer scientists think a lot about algorithms, the term.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Lecture 2 Computational Complexity
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
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:
Unsolvability and Infeasibility. Computability (Solvable) A problem is computable if it is possible to write a computer program to solve it. Can all problems.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Algorithm analysis, searching and sorting  best vs. average vs. worst case analysis  big-Oh.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
LIMITATIONS OF ALGORITHM POWER
Computer Science/Ch. Algorithmic Foundation of CS 4-1 Chapter 4 Chapter 4 Algorithmic Foundation of Computer Science.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Vishnu Kotrajaras, PhD.1 Data Structures
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Algorithms.
Recursive Algorithms Section 5.4.
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
Analysis of Algorithms
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
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 design and Analysis
Analysis of Algorithms
CSC 427: Data Structures and Algorithm Analysis
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Analysis of Algorithms
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.

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 –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 = rem 95 = 5

Euclid’s Algorithm 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 def gcd(A, B): if B == 0: return A else: return gcd(B, A % B) If B = 0, then gcd(A, B) = A. Otherwise, gcd(A, B) = gcd (B, A rem B).

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.

Classifying Problems Problems fall into two categories. –Computable problems can be solved using an algorithm. –Non-computable problems have no algorithm to solve them. We don’t necessarily know which category a problem is in. (this 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 later in the week.)

Measuring Efficiency We are (usually) concerned with the time an algorithm takes to complete. We count the number of “basic operations” as a function of the size of the input. –Why not measure time directly? –Why not count the number of instructions executed?

Example: Computing an Average 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)??? def average(array): sum = 0 for x in array: sum += x return sum / len(array)

Some Mathematical Background Let’s see some examples …

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

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)

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)

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: 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

Binary Search If we are searching in a sorted list, we choose the half that the value would be in. We continue to cut the area we are searching in half until we find the value we want, or there are no more values to check. Worst case? Best case?

Algorithms Exercise…