Docsity

Algorithms and Data Structures: Understanding the Basics - Prof. B. Karki, Study notes of Computer Science

Partial preview of the text

Download Algorithms and Data Structures: Understanding the Basics - Prof. B. Karki and more Study notes Computer Science in PDF only on Docsity!

Notion of Algorithm

Program: Algorithms + Data Structures

Program

Solving the problem

Data Structure

Problem’s instance

Algorithm

Problem’s solution

N. Wirth, Algorithms + Data Structures = Programs Prentice-Hall, Englewood Cliffs, NJ 1976

Motivation for Algorithm

 Theoretical importance

 The study of algorithms represents the core of computer science Proving the existence of a solution to problem and investigating the algorithm’s properties

 Practical importance

 A practitioner’s toolkit of known algorithms  Framework for designing and analyzing algorithms for new problems.

Example: gcd Algorithms

Problem: Finding the greatest common divisor (gcd) of two nonnegative, not-both-zero integers, m and n , denoted gcd( m, n )****.

Solution: Several algorithms exist for solving this problem

Euclid’s algorithm: Apply repeatedly the equality gcd( m, n ) = gcd( n, m mod n ) until m mod n is 0. The last value of m is the gcd.

Definition-based algorithm: Start by checking whether t = min{ m, n } divides both m and n : If it does, t is the answer; if not, decrease t by 1 and try again. The last value of t which divides both integers is the gcd.

Middle-school algorithm: Find the prime factors of both m and n , identify all the common factors whose product is the gcd.

Example: Binary Search

Algorithm BinarySearch ( A[0.. n - 1], K )

//Implements a nonrecursive binary search

//Input: An array A[0.. n -1] sorted in ascending order and a search key K

//Output: An index of the array’s element that is equal to K // or -1 if there is no such element

l ← 0; r ← n - while l ≤ r do m ←  ( l + r)/2if K = A[ m] return m else if K < A[m] r ← m - else l ← m + 1 return -

Characteristics of Algorithm

 Finiteness:

 terminates after a finite number of steps

 Definiteness:

 rigorously and unambiguously specified

 Input:

 valid inputs are clearly specified

 Output:

 can be proved to produce the correct output given a valid input

 Effectiveness:

 steps are sufficiently simple and basic.