lecture 1: introduction to algorithms steven skiena

25
Lecture 1: Introduction to Algorithms Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.stonybrook.edu/ ˜ skiena

Upload: others

Post on 15-Jan-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1: Introduction to Algorithms Steven Skiena

Lecture 1:Introduction to Algorithms

Steven Skiena

Department of Computer ScienceState University of New YorkStony Brook, NY 11794–4400

http://www.cs.stonybrook.edu/˜skiena

Page 2: Lecture 1: Introduction to Algorithms Steven Skiena

Syllabus / Course Mechanics

• Prerequisites (Data structures and linear algebra)

• Textbook

• Grading

• Homeworks

• Daily problems

• Exams

• Rules of the Game

Page 3: Lecture 1: Introduction to Algorithms Steven Skiena

1 Introduction to algorithms 1-272 Asymptotic notation 31-403 Logarithms and more 41-56 HW1 out4 Elementary data structures 65-835 Dictionary data structures 83-896 Hashing 89-987 Applications of Sorting 103-1088 Heapsort/Priority Queues 108-119 HW1 in / HW2out9 Mergesort/Quicksort/Binsort 120-138

Midterm 110 Data structures for graphs 145-16011 Breadth-first search 161-168 HW2 in / HW3 out12 Topological sort/connectivity 169-18313 Minimum spanning trees 191-20414 Shortest paths 205-21615 Exploiting graph algorithms 217-22416 Combinatorial search 230-238 HW3 in / HW4 out17 Program optimization 239-24718 Elements of dynamic programming 273-29019 Examples of dynamic programming 291-30020 Limitations of dynamic programming 301-310 HW4 in / HW5 out21 Dynamic programming review

Midterm 222 Reductions 316-32223 Easy reductions 323-32924 Harder reductions 330-33325 The NP-completeness challenge 334-340 HW 5 in

Final Exam

Page 4: Lecture 1: Introduction to Algorithms Steven Skiena

What Is An Algorithm?

Algorithms are the ideas behind computer programs.An algorithm is the thing which stays the same whether theprogram is in assembly language running on a supercomputerin New York or running on a cell phone in Kathmandu inPython!To be interesting, an algorithm has to solve a general,specified problem.An algorithmic problem is specified by describing the set ofinstances it must work on, and what desired properties theoutput must have.

Page 5: Lecture 1: Introduction to Algorithms Steven Skiena

Example Problem: Sorting

Input: A sequence of N numbers a1...anOutput: the permutation (reordering) of the input sequencesuch as a1 ≤ a2 . . . ≤ an.

We seek algorithms which are correct and efficient.A faster algorithm running on a slower computer will alwayswin for sufficiently large instances, as we shall see.Usually, problems don’t have to get that large before the fasteralgorithm wins.

Page 6: Lecture 1: Introduction to Algorithms Steven Skiena

Correctness

For any algorithm, we must prove that it always returns thedesired 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 optimizationproblems!Algorithms problems must be carefully specified to allow aprovably correct algorithm to exist. We can find the “shortesttour” but not the “best tour”.

Page 7: Lecture 1: Introduction to Algorithms Steven Skiena

Robot Tour Optimization

Suppose you have a robot arm equipped with a tool, say asoldering iron. To enable the robot arm to do a soldering job,we must construct an ordering of the contact points, so therobot 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.

Page 8: Lecture 1: Introduction to Algorithms Steven Skiena

Find the Shortest Robot Tour

You are given the job to program the robot arm. Give me analgorithm to find the most efficient tour!

Page 9: Lecture 1: Introduction to Algorithms Steven Skiena

Nearest Neighbor TourA popular solution starts at some point p0 and then walks toits nearest neighbor p1 first, then repeats from p1, etc. untildone.

Pick and visit an initial point p0p = p0i = 0While there are still unvisited points

i = i + 1Let pi be the closest unvisited point to pi−1Visit pi

Return to p0 from pi

Page 10: Lecture 1: Introduction to Algorithms Steven Skiena

Nearest Neighbor Tour is Wrong!

−1 0 1 3 11−21

−5

0 1 3 11−21

−5 −1

Starting from the leftmost point will not fix the problem.

Page 11: Lecture 1: Introduction to Algorithms Steven Skiena

Closest Pair Tour

Another idea is to repeatedly connect the closest pair ofpoints whose connection will not cause a cycle or a three-waybranch, until all points are in one tour.

Let n be the number of points in the setd =∞For i = 1 to n− 1 do

For each pair of endpoints (x, y) of partial pathsIf 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.

Page 12: Lecture 1: Introduction to Algorithms Steven Skiena

Closest Pair Tour is Wrong!

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

1 − ε

1 + ε

1 − ε

1 + ε

(l)1 + ε

1 − ε 1 − ε

1 + ε

(r)

Page 13: Lecture 1: Introduction to Algorithms Steven Skiena

A Correct Algorithm: Exhaustive Search

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

d =∞For each of the n! permutations Πi of the n points

If (cost(Πi) ≤ d) thend = cost(Πi) and Pmin = Πi

Return Pmin

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

Page 14: Lecture 1: Introduction to Algorithms Steven Skiena

Exhaustive Search is Slow!

Because it tries all n! permutations, it is much too slow to usewhen there are more than 10-20 points.No efficient, correct algorithm exists for the travelingsalesman problem, as we will see later.

Page 15: Lecture 1: Introduction to Algorithms Steven Skiena

Expressing Algorithms

We need some way to express the sequence of stepscomprising 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 ofthe algorithm.

Page 16: Lecture 1: Introduction to Algorithms Steven Skiena

Selecting the Right Jobs

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

Process Terminated

"Discrete" MathematicsHalting State Programming Challenges

Calculated Bets

Tarjan of the Jungle

The President’s Algorist Steiner’s Tree

The Four Volume Problem

Page 17: Lecture 1: Introduction to Algorithms Steven Skiena

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!

Page 18: Lecture 1: Introduction to Algorithms Steven Skiena

Earliest Job First

Start working as soon as there is work available:

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

Page 19: Lecture 1: Introduction to Algorithms Steven Skiena

Earliest Job First is Wrong!

The first job might be so long (War and Peace) that it preventsus from taking any other job.

Page 20: Lecture 1: Introduction to Algorithms Steven Skiena

Shortest Job First

Always take the shortest possible job, so you spend the leasttime 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 .

Page 21: Lecture 1: Introduction to Algorithms Steven Skiena

Shortest Job First is Wrong!

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

Page 22: Lecture 1: Introduction to Algorithms Steven Skiena

First Job to Complete

Take the job with the earliest completion date:

OptimalScheduling(I)While (I 6= ∅) do

Accept job j with the earliest completion date.Delete j, and whatever intersects j from I .

Page 23: Lecture 1: Introduction to Algorithms Steven Skiena

First Job to Complete is Optimal!

Proof: Other jobs may well have started before the first tocomplete (say, x), but all must at least partially overlap bothx and each other.Thus we can select at most one from the group.The first these jobs to complete is x, so selecting any job butx would only block out more opportunties after x.

Page 24: Lecture 1: Introduction to Algorithms Steven Skiena

Demonstrating Incorrectness

Searching for counterexamples is the best way to disprove thecorrectness 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. . .

Page 25: Lecture 1: Introduction to Algorithms Steven Skiena

Induction and Recursion

Failure to find a counterexample to a given algorithm doesnot mean “it is obvious” that the algorithm is correct.Mathematical induction is a very useful method for provingthe correctness of recursive algorithms.Recursion and induction are the same basic idea: (1) basiscase, (2) general assumption, (3) general case.

n∑i=1

i = n(n + 1)/2