Chapter 1: Introduction to Algorithm Design

1 / 60

# Chapter 1: Introduction to Algorithm Design - PowerPoint PPT Presentation

Chapter 1: Introduction to Algorithm Design. Dr. Paige Meeker CSC 336. What is an Algorithm?. In order to know how to create an algorithm and to understand the importance of algorithms, we should know what they are. So, what are they?. An Algorithm is…. The idea behind a computer program.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Chapter 1: Introduction to Algorithm Design' - rocco

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Chapter 1: Introduction to Algorithm Design

Dr. Paige Meeker

CSC 336

What is an Algorithm?
• In order to know how to create an algorithm and to understand the importance of algorithms, we should know what they are. So, what are they?
An Algorithm is…
• The idea behind a computer program.
• Stays the same, no matter what language is used to create the code.
• Should solve a general, specified problem
• So, what’s a problem?
A Problem is…
• Specified by describing the set of instances on which an algorithm must work and what properties are desired of the output.
• NOTE: A problem and an instance of a problem are two very different things!
Example Problem: Sorting
• The algorithmic problem known as sorting is defined as follows:
Example Problem: Sorting
• An instance of the sorting problem might be one of the following:
• An array of names
• A list of numbers
• A group of records
First Steps!
• In class, you will be given problems to solve. Discovering the general problem behind the instance given to you will lead you quite a way down the path toward solving the problem.
Correct and Efficient
• Solutions must be correct. For any algorithm, we must prove it always produces the desired output for all legal instances of the problem
• Example: Sorting – must provide sorted output even if
• The input is already sorted
• It contains repeated elements
• You have an empty list
• Etc…
Correct and Efficient
• Efficient we’ll talk about more later – let’s just say that no one likes to wait too long to get results for now…
• We also don’t want to strain the resources of our machines too badly – no one likes the dreaded blue screen of death.
A Sorting Algorithm (there are many!)
• Insertion Sort – anyone know how it works?
Insertion Sort
• Works the way people sort a handful of playing cards. Take an empty left hand and the cards face down on the table.
• Pick up one card at a time and place them into their appropriate position in your hand, comparing each new card with the ones in your hand from left to right until the right spot is found.
• At all times, the cards in the left hand are sorted
What else makes a good algorithm design?
• It should be easy to implement. (Didn’t think I’d say that, did you?)
• You may not be able to accomplish the three goals of design (correct, efficient, easy to implement) every time; however, they are what we’re shooting for.
Possible Solutions?
• Nearest Neighbor
• Start at some point and then walk to the nearest neighbor point. Repeat until done.
• Simple to understand and implement
• Makes sense
• Works on previous example
• Any problems?
Nearest Neighbor Won’t Work!
• Doesn’t work in all instances of the problem.
• While it always finds a tour, doesn’t always find the shortest one.
What about always starting at the leftmost point?
• Which is the leftmost point now?
• THUS: You are working to solve the GENERAL problem, not a particular INSTANCE of the problem.
So, now what?
• Ok – so we tried one thing and it didn’t work. Let’s say that always choosing the closest point is too restrictive –
• Try to repeatedly connect the closest pair of points whose connection will not create a problem (cause a cycle or a three-way branch) until all points are in one tour.
Closest Pair Tour
• Works GREAT for the last example… any problems?
Closest Pair Tour
• Consider a set of two rows of equally spaced points, with the rows slightly closer together than the neighboring points are spaced in each row.
How about just finding all paths and picking the best one? (aka: Exhaustive Search)
• Guaranteed to end up with the shortest tour.
• Correct – we pick the best of all possibilities
• REALLY SLOW! 20 points would produce 20! (2,432,902,008,176,640,000) orderings
• Not even realistic, as most circuit boards have about 1,000 points to deal with. The end of time may come before this algorithm solves the problem! (and then, why would we need a solution?
• This is also called the Traveling Salesman Problem – heard of it now?
Take Home Lesson
• There is a fundamental difference between algorithms which always produce a correct result and heuristics, which may usually do a good job but without providing any guarantee.
Why not just use a better computer?
• A faster algorithm running on a slower computer will always win for sufficiently large instances of a problem.
• Usually, problems don’t have to get that large before the faster algorithm wins.
Expressing Algorithms
• We need to be able to express the sequence of steps used in an algorithm in order to pass that information along to others.
• In order of increasing precision, we have English, Pseudocode, and Real Programming Languages (like Java or C++)
• Unfortunately, ease of expression moves in the reverse order!
Expressing Algorithms
• English is the most natural but least precise way to express a description of an algorithm. If you use English descriptions, you must be very careful to describe fully each step to be performed.
Expressing Algorithms
• Programming languages like Java, C, or C++ are precise ways of expressing algorithms, but difficult to write and understand
Expressing Algorithms
• Pseudocode is best defined as a programming language that never complains about syntax errors.
Expressing Algorithms
• What am I looking for?
• Describe the ideas of the algorithm in English
• Use Pseudocode or a programming language to clarify, in a formal way, the tricky details you are concerned you may have missed in the English description.
Take Home Lesson
• The heart of any algorithm is an idea. If your idea is not clearly revealed when you express an algorithm, they you are using too low-level a notation to describe it.
Problem Specifications
• You can expect problems to be specified in two parts:
• The set of allowable input instances
• The required properties of the algorithms output.
Problem Specifications
• Common Traps in Problem Specifications
• Sometimes, problem specifications allow too broad a class of input instances – it is your job to state any assumptions you make about the input and narrow it down to something that can be reasonably solved.
• Asking ill-defined questions for the output (find the “best” path – best should be defined – shortest, fastest, minimizing the number of turns, etc.)
• Don’t create compound goals for the algorithm – there should be a single criteria.
Take Home Lesson
• An important and honorable technique in algorithm design is to narrow the set of allowable instances until there is a correct and efficient algorithm.
Stated more formally:

Input: A set I of n intervals on the line

Output: What is the largest subset of mutually non-overlapping intervals which can be selected from I?

Your mission? Give an algorithm to solve the problem!

Ideas???

Ideas?
• Should work whenever work is available – earliest job first.
• Take the shortest job available first, so that I’m available more often.
• Try every possible permutation and see what works best!
• First job to terminate.
Earliest job first
• Start working as soon as there is work available. More formally:

EarliestJobFirst(I)

Accept the earliest starting job j from I which does not overlap any previously accepted job, and repeat until no such jobs remain.

Earliest Job first won’t work!
• The first job might be so long (War and Peace) that it prevents us from taking any other job.
Shortest Job First
• Always take the shortest possible job, so you spend the least time working (and thus the least time unavailable)

SortestJobFirst(I)

While (I != 0) do

Accept the shortest possible job j from I

Delete j, and intervals which intersect j from I

Shortest Job First Won’t Work!
• Taking the shortest job can prevent us from taking two longer jobs which barely overlap it.
Exhaustive Scheduling
• REALLY SLOW! – can be done, but takes 2n where n is the number of jobs
• Better than robot optimization (n! ) as there are only about a million subsets to deal with; still...
Does a solution exist?
• YES!! Think about the first job to terminate – other jobs may have started before that job, but all of these must partially overlap each other and we can select at most one from this group. Any overlappers could potentially block new jobs, so we can’t loose by choose this one!
First Job to Complete
• Take the job with the earliest completion date

OptimalScheduling(I)

While (I != 0) do

Accept job j with the earliest completion date

Delete j and whatever intersects j from I

Take Home Lesson
• Reasonable Looking algorithms can easily be incorrect. Algorithm correctness is a property that must be carefully demonstrated.
Demonstrating Incorrectness
• Searching for counterexamples is the best way to disprove the correctness of a heuristic
• Think about all small examples
• Think about examples with ties on your design criteria (pick the nearest point)
• Think about examples with extremes of big and small
Hunting for Counterexamples
• Think Small – several small examples are easier to understand and verify than one large ugly one. When algorithms fail, there is usually a very simple example which causes it to happen.
Hunting for Counterexamples
• Look for weaknesses – if the algorithms says “always take the biggest” think about ways that wouldn’t work – what if the input was all the same size? Suddenly, the algorithm has no way to choose a “good” starting point and may choose a less than optimal one.
Hunting for Counterexamples
• Look for extremes – Many counterexamples are mixtures of huge and tiny, few and many, near and far. It is usually easier to verify or reason about extremes.
Take Home Lesson
• Searching for counterexamples is the best way to disprove the correctness of a heuristic.
Induction and Recursion
• Failure to find counterexamples does not mean the algorithm is correct.
• Mathematical induction is a useful method for proving correctness of recursive algorithms
• Recursion and Induction basically the same thing:
• Base Case
• General Assumption
• General Case
Correctness of Insertion Sort
• The basis case consists of a single element, and by definition a one-element array is completely sorted.
• In general, we assume that the first n -1 elements of array A are completely sorted after n -1 iterations of insertion sort.
• To insert one last element x into A, we find where it goes (namely, the unique spot between the biggest element less than or equal to x and the smallest element greater than x). This is done by moving all the greater elements back by one position, creating room for x in the desired location.
Take Home Lesson
• Mathematical induction is usually the right way to verify the correctness of a recursive or incremental insertion algorithm.
Induction Explained Further:
• So, the general idea is to establish that a general statement is true of all natural numbers. To prove it, you:
• Prove the FIRST statement in an infinite sequence is true.
• Prove that if ANY ONE statement is true, then so is the next one.
And Further Still…
• The simplest and most common form of mathematical induction proves that a statement involving a natural number n holds for all values of n. The proof consists of two steps:
• The basis (base case): showing that the statement holds when n is equal to the lowest value that n is given in the question. Usually, n = 0 or n = 1.
• The inductive step: showing that if the statement holds for some n, then the statement also holds when n + 1 is substituted for n.
Induction Example
• The following statement will hold for all natural numbers:

0+1+2+…+n = n(n+1)/2

• In other words, the sum of the natural numbers is as stated above.
• PROVE IT!
Basis:
• Show it holds for some small n (ex: n=0)

0 = 0(0+1)/2 = 0*0/2 = 0

• Two sides of equation are equal – statement is true for n=0
Inductive Step
• Assume that this statement holds for all numbers up to “n”
• Show that the statement is true for “n+1”. In other words, show:

(0+1+2+…+n)+(n+1) = (n+1)((n+1)+1)/2

• is true.
Inductive Step:
• Using the assumption that the statement works up to “n”, we can rewrite the left side of the above equation as:

n(n+1)/2 + (n+1)

Algebraically, go to the proof
• Showing that P(n+1) holds!