Lecture 2: Introduction to Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

MATH 224 – Discrete Mathematics
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
8 Algorithms Foundations of Computer Science ã Cengage Learning.
ALGORITHMS Introduction. Definition Algorithm: Any well-defined computational procedure that takes some value or set of values as input and produces some.
What is an Algorithm? (And how do we analyze one?)
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Chapter 2: Algorithm Discovery and Design
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Chapter 2: Algorithm Discovery and Design
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
CS 1400 Chapter 1 Introduction and Background
What is an Algorithm? (And how do we analyze one?) COMP 122, Spring 04.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
The Fundamentals: Algorithms, the Integers & Matrices.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Discrete Mathematics Algorithms. Introduction  An algorithm is a finite set of instructions with the following characteristics:  Precision: steps are.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
CS4HS Columbia University Computer Science vs. Computer Programming Adam Cannon Department of Computer Science Columbia University July 7, 2011.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
CPSC 171 Introduction to Computer Science More Algorithm Discovery and Design.
Data Structure Introduction.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
ALGORITHMS.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Chapter 1. The Role of the Algorithms in Computer.
Lecture 1 INTRODUCTION TO ALGORITHMS Professor Uday Reddy
Chapter 16: Searching, Sorting, and the vector Type.
CSE15 Discrete Mathematics 03/06/17
Growth of Functions & Algorithms
Definition of Computer Science
Applied Discrete Mathematics Week 2: Functions and Sequences
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction Algorithms Order Analysis of Algorithm
Algorithm and Ambiguity
Enough Mathematical Appetizers!
Computation.
COMS W1004 Introduction to Computer Science and Programming in Java
Lecture 3: Analysis of Algorithms
Lecture 2 Introduction to Computer Science (continued)
Algorithms Chapter 3 With Question/Answer Animations
Introduction to pseudocode
Rosen 5th ed., §2.1 ~31 slides, ~1 lecture
Computing Functions with Turing Machines
Applied Discrete Mathematics Week 6: Computation
Algorithm Discovery and Design
Algorithm and Ambiguity
Discrete Mathematics and its Applications
Introduction to Computer Science
Algorithms.
Discrete Mathematics CS 2610
Presentation transcript:

Lecture 2: Introduction to Algorithms Review Linear Search Binary Search Selection Sort Reading

Review Algorithm: Textbook Definition: A well-ordered collection of unambiguous and effectively computable operations that when executed produces a result and halts in a finite amount of time.

Review Computer Science Textbook Definition: The study of algorithms including Their formal and mathematical properties Their hardware realizations Their linguistic realizations Their applications

Pseudocode A programming language is a notation for specifying instructions that a computer can execute Every (programming) language has a syntax and a semantics Syntax specifies how something is said (grammar) Semantics specifies what it means Pseudocode is an informal programming language with English-like constructs modeled to look like statements in a Java-like language Anyone able to program in any computer language should understand how to read pseudocode instructions. When in doubt just write it out in English.

Your first Algorithm Linear Search: (also called sequential search) Algorithm to determine whether a given name x appears on a list. Input: A list of n ≥1 names A[1], A[2], ... , A[n], and a given name x. Output. The message "Sorry, x is not on the list" if x is not on the list. Otherwise, the message: "x occurs at position i on the list."

Linear Search Here are the instructions for linear search written in pseudocode: found = "no"; i=1; while (found == "no" and i <= n) { if (A[i] == x) { found = "yes"; location = i; } i++; if (found == "no") print ("Sorry, " + x + " is not on the list"); else { print (x + " occurs at position " + location + " on the list");

Introduction to Algorithms Definition of an algorithm: A well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time. There are three components to an algorithm: A set of inputs, where each input is a finite sequence of items. A set of outputs, where each output is a finite sequence of items. A method consisting of a finite sequence of instructions, each one of which can be mechanically executed in a fixed length of time with a fixed amount of resources. The method must produce an output for each input in the set of possible inputs in a finite amount of time.

Introduction to Algorithms Input size: associated with each input is a measure of its size. How we measure the size can vary from problem to problem. Examples: Number of digits as in the number of digits in a number (Often we use binary digits). Number of characters in a string. Number of items as in the number of items to be searched or sorted.

Binary Search Searching a sorted list How can we exploit the structure that a sorted list provides? Start in the middle and determine in which half of the list our target lives Repeat this procedure eliminating half of the remaining list elements on each iteration What is the maximum number of comparisons required here?

Binary Search Input: A list of n ≥1 numbers A[1], A[2], ... , A[n], and a target number x. Output. The message "Sorry, x is not on the list" if x is not on the list. Otherwise, the message: "x occurs at position i on the list."

Pseudocode for Binary Search found = "no"; begin = 1; end = n; while (found == "no" and begin  end) { m = begin + floor((end-begin)/2) if(A[m] == x) found = "yes"; location = m; } if(A[m]>x) end = m-1; if(A[m]<x) begin = m+1; if (found == "no") print ("Sorry, " + x + " is not on the list"); else { print (x + " occurs at position " + location + " on the list");

Selection Sort Algorithm to sort the items on a list into nondecreasing order Input. A list of n ≥1 elements A[1], A[2], ... , A[n]. Output. The list sorted in nondecreasing order. for (i = 1; i < length(A); i++) { locmin=i; for (j=i+1;j <= length(A); j++) if (A[locmin] > A[j]) locmin=j; } exchange(A[locmin], A[i]);

Reading Chapters 1-3 in Schneider and Gersting