chapter 1 introduction to algorithm design n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 1: Introduction to Algorithm Design PowerPoint Presentation
Download Presentation
Chapter 1: Introduction to Algorithm Design

Loading in 2 Seconds...

play fullscreen
1 / 60

Chapter 1: Introduction to Algorithm Design - PowerPoint PPT Presentation


  • 231 Views
  • Uploaded on

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.

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

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


An Image/Link below is provided (as is) to download presentation

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
what is an algorithm
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
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
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
Example Problem: Sorting
  • The algorithmic problem known as sorting is defined as follows:
example problem sorting1
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
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
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 efficient1
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
A Sorting Algorithm (there are many!)
  • Insertion Sort – anyone know how it works?
insertion sort
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
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
Possible Solutions?
  • Nearest Neighbor
    • Start at some point and then walk to the nearest neighbor point. Repeat until done.
advantages to nearest neighbor
Advantages to Nearest Neighbor…
  • Simple to understand and implement
  • Makes sense
  • Works on previous example
  • Any problems?
nearest neighbor won t work
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
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
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
Closest Pair Tour
  • Works GREAT for the last example… any problems?
closest pair tour1
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
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
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
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
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 algorithms1
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 algorithms2
Expressing Algorithms
  • Programming languages like Java, C, or C++ are precise ways of expressing algorithms, but difficult to write and understand
expressing algorithms3
Expressing Algorithms
  • Pseudocode is best defined as a programming language that never complains about syntax errors.
expressing algorithms4
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 lesson1
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
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 specifications1
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 lesson2
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
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
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
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
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
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
Shortest Job First Won’t Work!
  • Taking the shortest job can prevent us from taking two longer jobs which barely overlap it.
exhaustive scheduling
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
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
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 lesson3
Take Home Lesson
  • Reasonable Looking algorithms can easily be incorrect. Algorithm correctness is a property that must be carefully demonstrated.
demonstrating incorrectness
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
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 counterexamples1
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 counterexamples2
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 lesson4
Take Home Lesson
  • Searching for counterexamples is the best way to disprove the correctness of a heuristic.
induction and recursion
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
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 lesson5
Take Home Lesson
  • Mathematical induction is usually the right way to verify the correctness of a recursive or incremental insertion algorithm.
induction explained further
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
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
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
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
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 step1
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
Algebraically, go to the proof
  • Showing that P(n+1) holds!