SlideShare a Scribd company logo
1 of 33
Download to read offline
Algorithms
National Chiao Tung University
Chun-Jen Tsai
04/13/2012
2/33
Algorithm
An algorithm is an ordered set of unambiguous,
executable steps that defines a terminating process
“Ordered” does not imply “sequential” → there are parallel
algorithms
An algorithm is an abstract concept
There can be several physical implementations of an
algorithm (using same or different programming languages)
Given same input, different implementations of an algorithm
should produce the same output
For example, an algorithm is like a story, and an
implementation is like a book on the story
3/33
Terminology Clarification
What is the differences between an algorithm, a
program, and a process?
Read the last paragraph of Sec 5.1 of the textbook carefully!
A program is a formal representation of an algorithm, which
can be executed by a computer
A process is the activity of executing an algorithm (or
equivalently, a program)
4/33
Algorithm Representation (1/2)
We must find a way to separate an algorithm from the
actual implementation, but still present it precisely
In 1950’s~1970’s, flowcharts is very popular in
describing an algorithm
→ too cumbersome for
sophisticated design
Today, we usually use a pseudocode language (not a
programming language) to describe an algorithm
start
condition
perform A perform B
Halt
Y
N
5/33
Algorithm Representation (2/2)
Theoretically, any languages (e.g. English) can be
used as a pseudocode language to describe
algorithms
In practice, a good pseudocode language must avoid
any ambiguities
A language with a small set of well-defined building blocks
(called primitives) can removes ambiguity
Each primitive is a single operation you can apply to the data
6/33
Origami is a
complicated
procedure
Key question:
What operations
can we perform
at each step?
Example: Origami
7/33
Origami Primitives
Primitives defined
in our programming
language limit our
implementation
of an algorithm
8/33
Pseudocode Example
Pseudocode primitives
Assignment: name ← expression;
Conditional selection: if (condition) then ( action )
Repeated execution: while (condition) do ( action )
Procedure: procedure name (parameters)
Example:
procedure Greetings
Count ← 3;
while (Count > 0) do
(
print the message “Hello”;
Count ← Count – 1;
)
9/33
Algorithm Discovery
The techniques to discover an algorithm to solve real
world problem often requires specific domain
knowledge that you won’t learn in Computer Science
→ to launch a rocket, you must know physics well!
However, a big algorithm is usually composed of
many small “standard algorithms” which you will learn
in Computer Science curriculum
For example, “sorting numbers” is a crucial small algorithm
for many large problems
10/33
Polya’s Problem Solving Phases
In 1945, G. Polya defined four problem solving
phases
Phase 1: Understand the problem
Phase 2: Devise a plan for solving the problem
Phase 3: Carry out the plan
Phase 4: Evaluate the solution for accuracy and for its
potential as a tool for solving other problems
11/33
A Sample Problem
Person A is charged with the task of determining the ages of
B’s three children.
B tells A that the product of the children’s ages is 36.
A replies that another clue is required.
B tells A the sum of the children’s ages.
A replies that another clue is needed.
B tells A that the oldest child plays the piano.
A tells B the ages of the three children.
How old are the three children?
12/33
Techniques for Problem Solving
Work the problem backwards (reverse-engineering)
Look for solutions of an easier, related problem
Stepwise refinement (top-down methodology)
Popular technique because it produces modular programs
Breaking a big problem into smaller ones (bottom-up
methodology)
The solution of each small problem must be unit-tested
before integrating it into the big solution
13/33
Iterative Structures in Algorithms
It often happens that an algorithm contains repeated
actions, each action is similar to previous one
Example: sequential search
procedure Search (List, TargetValue)
if (List empty) then
( Declare search a failure; )
else
(
Select the first entry in List to be TestEntry;
while (TargetValue > TestEntry and
there remain entries to be considered)
do ( Select the next entry in List as TestEntry; )
if (TargetValue = TestEntry) then
( Declare search a success; )
else
( Declare search a failure; )
)
14/33
Two Variants of Iterative Structure
Iterative structure can be implemented using one of
two methods
Loop structure
Recursive structure
An iterative structure is composed of two parts
Body of repetition
Repetitive control
15/33
Components of Repetitive Control
Initialize:
Establish an initial state that will be modified toward the
termination condition
Test:
Compare the current state to the termination condition and
terminate the repetition if equal
Modify:
Change the state in such a way that it moves toward the
termination condition
16/33
Loop Structure
The while loop: The repeat loop:
(body of repetition)
(body of repetition)
17/33
Example: Sorting (1/2)
Sorting the list:
Fred, Alex, Diana,
Byron, and Carol
alphabetically
The example
given here uses
insertion sort
18/33
Example: Sorting (2/2)
The pseudo code of insertion sort
procedure Sort(List)
N ← 2;
while (the value of N does not exceed the length of List) do
(
Select the Nth entry in List as the pivot entry;
Move the pivot entry to a temporary location, leaving a hole;
while (there is a name above the hole greater than the pivot) do
( move the name above the hole down into the hole, leaving
a hole above the name; )
Move the pivot entry into the hole in List;
N ← N + 1;
)
19/33
Recursive Structures
Recursive structures provides an alternative to the
iterative structures
Example: binary search
Goal: find John
20/33
Pseudo Code of Binary Search (1/2)
The pseudo code of binary search can be described
elegantly using recursive structure:
procedure Search(List, TargetValue)
if (List empty) then
( Report that the search failed; )
else
(
Select the middle entry in List to be the first TestEntry;
if (TargetValue = TestEntry) then
( Report that the search succeeded; )
else
(
if (TargetValue < TestEntry) then
( Search(Listtop, TargetValue); )
else
( Search(Listbottom, TargetValue); )
)
)
21/33
Pseudo Code of Binary Search (2/2)
Alice
Bob
Carol
David
Elaine
Fred
George
Harry
Irene
John
Kelly
Larry
Mary
Nancy
Oliver
Irene
John
Kelly
Larry
Mary
Nancy
Oliver
Search(ListBottom, “John”)
Irene
John
Kelly
Search(Listtop, “John”)
22/33
Recursive Control
Note that, in a recursive structure, repetitive control is
achieved by first testing the termination condition
before recursive calls
procedure my_function(parameters)
if (termination condition is true) then
(
Ends body of repetition
)
else
(
Perform recursive calls
)
23/33
Algorithm Efficiency
Question: when the number of input data grows
linearly, what is the growth of the number of
operations of an algorithm?
→ The answer to this question is the complexity of
the algorithm
Since the number of operations for processing a set
of data depends on both the size of the data and the
data pattern, an algorithm’s best case, worst case,
and average case complexity can be quite different
24/33
Example: Insertion Sort (1/2)
Applying the insertion sort in a worst-case situation
25/33
Example: Insertion Sort (2/2)
Graph of the worst-case analysis of the insertion sort
algorithm
26/33
Complexity Measure
The complexity of an algorithm can be classified
using the bit-theta notation, Θ, of the input data size n
Θ(f(n)) means that if the input data size is n, the
number of operations, c(n), of the algorithm grows at
the same speed of f(n), within constant factors.
That is, given constants k1 ≥ k2 > 0, as n → ∞,
k2⋅f(n) ≤ c(n) ≤ k1⋅f(n)
For example, the complexity of insertion sort is Θ(n2)
and the complexity of binary search is Θ(log2 n)
27/33
Software Verification
Once we have designed an algorithm, the next logical
question to ask is:
How do I prove the correctness of the algorithm?
Software verification’s goal is to prove that an
algorithm works by a formal procedure instead of
intuitive arguments
28/33
Example: Chain Separating (1/3)
A traveler has a gold chain of seven links. He must stay at an
isolated hotel for seven nights. The rent each night consists of
one link from the chain.
What is the fewest number of links that must be cut so that the
traveler can pay the hotel one link of the chain each morning
without paying for lodging in advance?
One possible solution:
Key question: is this the correct answer?
29/33
Example: Chain Separating (2/3)
In fact, the problem can be solved by using one
single cut!
30/33
Example: Chain Separating (3/3)
How do we prove that the 2nd answer is optimal?
Proof:
On the first morning, a single link must be given to the hotel;
at least one cut is needed
Since our solution uses only one cut and there can be no
other solution using less than one cut, we have found the
optimal solution
31/33
Software Verification
Proof of correctness of algorithm is composed of
several steps
First, some conditions are true before the execution of the
algorithm; these conditions are called preconditions
Then, some statements, called assertions, throughout the
algorithm must be established
At the end, an assertion must specify the desired output of
the algorithm
If, given the preconditions, each identified assertion is true
when the execution reaches that particular point, then the
algorithm is correct
32/33
Example of Assertions
An assertion at a point in a loop that is true every
time that point in the loop is reached is known as
loop invariant
Loop invariant for the
insertion sort:
“Every time we reach here,
the first N-1 position are
sorted”
33/33
Software Testing
Formal algorithm verification techniques have not
been powerful enough to apply to general algorithms
Most programs today are “verified” by testing under
various conditions (called test points), nevertheless,
there is no guarantee that a “tested” program is
correct under any circumstances

More Related Content

What's hot (20)

Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
Fuzzy+logic
Fuzzy+logicFuzzy+logic
Fuzzy+logic
 
Fuzzy Logic
Fuzzy LogicFuzzy Logic
Fuzzy Logic
 
Python second ppt
Python second pptPython second ppt
Python second ppt
 
While loop
While loopWhile loop
While loop
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automata
 
Definitions of Functional Programming
Definitions of Functional ProgrammingDefinitions of Functional Programming
Definitions of Functional Programming
 
Daa chapter4
Daa chapter4Daa chapter4
Daa chapter4
 
Python Fundamentals Class 11
Python Fundamentals Class 11Python Fundamentals Class 11
Python Fundamentals Class 11
 
Fuzzy logic
Fuzzy logicFuzzy logic
Fuzzy logic
 
Fuzzy logic
Fuzzy logicFuzzy logic
Fuzzy logic
 
Intelligent Control and Fuzzy Logic
Intelligent Control and Fuzzy LogicIntelligent Control and Fuzzy Logic
Intelligent Control and Fuzzy Logic
 
Recursion CBSE Class 12
Recursion CBSE Class 12Recursion CBSE Class 12
Recursion CBSE Class 12
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Utility Classes
Utility ClassesUtility Classes
Utility Classes
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Python interview questions and answers
Python interview questions and answersPython interview questions and answers
Python interview questions and answers
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 

Viewers also liked

Notiuni generale despre dezastre
Notiuni generale despre dezastreNotiuni generale despre dezastre
Notiuni generale despre dezastrePompierii Români
 
ds-threat-intelligence-exchange
ds-threat-intelligence-exchangeds-threat-intelligence-exchange
ds-threat-intelligence-exchangeRobert D. Diaz
 
şIrincan memmedova 126-hidir nebi̇ bayrami
şIrincan memmedova 126-hidir nebi̇ bayramişIrincan memmedova 126-hidir nebi̇ bayrami
şIrincan memmedova 126-hidir nebi̇ bayramiShirincan Mammadova
 
Deep Visibility for Production Microservices
Deep Visibility for Production MicroservicesDeep Visibility for Production Microservices
Deep Visibility for Production MicroservicesPaul Bauer
 
Legea nr. 80 din 1995 Statutul cadrelor militare
Legea nr. 80 din 1995 Statutul cadrelor militareLegea nr. 80 din 1995 Statutul cadrelor militare
Legea nr. 80 din 1995 Statutul cadrelor militarePompierii Români
 
Data Manipulation
Data ManipulationData Manipulation
Data ManipulationAsfi Bhai
 
Protectia populatiei,bunurilor materiale
Protectia populatiei,bunurilor  materialeProtectia populatiei,bunurilor  materiale
Protectia populatiei,bunurilor materialePompierii Români
 
Introduction to Dr. Eric Tao at CSUMB - 4 slides
Introduction to Dr. Eric Tao at CSUMB -   4 slidesIntroduction to Dr. Eric Tao at CSUMB -   4 slides
Introduction to Dr. Eric Tao at CSUMB - 4 slidesEric Tao
 

Viewers also liked (16)

Notiuni generale despre dezastre
Notiuni generale despre dezastreNotiuni generale despre dezastre
Notiuni generale despre dezastre
 
Faceadd
FaceaddFaceadd
Faceadd
 
podvezi.by
podvezi.bypodvezi.by
podvezi.by
 
Libertas.by
Libertas.byLibertas.by
Libertas.by
 
ds-threat-intelligence-exchange
ds-threat-intelligence-exchangeds-threat-intelligence-exchange
ds-threat-intelligence-exchange
 
Faceadd
FaceaddFaceadd
Faceadd
 
Libertas.by
Libertas.byLibertas.by
Libertas.by
 
şIrincan memmedova 126-hidir nebi̇ bayrami
şIrincan memmedova 126-hidir nebi̇ bayramişIrincan memmedova 126-hidir nebi̇ bayrami
şIrincan memmedova 126-hidir nebi̇ bayrami
 
Deep Visibility for Production Microservices
Deep Visibility for Production MicroservicesDeep Visibility for Production Microservices
Deep Visibility for Production Microservices
 
Faceadd
FaceaddFaceadd
Faceadd
 
şIrincan mammadova 126
şIrincan mammadova 126şIrincan mammadova 126
şIrincan mammadova 126
 
şIrincan məmmədova 126
şIrincan məmmədova 126şIrincan məmmədova 126
şIrincan məmmədova 126
 
Legea nr. 80 din 1995 Statutul cadrelor militare
Legea nr. 80 din 1995 Statutul cadrelor militareLegea nr. 80 din 1995 Statutul cadrelor militare
Legea nr. 80 din 1995 Statutul cadrelor militare
 
Data Manipulation
Data ManipulationData Manipulation
Data Manipulation
 
Protectia populatiei,bunurilor materiale
Protectia populatiei,bunurilor  materialeProtectia populatiei,bunurilor  materiale
Protectia populatiei,bunurilor materiale
 
Introduction to Dr. Eric Tao at CSUMB - 4 slides
Introduction to Dr. Eric Tao at CSUMB -   4 slidesIntroduction to Dr. Eric Tao at CSUMB -   4 slides
Introduction to Dr. Eric Tao at CSUMB - 4 slides
 

Similar to Algorithms from National Chiao Tung University

Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question'shammad463061
 
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEM
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEMCOMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEM
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEMijfcstjournal
 
Two-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential EvolutionTwo-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential EvolutionXin-She Yang
 
Comparative study of different algorithms
Comparative study of different algorithmsComparative study of different algorithms
Comparative study of different algorithmsijfcstjournal
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
A Review On Genetic Algorithm And Its Applications
A Review On Genetic Algorithm And Its ApplicationsA Review On Genetic Algorithm And Its Applications
A Review On Genetic Algorithm And Its ApplicationsKaren Gomez
 
Csallner algorithms1
Csallner algorithms1Csallner algorithms1
Csallner algorithms1seshagiri rao
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptxSayanSen36
 
Cuckoo Search: Recent Advances and Applications
Cuckoo Search: Recent Advances and ApplicationsCuckoo Search: Recent Advances and Applications
Cuckoo Search: Recent Advances and ApplicationsXin-She Yang
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codehamza javed
 
Integrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural LanguagesIntegrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural Languagesbutest
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxdickonsondorris
 
2.Problems Problem Spaces and Search.ppt
2.Problems Problem Spaces and Search.ppt2.Problems Problem Spaces and Search.ppt
2.Problems Problem Spaces and Search.pptDr. Naushad Varish
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 

Similar to Algorithms from National Chiao Tung University (20)

Discrete structure ch 3 short question's
Discrete structure ch 3 short question'sDiscrete structure ch 3 short question's
Discrete structure ch 3 short question's
 
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEM
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEMCOMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEM
COMPARATIVE STUDY OF DIFFERENT ALGORITHMS TO SOLVE N QUEENS PROBLEM
 
Two-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential EvolutionTwo-Stage Eagle Strategy with Differential Evolution
Two-Stage Eagle Strategy with Differential Evolution
 
Comparative study of different algorithms
Comparative study of different algorithmsComparative study of different algorithms
Comparative study of different algorithms
 
Algoritmos
AlgoritmosAlgoritmos
Algoritmos
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
A Review On Genetic Algorithm And Its Applications
A Review On Genetic Algorithm And Its ApplicationsA Review On Genetic Algorithm And Its Applications
A Review On Genetic Algorithm And Its Applications
 
Csallner algorithms1
Csallner algorithms1Csallner algorithms1
Csallner algorithms1
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
algorithm Unit 5
algorithm Unit 5 algorithm Unit 5
algorithm Unit 5
 
Conditional Statements
Conditional StatementsConditional Statements
Conditional Statements
 
Cuckoo Search: Recent Advances and Applications
Cuckoo Search: Recent Advances and ApplicationsCuckoo Search: Recent Advances and Applications
Cuckoo Search: Recent Advances and Applications
 
Time Complexity Analysis in Data Structure.docx
Time Complexity Analysis in Data Structure.docxTime Complexity Analysis in Data Structure.docx
Time Complexity Analysis in Data Structure.docx
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 
Integrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural LanguagesIntegrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural Languages
 
recursion.ppt
recursion.pptrecursion.ppt
recursion.ppt
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docxUnit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
 
2.Problems Problem Spaces and Search.ppt
2.Problems Problem Spaces and Search.ppt2.Problems Problem Spaces and Search.ppt
2.Problems Problem Spaces and Search.ppt
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Algorithms from National Chiao Tung University

  • 1. Algorithms National Chiao Tung University Chun-Jen Tsai 04/13/2012
  • 2. 2/33 Algorithm An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process “Ordered” does not imply “sequential” → there are parallel algorithms An algorithm is an abstract concept There can be several physical implementations of an algorithm (using same or different programming languages) Given same input, different implementations of an algorithm should produce the same output For example, an algorithm is like a story, and an implementation is like a book on the story
  • 3. 3/33 Terminology Clarification What is the differences between an algorithm, a program, and a process? Read the last paragraph of Sec 5.1 of the textbook carefully! A program is a formal representation of an algorithm, which can be executed by a computer A process is the activity of executing an algorithm (or equivalently, a program)
  • 4. 4/33 Algorithm Representation (1/2) We must find a way to separate an algorithm from the actual implementation, but still present it precisely In 1950’s~1970’s, flowcharts is very popular in describing an algorithm → too cumbersome for sophisticated design Today, we usually use a pseudocode language (not a programming language) to describe an algorithm start condition perform A perform B Halt Y N
  • 5. 5/33 Algorithm Representation (2/2) Theoretically, any languages (e.g. English) can be used as a pseudocode language to describe algorithms In practice, a good pseudocode language must avoid any ambiguities A language with a small set of well-defined building blocks (called primitives) can removes ambiguity Each primitive is a single operation you can apply to the data
  • 6. 6/33 Origami is a complicated procedure Key question: What operations can we perform at each step? Example: Origami
  • 7. 7/33 Origami Primitives Primitives defined in our programming language limit our implementation of an algorithm
  • 8. 8/33 Pseudocode Example Pseudocode primitives Assignment: name ← expression; Conditional selection: if (condition) then ( action ) Repeated execution: while (condition) do ( action ) Procedure: procedure name (parameters) Example: procedure Greetings Count ← 3; while (Count > 0) do ( print the message “Hello”; Count ← Count – 1; )
  • 9. 9/33 Algorithm Discovery The techniques to discover an algorithm to solve real world problem often requires specific domain knowledge that you won’t learn in Computer Science → to launch a rocket, you must know physics well! However, a big algorithm is usually composed of many small “standard algorithms” which you will learn in Computer Science curriculum For example, “sorting numbers” is a crucial small algorithm for many large problems
  • 10. 10/33 Polya’s Problem Solving Phases In 1945, G. Polya defined four problem solving phases Phase 1: Understand the problem Phase 2: Devise a plan for solving the problem Phase 3: Carry out the plan Phase 4: Evaluate the solution for accuracy and for its potential as a tool for solving other problems
  • 11. 11/33 A Sample Problem Person A is charged with the task of determining the ages of B’s three children. B tells A that the product of the children’s ages is 36. A replies that another clue is required. B tells A the sum of the children’s ages. A replies that another clue is needed. B tells A that the oldest child plays the piano. A tells B the ages of the three children. How old are the three children?
  • 12. 12/33 Techniques for Problem Solving Work the problem backwards (reverse-engineering) Look for solutions of an easier, related problem Stepwise refinement (top-down methodology) Popular technique because it produces modular programs Breaking a big problem into smaller ones (bottom-up methodology) The solution of each small problem must be unit-tested before integrating it into the big solution
  • 13. 13/33 Iterative Structures in Algorithms It often happens that an algorithm contains repeated actions, each action is similar to previous one Example: sequential search procedure Search (List, TargetValue) if (List empty) then ( Declare search a failure; ) else ( Select the first entry in List to be TestEntry; while (TargetValue > TestEntry and there remain entries to be considered) do ( Select the next entry in List as TestEntry; ) if (TargetValue = TestEntry) then ( Declare search a success; ) else ( Declare search a failure; ) )
  • 14. 14/33 Two Variants of Iterative Structure Iterative structure can be implemented using one of two methods Loop structure Recursive structure An iterative structure is composed of two parts Body of repetition Repetitive control
  • 15. 15/33 Components of Repetitive Control Initialize: Establish an initial state that will be modified toward the termination condition Test: Compare the current state to the termination condition and terminate the repetition if equal Modify: Change the state in such a way that it moves toward the termination condition
  • 16. 16/33 Loop Structure The while loop: The repeat loop: (body of repetition) (body of repetition)
  • 17. 17/33 Example: Sorting (1/2) Sorting the list: Fred, Alex, Diana, Byron, and Carol alphabetically The example given here uses insertion sort
  • 18. 18/33 Example: Sorting (2/2) The pseudo code of insertion sort procedure Sort(List) N ← 2; while (the value of N does not exceed the length of List) do ( Select the Nth entry in List as the pivot entry; Move the pivot entry to a temporary location, leaving a hole; while (there is a name above the hole greater than the pivot) do ( move the name above the hole down into the hole, leaving a hole above the name; ) Move the pivot entry into the hole in List; N ← N + 1; )
  • 19. 19/33 Recursive Structures Recursive structures provides an alternative to the iterative structures Example: binary search Goal: find John
  • 20. 20/33 Pseudo Code of Binary Search (1/2) The pseudo code of binary search can be described elegantly using recursive structure: procedure Search(List, TargetValue) if (List empty) then ( Report that the search failed; ) else ( Select the middle entry in List to be the first TestEntry; if (TargetValue = TestEntry) then ( Report that the search succeeded; ) else ( if (TargetValue < TestEntry) then ( Search(Listtop, TargetValue); ) else ( Search(Listbottom, TargetValue); ) ) )
  • 21. 21/33 Pseudo Code of Binary Search (2/2) Alice Bob Carol David Elaine Fred George Harry Irene John Kelly Larry Mary Nancy Oliver Irene John Kelly Larry Mary Nancy Oliver Search(ListBottom, “John”) Irene John Kelly Search(Listtop, “John”)
  • 22. 22/33 Recursive Control Note that, in a recursive structure, repetitive control is achieved by first testing the termination condition before recursive calls procedure my_function(parameters) if (termination condition is true) then ( Ends body of repetition ) else ( Perform recursive calls )
  • 23. 23/33 Algorithm Efficiency Question: when the number of input data grows linearly, what is the growth of the number of operations of an algorithm? → The answer to this question is the complexity of the algorithm Since the number of operations for processing a set of data depends on both the size of the data and the data pattern, an algorithm’s best case, worst case, and average case complexity can be quite different
  • 24. 24/33 Example: Insertion Sort (1/2) Applying the insertion sort in a worst-case situation
  • 25. 25/33 Example: Insertion Sort (2/2) Graph of the worst-case analysis of the insertion sort algorithm
  • 26. 26/33 Complexity Measure The complexity of an algorithm can be classified using the bit-theta notation, Θ, of the input data size n Θ(f(n)) means that if the input data size is n, the number of operations, c(n), of the algorithm grows at the same speed of f(n), within constant factors. That is, given constants k1 ≥ k2 > 0, as n → ∞, k2⋅f(n) ≤ c(n) ≤ k1⋅f(n) For example, the complexity of insertion sort is Θ(n2) and the complexity of binary search is Θ(log2 n)
  • 27. 27/33 Software Verification Once we have designed an algorithm, the next logical question to ask is: How do I prove the correctness of the algorithm? Software verification’s goal is to prove that an algorithm works by a formal procedure instead of intuitive arguments
  • 28. 28/33 Example: Chain Separating (1/3) A traveler has a gold chain of seven links. He must stay at an isolated hotel for seven nights. The rent each night consists of one link from the chain. What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance? One possible solution: Key question: is this the correct answer?
  • 29. 29/33 Example: Chain Separating (2/3) In fact, the problem can be solved by using one single cut!
  • 30. 30/33 Example: Chain Separating (3/3) How do we prove that the 2nd answer is optimal? Proof: On the first morning, a single link must be given to the hotel; at least one cut is needed Since our solution uses only one cut and there can be no other solution using less than one cut, we have found the optimal solution
  • 31. 31/33 Software Verification Proof of correctness of algorithm is composed of several steps First, some conditions are true before the execution of the algorithm; these conditions are called preconditions Then, some statements, called assertions, throughout the algorithm must be established At the end, an assertion must specify the desired output of the algorithm If, given the preconditions, each identified assertion is true when the execution reaches that particular point, then the algorithm is correct
  • 32. 32/33 Example of Assertions An assertion at a point in a loop that is true every time that point in the loop is reached is known as loop invariant Loop invariant for the insertion sort: “Every time we reach here, the first N-1 position are sorted”
  • 33. 33/33 Software Testing Formal algorithm verification techniques have not been powerful enough to apply to general algorithms Most programs today are “verified” by testing under various conditions (called test points), nevertheless, there is no guarantee that a “tested” program is correct under any circumstances