SlideShare a Scribd company logo
Design and Analysis of Algorithms
Qilong Feng
2023/2/23 Central South University 2
Course Outline
 Instructor
 Qilong Feng
 Office number:Computer building 413
 e-mail : csufeng@mail.csu.edu.cn
 Textbook
 Introduction to The Design & Analysis of Algorithms
(ISBN 01-2003-2170)
 References:
 T. H. Cormen, C. E. Leiserson, R. L. Rivest, Introduction to
Algorithms, The MIT Press. http://theory.lcs.mit.edu/~clr/
Copyright Li Zimao
@ 2007-2008-1 SCUEC
Textbook Cover
What does the cover include?
Puzzles, Notions, and Stories
of Algorithms.
2023/2/23 Central South University 4
Algorithms
What is algorithm?
2023/2/23 Central South University 5
Algorithm and Philosophy
Example: Given a jar with unlimited capacity, and unlimited
number of balls. The balls are labeled from 1.
When 1minutes close to 12:00, do: put the 1~10 balls into the jar,
and take the ball with label 10 out of the jar.
When 1/2 minutes close to 12:00, do: put the 11~20 balls into the
jar, and take the ball with label 20 out of the jar.
When 1/4 minutes close to 12:00, do: put the 21~30 balls into the
jar, and take the ball with label 30 out of the jar.
……
2023/2/23 Central South University 6
Algorithm and Philosophy
Assume that putting and getting balls takes no time.
Question: When it is 12:00, how many balls are in the jar?
Answer:
Go on the above process untill 12:00
Why?
Infinity.
2023/2/23 Central South University 7
Algorithm and Philosophy
Are you sure? Let us change some rules:
When 1minutes close to 12:00, do: put the 1~10 balls into the jar, and
take the ball with label 1 out of the jar.
When 1/2 minutes close to 12:00, do: put the 11~20 balls into the jar,
and take the ball with label 2 out of the jar.
When 1/4 minutes close to 12:00, do: put the 21~30 balls into the jar,
and take the ball with label 3 out of the jar.
……
Question: When it is 12:00, how many balls are in the jar?
Answer: 0 Why?
2023/2/23 Central South University 8
Algorithm and Philosophy
The order of taking balls changes everything!
I don’t think it is right!!!
When 1minutes close to 12:00, do: put the 1~10 balls into the jar, and
take the ball randomly out of the jar.
When 1/2 minutes close to 12:00, do: put the 11~20 balls into the jar,
and take the ball randomly out of the jar.
When 1/4 minutes close to 12:00, do: put the 21~30 balls into the jar,
and take the ball randomly out of the jar.
…… Answer: 0
2023/2/23 Central South University 9
Algorithm and Philosophy
Too amazing!
The really different part for the above three methods: the label of ball
when taking it out of jar.
Labels change the results!!!!
Labels change the way of our thinking.
Nothing is infinity, and infinity is nothing!
∞
The differences between “nothing” and “infinity” only exist in
human’s mind!
2023/2/23 Central South University 10
Algorithm and Philosophy
In some sense, algorithm is a way of thinking, or
algorithm is one kind of Philosophy.
2023/2/23 Central South University 11
Algorithms
 A sequence of computational steps that transform
the input into output.
 solving a well-specified computational problem.
What is algorithm?
2023/2/23 Central South University 12
Algorithms
 A sequence of computational steps that transform
the input into output.
 solving a well-specified computational problem.
What is algorithm?
2023/2/23 Central South University 13
Example of well-specified problem: Sorting
 Input: a sequence of numbers: 1, 100, 8, 25, 11, 9, 2, 1,
200.
 Output: a sorted (increasing order or decreasing order)
sequence of numbers
 1, 2, 8, 9, 11, 25, 100, 200.
Another example:
Create web page (your homepage) using HTML.
-No computational steps.
2023/2/23 Central South University 14
Algorithms
An algorithm is a sequence of unambiguous instructions for
solving a computational problem, i.e., for obtaining a required
output for any input in a finite amount of time.
“computer”
problem
algorithm
input output
2023/2/23 Central South University 15
Properties of Algorithms
 Finiteness
terminates after a finite number of steps
 Definiteness
Each step must be rigorously and unambiguously specified.
 Input
Valid inputs must be clearly specified.
 Output
can be proved to produce the correct output given a valid input.
 Effectiveness
Steps must be sufficiently simple and basic.
-e.g., check if 2 is the largest integer n for which there is a solution
to the equation xn + yn = zn in positive integers x, y, and z
2023/2/23 Central South University 16
Examples
 Is the following a legitimate algorithm?
i 1
While (i <= 10) do
a  i + 1
Print the value of a
End of loop
Stop
2023/2/23 Central South University 17
Algorithms
Why do we study algorithm?
2023/2/23 Central South University 18
Algorithms
 Algorithm is the soul of computer.
 Algorithm is everywhere.
 Algorithm can train our way of thinking.
 This course will make you different.
2023/2/23 Central South University 19
Computing the Greatest Common Divisor of
Two Integers
 Gcd(m, n): the largest integer that divides both m and n.
2023/2/23 Central South University 20
Computing the Greatest Common Divisor of
Two Integers
 Gcd(m, n): the largest integer that divides both m and n.
 First try -- Euclid’s algorithm:
2023/2/23 Central South University 21
Computing the Greatest Common Divisor of
Two Integers
 Gcd(m, n): the largest integer that divides both m and n.
 First try -- Euclid’s algorithm:
gcd(m, n) = gcd(n, m mod n)
 Step1: If n = 0, return the value of m as the answer and
stop; otherwise, proceed to Step 2.
 Step2: Divide m by n and assign the value of the
remainder to r.
 Step 3: Assign the value of n to m and the value of r to n.
Go to Step 1.
2023/2/23 Central South University 22
Descriptions of Algorithms
 Flow chart
 Programs
 Natural languages :Ambiguous
 Pseudo-code
 A mixture of a natural language and programming
language-like structures
 Precise and succinct.
 Pseudocode in this course
 omits declarations of variables
 use indentation to show the scope of such statements as
for, if, and while.
 use  for assignment
2023/2/23 Central South University 23
Pseudocode of Euclid’s Algorithm
Algorithm Euclid(m, n)
while n ≠ 0 do
r  m mod n
m  n
n  r
return m
Questions:
 Finiteness: how do we know that Euclid’s algorithm actually comes
to a stop?
 Definiteness: non ambiguity
 Effectiveness: effectively computable.
2023/2/23 Central South University 24
Second Try for gcd(m, n)
 Consecutive Integer Algorithm
 Step1: Assign the value of min{m, n} to t.
 Step2: Divide m by t. If the remainder of this division is 0, go
to Step3;otherwise, go to Step 4.
 Step3: Divide n by t. If the remainder of this division is 0,
return the value of t as the answer and stop; otherwise,
proceed to Step4.
 Step4: Decrease the value of t by 1. Go to Step2.
 Questions
 Which algorithm is faster, the Euclid’s or this one?
2023/2/23 Central South University 25
Third try for gcd(m, n)
 Middle-school procedure (brute force)
 Step1: Find the prime factors of m.
 Step2: Find the prime factors of n.
 Step3: Identify all the common factors in the two
prime found in Step1 and Step2.
 Step4: Compute the product of all the common factors
and return it as the gcd of the numbers given.
 Question
 Is this a legitimate algorithm?
2023/2/23 Central South University 26
What can we learn from the previous 3
examples?
 The same algorithm can be represented in several
different ways. (different pseudocode)
 There might exists more than one algorithm for a
certain problem!
 Algorithms for the same problem can be based on
very different ideas and can solve the problem with
dramatically different speeds.!
2023/2/23 Central South University 27
The Process of Design an Algorithm
 Understanding the problem
 Asking questions, do a few examples by hand, think
about special cases, etc.
 Design an algorithm
 Proving correctness
 Analyzing an algorithm
 Time efficiency : how fast the algorithm runs
 Space efficiency: how much extra memory the
algorithm needs.
 Coding an algorithm

More Related Content

Similar to Algorithms

Introduction to data mining and machine learning
Introduction to data mining and machine learningIntroduction to data mining and machine learning
Introduction to data mining and machine learning
Tilani Gunawardena PhD(UNIBAS), BSc(Pera), FHEA(UK), CEng, MIESL
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introductionLow Ying Hao
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
jvjfvvoa
 
COMBINATORICS.ppt
COMBINATORICS.pptCOMBINATORICS.ppt
COMBINATORICS.ppt
svhnkrishnakumari
 
Unit i
Unit iUnit i
Unit i
guna287176
 
Special webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat mathSpecial webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat math
CareerGOD
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
Ashutosh Satapathy
 
Andrew_Hair_Assignment_3
Andrew_Hair_Assignment_3Andrew_Hair_Assignment_3
Andrew_Hair_Assignment_3Andrew Hair
 
Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)
Global R & D Services
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithmsrajatmay1992
 
Complex variable transformation presentation.pptx
Complex variable transformation presentation.pptxComplex variable transformation presentation.pptx
Complex variable transformation presentation.pptx
HuzaifaAhmad51
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
Muhammad Arish
 
Chapter one
Chapter oneChapter one
Chapter one
mihiretu kassaye
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
Ganesh Solanke
 
2013 gate cs
2013 gate cs2013 gate cs
Probability module 1
Probability module 1Probability module 1
Probability module 1
Richard Paulino
 
Algorithm & data structures lec1
Algorithm & data structures lec1Algorithm & data structures lec1
Algorithm & data structures lec1
s_raza
 

Similar to Algorithms (20)

Introduction to data mining and machine learning
Introduction to data mining and machine learningIntroduction to data mining and machine learning
Introduction to data mining and machine learning
 
Unit 2
Unit 2Unit 2
Unit 2
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Unit i
Unit iUnit i
Unit i
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
COMBINATORICS.ppt
COMBINATORICS.pptCOMBINATORICS.ppt
COMBINATORICS.ppt
 
Unit i
Unit iUnit i
Unit i
 
Special webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat mathSpecial webinar on tips for perfect score in sat math
Special webinar on tips for perfect score in sat math
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Andrew_Hair_Assignment_3
Andrew_Hair_Assignment_3Andrew_Hair_Assignment_3
Andrew_Hair_Assignment_3
 
Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)Grds international conference on pure and applied science (9)
Grds international conference on pure and applied science (9)
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Complex variable transformation presentation.pptx
Complex variable transformation presentation.pptxComplex variable transformation presentation.pptx
Complex variable transformation presentation.pptx
 
design and analysis of algorithm
design and analysis of algorithmdesign and analysis of algorithm
design and analysis of algorithm
 
Chapter one
Chapter oneChapter one
Chapter one
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
2013 gate cs
2013 gate cs2013 gate cs
2013 gate cs
 
Probability module 1
Probability module 1Probability module 1
Probability module 1
 
Algorithm & data structures lec1
Algorithm & data structures lec1Algorithm & data structures lec1
Algorithm & data structures lec1
 

Recently uploaded

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
oz8q3jxlp
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Linda486226
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 

Recently uploaded (20)

一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
一比一原版(Deakin毕业证书)迪肯大学毕业证如何办理
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdfSample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
Sample_Global Non-invasive Prenatal Testing (NIPT) Market, 2019-2030.pdf
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 

Algorithms

  • 1. Design and Analysis of Algorithms Qilong Feng
  • 2. 2023/2/23 Central South University 2 Course Outline  Instructor  Qilong Feng  Office number:Computer building 413  e-mail : csufeng@mail.csu.edu.cn  Textbook  Introduction to The Design & Analysis of Algorithms (ISBN 01-2003-2170)  References:  T. H. Cormen, C. E. Leiserson, R. L. Rivest, Introduction to Algorithms, The MIT Press. http://theory.lcs.mit.edu/~clr/
  • 3. Copyright Li Zimao @ 2007-2008-1 SCUEC Textbook Cover What does the cover include? Puzzles, Notions, and Stories of Algorithms.
  • 4. 2023/2/23 Central South University 4 Algorithms What is algorithm?
  • 5. 2023/2/23 Central South University 5 Algorithm and Philosophy Example: Given a jar with unlimited capacity, and unlimited number of balls. The balls are labeled from 1. When 1minutes close to 12:00, do: put the 1~10 balls into the jar, and take the ball with label 10 out of the jar. When 1/2 minutes close to 12:00, do: put the 11~20 balls into the jar, and take the ball with label 20 out of the jar. When 1/4 minutes close to 12:00, do: put the 21~30 balls into the jar, and take the ball with label 30 out of the jar. ……
  • 6. 2023/2/23 Central South University 6 Algorithm and Philosophy Assume that putting and getting balls takes no time. Question: When it is 12:00, how many balls are in the jar? Answer: Go on the above process untill 12:00 Why? Infinity.
  • 7. 2023/2/23 Central South University 7 Algorithm and Philosophy Are you sure? Let us change some rules: When 1minutes close to 12:00, do: put the 1~10 balls into the jar, and take the ball with label 1 out of the jar. When 1/2 minutes close to 12:00, do: put the 11~20 balls into the jar, and take the ball with label 2 out of the jar. When 1/4 minutes close to 12:00, do: put the 21~30 balls into the jar, and take the ball with label 3 out of the jar. …… Question: When it is 12:00, how many balls are in the jar? Answer: 0 Why?
  • 8. 2023/2/23 Central South University 8 Algorithm and Philosophy The order of taking balls changes everything! I don’t think it is right!!! When 1minutes close to 12:00, do: put the 1~10 balls into the jar, and take the ball randomly out of the jar. When 1/2 minutes close to 12:00, do: put the 11~20 balls into the jar, and take the ball randomly out of the jar. When 1/4 minutes close to 12:00, do: put the 21~30 balls into the jar, and take the ball randomly out of the jar. …… Answer: 0
  • 9. 2023/2/23 Central South University 9 Algorithm and Philosophy Too amazing! The really different part for the above three methods: the label of ball when taking it out of jar. Labels change the results!!!! Labels change the way of our thinking. Nothing is infinity, and infinity is nothing! ∞ The differences between “nothing” and “infinity” only exist in human’s mind!
  • 10. 2023/2/23 Central South University 10 Algorithm and Philosophy In some sense, algorithm is a way of thinking, or algorithm is one kind of Philosophy.
  • 11. 2023/2/23 Central South University 11 Algorithms  A sequence of computational steps that transform the input into output.  solving a well-specified computational problem. What is algorithm?
  • 12. 2023/2/23 Central South University 12 Algorithms  A sequence of computational steps that transform the input into output.  solving a well-specified computational problem. What is algorithm?
  • 13. 2023/2/23 Central South University 13 Example of well-specified problem: Sorting  Input: a sequence of numbers: 1, 100, 8, 25, 11, 9, 2, 1, 200.  Output: a sorted (increasing order or decreasing order) sequence of numbers  1, 2, 8, 9, 11, 25, 100, 200. Another example: Create web page (your homepage) using HTML. -No computational steps.
  • 14. 2023/2/23 Central South University 14 Algorithms An algorithm is a sequence of unambiguous instructions for solving a computational problem, i.e., for obtaining a required output for any input in a finite amount of time. “computer” problem algorithm input output
  • 15. 2023/2/23 Central South University 15 Properties of Algorithms  Finiteness terminates after a finite number of steps  Definiteness Each step must be rigorously and unambiguously specified.  Input Valid inputs must be clearly specified.  Output can be proved to produce the correct output given a valid input.  Effectiveness Steps must be sufficiently simple and basic. -e.g., check if 2 is the largest integer n for which there is a solution to the equation xn + yn = zn in positive integers x, y, and z
  • 16. 2023/2/23 Central South University 16 Examples  Is the following a legitimate algorithm? i 1 While (i <= 10) do a  i + 1 Print the value of a End of loop Stop
  • 17. 2023/2/23 Central South University 17 Algorithms Why do we study algorithm?
  • 18. 2023/2/23 Central South University 18 Algorithms  Algorithm is the soul of computer.  Algorithm is everywhere.  Algorithm can train our way of thinking.  This course will make you different.
  • 19. 2023/2/23 Central South University 19 Computing the Greatest Common Divisor of Two Integers  Gcd(m, n): the largest integer that divides both m and n.
  • 20. 2023/2/23 Central South University 20 Computing the Greatest Common Divisor of Two Integers  Gcd(m, n): the largest integer that divides both m and n.  First try -- Euclid’s algorithm:
  • 21. 2023/2/23 Central South University 21 Computing the Greatest Common Divisor of Two Integers  Gcd(m, n): the largest integer that divides both m and n.  First try -- Euclid’s algorithm: gcd(m, n) = gcd(n, m mod n)  Step1: If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2.  Step2: Divide m by n and assign the value of the remainder to r.  Step 3: Assign the value of n to m and the value of r to n. Go to Step 1.
  • 22. 2023/2/23 Central South University 22 Descriptions of Algorithms  Flow chart  Programs  Natural languages :Ambiguous  Pseudo-code  A mixture of a natural language and programming language-like structures  Precise and succinct.  Pseudocode in this course  omits declarations of variables  use indentation to show the scope of such statements as for, if, and while.  use  for assignment
  • 23. 2023/2/23 Central South University 23 Pseudocode of Euclid’s Algorithm Algorithm Euclid(m, n) while n ≠ 0 do r  m mod n m  n n  r return m Questions:  Finiteness: how do we know that Euclid’s algorithm actually comes to a stop?  Definiteness: non ambiguity  Effectiveness: effectively computable.
  • 24. 2023/2/23 Central South University 24 Second Try for gcd(m, n)  Consecutive Integer Algorithm  Step1: Assign the value of min{m, n} to t.  Step2: Divide m by t. If the remainder of this division is 0, go to Step3;otherwise, go to Step 4.  Step3: Divide n by t. If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step4.  Step4: Decrease the value of t by 1. Go to Step2.  Questions  Which algorithm is faster, the Euclid’s or this one?
  • 25. 2023/2/23 Central South University 25 Third try for gcd(m, n)  Middle-school procedure (brute force)  Step1: Find the prime factors of m.  Step2: Find the prime factors of n.  Step3: Identify all the common factors in the two prime found in Step1 and Step2.  Step4: Compute the product of all the common factors and return it as the gcd of the numbers given.  Question  Is this a legitimate algorithm?
  • 26. 2023/2/23 Central South University 26 What can we learn from the previous 3 examples?  The same algorithm can be represented in several different ways. (different pseudocode)  There might exists more than one algorithm for a certain problem!  Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds.!
  • 27. 2023/2/23 Central South University 27 The Process of Design an Algorithm  Understanding the problem  Asking questions, do a few examples by hand, think about special cases, etc.  Design an algorithm  Proving correctness  Analyzing an algorithm  Time efficiency : how fast the algorithm runs  Space efficiency: how much extra memory the algorithm needs.  Coding an algorithm