Computer Engineering Essentials 2013

Algorithms

Prabhas Chongstitvatana
office  Eng Building 4, Floor 18, Room 13
email prabhas at chula dot ac dot th
What is algorithms
Algorithm as Pseudo Code
Inventing an Algorithm
Iterative vs Recursive
Efficiency 

presentaton slides (26 slides)

Staffs in the department whose research are related to algorithms

Previous lecture 2012 hardware

Definition

An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

Examples

The Euclidean algorithm finds the greatest common divisor of two positive integers X and Y by the following process:

As long as the value of neither X nor Y is zero, continue dividing the larger of the values by the smaller and assigning X and Y the values of the divisor and remainder, respectively. (The final value of X is the greatest common divisor.)

To make it easier to use, we can write it in "pseudocode" (the one without details syntax or data structure operation of a computer language). Assume x > y
function gcd(x, y)
    while y ≠ 0
         t = y
         y = x mod t
         x = t
return x

Inventing an algorithm

G. Polya, "How to Solve It", 2nd ed., Princeton University Press, 1957
a web that summarises this book  http://www.math.utah.edu/~pa/math/polya.html

Sequential algorithm

An example, searching for a name in a list of names (non-structured).  Looking for the item by going through the list one-by-one.

function search( list, target )
   if list is empty   return NO
   else
      pick the first entry in the list to be x
      while x not-equal target and there is still item in the list
          pick the next entry to be x    
      if x equals target  return YES
      else return NO

You can search in a structured list (such as arrange entries in ascending order) and it will be much faster.

Recursive algorithm


let list[i]  be a list where items are arranged in ascending order
start, end indicate the range of search in list[.]

function bsearch ( list, start, end, target ) 
   mid = (start + end) / 2     ****
   if target equals list[mid] return YES
   else
      if start > end return NO
   else
      if target < list[mid]   return bsearch( list, start, mid, target )
      else return bsearch( list, mid+1, end, target)

To prevent overflow, the line *** should be mid = start + (end - start) / 2
In one comparison, the work is reduced by half.

An even faster algorithm is "Hash function"  (look it up on the web).  In hash, we compute an index from target and use that to access the list.  It is very fast because it runs in constant time (independent of the size of the list).

A related algorithm to search is an algorithm to order (sort) the list (so that it can be searched by a fast algorithm).

Insertion sort

Move the pivot entry to a temporary location leaving a hole in List
while (there is a name above the hole and that name is greater than the pivot) do
   (move the name above the hole down into
    the hole leaving a hole above the name)
Move the pivot entry into the hole in List.

Efficiency

When comparing algorithms, we want to measure efficiency independent of physical properties such as how fast a computer is, what language is used to implement the algorithm, what operating system it runs on etc.  So, we count the number of time some "basic operation" must be executed to complete the program (the program terminate).  The measurement of this "running time" against the size of the problem is our efficiency measure. 

How we can prove that Insertion sort algorihm is correct?

Future

Many areas are still unexplored. The following topics are still active:
Probabilistic algorithm
Approximate algorithm

Some unsolved problem

Colartz conjecture is one example.  The algorithm is so simple but its "running time" analysis is still beyond our mathematical tool for the time being.
https://en.wikipedia.org/wiki/Collatz_conjecture

Enjoy!

last update 16 July 2013