31.08.2015 Views

Objectives Experiment #1 Introduction to Algorithms

Lab 1 - Rabie Ramadan

Lab 1 - Rabie Ramadan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Cairo University<br />

Faculty of Engineering<br />

3 rd Year Computer Engineering Department<br />

CMPN302 Winter 2012<br />

<strong>Objectives</strong><br />

<strong>Experiment</strong> <strong>#1</strong><br />

<strong>Introduction</strong> <strong>to</strong> <strong>Algorithms</strong><br />

After this lab, the student should be able <strong>to</strong><br />

Understand that any problem may have different algorithms <strong>to</strong> solve it<br />

Understand the Pseudo Code of an algorithm<br />

Comment about different algorithms' performance<br />

Problem Definitions:<br />

Greatest Common Divisor:<br />

The Greatest Common Divisor (gcd(m,n)) of any 2 non negative, not both zeros<br />

integers is defined as the largest integer that divides both m and n evenly with a<br />

reminder of zero.<br />

Examples of gcd problem:<br />

1. gcd(60,24) = 12<br />

2. gcd(60,0) = 0<br />

There are different algorithms used <strong>to</strong> solve gcd problem. Two of them are:<br />

Requirements<br />

1- Euclid's algorithm<br />

o Is based on repeated application of the equality<br />

gcd(m,n) = gcd(n, m mod n) until the second number become 0<br />

2- Consecutive integer checking algorithm<br />

o Is based on the fact that the greatest possible common divisor is the<br />

smallest of m and n<br />

1- Implement the two algorithms described above.<br />

2- Run both algorithms on the following data<br />

a. 1050,2100<br />

b. 30,45<br />

c. 13,11<br />

d. 99,60<br />

e. 300,200<br />

f. 1000001,99999<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 1/5


3- For the previous data fill the following table.<br />

input Euclid Brute Force<br />

# of iterations # of iterations<br />

1050,2100<br />

30,45<br />

13,11<br />

99,60<br />

300,200<br />

1000001,99999<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 2/5


Prime Numbers:<br />

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors<br />

other than 1 and itself. One of the most common problems is finding the Prime numbers in a<br />

certain range, this can be done using:<br />

1. Brute Force algorithm:<br />

By testing all numbers one by one.<br />

2. Sieve of Era<strong>to</strong>sthenes<br />

Algorithm:<br />

To find all the prime numbers less than or equal <strong>to</strong> a given integer n by Sieve method:<br />

Requirements<br />

1. Create a list of consecutive integers from 2 <strong>to</strong> n: (2, 3, 4, ..., n).<br />

2. Initially, let p equal 2, the first prime number.<br />

3. Starting from p, count up in increments of p and mark each of these numbers greater<br />

than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them<br />

may have already been marked.<br />

4. Find the first number greater than p in the list that is not marked.If there was no such<br />

number, s<strong>to</strong>p. Otherwise, let p now equal this number (which is the next prime), and<br />

repeat from step 3.<br />

When the algorithm terminates, all the numbers in the list that are not marked are<br />

prime.<br />

1- Implement the two algorithms described above.<br />

2- Run both algorithms on the following data<br />

a. 100<br />

b. 1000<br />

c. 10000<br />

d. 100000<br />

e. 200000<br />

3- For the previous data fill the following table.<br />

input Brute Force Sieve<br />

Time in MS Time in MS<br />

100<br />

1000<br />

10000<br />

100000<br />

200000<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 3/5


AnotherFibonacci<br />

AnotherFibonacci sequence begins by defining the first three elements A[0], A[1]<br />

and A[2]. The remaining elements are calculated using the following recurrence:<br />

A[i] = A[i-1] + A[i-2] + A[i-3]<br />

You are given a int[] A which contains exactly one element that is equal <strong>to</strong> -1, you must<br />

replace this element with a positive number in a way that the sequence becomes a<br />

AnotherFibonacci sequence. Return this number.<br />

If no such positive number exists, return -1.<br />

It’s required that you implement and test this following class by the example data, and write the<br />

complexity of your algorithm if you can (in terms of time, iterations, or number of basic operations)<br />

Definition<br />

Class: AnotherFibonacci<br />

Method:<br />

complete<br />

Parameters: array of integers<br />

Returns:<br />

int<br />

Method signature (in C#): int complete(int[] A)<br />

Notes<br />

- The constraints for the elements of the input int[] A do not necessarily apply for the<br />

replacement <strong>to</strong> the missing element.<br />

Constraints<br />

- A will contain between 4 and 20 elements, inclusive.<br />

- Each element of A will be -1 or between 1 and 1000000, inclusive.<br />

- Exactly one element of A will be -1.<br />

Examples<br />

0)<br />

input = {1,2,3,-1}<br />

Returns: 6<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 4/5


1)<br />

input = {10, 20, 30, 60, -1 , 200}<br />

Returns: 110<br />

2)<br />

input = {1, 2, 3, 5, -1}<br />

Returns: -1<br />

No replacement can make this sequence AnotherFibonacci as 5 is not equal <strong>to</strong> 1+2+3.<br />

3)<br />

{1, 1, -1, 2, 3}<br />

Returns: -1<br />

The missing element must be 0 for this sequence <strong>to</strong> be AnotherFibonacci. Since this is not a positive<br />

integer, return -1.<br />

4)<br />

{-1, 7, 8, 1000000}<br />

Returns: 999985<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 5/5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!