Algorithms Worksheet, Assignments of Algorithms and Programming

A set of questions related to algorithms. It defines the stages of an algorithm, recurrence relation, and list of elements of a set. The questions include finding recursive and closed formulas for a sequence, writing algorithms to output the nth term of a sequence, counting the number of times a number appears in a list, and describing the relationship between input and output of an algorithm. suitable for students studying algorithms and data structures.

Typology: Assignments

2021/2022

Uploaded on 05/11/2023

paulina
paulina 🇺🇸

4.4

(13)

240 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms worksheet
Work in groups or alone and use the time today to answer all of the questions below.
You may use your books or notes from class, and do not turn in this assignment: it is yours
to study for the exam next week. Recall that:
Defn: An algorithm consists of three stages: input, procedure, and output. All arith-
metic operations (+,,×,÷) and comparisons (, <, =) must occur in the procedure
stage. The complexity of the algorithm is the total number of arithmetic operations and/or
comparisons used for the algorithm to run for an input in the worst case.
Defn: A recurrence relation an=ran1+san2has solution
an=(c1xn
1+c2xn
2if x16=x2,
c1xn
0+c2nxn
0if x1=x2= 0,
where x1, x2are the roots of the characteristic polynomial
x2rx s= (xx1)(xx2)=0,
and c1, c2are unknown constants which are solved for using the initial conditions of the
recursively defined sequence.
Defn: Alist of elements of a set Ais an ordered n-tuple, or element of An=A×A× · · · × A
| {z }
ntimes
.
1. Find a recursive formula and a closed formula for the sequence -1, 17, 11, 113, 179, 857 . . .
2. Write an algorithm that outputs the nth term of the sequence above using the recursive
definition of the sequence. Now write an algorithm that outputs the nth term of the
sequence using the closed formula using only arithmetic operations. You may not use
”exponents” but only multiplication ×. Find the complexity of each algorithm.
3. Write an algorithm takes takes as input a list of real numbers L= (a1, . . . , an) and outputs
(1) the number of times the number 5 appears in the list, and (2) the number of times an
entry is between 0 and 5, non-inclusive.
4. Describe the relationship between the input and the output of the algorithm below. In
other words, what does the algorithm do? What is the maximum value of sif d=n= 5?
What is the complexity of the algorithm for fixed d? for fixed n?
Input: A natural number dand a list L= (a1, . . . , an) of real numbers.
Procedure: Initialize: s=0.
Step 1:
For i= 1, . . . ,n
For j= 1, . . . ,d
If ai=j, then set sto s+ 1.
Output: s.

Partial preview of the text

Download Algorithms Worksheet and more Assignments Algorithms and Programming in PDF only on Docsity!

Algorithms worksheet

Work in groups or alone and use the time today to answer all of the questions below. You may use your books or notes from class, and do not turn in this assignment: it is yours to study for the exam next week. Recall that: Defn: An algorithm consists of three stages: input, procedure, and output. All arith- metic operations (+, −, ×, ÷) and comparisons (≤, <, =) must occur in the procedure stage. The complexity of the algorithm is the total number of arithmetic operations and/or comparisons used for the algorithm to run for an input in the worst case. Defn: A recurrence relation an = ran− 1 + san− 2 has solution

an =

c 1 xn 1 + c 2 xn 2 if x 1 6 = x 2 , c 1 xn 0 + c 2 nxn 0 if x 1 = x 2 = 0, where x 1 , x 2 are the roots of the characteristic polynomial x^2 − rx − s = (x − x 1 )(x − x 2 ) = 0, and c 1 , c 2 are unknown constants which are solved for using the initial conditions of the recursively defined sequence. Defn: A list of elements of a set A is an ordered n-tuple, or element of An^ = A︸ × A × · · · ×︷︷ A︸ n times

  1. Find a recursive formula and a closed formula for the sequence -1, 17, 11, 113, 179, 857...
  2. Write an algorithm that outputs the nth term of the sequence above using the recursive definition of the sequence. Now write an algorithm that outputs the nth term of the sequence using the closed formula using only arithmetic operations. You may not use ”exponents” but only multiplication ×. Find the complexity of each algorithm.
  3. Write an algorithm takes takes as input a list of real numbers L = (a 1 ,... , an) and outputs (1) the number of times the number 5 appears in the list, and (2) the number of times an entry is between 0 and 5, non-inclusive.
  4. Describe the relationship between the input and the output of the algorithm below. In other words, what does the algorithm do? What is the maximum value of s if d = n = 5? What is the complexity of the algorithm for fixed d? for fixed n? Input: A natural number d and a list L = (a 1 ,... , an) of real numbers. Procedure: Initialize: s=0. Step 1: For i = 1,... , n For j = 1,... , d If ai = j, then set s to s + 1. Output: s.