Download presentation
Presentation is loading. Please wait.
Published byAdelia Wade Modified over 9 years ago
1
Lecture 1 INTRODUCTION TO ALGORITHMS Professor Uday Reddy U.Reddy@cs.bham.ac.uk
2
8/10/2002IntroCS, 2002-03 - © 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
3
8/10/2002IntroCS, 2002-03 - © Uday ReddyAlgorithms - 3 Example: Decimal Addition r Add two decimal numbers r Example: 346 462 1 808 carry sum
4
8/10/2002IntroCS, 2002-03 - © 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.
5
8/10/2002IntroCS, 2002-03 - © 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
6
8/10/2002IntroCS, 2002-03 - © 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
7
8/10/2002IntroCS, 2002-03 - © 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
8/10/2002IntroCS, 2002-03 - © 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
9
8/10/2002IntroCS, 2002-03 - © 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...
10
8/10/2002IntroCS, 2002-03 - © 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.
11
8/10/2002IntroCS, 2002-03 - © 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
12
8/10/2002IntroCS, 2002-03 - © 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
13
8/10/2002IntroCS, 2002-03 - © Uday ReddyAlgorithms - 13 SUM table
14
8/10/2002IntroCS, 2002-03 - © Uday ReddyAlgorithms - 14 CARRY table
15
8/10/2002IntroCS, 2002-03 - © 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; }
16
8/10/2002IntroCS, 2002-03 - © 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
17
8/10/2002IntroCS, 2002-03 - © 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.
18
8/10/2002IntroCS, 2002-03 - © Uday ReddyAlgorithms - 18 Testing (contd)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.