0% found this document useful (0 votes)
2 views

3.Algorithms I

The document provides an overview of algorithms, emphasizing their structure as a sequence of steps to solve problems, akin to a recipe. It discusses the characteristics of algorithms, such as completeness, correctness, and efficiency, and explores various ways to express them, including step-by-step instructions and computational models. Additionally, it covers algorithmic skills necessary for problem-solving, such as identifying conditions and combining operations, illustrated through examples like drawing a square and calculating the GCD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3.Algorithms I

The document provides an overview of algorithms, emphasizing their structure as a sequence of steps to solve problems, akin to a recipe. It discusses the characteristics of algorithms, such as completeness, correctness, and efficiency, and explores various ways to express them, including step-by-step instructions and computational models. Additionally, it covers algorithmic skills necessary for problem-solving, such as identifying conditions and combining operations, illustrated through examples like drawing a square and calculating the GCD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

COL100

Introduction to Computer Science


Algorithm: How to Solve a Problem
‣ Instructions or Recipe
a. Boil potato cubes until half-cooked
b. Drain, cool, and fry until golden Input
In this order ?

c. Boil onions until transparent


Ingredients
d. Drain onions & rinse well
Algorithm ➜ Program
e. Puree onions, tomatoes, cashews into a paste Steps
f. Temper bayleaf, cardamom ‣ in some language
‣ depends on cooks’ capability and
g. Add ginger, garlic and saute tools
h. Add puree, spices (Axioms)
i. Sauté until the paste begins to leave pan’s sides Dish(es)
j. Whisk yogurt in a small bowl
k. Add it to the spice paste Output
l. Sauté until paste thickens
m.Add fried potatoes and water, simmer
Algorithm is ..
‣ Algorithm encompasses a set of actions, which is
• Complete:
all the
- Ends and produces answer on every input Could be done purely
as a mental exercise
• Correct:
- Every answer is correct (whichever the input)
‣ Algorithm should be efficient
• Based on a cost model for actions
- Need not give running time, but allows comparison with other algorithms
• On every input, or on “average,” or with a high probability
Expressing Algorithms Computational Model

‣ A notation system

‣ A set of axioms: elementary operations and objects

‣ Ways to expand these Can translate into action

• Combine into compound expressions, actions, and objects

- e.g., (a + b + c), a and b, a after b, ..


• Alternatively, break large tasks into smaller ones

‣ Abstract: generalize actions for a class of objects/tasks


• Create named expressions, actions, objects and reuse in similar situation
- e.g., Fry, Boil, Saute
Ways to Express Algorithms
‣ A step by step recipe Aloo-dum = Boiled mixture of fried potato and curry,
where curry = Boiled mixture of fried spices
‣ An expression and fried potato = fry peeled potato
where ..
‣ A specification of conditions and action allowed in each condition
• Steps can be ‘randomized' In case of peeled potato, boil potato
otherwise, peel and then boil potato
‣ I/O table In case of overboiled potato, cool potato
• Including condition-action table otherwise, fry potato

‣ Combination of all of the above


Concise ways to express repetitive steps:
Iterative or Recursive
Algorithmic Skills Develop this semester

‣ Decide what to do under different condition


• Starting with the initial input data as the initial condition

‣ Be able to identify, classify, and express conditions


• Conditions can evolve and are specified as a function of
- initial and additional input and intermediate computation and data
‣ Understand the primitive operations available

‣ Describe what to do (building up new operations)


• Using primitive operations, and previously built-up operations
• Understand how to present and combine algorithms
Algorithm: Draw Square Tools: Compass, Pencil,
Straight edge
‣ Input: Two points on a paper, labeled P and Q
P
Q

‣ Output: Draw any square of side length |PQ| Find R, S, s.t. PQRS is the square
1. Draw line L1 ⟂ PQ passing P
How long?
Concurrent
b. Draw line L2 ⟂ PQ through Q

2. Choose a side H (half-space of PQ) Imperative Style


3. In H, mark point R on L1 such that |PR| = |PQ|
b. In H, mark S on L2 such that |QS| = |PQ| Concurrent

4. Draw PQRS
1. PQRS is a square with side |PQ| No matter what P and Q are

Is the problem unsolvable in some cases? 2. Can it be done in fewer steps


Algorithm: Draw Square Tools:Compass, Pencil, Straight edge
1. Draw line L, PQ extended by Δ >> 0
‣ Input: Two points on a paper, labeled P and Q
2. Set Jaw width ≤ Δ
‣ Output: Draw any square of side length |PQ|
3. Mark off X on L from P away from Q
1. Draw line L1 ⟂ PQ passing P Not elementary b. Mark off Y on L from P towards Q
b. Draw line L2 ⟂ PQ through Q 4. Select r1 > XP
5. Draw circle Cx of radius r1 from X
2. Choose a side H (half-space of PQ) Elementary
b. Draw circle Cy of radius r1 from Y
3. In H, mark point R on L1 such that |PR| = |PQ|
6. Find intersection of Cx and Cy = Px,Py
b. In H, mark S on L2 such that |QS| = |PQ| 7. Draw L connecting P and P
1 x y

4. Draw PQRS R 1. Draw circle Cp at P with radius |PQ|


L1
2. Extend L1 until Cp
1. Anchor at P and Q x P y Q
3. Mark R at intersection of L1 and Cp
2. Draw full arc
Algorithmic Operation
‣ Elementary operations of the toolset (e.g. draw line PQ; Anchor P,Q; Draw arc,..)

‣ Combine elementary operations to form complex tasks


• Abstraction: Name complex tasks and their products
- Like building new capabilities of the toolset
- e.g., “draw a perpendicular line”
‣ For computers
• Elementary processes are arithmetic and logical operations
• Libraries can add additional capabilities (from basic arithmetic, logical Ops)
• Formal languages allow their unambiguous use and combination
Find an Iterative Procedure
Can n be -ve?
‣ a and n are given, n is an integer stands for
☚ stands for ‘multiplication’ [0,n) ∈ 𝕀


a * a * a * .. n times
Python:
Create ‘new version’ Python:
‣ Iterate n times of P (i is hidden) P=1
P=1
for i in range(n):
Pi+1 = Pi * a P=1 i=0
Not P=P*a
n times: while(i < n):
With P0 = 1 mathematical
equality ☚ P=P*a i=i+1
‣ Why is this correct? P=P*a


Indentation based grouping
‣ Can this be done in fewer than n multiplications?
-1
• Let b = a*a
• Let c = b*b?
- b * b * b .. n/2 times What if n is odd?
n/2 multiplications
• We will revisit this
GCD(a,b), given positive integers a, b Iterative Procedure

‣ Euclid’s algorithm (WLOG, assume b < a) GCD(60, 18)


• GCD(a, b) = GCD(a-b, b). If a is divisible by b, GCD(a, b) = b 42
60%18
24
- GCD(a-b, b) = GCD(a-2b, b) if(a-b > b); Or GCD(a-b, 2b-a) if(a-b < b)
6
• Keep repeating. But how many times can be subtract?
= GCD(18, 6)
• GCD(a, b) = GCD(b, a%b), Or b if a%b = 0
- (a%b denotes “remainder of a ÷ b” and a/b denotes a ÷ b)
if (a < b):
if (a < b) swap values such that a >= b ☚ if (a < b): sav = b
Repeat until rem > 0: b=a b=a
rem = a % b a=b a=b
result b = rem and a = b ☚ b = rem then a = b Python: (a, b) = (b, a)
a=b b = rem
GCD(a,b), given positive integers a, b Iterative Procedure

‣if Euclid’s
(a < b):
algorithm (WLOG, assume b < a) GCD(60, 18)Initialized?
if (a < b):
if (a < b): if (a < b):
•(a,b)
GCD(a, b) = GCD(a-b, b). If a is divisible by b, GCD(a, b) = b 42
= (b,a) (a,b) = (b,a) Test for (a,b) = (b,a) (a,b) = (b,a)
60%18
equality 24 ?
if(b =- 0):
GCD(a-b,Recursion while True:
b) = GCD(a-2b, % b if(a-b <while
rem = a 2b-a)
b) if(a-b > b); Or GCD(a-b, b) rem > 0:


6

return a rem = a % b while rem > 0: rem = a % b
• Keep repeating. But how many times can be subtract?
else: if(rem == 0): return b rem = a % b (a, b) = (b, rem)6)
= GCD(18,
•return
GCD(a, b) = GCD(b,
GCD(b, a%b) a%b), (a, b)Or=b(b,
if a%b
rem) = 0 (a, b) = (b, rem) return b
otherwise Is rem = 0?

- (a%b denotes Why


“remainder mustof rem
a ÷ reach
b” and 0?
a/b return
denotes b a ÷ b)

if (a < b):
if (a < b) swap values such that a >= b ☚ if (a < b): sav = b
Repeat until rem > 0: b=a b=a
rem = a % b a=b a=b
result b = rem and a = b ☚ b = rem then a = b Python: (a, b) = (b, a)
a=b b = rem
Brief Review
‣ How to think algorithmically
• Know the elementary steps, and how to build on top of them
• Decide the important cases and what to do for each case conditions
• Understand how to break complex operations into simpler ones
‣ How to express algorithms creates function


P=1
• Step-by-step recipe (Imperative style) def Power(a,n):
i=0
• Expressions combining many operations P=1
while i < n:
for i in range(n):
• Case-by-case description of actions i=i+1
P=P*a
‣ Converting algorithms into programs P=P*a
return P
Simple English symbols, no greek/subscripts (word is ok)
function’s output


Subscripts are hidden
Operators

Iterate


Iterate,


Mind the spaces = * / % == () :
keep checking condition that many times

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy