Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Introduction to Algorithms Quicksort
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.
Recursion Michael Ernst CSE 190p University of Washington To seal: moisten flap, fold over, and seal.
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Introduction to Analysis of Algorithms
Quicksort.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
1 Data Structures A program solves a problem. A program solves a problem. A solution consists of: A solution consists of:  a way to organize the data.
Algorithm Efficiency and Sorting
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
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
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.
Lecture 2 Computational Complexity
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
The Fundamentals. Algorithms What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
Searching Topics Sequential Search Binary Search.
Vishnu Kotrajaras, PhD.1 Data Structures
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
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.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
CMPT 438 Algorithms.
Algorithms.
Introduction to Algorithms
Introduction to Algorithms
Sorting by Tammy Bailey
Teach A level Computing: Algorithms and Data Structures
Big-Oh and Execution Time: A Review
Algorithm design and Analysis
Divide and Conquer Algorithms Part I
Introduction to Algorithms
Presentation transcript:

Algorithms Friday 7th Week

Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily tied to computers –There can be many algorithms to solve the same problem

Characteristics of an Algorithm 1.Precise steps 2.Effective steps 3.Has an output 4.Terminates eventually

Trivial Algorithm Computing an average: 1.Add up all of the values 2.Divide by the number of values

Greatest Common Divisor Computing the Greatest Common Divisor (gcd) of two numbers 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

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

Program vs. Algorithm Problem: Sort a list of numbers Algorithms: –Quicksort –Insertion Sort –Merge Sort Programs: –Using JavaScript to implement Insertion Sort –Using Pascal to implement Quicksort

Algorithms to Solve Problems Problems can be categorized into two categories –Computable problems can be solved using an algorithm. –Non-computable problems have no algorithm to solve them. We don’t necessariliy know which category a problem is in. (this is non- computable)

Computable Problems Tractable –There is an efficient algorithm to solve the problem. Intractable –There is an algorithm to solve the problem but the best known algorithm is not efficient.

Examples Sorting: tractable Chess Playing: intractable Halting Problem: non-computable

How do we measure efficiency? To do this, we count the number of steps an algorithm takes. For example, consider the following algorithm which adds up the numbers in an array:

Add up the numbers in an array sum = 0; for (var i=0; i<numArray.length; i++) { sum += numArray[i]; } The statement inside the for loop gets executed numArray.length times – it depends on what the length is. If the length is n, we say this algorithm is “on the order of n”, or, this algorithm is O(n).

Definition of O(g(n)) f(n)=O(g(n)) means that for all n larger than some threshold, f(n) <= C*g(n) for some constant C, Or, the ratio f(n)/g(n) <= C. This means that f grows no faster than g, or f is proportional to g. CONSTANTS DON’T MATTER f could represent time an algorithm takes, or the number of steps executed.

Big O Analysis What is the worst case running time on input of size N, discounting constants

Comparison of Functions Look at graphs of f(x)=x, g(x)=x 2, h(x)=x 3, j(x)=logx k(x) = 2 x How do they grow? How do they compare with each other?

Tractable vs. Intractable Polynomial time algorithms are considered tractable. Exponential time algorithms are considered intractable.

Sorting (Insertion Sort) function insertionSort (numArray) { for ( var i=1; i < numArray.length; i++) { var temp = numArray[i]; for (var j = i; (j > 0) && ( numArray[j-1] > temp); j--) { numArray[j] = numArray[j-1]; } numArray[j] = temp; }

Sorting (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, cont) 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 Can Be Deceiving Big O analysis is concerned with worst case behavior Sometimes we know that we are not dealing with the worst case

Search the Array for (var i=0; i<numArray.length; i++) { if (numArray[i] == “what we want”) stop and jump for joy; }

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.

Algorithms Exercise