Algorithms and Efficiency of Algorithms February 4th.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
The Efficiency of Algorithms Chapter 3 CS OUR NEXT QUESTION IS: "How do we know we have a good algorithm?" In the lab session, you will explore.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
The Efficiency of Algorithms
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
CS0007: Introduction to Computer Programming Array Algorithms.
Planning under Uncertainty
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Efficiency of Algorithms
CS107 Introduction to Computer Science
Chapter 3 The Efficiency of Algorithms
Chapter 2: Algorithm Discovery and Design
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
CHAPTER 3 CS OUR NEXT QUESTION IS: "How do we know we have a good algorithm?" In the lab session, you will explore algorithms that are related.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
CS107 Introduction to Computer Science Lecture 2.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Chapter 2 The Algorithmic Foundations of Computer Science
CHAPTER 2 CS
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
Chapter 2: Design of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Chapter 3: The Efficiency of Algorithms
Designing Algorithms Csci 107 Lecture 4.
Complexity (Running Time)
Efficiency of Algorithms Csci 107 Lecture 6. Last time –Algorithm for pattern matching –Efficiency of algorithms Today –Efficiency of sequential search.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Chapter 2: Algorithm Discovery and Design
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
1 Chapter 3: Efficiency of Algorithms Quality attributes for algorithms Correctness: It should do things right No flaws in design of the algorithm Maintainability.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
CSC 211 Data Structures Lecture 13
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
CSCI-100 Introduction to Computing
ALGORITHMS.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
Searching Topics Sequential Search Binary Search.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Lecture – 2 on Data structures
Algorithm Analysis CSE 2011 Winter September 2018.
The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms
Algorithm Discovery and Design
Chapter 3: The Efficiency of Algorithms
24 Searching and Sorting.
Discovery and Design of Algorithms
Discrete Mathematics CS 2610
Invitation to Computer Science 5th Edition
Presentation transcript:

Algorithms and Efficiency of Algorithms February 4th

Today Outline More algorithms Variations of sequential search Practical applications Pattern matching Data cleanup There are many different algorithms to solve the same problem. So, how do we know when we have a good one??? I.e., how do we measure the efficiency of an algorithm?

Write algorithms for Find all occurences of target Find number of occurences of target Find number of values larger than target Find largest Find smallest Find sum Find average

A Search Application in Bioinformatics Human genome: sequence of billions of nucleotides Gene –Determines human behavior –Sequence of tens of thousands of nucleotides{T,C,A,G} –The sequence is not fully known, only a portion of it Problem: How to determine a gene in the human genome? Genome: …….TCAGGCTAATCGTAGG……. Gene probe: TAATC Idea: Find all matches of the probe within the genome and then examine the nucleotides in that neighborhood

A Search Application in Bioinformatics Problem: –Suppose we have a text T = TCAGGCTAATCGTAGG and a pattern P = TA. Design an algorithm that searches T to find the position of every instance of P that appears E.g., for this text, the algorithm should return the answer: There is a match at position 7 There is a match at position 13 This problem is a variation of the search algorithm, except that for every possible starting position every character of P must be compared with a character of T.

Pattern Matching Input –Text of n characters T1, T2, …, Tn –Pattern of m (m < n) characters P1, P2, …Pm Output: –Location (index) of every occurrence of pattern within text Algorithm: –What is the idea?

Pattern Matching Algorithm idea: –Check if pattern matches starting at position 1 –Then check if it matches starting at position 2 –…and so on How to check if pattern matches text starting at position k? –Check that every character of pattern matches corresponding character of text How many loops will you need?

Pattern Matching Algorithm idea –Get input (text and pattern) –Set starting location k to 1 –Repeat until reach end of text Attempt to match every character in the pattern beginning at pos k in text If there was a match, print k Add 1 to k –Stop Question: is this an algorithm? –Yes, at a high level of abstraction –Now we need to write in pseudocode

Pattern Matching Algorithm (Fig. 2.12) Get values for n, m, the text T 1 T 2 …T n and the pattern P 1 P 2 …P m Set k to 1 Repeat until k>n-m+1 Set i to 1 Set Mismatch to NO Repeat until either (i>m) or (Mismatch = YES) if P i ≠ T k+(i-1) then Set Mismatch to YES else Increment i by 1 if Mismatch = NO then Print the message “There is a match at position” k increment k by 1 Stop

Variations on the pattern matching algorithm Find only the first match for P in T. Find only the last match for P in T.

Comparing Algorithms Algorithm –Design –Correctness –Efficiency –Also, clarity, elegance, ease of understanding There are many ways to solve a problem –Conceptually –Also different ways to write pseudocode for the same conceptual idea How to compare algorithms?

Efficiency of Algorithms Efficiency: Amount of resources used by an algorithm Space (number of variables) Time (number of instructions) When design algorithm must be aware of its use of resources If there is a choice, pick the more efficient algorithm!

Efficiency of Algorithms Does efficiency matter? Computers are so fast these days… Yes, efficiency matters a lot! –There are problems (actually a lot of them) for which all known algorithms are so inneficient that they are impractical –Remember the shortest-path-through-all-cities problem from Lab1…

Efficiency of Algorithms How to measure time efficiency? Running time: let it run and see how long it takes –On what machine? –On what inputs? Time efficiency depends on input Example: the sequential search algorithm –In the best case, how fast can the algorithm halt? –In the worst case, how fast can the algorithm halt?

Time Efficiency We want a measure of time efficiency which is independent of machine, speed etc –Look at an algorithm pseudocode and estimate its running time –Look at 2 algorithm pseudocodes and compare them Efficiency of an algorithm: –the number of pseudocode instructions (steps) executed Is this accurate? –Not all instructions take the same amount of time… –But..Good approximation of running time in most cases

Data Cleanup Algorithms What are they? A systematic strategy for removing errors from data. Why are they important? Errors occur in all real computing situations. How are they related to the search algorithm? To remove errors from a series of values, each value must be examined to determine if it is an error. E.g., suppose we have a list d of data values, from which we want to remove all the zeroes (they mark errors), and pack the good values to the left. Legit is the number of good values remaining when we are done. d 1 d 2 d 3 d 4 d 5 d 6 d 7 d Legit

Data Cleanup: Copy-Over algorithm Idea: Scan the list from left to right and copy non-zero values to a new list Copy-Over Algorithm (Fig 3.2) Get values for n and the list of n values A1, A2, …, An Set left to 1 Set newposition to 1 While left <= n do If A left is non-zero Copy A left into B newposition (Copy it into position newposition in new list Increase left by 1 Increase newposition by 1 Else increase left by 1 Stop

Data Cleanup: The Shuffle-Left Algorithm Idea: –go over the list from left to right. Every time we see a zero, shift all subsequent elements one position to the left. –Keep track of nb of legitimate (non-zero) entries How does this work? How many loops do we need?

Shuffle-Left Algorithm (Fig 3.1) 1 Get values for n and the list of n values A1, A2, …, An 2 Set legit to n 3 Set left to 1 4 Set right to 2 5 Repeat steps 6-14 until left > legit 6if A leftt ≠ 0 7Increase left by 1 8Increase right by 1 9else 10Reduce legit by 1 11 Repeat until right > n 12 Copy A ight into A right-1 13 Increase right by 1 14Set right to left Stop

Exercising the Shuffle-Left Algorithm d 1 d 2 d 3 d 4 d 5 d 6 d 7 d legit

Data Cleanup: The Converging-Pointers Algorithm Idea: –One finger moving left to right, one moving right to left –Move left finger over non-zero values; – If encounter a zero value then Copy element at right finger into this position Shift right finger to the left

Converging Pointers Algorithm (Fig 3.3) 1 Get values for n and the list of n values A1, A2,…,An 2 Set legit to n 3 Set left to 1 4 Set right to n 5 Repeat steps 6-10 until left ≥ right 6 If the value of A left ≠0 then increase left by 1 7 Else 8 Reduce legit by 1 9 Copy the value of A right to A left 10Reduce right by 1 11 if A left =0 then reduce legit by Stop

Exercising the Converging Pointers Algorithm d 1 d 2 d 3 d 4 d 5 d 6 d 7 d legit

Measuring Efficiency by Counting Steps The efficiency of an algorithm is the number of steps that it takes to complete its task. Sometimes, this is called the complexity of an algorithm. Efficiency depends on the data. E.g., the search algorithm takes fewer steps to locate a value at the beginning of a list than to locate a value at the end of the list. The “worst case” efficiency is the maximum number of steps that an algorithm can take for any collection of data values. If the input has size n, efficiency will be a function of n