Unit 1
Unit 1
Unit 1
•Symbols used:
+, -, *, /, ()
Keyword used:
IF, THEN, ELSE
Keyword used:
DOWHILE, ENDDO
Statement a
Statement b
Statement c
• Add 1 to pageCount
• Print heading line 1
• Print heading line 2
• Set lineCount to zero
• Read customer record
Selection
Presentation of condition and the choice between two actions
Example:
Set student_total to zero
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO
DESIGNING A SOLUTION ALGORITHM
Number1 total
Number2
Number3
Solution Algorithm
• Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END
• Comments : //
• Blocks :{and }.
• Statements are delimited by ;.
• An identifier begins with a letter.
• The data types of variables are not explicitly
declared.
• Assignment of values to variables
(variable):=(expression);
• Elements of multidimensional arrays are
accessed using[ and ].
• Looping statements :for,while,and repeat-until.
• The while loop takes the following form:
while(condition) do
{ (statement 1)
(statement n)
}
• The general form of a for loop is
for variable:=value l to value 2 step do
{ (statement 1)
(statement n)
}
• A repeat-until statement is constructed as follows:
repeat
(statement 1)
(statement n)
Until(condition)
• A conditional statement has the following
forms:
– if (condition) then (statement)
– if (condition) then (statement1) else (statement2)
– case
{
: (condition 1): (statement1)
: (condition n): (statement n)
:else:(statement n + 1) }
Recursive Algorithms
• A recursive function is a function that is
defined in terms of itself.
• Similarly, an algorithm is said to be recursive if
the same algorithm is invoked in the body.
• An algorithm that calls itself is direct recursive.
• Algorithm A is said to be indirect recursive if it
calls another algorithm which in turn calls A.
Recursion
• In some problems, it may be natural to define the
problem in terms of the problem itself.
• Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
• Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
Tower of Hanoi
• The disks must be moved within one week.
Assume one disk can be moved in 1 second. Is
this possible?
Space = 3
int sum(int a[], int n)
{
int x = 0; // 4 bytes for x
for(int i = 0; i < n; i++) // 4 bytes for i
{
x = x + a[i];
}
return(x);
}
Space(A) = Fixed Components(A) + Variable Components(A)
Space = 3 + n
AlgorithmRSum(a,n)
{
if (n <0) thenreturn0.0;
Else return RSum (a,n-1)+ a[n];
}
Space(A) = Fixed Components(A) + Variable Components(A)
Algorithm Sum(number,size) 0
0 -
{ 0 - 0
result=0.0;
1 1 1
return result;
1 1 1
} 0 - 0
Total 2size + 3
Asymptotic Analysis
• Asymptotic analysis of an algorithm refers to
defining the mathematical boundation
/framing of its run-time performance.
• Using asymptotic analysis, we can very well
conclude the best case, average case, and worst
case scenario of an algorithm.
• Asymptotic analysis refers to computing the
running time of any operation in mathematical
units of computation.
Why is Asymptotic Notation Important?
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Ω (omega)
Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
• The notation θ(n) is the formal way to express
both the lower bound and the upper bound of
an algorithm's running time.
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
Amortize Analysis
K=0 k=1
K=2
A Divide Conquer Solution
• Size k=1 (i.e. 2×2) problem has a simple
solution.
• Divide larger boards into smaller ones.
• Combine solutions to smaller problems.
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
Binary Search
• ai 1≤ i ≤ n is a list of elements that are sorted
in nondecreasing order.
• Determine whether a given element x is
present in the list.
• MID = low + high
2
• If x < mid, then high = mid-1
• If x > mid, then low = mid+1
Example
Number to Search
85 > 68 85 < 92
index 0 1 2 3 4 5 6 7 8
1 10 55 66 68 85 92 101 110
85 > 68 85 < 92
index 0 1 2 3 4 5 6 7 8
1 10 55 66 68 85 92 101 110
85 < 92
index 0 1 2 3 4 5 6 7 8
1 10 55 66 68 85 92 101 110
85 = 85
index 0 1 2 3 4 5 6 7 8
1 10 55 66 68 85 92 101 110
Set lowerBound = 1
Set upperBound = n
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
6 3 17 25 31 55 62 53 36 42 47 29 8 4 12
Finding the Maximum and Minimum
• Find the maximum and minimum items in a
set of n elements.
• Divide and Conquer Approach
the array is divided into two halves.
maximum and minimum numbers in each halves
are found.
return the maximum of two maxima of each half
and the minimum of two minima of each half.
82 36 49 91 12 14 6 76 92 55
82 36 49 91 12 14 6 76 92 55
82 36 49 91 12 14 6 76 92 55
82 36 49 14 6 76
Max Min
92 6
82 36 49 49 91 12 14 6 76 76 92 55
Time Complexity
Merge Sort
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself
for the two halves and then merges the two
sorted halves.
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 3 5 4 9 1 7
2 3 5 8 1 4 7 9
1 2 3 4 5 7 8 9
Time Complexity
Quick Sort
• Sorting Algorithm
• It finds the element called pivot which divides
the array into two halves.
• Elements in the first half are smaller than the
pivot.
• Elements in the second half are greater than
the pivot.
Divide and Conquer Solution
• Find the pivot that divides the array into two
halves.
• Quick sort the left half.
• Quick sort the right half.
Is Pivot < right? Pivot = 5
Pivot Right = 4
No
Array Index 0 1 2 3 4 5
Array Elements 5 2 6 1 3 4
Left Right
Is Pivot > left? Pivot = 5
Pivot Left = 4
Yes
Array Index 0 1 2 3 4 5
Array Elements 4 2 6 1 3 5
Left Right
Is Pivot > left? Pivot = 5
Left =2
Yes Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 6 1 3 5
Left Right
Is Pivot > left? Pivot = 5
Left =6
No Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 6 1 3 5
Left Right
Is Pivot < Right? Pivot = 5
Yes Right =6
Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 5 1 3 6
Left Right
Is Pivot < Right? Pivot = 5
No Right =3
Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 5 1 3 6
Left Right
Is Pivot > Left? Pivot = 5
Left =3
Pivot Yes
Array Index 0 1 2 3 4 5
Array Elements 4 2 3 1 5 6
Left Right
Is Pivot > Left? Pivot = 5
Left =1
Yes Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 3 1 5 6
Left Right
Pivot
Array Index 0 1 2 3 4 5
Array Elements 4 2 3 1 5 6
Left Right
Is Pivot < Right? Pivot = 4
Right =1
Pivot No
Array Index 0 1 2 3 4 5
Array Elements 4 2 3 1 5 6
Left Right
Is Pivot > Left Pivot = 4
Left =1
Pivot Yes
Array Index 0 1 2 3 4 5
Array Elements 1 2 3 4 5 6
Left Right
Is Pivot > Left Pivot = 4
Yes Left =2
Pivot
Array Index 0 1 2 3 4 5
Array Elements 1 2 3 4 5 6
Left Right
Is Pivot > Left Pivot = 4
Yes Left =3
Pivot
Array Index 0 1 2 3 4 5
Array Elements 1 2 3 4 5 6
Left Right
Is Pivot < right Pivot = 1
Yes Right =3
Pivot
Array Index 0 1 2 3 4 5
Array Elements 1 2 3 4 5 6
Left Right
Selection Sort
• Sorting Algorithm
• Initially whole array is unsorted.
• Considered into two parts
Unsorted
Sorted
• Selection : select the lowest element in the
remaining array.
• Swapping : Bring it to the starting position.
• Counter shift: Change the counter for
unsorted array be one.
Sorted Array Unsorted Array
64 25 12 22 11
64 25 12 12 11
Sorted Array Unsorted Array
11 25 12 22 64
25 12
Sorted Array Unsorted Array
11 12 25 22 64
25 22
Sorted Array Unsorted Array
11 12 22 25 64
Strassen’s Matrix Multiplication