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

An introduction to algorithms and data structures from the perspective of computer science. It covers the concept of an algorithm, its motivation, and examples of algorithms such as euclid's algorithm for finding the greatest common divisor and sequential and binary search algorithms. The document also emphasizes the characteristics of algorithms, including finiteness, definiteness, input, output, and effectiveness.

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-bkf
koofers-user-bkf 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B.B. Karki, LSU
1.4
CSC 3102
Notion of Algorithm
pf3
pf4
pf5
pf8

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

l0; rn - while l ≤ r do m ←  ( l + r)/2if K = A[ m] return m else if K < A[m] rm - else lm + 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.