Algorithms: Understanding the Ideas Behind Computer Programs, Slides of Data Structures and Algorithms

An introduction to algorithms, explaining what they are and their importance in computer programming. It covers the concept of algorithmic problems, correctness, and efficiency, using sorting as an example. The document also discusses two popular algorithms for finding the shortest robot tour and the movie star scheduling problem, and their limitations.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Algorithms
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Algorithms: Understanding the Ideas Behind Computer Programs and more Slides Data Structures and Algorithms in PDF only on Docsity!

Introduction to Algorithms

What Is An Algorithm?

Algorithms are the ideas behind computer programs. An algorithm is the thing which stays the same whether the program is in Pascal running on a Cray in New York or is in BASIC running on a Macintosh in Kathmandu! To be interesting, an algorithm has to solve a general, specified problem. An algorithmic problem is specified by describing the set of instances it must work on and what desired properties the output must have.

Correctness

For any algorithm, we must prove that it always returns the desired output for all legal instances of the problem. For sorting, this means even if (1) the input is already sorted, or (2) it contains repeated elements. Algorithm correctness is not obvious in many optimization problems!

Robot Tour Optimization

Suppose you have a robot arm equipped with a tool, say a soldering iron. To enable the robot arm to do a soldering job, we must construct an ordering of the contact points, so the robot visits (and solders) the points in order. We seek the order which minimizes the testing time (i.e. travel distance) it takes to assemble the circuit board.

Nearest Neighbor Tour

A popular solution starts at some point p 0 and then walks to its nearest neighbor p 1 first, then repeats from p 1 , etc. until done.

Pick and visit an initial point p 0 p = p 0 i = 0 While there are still unvisited points i = i + 1 Let pi be the closest unvisited point to pi− 1 Visit pi Return to p 0 from pi

Nearest Neighbor Tour is Wrong!

-21 -5^ -1^0 1 3

-21 -5 -1^0 1 3

Starting from the leftmost point will not fix the problem.

Closest Pair Tour is Wrong!

Although it works correctly on the previous example, other data causes trouble:

A Correct Algorithm: Exhaustive Search

We could try all possible orderings of the points, then select the one which minimizes the total length:

d = ∞ For each of the n! permutations Πi of the n points If (cost(Πi) ≤ d) then d = cost(Πi) and Pmin = Πi Return Pmin

Since all possible orderings are considered, we are guaranteed to end up with the shortest possible tour.

Efficiency Why Not Use a Supercomputer?

A faster algorithm running on a slower computer will always win for sufficiently large instances, as we shall see. Usually, problems don’t have to get that large before the faster algorithm wins.

Expressing Algorithms

We need some way to express the sequence of steps comprising an algorithm. In order of increasing precision, we have English, pseu- docode, and real programming languages. Unfortunately, ease of expression moves in the reverse order. I prefer to describe the ideas of an algorithm in English, moving to pseudocode to clarify sufficiently tricky details of the algorithm. Algorithms problems must be carefully specified to allow a provably correct algorithm to exist. We can find the “shortest tour” but not the “best tour”. %swallow

The Movie Star Scheduling Problem

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? Give an algorithm to solve the problem!

Earliest Job First

Start working as soon as there is work available:

EarliestJobFirst(I) Accept the earlest starting job j from I which does not overlap any previously accepted job, and repeat until no more such jobs remain.

Shortest Job First

Always take the shortest possible job, so you spend the least time working (and thus unavailable).

ShortestJobFirst(I) While (I 6 = ∅) do Accept the shortest possible job j from I. Delete j, and intervals which intersect j from I.

Shortest Job First is Wrong!

Taking the shortest job can prevent us from taking two longer jobs which barely overlap it.