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

Lec Week 2

1. The document discusses an introduction to algorithms course, covering topics like what is an algorithm, induction, recursion, and signing up for Hackerrank to practice algorithm problems. 2. It provides examples of algorithms to determine if a number is prime and to calculate a factorial, and discusses how induction and recursion can be used to analyze algorithms. 3. Students are encouraged to sign up for Hackerrank using their student email and real name to practice algorithm problems and help with homework, quizzes, and exams for the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lec Week 2

1. The document discusses an introduction to algorithms course, covering topics like what is an algorithm, induction, recursion, and signing up for Hackerrank to practice algorithm problems. 2. It provides examples of algorithms to determine if a number is prime and to calculate a factorial, and discusses how induction and recursion can be used to analyze algorithms. 3. Students are encouraged to sign up for Hackerrank using their student email and real name to practice algorithm problems and help with homework, quizzes, and exams for the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CSS 528: Algorithms and Competitive Programming

Week 2: Introduction to Algorithms (Chapter 1)

Topics:

1. What is algorithm?
2. Are all algorithms correct?
3. What is induction and recursion?
4. How to sign up at Hackerrank and start solving practice problems there?

What is an algorithm?
In general, algorithm is a procedure to accomplish a specific task. In other words, algorithm is a way to solve
some problem.

The word algorithm comes from the name Muhammad ibn Musa al-Khwarizmi, ancient mathematician who wrote
a treatise on solving quadratic equations.

In general, anything that solves problem could be classified as algorithm. For example, here is an algorithm to
get a decent grade on CSS528:

1. Read the textbook


2. Practice, i.e. solve problems
3. Attend and solve homeworks, quizzes and exams.

However, in this course we will look at more formal way of looking at algorithms, i.e. well-defined problems and
well-defined steps to solve the given problem.

Example of well-defined problem and well-defined algorithm


Problem: Given an integer n, determine if it is prime number or not.

Input: n , 1 <= n <= 10^9

Output: 1 if n is prime, otherwise 0

n = a b, a <= b n = a b >= a * a = a^2, we see that a^2 <= n, that means a <= sqrt(n)

Pseudocode:

1. Go through numbers from [2, √− n] and check if it divides n.

2. If some of these numbers, say k , divide n, n can't be prime.


3. If none of these numbers divide n, n is prime.
In [1]: # implementation

# libraries
import math

# algorithm written as a function


def is_prime(n):
# if n=1, it is not prime
if n == 1:
return 0
# set numbers to check
lower = 2
upper = int(math.sqrt(n)) + 1
candidates = range(lower, upper + 1)
# at first assume that n is prime
result = 1
# go through each number above and check if it divides n
for num in candidates:
if n % num == 0:
# if num divides n, n can't be prime
result = 0
return result
# if no number from above list divides n, then result variable stays 1
return result

In [2]: # let's some tests


tests = {
'test_1': {
'input': 1,
'output': 0
},
'test_2': {
'input': 12,
'output': 0
},
'test_3': {
'input': 997,
'output': 1
}
}
In [3]: # run the above test
for test_name, test_content in tests.items():
input_ = test_content['input']
correct_output = test_content['output']
algorithm_output = is_prime(input_)
test_passed = (correct_output == algorithm_output)
if test_passed:
print(f"{test_name} is passed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")
else:
print(f"{test_name} is failed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")

test_1 is passed. Correct output: 0. Algorithm's output: 0


test_2 is passed. Correct output: 0. Algorithm's output: 0
test_3 is passed. Correct output: 1. Algorithm's output: 1

Are all algorithms correct?

Ans: Some algorithms could be incorrect. If this is a case, it is hard to call this procedure "algorithm".

Note: For this reason, correctness of an algorithm is very important. One should check the correctness of the
given algorithm.

Let us come up with incorrect algorithms for identiyfing prime numbers

Pseudo code 2:

1. If n is divisible by 2, then n is not prime.


2. Otherwise n is prime.

In [4]: # implementation of pseudocode 2


def is_prime_2(n):
if n % 2 == 0:
return 0
return 1
In [5]: # run the above test
for test_name, test_content in tests.items():
input_ = test_content['input']
correct_output = test_content['output']
algorithm_output = is_prime_2(input_)
test_passed = (correct_output == algorithm_output)
if test_passed:
print(f"{test_name} is passed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")
else:
print(f"{test_name} is failed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")

test_1 is failed. Correct output: 0. Algorithm's output: 1


test_2 is passed. Correct output: 0. Algorithm's output: 0
test_3 is passed. Correct output: 1. Algorithm's output: 1

Lesson: sometimes your algorithm might pass all you tests, but still be incorrect.

Pseudo code 3:

1. Output a randomly chosen value from {0,1}

In [6]: from random import randint


# implementation of pseudocode 3
def is_prime_3(n):
return randint(0,1)

In [7]: # run the above test


for test_name, test_content in tests.items():
input_ = test_content['input']
correct_output = test_content['output']
algorithm_output = is_prime_3(input_)
test_passed = (correct_output == algorithm_output)
if test_passed:
print(f"{test_name} is passed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")
else:
print(f"{test_name} is failed. Correct output: {correct_output}. Algor
ithm's output: {algorithm_output}")

test_1 is passed. Correct output: 0. Algorithm's output: 0


test_2 is failed. Correct output: 0. Algorithm's output: 1
test_3 is failed. Correct output: 1. Algorithm's output: 0

What is induction and recursion

When analyzing algorithms, it is useful to work with induction and recursion


Induction

Induction is about going up

For instance let's prove that 2^n >= n, for all n >= 1

1. Base of induction n=1: 2^1 >= 1

1. Induction assumption: suppose 2^k >= k, for some k >= 1

1. Induction step: 2^(k+1) >= k+1

1. (continued) 2^(k+1) = 2^k 2 >= k 2 = k + k >= k + 1


Recursion

Recursion is about going down

Let's use recursion to find n!

Problem

Given n, find n! = 1 2 3 ... n

Q: How can we use recursion to solve this problem?

Observe that n! = 1 2 3 ... (n-1) n = (n-1)! n

Hence, we can reduce the problem of finding n! to finding (n-1)!

Pseudocode

1. if n = 1, n! = 1.
2. if n > 1, then n! = (n-1)! * n

In [8]: # implementation
def factorial(n):
if n == 1:
return 1
return factorial(n-1) * n
In [9]: print('3!: {}'.format(factorial(3)))
print('4!: {}'.format(factorial(4)))
print('5!: {}'.format(factorial(5)))

3!: 6
4!: 24
5!: 120

How to sign up at Hackerrank and start solving practice problems


there?

Hackerrank is one of algorithmic solving problems. There you could find many problems related to the topics we
will cover in this course. You are encouraged to practice solving problems related to topics that we cover for
several reasons:

1. You will learn to write a correct code.


2. You will learn to write code faster (important for quizzes).
3. You will learn to think algorithmically.

Here is the link to Hackerrank: Hackerrank (Login Page) (https://www.hackerrank.com/access-account/)

In this course we will use Hackerrank for homeworks, quizzes and final exams. So please, register to Hackerrank
(as a developer, not a company) as

1. Use your SDU student email.


2. Use your real name and surname.
3. This will help instructors to assign you a proper grade.

Please note that there are many algorithmic problem solving platforms besides Hackerrank, such as

1. Coderbyte
2. Project Euler
3. Leetcode
4. CodeWars
5. Codility
6. CodeChef
7. etc

If you find these platforms useful you could practice problem solving skills there as well. In general, these
platforms are quite silimar to each other.

In [ ]:

You might also like