SlideShare a Scribd company logo
Lecture 1:
  Introduction to Algorithms
         Steven Skiena

Department of Computer Science
 State University of New York
 Stony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena
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.
Example: Sorting
Input: A sequence of N numbers a1 ...an
Output: the permutation (reordering) of the input sequence
such as a1 ≤ a2 . . . ≤ an.
We seek algorithms which are correct and efficient.
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.
Find the Shortest Robot Tour




You are given the job to program the robot arm. Give me an
algorithm to find the best tour!
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
Nearest Neighbor Tour is Wrong!




        -21                  -5   -1   0   1   3   11




        -21                  -5   -1   0   1   3   11




Starting from the leftmost point will not fix the problem.
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.
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.
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.
No efficient, correct algorithm exists for the traveling
salesman problem, as we will see later.
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
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                         Process Terminated
                                           Halting State            Programming Challenges
            "Discrete" Mathematics                                                             Calculated Bets
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.
Earliest Job First is Wrong!
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 unavailable).
ShortestJobFirst(I)
      While (I = ∅) 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.
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.
First Job to Complete is Optimal!
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
out more opportunties to the right of x.
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. . .
Induction and Recursion
Failure to find a counterexample to a given algorithm does
not mean “it is obvious” that the algorithm is correct.
Mathematical induction is a very useful method for proving
the correctness of recursive algorithms.
Recursion and induction are the same basic idea: (1) basis
case, (2) general assumption, (3) general case.
                     n
                          i = n(n + 1)/2
                    i=1
Ad

Recommended

Algorithm chapter 10
Algorithm chapter 10
chidabdu
 
P vs NP
P vs NP
Mikel Qafa
 
P versus NP
P versus NP
Farid El Hajj
 
27 NP Completness
27 NP Completness
Andres Mendez-Vazquez
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
Np completeness
Np completeness
Rajendran
 
Calc 3.3a
Calc 3.3a
hartcher
 
Daa notes 3
Daa notes 3
smruti sarangi
 
Introduction to NP Completeness
Introduction to NP Completeness
Gene Moo Lee
 
Computability and Complexity
Computability and Complexity
Edward Blurock
 
Turing machine
Turing machine
MuhammadSamranTanvee
 
NP Complete Problems in Graph Theory
NP Complete Problems in Graph Theory
Seshagiri Rao Kornepati
 
Np Completeness
Np Completeness
Rajan Shah
 
lecture 27
lecture 27
sajinsc
 
Divide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Notion of an algorithm
Notion of an algorithm
Nisha Soms
 
Greedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
2009 CSBB LAB 新生訓練
2009 CSBB LAB 新生訓練
Abner Huang
 
09 cv mil_classification
09 cv mil_classification
zukun
 
10 cv mil_graphical_models
10 cv mil_graphical_models
zukun
 
Skiena algorithm 2007 lecture22 np completeness challenge
Skiena algorithm 2007 lecture22 np completeness challenge
zukun
 
Fcv hist lowe
Fcv hist lowe
zukun
 
Fcv poster ji
Fcv poster ji
zukun
 
Fcv appli science_golland
Fcv appli science_golland
zukun
 
Fcv taxo chellappa
Fcv taxo chellappa
zukun
 
Fcv acad ind_martin
Fcv acad ind_martin
zukun
 
Fcv scene efros
Fcv scene efros
zukun
 
Fcv scene lazebnik
Fcv scene lazebnik
zukun
 
Fcv rep tenenbaum
Fcv rep tenenbaum
zukun
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
zukun
 

More Related Content

What's hot (10)

Introduction to NP Completeness
Introduction to NP Completeness
Gene Moo Lee
 
Computability and Complexity
Computability and Complexity
Edward Blurock
 
Turing machine
Turing machine
MuhammadSamranTanvee
 
NP Complete Problems in Graph Theory
NP Complete Problems in Graph Theory
Seshagiri Rao Kornepati
 
Np Completeness
Np Completeness
Rajan Shah
 
lecture 27
lecture 27
sajinsc
 
Divide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Notion of an algorithm
Notion of an algorithm
Nisha Soms
 
Greedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
2009 CSBB LAB 新生訓練
2009 CSBB LAB 新生訓練
Abner Huang
 
Introduction to NP Completeness
Introduction to NP Completeness
Gene Moo Lee
 
Computability and Complexity
Computability and Complexity
Edward Blurock
 
Np Completeness
Np Completeness
Rajan Shah
 
lecture 27
lecture 27
sajinsc
 
Divide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
Notion of an algorithm
Notion of an algorithm
Nisha Soms
 
2009 CSBB LAB 新生訓練
2009 CSBB LAB 新生訓練
Abner Huang
 

Viewers also liked (20)

09 cv mil_classification
09 cv mil_classification
zukun
 
10 cv mil_graphical_models
10 cv mil_graphical_models
zukun
 
Skiena algorithm 2007 lecture22 np completeness challenge
Skiena algorithm 2007 lecture22 np completeness challenge
zukun
 
Fcv hist lowe
Fcv hist lowe
zukun
 
Fcv poster ji
Fcv poster ji
zukun
 
Fcv appli science_golland
Fcv appli science_golland
zukun
 
Fcv taxo chellappa
Fcv taxo chellappa
zukun
 
Fcv acad ind_martin
Fcv acad ind_martin
zukun
 
Fcv scene efros
Fcv scene efros
zukun
 
Fcv scene lazebnik
Fcv scene lazebnik
zukun
 
Fcv rep tenenbaum
Fcv rep tenenbaum
zukun
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
zukun
 
Fcv rep a_berg
Fcv rep a_berg
zukun
 
Fcv hum mach_belongie
Fcv hum mach_belongie
zukun
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
 
04 structured support vector machine
04 structured support vector machine
zukun
 
19 cv mil_temporal_models
19 cv mil_temporal_models
zukun
 
Fcv hum mach_t_berg
Fcv hum mach_t_berg
zukun
 
CVPR2012: Tutorial: Graphcut-based Optimisation for Computer Vision
CVPR2012: Tutorial: Graphcut-based Optimisation for Computer Vision
zukun
 
Pp présentation apsad 93 part1
Pp présentation apsad 93 part1
gorbi94
 
09 cv mil_classification
09 cv mil_classification
zukun
 
10 cv mil_graphical_models
10 cv mil_graphical_models
zukun
 
Skiena algorithm 2007 lecture22 np completeness challenge
Skiena algorithm 2007 lecture22 np completeness challenge
zukun
 
Fcv hist lowe
Fcv hist lowe
zukun
 
Fcv poster ji
Fcv poster ji
zukun
 
Fcv appli science_golland
Fcv appli science_golland
zukun
 
Fcv taxo chellappa
Fcv taxo chellappa
zukun
 
Fcv acad ind_martin
Fcv acad ind_martin
zukun
 
Fcv scene efros
Fcv scene efros
zukun
 
Fcv scene lazebnik
Fcv scene lazebnik
zukun
 
Fcv rep tenenbaum
Fcv rep tenenbaum
zukun
 
Skiena algorithm 2007 lecture06 sorting
Skiena algorithm 2007 lecture06 sorting
zukun
 
Fcv rep a_berg
Fcv rep a_berg
zukun
 
Fcv hum mach_belongie
Fcv hum mach_belongie
zukun
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
zukun
 
04 structured support vector machine
04 structured support vector machine
zukun
 
19 cv mil_temporal_models
19 cv mil_temporal_models
zukun
 
Fcv hum mach_t_berg
Fcv hum mach_t_berg
zukun
 
CVPR2012: Tutorial: Graphcut-based Optimisation for Computer Vision
CVPR2012: Tutorial: Graphcut-based Optimisation for Computer Vision
zukun
 
Pp présentation apsad 93 part1
Pp présentation apsad 93 part1
gorbi94
 
Ad

Similar to Skiena algorithm 2007 lecture01 introduction to algorithms (20)

lecture1 .pdf introduction to algorithms
lecture1 .pdf introduction to algorithms
kero01289992383
 
2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou
vafopoulos
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
algorithms-1 master in computer application
algorithms-1 master in computer application
hydratedpriyanshuvlo
 
analysis of algorithms and asymptotic complexity
analysis of algorithms and asymptotic complexity
anurag721001
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
9. chapter 8 np hard and np complete problems
9. chapter 8 np hard and np complete problems
Jyotsna Suryadevara
 
(1) collections algorithms
(1) collections algorithms
Nico Ludwig
 
Algorithm in computer science
Algorithm in computer science
Riazul Islam
 
the halting_problem
the halting_problem
Rajendran
 
Daa notes 2
Daa notes 2
smruti sarangi
 
The Complexity Of Primality Testing
The Complexity Of Primality Testing
Mohammad Elsheikh
 
Analysis of algorithms
Analysis of algorithms
Mallikarjun Biradar
 
Introduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
01-algo.ppt
01-algo.ppt
ssuser8d64ca
 
Asymptotic Notations
Asymptotic Notations
NagendraK18
 
DAA UNIT 3
DAA UNIT 3
Dr. SURBHI SAROHA
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
Shiwani Gupta
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
lecture1 .pdf introduction to algorithms
lecture1 .pdf introduction to algorithms
kero01289992383
 
2010 3-24 cryptography stamatiou
2010 3-24 cryptography stamatiou
vafopoulos
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
algorithms-1 master in computer application
algorithms-1 master in computer application
hydratedpriyanshuvlo
 
analysis of algorithms and asymptotic complexity
analysis of algorithms and asymptotic complexity
anurag721001
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
9. chapter 8 np hard and np complete problems
9. chapter 8 np hard and np complete problems
Jyotsna Suryadevara
 
(1) collections algorithms
(1) collections algorithms
Nico Ludwig
 
Algorithm in computer science
Algorithm in computer science
Riazul Islam
 
the halting_problem
the halting_problem
Rajendran
 
The Complexity Of Primality Testing
The Complexity Of Primality Testing
Mohammad Elsheikh
 
Introduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
Asymptotic Notations
Asymptotic Notations
NagendraK18
 
module1_Introductiontoalgorithms_2022.pdf
module1_Introductiontoalgorithms_2022.pdf
Shiwani Gupta
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
Ad

More from zukun (20)

My lyn tutorial 2009
My lyn tutorial 2009
zukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
zukun
 
ETHZ CV2012: Information
ETHZ CV2012: Information
zukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statistics
zukun
 
Lecture9 camera calibration
Lecture9 camera calibration
zukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
zukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluation
zukun
 
Modern features-part-3-software
Modern features-part-3-software
zukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptors
zukun
 
Modern features-part-1-detectors
Modern features-part-1-detectors
zukun
 
Modern features-part-0-intro
Modern features-part-0-intro
zukun
 
Lecture 02 internet video search
Lecture 02 internet video search
zukun
 
Lecture 01 internet video search
Lecture 01 internet video search
zukun
 
Lecture 03 internet video search
Lecture 03 internet video search
zukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
zukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
zukun
 
Gephi tutorial: quick start
Gephi tutorial: quick start
zukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
Object recognition with pictorial structures
Object recognition with pictorial structures
zukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 
My lyn tutorial 2009
My lyn tutorial 2009
zukun
 
ETHZ CV2012: Tutorial openCV
ETHZ CV2012: Tutorial openCV
zukun
 
ETHZ CV2012: Information
ETHZ CV2012: Information
zukun
 
Siwei lyu: natural image statistics
Siwei lyu: natural image statistics
zukun
 
Lecture9 camera calibration
Lecture9 camera calibration
zukun
 
Brunelli 2008: template matching techniques in computer vision
Brunelli 2008: template matching techniques in computer vision
zukun
 
Modern features-part-4-evaluation
Modern features-part-4-evaluation
zukun
 
Modern features-part-3-software
Modern features-part-3-software
zukun
 
Modern features-part-2-descriptors
Modern features-part-2-descriptors
zukun
 
Modern features-part-1-detectors
Modern features-part-1-detectors
zukun
 
Modern features-part-0-intro
Modern features-part-0-intro
zukun
 
Lecture 02 internet video search
Lecture 02 internet video search
zukun
 
Lecture 01 internet video search
Lecture 01 internet video search
zukun
 
Lecture 03 internet video search
Lecture 03 internet video search
zukun
 
Icml2012 tutorial representation_learning
Icml2012 tutorial representation_learning
zukun
 
Advances in discrete energy minimisation for computer vision
Advances in discrete energy minimisation for computer vision
zukun
 
Gephi tutorial: quick start
Gephi tutorial: quick start
zukun
 
EM algorithm and its application in probabilistic latent semantic analysis
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
Object recognition with pictorial structures
Object recognition with pictorial structures
zukun
 
Iccv2011 learning spatiotemporal graphs of human activities
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 

Recently uploaded (20)

“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Improving Data Integrity: Synchronization between EAM and ArcGIS Utility Netw...
Safe Software
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 

Skiena algorithm 2007 lecture01 introduction to algorithms

  • 1. Lecture 1: Introduction to Algorithms Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. 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.
  • 3. Example: Sorting Input: A sequence of N numbers a1 ...an Output: the permutation (reordering) of the input sequence such as a1 ≤ a2 . . . ≤ an. We seek algorithms which are correct and efficient.
  • 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 optimization problems!
  • 5. 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.
  • 6. Find the Shortest Robot Tour You are given the job to program the robot arm. Give me an algorithm to find the best tour!
  • 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
  • 8. Nearest Neighbor Tour is Wrong! -21 -5 -1 0 1 3 11 -21 -5 -1 0 1 3 11 Starting from the leftmost point will not fix the problem.
  • 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.
  • 10. Closest Pair Tour is Wrong! Although it works correctly on the previous example, other data causes trouble:
  • 11. 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.
  • 12. 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. No efficient, correct algorithm exists for the traveling salesman problem, as we will see later.
  • 13. 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. 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
  • 15. 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 Process Terminated Halting State Programming Challenges "Discrete" Mathematics Calculated Bets
  • 16. 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. 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. Earliest Job First is Wrong! The first job might be so long (War and Peace) that it prevents us from taking any other job.
  • 19. 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. Shortest Job First is Wrong! Taking the shortest job can prevent us from taking two longer jobs which barely overlap it.
  • 21. 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. First Job to Complete is Optimal! 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 out more opportunties to the right of x.
  • 23. 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. Induction and Recursion Failure to find a counterexample to a given algorithm does not mean “it is obvious” that the algorithm is correct. Mathematical induction is a very useful method for proving the correctness of recursive algorithms. Recursion and induction are the same basic idea: (1) basis case, (2) general assumption, (3) general case. n i = n(n + 1)/2 i=1