0% found this document useful (0 votes)
10 views9 pages

.Assignment 1

The document states that the training data is current only up to October 2023. No additional information or context is provided. It emphasizes the limitation of the data's recency.

Uploaded by

omar155677
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)
10 views9 pages

.Assignment 1

The document states that the training data is current only up to October 2023. No additional information or context is provided. It emphasizes the limitation of the data's recency.

Uploaded by

omar155677
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/ 9

‫المملكة العربية السعودية وزارة التعليم العالى‬

KINGDOM OF SAUDI ARABIA ‫جــامـعــة جـــــازان‬


MINISTRY OF HIGHER EDUCATION ‫كلية الحاسب اآللى ونظم المعلومات‬
JAZAN UNIVERSITY
COLLEGE OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY

ASSIGNMENT- I
Term: First Semester Academic Year: 2024-25

Course Name: Design & Analysis of Algorithms Course Code: COMP-322


Date: 08-02-2025 Submission Date: 22-02-2025
Section Number: 2328 Level: 6
Maximum Marks: 10
Name : Reham yahya hamithi ID: 202304237

ANSWER ALL QUESTIONS:

Q1. Answer the following questions:

Q1A: Using limit compare the order of the growth of two functions. (1 Mark)

ሺ𝒂ሻ𝑛3 𝑎𝑛𝑑 ½ 𝑛ሺ𝑛 − 1ሻ ሺ𝒃ሻ3𝑛 𝑎𝑛𝑑 4𝑛

(a) n3 and ½ n(n-1)

(b) 3n and 4n
Q1B: For each of the following functions C(n), indicate how much the running time T(n)
will change if its input is increased twofold. (1 Mark)

(aሻ √𝑛3 ሺ𝒃ሻ 𝑛3


(a) √(n^3 ) = n3/2
Doubling n:
𝑇ሺ2𝑛ሻ = ሺ2𝑛ሻ3/2 = 23/2 𝑛3/2 ≈ 2.83 𝑇ሺ𝑛ሻ
The running time increases by a factor of about 2.83.

(b) 𝒏𝟑
Doubling n:
𝑇ሺ2𝑛ሻ = ሺ2𝑛ሻ3 = 8𝑛3 = 8 𝑇ሺ𝑛ሻ .
The running time increases by a factor of 8.

Q2. Answer the following questions:

Q2A: Indicate whether the first function of each of the following pairs has a smaller,
same, or larger order of growth (to within a constant multiple) than the second function.
(1 Mark)
𝑛−1
ሺ𝒂ሻ 2 𝑎𝑛𝑑 2𝑛 ሺ𝒃ሻ 𝑙𝑜𝑔23 𝑛 𝑎𝑛𝑑 𝑙𝑜𝑔2 𝑛2

𝑛−1
ሺ𝒂ሻ 2 𝒂𝒏𝒅 𝟐𝒏

(b) log23 n and log2 n2


Q2B: How many comparisons (both successful and unsuccessful) are made by the
brute-force string-matching algorithm in searching for each of the following patterns in
the binary text of 400 sixes? (1 Mark)

(a) 66554433 (b) 600066

(a) Pattern: 66554433

1. Pattern Length (mm): 8.


2. Text Length (nn): 400.
3. Total Possible Starting Positions: 𝑛 − 𝑚 + 1 = 400 − 8 + 1 = 393𝑛 − 𝑚 + 1 = 400 − 8 + 1 =
393.
Comparisons:
• For each starting position, the algorithm performs up to m=8m=8 comparisons.
• Since the text consists of all sixes ("666...666") and the pattern contains non-six characters ,
no match will be found.
• At each starting position, the algorithm will perform 8 comparisons before determining a
mismatch.

Total Comparisons:

Total Comparisons=Number of Starting Positions×Comparisons per PositionTotal


Comparisons= 393 × 8 = 3144
(b) Pattern: 600066

1. Pattern Length (mm): 6.


2. Text Length (nn): 400.
3. Total Possible Starting Positions
: 𝑛 − 𝑚 + 1 = 400 − 6 + 1 = 395𝑛 − 𝑚 + 1 = 400 − 6 + 1 = 395.
Comparisons:
• For each starting position, the algorithm performs up to m=6m=6 comparisons.
• The text consists of all sixes ("666...666"), and the pattern contains non-six characters
, so no match will be found.
• At each starting position, the algorithm will perform 6 comparisons before determining a
mismatch.
• Total Comparisons:
Total Comparisons=Number of Starting Positions×Comparisons per Position
= 395 × 6 = 2370

Summary of Results

Pattern Length Text Length Starting Comparisons per Total


Pattern
(mm) (nn) Positions Position Comparisons
66554433 8 400 393 8 3144
600066 6 400 395 6 2370
Q3. Consider the following recursive algorithm for computing the sum of the first n cubes:
𝑆ሺ𝑛ሻ = 13 + 23 + . . . + 𝑛3. (2 Marks)

Algorithm S(n)

//Input: A positive integer n

//Output: The sum of the

first n cubes if n = 1 return

else return S(n − 1) + n * n * n

Write a pseudo-code for the non-recursive algorithm and determine its efficiency.

Pseudo-code for Non-Recursive Algorithm

Algorithm SumOfCubes(n)

Algorithm S(n)

sum = 0

for i = 1 to n do

sum = sum + i * i * i

end for

return sum

Efficiency Analysis

• Time Complexity: O(n).


• Space Complexity: O(1).

This algorithm is efficient and avoids the overhead of recursion.


Q4. Solve the following recurrence relations using backward substution method. (2 Marks)

(a) 𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛 − 1ሻ + 𝑛 𝑓𝑜𝑟 𝑛 > 1, 𝑇ሺ1ሻ = 1 (initial condition)

1) Expand the Recurrence:

𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛 − 1ሻ + 𝑛

Substitute 𝑇ሺ𝑛 − 1ሻ
𝑇ሺ𝑛 − 1ሻ = 𝑇ሺ𝑛 − 2ሻ + ሺ𝑛 − 1ሻ
So:

𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛 − 2ሻ + ሺ𝑛 − 1ሻ + 𝑛

Continue expanding until the base case:

𝑇ሺ𝑛ሻ = 𝑇ሺ1ሻ + 2 + 3 + ⋯ + ሺ𝑛 − 1ሻ + 𝑛

2) Substitute the Base Case:


𝑇ሺ1ሻ = 1

So:
𝑇ሺ𝑛ሻ = 1 + 2 + 3 + ⋯ + 𝑛

3) Simplify the Sum:


The sum of the first n natural numbers is:

𝑛ሺ𝑛 + 1ሻ
𝑇ሺ𝑛ሻ =
2
ሺ𝒃ሻ𝑇ሺ𝑛ሻ = 𝑇ሺ𝑛/3ሻ + 1 𝑓𝑜𝑟 𝑛 > 1, 𝑇 ሺ1ሻ = 1ሺ𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛ሻ ሺ𝑠𝑜𝑙𝑣𝑒 𝑓𝑜𝑟 𝑛 = 3𝑘ሻ

1. Assume 𝑛 = 3𝑘 𝑛
Let 𝑛 = 3𝑘 𝑛 Then, the recurrence becomes:

𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−1 ሻ + 1

2. Expand the Recurrence:


Start with the recurrence:

𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−1 ሻ + 1

Substitute 𝑇ሺ3𝑘−1 ሻ

𝑇ሺ3𝑘ሻ = 𝑇ሺ3𝑘−2 ሻ + 1

So:
𝑇ሺ3𝑘 ሻ = 𝑇ሺ3𝑘−2 ሻ + 1 + 1 = 𝑇ሺ3𝑘−2 ሻ + 2

Continue expanding:
𝑇ሺ3𝑘 ሻ = 𝑇ሺ3𝑘−3 ሻ + 3

Repeat until the base case:


𝑇ሺ3𝑘 ሻ = 𝑇ሺ1ሻ + 𝑘

3. Substitute the Base Case:


The base case is 𝑇ሺ1ሻ = 1. Substitute this into the expanded recurrence:
𝑇ሺ3𝑘 ሻ = 1 + 𝑘

Express in Terms of n:
𝑘
Since 𝑛 = 3 , we have 𝑘 = 𝑙𝑜𝑔3𝑛. Substitute this into the equation:

𝑇ሺ𝑛ሻ = 1 + 𝑙𝑜𝑔3𝑛
Q5. Answer the following questions: (2 Marks)
(a) In solving the string-matching problem, would there be any advantage in comparing
pattern and text characters right-to-left instead of left-to-right?

1. Early Mismatch Detection:

1. When comparing characters from right-to-left, mismatches are often detected earlier,
especially if the mismatch occurs near the end of the pattern.
2. This allows the algorithm to skip unnecessary comparisons and shift the pattern more
efficiently, reducing the total number of comparisons.

2. Efficient Shifts:

• Right-to-left comparison enables the use of powerful heuristics like the bad character
rule and the good suffix rule (used in the Boyer-Moore algorithm).
• These rules rely on the rightmost mismatched character to compute the maximum safe shift
of the pattern, making the algorithm more efficient.

3. Reduced Comparisons:

• In practice, right-to-left comparison can reduce the total number of character comparisons,
especially when the pattern and text share common prefixes but differ in later characters.

Example: Boyer-Moore Algorithm

The Boyer-Moore algorithm is a classic example of a string-matching algorithm that uses right-to-
left comparison. Its key features include:

• Comparing the pattern and text from right-to-left.


• Using the bad character rule to shift the pattern based on the rightmost mismatched
character.
• Using the good suffix rule to further optimize shifts based on matched suffixes.

When Right-to-Left is Not Beneficial:

• If mismatches frequently occur near the beginning of the pattern, left-to-right comparison
might be equally efficient.
• Right-to-left comparison requires more complex logic and preprocessing, which can increase
the overhead of the algorithm.
(b) Sort the list Z, X, V, T, Q, N, A, B in alphabetical order by bubble sort.
Initial List:

Bubble sort table for list: 𝑍, 𝑋, 𝑉, 𝑇, 𝑄, 𝑁, 𝐴, 𝐵

Pass Step List after swap


1 Z↔X X, Z, V, T, Q, N, A, B
Z↔V X, V, Z, T, Q, N, A, B
Z↔T X, V, T, Z, Q, N, A, B
Z↔Q X, V, T, Q, Z, N, A, B
Z↔N X, V, T, Q, N, Z, A, B
Z↔A X, V, T, Q, N, A, Z, B
Z↔B X, V, T, Q, N, A, B, Z
2 X↔V V, X, T, Q, N, A, B, Z
X↔T V, T, X, Q, N, A, B, Z
X↔Q V, T, Q, X, N, A, B, Z
X↔N V, T, Q, N, X, A, B, Z
X↔A V, T, Q, N, A, X, B, Z
X↔B V, T, Q, N, A, B, X, Z
3 V↔T T, V, Q, N, A, B, X, Z
V↔Q T, Q, V, N, A, B, X, Z
V↔N T, Q, N, V, A, B, X, Z
V↔A T, Q, N, A, V, B, X, Z
V↔B T, Q, N, A, B, V, X, Z
4 T↔Q Q, T, N, A, B, V, X, Z
T↔N Q, N, T, A, B, V, X, Z
T↔A Q, N, A, T, B, V, X, Z
T↔B Q, N, A, B, T, V, X, Z
5 Q↔N N, Q, A, B, T, V, X, Z
Q↔A N, A, Q, B, T, V, X, Z
Q↔B N, A, B, Q, T, V, X, Z
6 N↔A A, N, B, Q, T, V, X, Z
N↔B A, B, N, Q, T, V, X, Z
7 No Swaps Needed A, B, N, Q, T, V, X, Z

Final Sorted List:


A, B, N, Q, T, V, X, Z
Important Instructions:

⮚ Submit the soft copy of your assignment on Blackboard. Do not submit the hard copy
image.

⮚ Make the Assignment Name like this (your ID.Assignment_1)


⮚ Take proper care during submission of Assignment; submit it in proper group and
proper document. After the submission kindly check the document again.

⮚ Submit the Assignment soft copy BEFORE 23:59hrs of the due date.
⮚ If you submit your assignment late. Each day, 10% penalty will incur of the total
marks scored by the students.

COURSE TEACHER
Mohammad Shahnawaz Nasir

You might also like