Lecture 1: Introduction to Algorithms Tyler Moore SMU Computer - - PowerPoint PPT Presentation

lecture 1 introduction to algorithms
SMART_READER_LITE
LIVE PREVIEW

Lecture 1: Introduction to Algorithms Tyler Moore SMU Computer - - PowerPoint PPT Presentation

Lecture 1: Introduction to Algorithms Tyler Moore SMU Computer Science and Engineering CSE 3353 These slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more


slide-1
SLIDE 1

Lecture 1: Introduction to Algorithms Tyler Moore SMU Computer Science and Engineering CSE 3353

These slides have been adapted from the slides written by Prof. Steven Skiena at SUNY Stony Brook, author of Algorithm Design Manual. For more information see http://www .cs.sunysb.edu/~skiena/

1

slide-2
SLIDE 2

What Is An Algorithm?

  • Algorithms are the ideas behind computer programs
  • Algorithms don’t change regardless of whether it is written in

C++, Java, Python, Fortran, COBOL, Pascal, or Haskell.

  • Up to now

, you have been taught how to write code that solves particular problems or constructs a useful data structure, but in this course we take a step back and study how to computationally solve general problems

  • T
  • 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.

2

slide-3
SLIDE 3

Example: Sorting

Input: A sequence of N numbers a1...an Output: the permutation (reordering) of the input sequence such as a1 ≤ a2 . . . ≤ an. W e seek algorithms which are correct and efficient.

3

slide-4
SLIDE 4

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
  • ptimization problems!

4

slide-5
SLIDE 5

Robot Tour Optimization

5

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. W e seek the order which minimizes the testing time (i.e. travel distance) it takes to assemble the circuit board. Input: A set S of points on a plane Output: The shortest cycle tour that visits each point in S

slide-6
SLIDE 6

Find the Shortest Robot Tour

Y

  • u are given the job to program the robot arm. Give me an

algorithm to find the best tour!

6

slide-7
SLIDE 7

Nearest Neighbor Tour

A popular solution starts at some point p0 and then walks to its nearest neighbor p1 first, then repeats from p1, etc. until done. Pick and visit an initial point p0 p = p0 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 p0 from pi

7

slide-8
SLIDE 8

Nearest Neighbor Tour is Wrong!

  • 1 0 1

3 11

  • 21
  • 5
  • 1 0 1

3 11

  • 21
  • 5

Starting from the leftmost point will not fix the problem.

8

slide-9
SLIDE 9

Closest Pair Tour

Another idea is to repeatedly connect the closest pair of points whose connection will not cause a cycle or a three-way branch, until all points are in one tour. Let n be the number of points in the set d= ∞ For i = 1 to n − 1 do For each pair of endpoints (x, y) of partial paths If dist(x, y) ≤ d then xm = x, ym = y, d = dist(x, y) Connect (xm, ym) by an edge Connect the two endpoints by an edge.

9

slide-10
SLIDE 10

Closest Pair Tour is Wrong!

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

10

slide-11
SLIDE 11

A Correct Algorithm: Exhaustive Search

11

W e 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.

slide-12
SLIDE 12

Why are there n! orderings?

12 B A D C

  • 1. Choose one of ABCD (4 choices), say A
  • 2. Choose one in BCD (3 choices), say B
  • 3. Choose one in CD (2 choices), say C
  • 4. Choose one in D (1 choice), say D

Multiply the # of choices at each step to get the total # of orderings

slide-13
SLIDE 13

Exhaustive Search is Slow!

Because it tries all n! permutations, it is much too slow to use when there are more than 10-20 points. 20!≈2,432,902,000,000,000,000 configurations! No efficient, correct algorithm exists for the traveling salesman problem, as we will see later.

13

slide-14
SLIDE 14

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.

14

slide-15
SLIDE 15

Expressing Algorithms

  • W

e need some way to express the sequence of steps comprising an algorithm.

  • In order of increasing precision, we have English,

pseudocode, 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. W e can find the “shortest tour” but not the “best tour”.

15

slide-16
SLIDE 16

Selecting the Right Jobs

A movie star wants to the select the maximum number of staring roles such that no two jobs require his presence at the same time.

Tarjan of the Jungle The Four Volume Problem The President’s Algorist Steiner’s Tree Halting State "Discreet" Mathematics Calculated Bets Process Terminated Programming Challenges

16

slide-17
SLIDE 17

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!

17

slide-18
SLIDE 18

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.

18

slide-19
SLIDE 19

Earliest Job First is Wrong!

The first job might be so long (W ar and Peace) that it prevents us from taking any other job.

19

slide-20
SLIDE 20

Shortest Job First

Always take the shortest possible job, so you spend the least time working (and thus unavailable). ShortestJobFirst(I) While (I ≠ ∅ ) do Accept the shortest possible job j from I . Delete j , and intervals which intersect j from I .

20

slide-21
SLIDE 21

Shortest Job First is Wrong!

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

21

slide-22
SLIDE 22

First Job to Complete

Take the job with the earliest completion date: OptimalScheduling(I) While (I ≠ ∅ ) do Accept job j with the earliest completion date. Delete j , and whatever intersects j from I .

22

The President’s Algorist Steiner’s Tree Halting State "Discreet" Mathematics Calculated Bets Process Terminated Programming Challenges

Tarjan of the Jungle The Four Volume Problem

slide-23
SLIDE 23

First Job to Complete is Optimal!

Why should you believe me?

  • Other jobs may well have started before the first to complete

(x), but all must at least partially overlap each other.

  • Thus we can select at most one from the group.
  • The first these jobs to complete is x, so the rest can only block
  • ut more opportunties to the right of x.

23

slide-24
SLIDE 24

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 decision criteria

(e.g. pick the nearest point)

  • Think about examples with extremes of big and small. . .

24

slide-25
SLIDE 25

Problem of the Day

The knapsack problem is as follows: given a set of integers S = {s1, s2, . . . , sn}, and a given target number T , find a subset of S which adds up exactly to T . For example, within S = {1, 2, 5, 9, 10} there is a subset which adds up to T = 22 but not T = 23. Find counterexamples to each of the following algorithms for the knapsack problem. That is, give an S and T such that the subset is selected using the algorithm does not leave the knapsack completely full, even though such a solution exists.

slide-26
SLIDE 26

Problem of the Day

1. Put the elements of S in the knapsack in left to right

  • rder if they fit, i.e. the first-fit algorithm?

2. Put the elements of S in the knapsack from smallest to largest, i.e. the best-fit algorithm? 3. Put the elements of S in the knapsack from largest to smallest? Due Thursday at the beginning of class

slide-27
SLIDE 27

A solution to part 1

1. Put the elements of S in the knapsack in left to right

  • rder if they fit, i.e. the first-fit algorithm?
  • Hint: Think extreme!