Chapter 1 Programming: A General Overview

Similar documents
Chapter 1 Programming: A General Overview

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Fall Recursion and induction. Stephen Brookes. Lecture 4

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Introduction to the Analysis of Algorithms. Algorithm

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

1. Chapter 1, # 1: Prove that for all sets A, B, C, the formula

To illustrate what is intended the following are three write ups by students. Diagonalization

We would like to find the value of the right-hand side of this equation. We know that

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

Lecture Notes for Chapter 2: Getting Started

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

Is This Algorithm Fast? Algorithm Analysis. Attendance Question 1. Grading Algorithms

15 212: Principles of Programming. Some Notes on Induction

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Design and Analysis of Computer Algorithm Lecture 1. Assoc. Prof.Pradondet Nilagupta Department of Computer Engineering

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

Searching Algorithms/Time Analysis

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Formal Semantics of Programming Languages

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Formal Semantics of Programming Languages

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Primes in Classes of the Iterated Totient Function

CS 310 Advanced Data Structures and Algorithms

Hash Table and Hashing

Strategies for Proofs

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Notes on Turing s Theorem and Computability

CSE 20 DISCRETE MATH. Winter

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

CPSC 121 Some Sample Questions for the Final Exam Tuesday, April 15, 2014, 8:30AM

CSE505, Fall 2012, Midterm Examination October 30, 2012

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Notes for Recitation 8

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Indicate the option which most accurately completes the sentence.

Specifications. Prof. Clarkson Fall Today s music: Nice to know you by Incubus

Discrete Mathematics Exam File Fall Exam #1

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Choice of C++ as Language

DISCRETE MATHEMATICS

CS 3410 Ch 7 Recursion

CS2 Algorithms and Data Structures Note 1

Same idea, different implementation. Is it important?

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

CS 261 Data Structures. Big-Oh Analysis: A Review

Definition For vertices u, v V (G), the distance from u to v, denoted d(u, v), in G is the length of a shortest u, v-path. 1

CSCI 261 Computer Science II

Module 2 Congruence Arithmetic pages 39 54

Assignment 1 (concept): Solutions

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 2 Analysis of Fork-Join Parallel Programs

correlated to the Michigan High School Mathematics Content Expectations

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Today. Finish Euclid. Bijection/CRT/Isomorphism. Review for Midterm.

Reasoning about programs. Chapter 9 of Thompson

Week 5 Tutorial Structural Induction

CS61B, Spring 2003 Discussion #15 Amir Kamil UC Berkeley 4/28/03

1 / 43. Today. Finish Euclid. Bijection/CRT/Isomorphism. Fermat s Little Theorem. Review for Midterm.

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

ASYMPTOTIC COMPLEXITY

Fun facts about recursion

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Distributed Computing over Communication Networks: Leader Election

Binary Search to find item in sorted array

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Discrete Mathematics SECOND EDITION OXFORD UNIVERSITY PRESS. Norman L. Biggs. Professor of Mathematics London School of Economics University of London

Ch 3.4 The Integers and Division

14 More Graphs: Euler Tours and Hamilton Cycles

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Computer Science 236 Fall Nov. 11, 2010

CS126 Final Exam Review

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

We will give examples for each of the following commonly used algorithm design techniques:

1. Suppose you are given a magic black box that somehow answers the following decision problem in polynomial time:

Connecting Statements. Today. First there was logic jumping forward.. ..and then proofs and then induction...

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Midterm CSE 21 Spring 2012

Hashing. Manolis Koubarakis. Data Structures and Programming Techniques

COS 126 General Computer Science Spring Written Exam 1

Lecture 1. 1 Notation

About the Author. Dependency Chart. Chapter 1: Logic and Sets 1. Chapter 2: Relations and Functions, Boolean Algebra, and Circuit Design

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Organisation. Assessment

6. Control Statements II

Transcription:

Introduction Chapter 1 Programming: A General Overview This class is an introduction to the design, implementation, and analysis of algorithms. examples: sorting large amounts of data organizing information for efficient and finding the between nodes in a graph looking for substrings 2 Analysis of Algorithms to be able to assess the of an algorithm, we need to be able to characterize (quantify) the amount of time it requires to execute standard approach: construct a mathematical model of the amount of work as a function of the of the problem allows us to study how much is required by an algorithm as the size of the problem grows gives us a basis for different algorithms to solve the same problem Run Time Example suppose our computer can perform operations per second we have two algorithms that perform operation on a set of numbers Algorithm A requires operations Algorithm B requires operations we need to compute in less than a second 3 4

Run Time Example Algorithm A requires operations and runs in = seconds we would need a computer that runs times faster! if processor speeds double roughly every months, the hardware will catch up in months, or about years Run Time Example meanwhile, Algorithm B requires or seconds, or ms we can get by with a machine that runs at the speed! 5 6 Factors Affecting the Running Time of an Algorithm Hardware what is the clock speed of the chip? what is the memory hierarchy? how many? Compiler what code does the compiler generate? Program how efficient is the use of instructions and? Data what is the size of the? how will it affect execution time? Environment what is the operating system? what is the system load? how do they interact with the code? Example: dot product The dot product of two vectors and is C code: double dot (double *x, double *y, int n) double sum = 0.0; for (int i = 0; i < n; i++) sum += x[i] * y[i]; return sum; Let be the runtime of the dot product on vectors of length. 7 8

Example: dot product We cannot give a good of the total runtime of the algorithm by simply looking at the code. programming code generation computing environment etc. We will focus instead on and generally ignore the speed/efficiency of, though this has a major impact on performance. Example: dot product We will frequently use to express algorithms in a way that is independent of any specific programming language: human language + math + programming language constructs dot product: sum = 0 for i = 1 to n sum += x[i] * y[i]; end double dot (double *x, double *y, int n) double sum = 0.0; for (int i = 0; i < n; i++) sum += x[i] * y[i]; return sum; 9 10 Pseudocode pseudocode allows us to ignore the peculiarities of any given programming language and concentrate on the essential operations it should be complete enough that it could be given to any (competent) programmer for implementation in any reasonable programming language pseudocode should specify all and the conditions Example: dot product Inside the loop there is multiplication and addition; and these loop operations are executed times: sum = 0 for i = 1 to n sum += x[i] * y[i]; end It is reasonable to predict that the running time is proportional to, where is an (unknown) constant, regardless of the actual instructions being executed! This tells us how the execution time grows: if should. doubles, then 11 12

Example: dot product observed execution times On an Intel E5-4627v2 CPU: Selection Problem Selection problem: suppose you have numbers and would like to determine the largest how would you try to solve this problem? When increases by a factor of, increases (roughly) by a factor of. 13 14 Selection Problem one straightforward solution: read the numbers into an array and sort them in decreasing order return the element in position better algorithm: read the first elements into an array and sort them in decreasing order as a new element is considered, it if it is smaller than the element in the array otherwise, place it in its correct spot, bumping one element out of the array when the algorithm ends, the element is in the position Selection Problem for and : both algorithms would take several days to run about a second! Just because an algorithm works, therefore, does not necessarily make it a algorithm. 15 16

Mathematics Review: Exponents Mathematics Review: Logarithms All logarithms are base unless specified otherwise. A, 17 18 Mathematics Review: Logarithms Mathematics Review: Series 19 20

Mathematics Review: Series Mathematics Review: Modular Arithmetic is congruent to modulo i.e., the is the same when or is divided by 21 22 Proofs Proofs most common proofs in algorithm analysis: proof by proof by proof by Proof by Counterexample Prove or disprove: All prime numbers are odd. Proof: Since 2 is prime and is not odd, then the original premise is false. Proof by Contradiction Prove or disprove: The sum of two odd numbers is odd. Proof: Let and both be odd numbers therefore, and if then which is even this contradicts the original statement, which is therefore false, 23 24

Proofs Proofs Proof by Mathematical Induction very useful in studying algorithms must include three parts: base case (almost always trivial) inductive hypothesis (assume true for some ) inductive step (show true for ) Inductive Proof exercises numbers: must use the Show: for 25 26 Recursion Recursion We will study both recursive functions and relations in this course. We often use proofs to show various properties about these recursive problems. recursive function: int f(int x) if (x == 0) return 0; else return 2 * f(x 1) + x*x; int f(int x) if (x == 0) return 0; else return 2 * f(x 1) + x*x; Note that the first part of the function establishes the, that is, the value for which the function is directly known without resorting to recursion. This is not circular logic since we use terms that progressively get to the base case. 27 28

Recursion Four rules of recursion: Base cases a base case must always exist, which can be solved without Making progress for cases solved recursively, the recursive call must always be to a case that makes progress toward the base case Design rule assume that all recursive calls Compound interest rule never duplicate work by solving the same instance of a problem in separate Recursion void printout (int n) if (n >= 10) printout (n / 10); printdigit (n % 10); Prove the above program works using induction for. example: looking up words in a dictionary 29 30