Lecture 1 INTRODUCTION TO ALGORITHMS Professor Uday Reddy

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
Hardware Description Language (HDL)
Discrete Math for Computer Science. Mathematical Model Real-world Problem Computerized Solution Abstract Model Transformed Model picture of the real worldpicture.
Dependency Analysis We want to “parallelize” program to make it run faster. For that, we need dependence analysis to ensure correctness.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
08/07/041 CSE-221 Digital Logic Design (DLD) Lecture-8:
Chapter 2: Algorithm Discovery and Design
Lecture 14 Go over midterm results Algorithms Efficiency More on prime numbers.
Programming Fundamentals (750113) Ch1. Problem Solving
1 Introduction to Computability Theory Lecture11: The Halting Problem Prof. Amos Israeli.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Binary Numbers.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
Digit Sums of the factors of a number An Investigation.
ADDERS Half Adders Recall that the basic rules of binary addition are as indicated below in Table 2-9. A circuit known as the half-adder carries out these.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Chapter 2: General Problem Solving Concepts
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
B.Ramamurthy11/9/20151 Computers and Programming Bina Ramamurthy 127 Bell Hall
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
CSCI-100 Introduction to Computing
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
Introducing block scheme programming March 17. Algorithm / Flow chart An algorithm or a flowchart is a step-by-step procedure for solving a particular.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
1 Introduction to Turing Machines
1 Turing Machines. 2 The Language Hierarchy Regular Languages Context-Free Languages ? ?
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Computer Systems Architecture Edited by Original lecture by Ian Sunley Areas: Computer users Basic topics What is a computer?
1 Computing Functions with Turing Machines. 2 A function Domain Result Region has:
Lecture 1 Gunjeet kaur Dronacharya group of institutions.
PROGRAMMING: What’s It All About?
Today’s Agenda Exam 2 Part 2 (11:15am-12:30pm)
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
CS1001 Programming Fundamentals 3(3-0) Lecture 2
The Selection Structure
Algorithm and Ambiguity
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Lecture 2: Introduction to Algorithms
Algorithms and Flow Charts
Computing Functions with Turing Machines
Introduction to Algorithms and Programming
Algorithm and Ambiguity
The Programming Language L
Introduction to Algorithms - 1
Flowcharts and Pseudo Code
Stumpf and Teague Object-Oriented Systems Analysis and Design with UML
Introduction to Programming
Basic Concepts of Algorithm
The Programming Language L
Presentation transcript:

Lecture 1 INTRODUCTION TO ALGORITHMS Professor Uday Reddy

8/10/2002IntroCS, © Uday ReddyAlgorithms - 2 Algorithms r Algorithm: A rigorous description of a problem-solving method. r Program: Implementation of an algorithm for execution on a computer (in a programming language) r Rigorous: m Unambiguous m Should be clear what the steps are, and whether executable mechanically

8/10/2002IntroCS, © Uday ReddyAlgorithms - 3 Example: Decimal Addition r Add two decimal numbers r Example: carry sum

8/10/2002IntroCS, © Uday ReddyAlgorithms - 4 Informal description r Add the last digits of the two numbers. r If the result is less than 10, m that is the last digit of the result. r If not, m Add 1 to the previous digits (called the “carry”) r Add the previous two digits and any carry from the last position. r Repeat these steps until all the digits are added.

8/10/2002IntroCS, © Uday ReddyAlgorithms - 5 Rigorous Algorithm r Step 1: Specify the inputs and outputs Inputs: Two decimal digit sequences a 1 a 2 … a n and b 1 b 2 … b n (of length n each) Outputs: Decimal digit sequence s 0 s 1 s 2 … s n (of length n+1) representing the sum of the input numbers

8/10/2002IntroCS, © Uday ReddyAlgorithms - 6 Naming r The algorithm takes numbers as inputs. r We don’t know in advance what numbers these will be. r The algorithm should work no matter what numbers are given as inputs. r So, we refer to the inputs in the algorithm symbolically by names: a 1 a 2 … a n and b 1 b 2 …. b n

8/10/2002IntroCS, © Uday ReddyAlgorithms - 7 Naming (contd) r It does not matter what names we use. But, we have to be consistent. r Note: The algorithm does not give values to the input variables. The user does. r The algorithm gives values to the output variables, which the user will accept as the result.

8/10/2002IntroCS, © Uday ReddyAlgorithms - 8 Step 2: Structure of the algorithm r Note that the addition procedure is a repetitive process: r We must add the digits m in position n m in position n-1 m and, so on till we add digits in position 1. for i in n, n-1, … 1 do { …….. } a 1 a 2 ….. a n b 1 b 2 ….. b n s 0 s 1 s 2 ….. s n

8/10/2002IntroCS, © Uday ReddyAlgorithms - 9 Remarks r We have introduced a new symbolic name i for the position in the sequence during the repetitive process. r The value of i is expected to be different in each iteration: m The first time, it is n. m The second time, it is n-1. m And so on...

8/10/2002IntroCS, © Uday ReddyAlgorithms - 10 Step 3: Handling carry digits r Recall that we expect to have carry digits during the addition. r Potentially, every position i has a carry digit coming from the right. r Use another symbolic digit sequence c 0 c 1 … c n (of length n+1) for these carry digits. r The last carry digit (c n ) is always 0. r Other carry digits will be defined through the repetitive process.

8/10/2002IntroCS, © Uday ReddyAlgorithms - 11 Step 4: Steps in an iteration r Define the steps in each iteration: r We need to add three decimal digits to produce a sum digit and a carry digit. r Idea: Do this two digits at a time: add a i and b i, and add the result of this to c i a i b i c i c i-1 s i

8/10/2002IntroCS, © Uday ReddyAlgorithms - 12 Adding two digits r Adding two decimal digits to produce a sum digit and a carry digit. r We can define two tables SUM and CARRY for defining these values. a b c s

8/10/2002IntroCS, © Uday ReddyAlgorithms - 13 SUM table

8/10/2002IntroCS, © Uday ReddyAlgorithms - 14 CARRY table

8/10/2002IntroCS, © Uday ReddyAlgorithms - 15 Steps in an iteration r Write down the steps from the analysis: {local digits x, c, d; x  SUM(a i, b i ); c  CARRY(a i, b i ); s i  SUM(x, c i ); d  CARRY(x, c i ); if c = 1 or d = 1 then c i-1  1; else c i-1  0; }

8/10/2002IntroCS, © Uday ReddyAlgorithms - 16 The overall algorithm inputs digit sequences a 1 a 2... a n, b 1 b 2... b n outputs digit sequence s 0 s 1... s n local digit sequence c 0 c 1... c n ; c n  0; -- the last carry digit is 0 for i in n, n-1,..., 1 do { (steps from the previous slide) } s 0  c 0 ; -- the final digit is just the carry

8/10/2002IntroCS, © Uday ReddyAlgorithms - 17 Testing the Algorithm r To make sure that we have made no mistakes in the algorithm, we test its behavior for sample inputs: 346 and 462. r Here are the values of the various variables through the execution of the algorithm.

8/10/2002IntroCS, © Uday ReddyAlgorithms - 18 Testing (contd)