Post on 14-Mar-2016
description
高等計算機演算法高等計算機演算法Advanced Computer AlgorithmsAdvanced Computer Algorithms
伍麗樵伍麗樵 (Lih-Chyau Wuu)(Lih-Chyau Wuu)
雲林科技大學電資所雲林科技大學電資所
課程內容課程內容 :: Design MethodsDesign Methods Analysis MethodsAnalysis Methods The theory of NP-completenessThe theory of NP-completeness
Design MethodsDesign Methods
The Greedy MethodThe Greedy Method The Divide-and-Conquer strategyThe Divide-and-Conquer strategy Prune-and-SearchPrune-and-Search Tree Searching strategyTree Searching strategy Dynamic ProgrammingDynamic Programming Approximation AlgorithmsApproximation Algorithms Randomized AlgorithmsRandomized Algorithms On-Line AlgorithmsOn-Line Algorithms
Analysis MethodsAnalysis Methods
Complexity of algorithmsComplexity of algorithms Best, Average and Worst Case AnalysesBest, Average and Worst Case Analyses The Lower Bounds of ProblemsThe Lower Bounds of Problems Amortized Analysis Amortized Analysis
教科書教科書Introduction to the Design and Analysis of AlgorithmsIntroduction to the Design and Analysis of AlgorithmsR.C.T Lee R.C. Chang S.S. Tseng Y.T. TsaiR.C.T Lee R.C. Chang S.S. Tseng Y.T. TsaiSecond EditionSecond Edition旗標代理旗標代理
分數分數期中考 期中考 35%35%期末考 期末考 35% 35% 報告 報告 30% 30%
AlgorithmAlgorithm
A number of rules, which are to be followed in a A number of rules, which are to be followed in a prescribed order, for solving a specific type of prescribed order, for solving a specific type of problemsproblems
A method that can be used by a computer to solve A method that can be used by a computer to solve a problem.a problem.
Computer Algorithm – Five CriteriaComputer Algorithm – Five Criteria
InputInput OutputOutput FinitenessFiniteness DefinitenessDefiniteness EffectivenessEffectiveness
Motivations of studying AlgorithmsMotivations of studying Algorithms
Algorithm is everywhereAlgorithm is everywhere Use computers efficientlyUse computers efficiently Optimal solutionOptimal solution NP-complete problems(Decision problems)NP-complete problems(Decision problems) NP-hard problems(Optimization problems)NP-hard problems(Optimization problems)
nextnext
Use computers efficientlyUse computers efficiently Sorting algorithmsSorting algorithms
11, 7, 14, 1, 5, 9, 1011, 7, 14, 1, 5, 9, 10
↓ ↓sortsort 1, 5, 7, 9, 10, 11, 141, 5, 7, 9, 10, 11, 14 Insertion sortInsertion sort Quick sortQuick sort
Comparison with Two Algorithms Comparison with Two Algorithms Implemented on Two ComputersImplemented on Two Computers
0
5
10
15
20
25
30
35
N(The numer of data items)
T(Secons)
Insertionsort byVAX8800Quicksortby PC/XT
The study of Algorithms:The study of Algorithms:
How to design algorithmsHow to design algorithms How to validate algorithmsHow to validate algorithms How to analyze algorithmsHow to analyze algorithms How to test a programHow to test a program
How to design algorithmHow to design algorithmThe study of algorithm design is almost aThe study of algorithm design is almost astudy of strategiesstudy of strategies
P1 P2 P3 Pn… …
S1
S2
Sn
.
.
.
.
.
.
X X X
X
X
X
X
X
How to analyze algorithmHow to analyze algorithm Measure the goodness of Measure the goodness of algorithmsalgorithms
• efficiency?efficiency?((time/spacetime/space))• asymptotic notations: asymptotic notations: O( )O( )• worst caseworst case• average caseaverage case• amortizedamortized
Measure the difficulty of Measure the difficulty of problemsproblems• NP-completeNP-complete• undecidableundecidable• lower boundlower bound
Is the algorithm Is the algorithm optimaloptimal? ?
時間複雜度時間複雜度 (time complexity)-(time complexity)-worst case executing timeworst case executing time 輸入量為輸入量為 nn 時時 ,, 演算法的最大執行時間演算法的最大執行時間 O(1):O(1): 常數時間常數時間 (constant time)(constant time) O(n):O(n): 線性時間線性時間 (linear time)(linear time) O(logO(log22n):n): 次線性時間次線性時間 (sub-linear time)(sub-linear time) O(nO(n22):): 平方時間平方時間 (quadratic time)(quadratic time) O(nO(n33):): 立方時間立方時間 (cubic time)(cubic time) O(2O(2nn):): 指數時間指數時間 (exponential time)(exponential time) O(1)<O(logO(1)<O(log22n)<O(n)<O(nlogn)<O(n)<O(nlog22n)<O(nn)<O(n22)<O(n)<O(n33)<O(2)<O(2nn))
Algorithm Algorithm vs. Programvs. Program
AlgorithmAlgorithm 的表示方法的表示方法 **虛擬碼虛擬碼 (Pseudo Code)(Pseudo Code) SPARKS, PASCAL-like, Nature LanguageSPARKS, PASCAL-like, Nature Language **流程圖表示法流程圖表示法 (Flow-chart representation)(Flow-chart representation) A program is the expression of an algorithm in a programming languageA program is the expression of an algorithm in a programming language
Background for Learning AlgorithmsBackground for Learning Algorithms
Clear brainClear brain Data structureData structure Discrete mathematicsDiscrete mathematics
NP-complete problems:NP-complete problems:No No efficientefficient algorith algorithmsms
Easy problem: polynomial-time algorithmEasy problem: polynomial-time algorithm Difficult problem: exponential-time algorithmDifficult problem: exponential-time algorithm
Garey & Johnson “Computers & Intracability”Garey & Johnson “Computers & Intracability”
N1: Satisfiability ProblemN1: Satisfiability Problem
Given a Boolean formula S, is there anGiven a Boolean formula S, is there an assignment to make S true?assignment to make S true? &: and !: or ~: not&: and !: or ~: not
S=A& (~B !C) &~CS=A& (~B !C) &~C
S=A& ~AS=A& ~A Worst case: n variables --> 2Worst case: n variables --> 2nn combinations combinations
N2: Bin Packing ProblemN2: Bin Packing Problem
C1 C2 C3 C4 C5 C6 C7 C1 C2 C3 C4 C5 C6 C7 C8C8
7 1 3 4 1 3 3 7 1 3 4 1 3 3 88
B=15B=15How many bags are necessary?How many bags are necessary?
N3: 0/1 Knapsack ProblemN3: 0/1 Knapsack Problem
PP11 PP22 PP33 PP44 PP55 PP66 PP77 PP88
ValueValue 1010 55 11 99 33 44 1111 1717
WeightWeight 77 33 33 1010 11 99 2222 1515
M(weight limit)=14best solution: P1, P2, P3, P5(optimal)This problem is NP-complete.
N4: Traveling Salesperson ProblemN4: Traveling Salesperson Problem
Given a graph (vertex-city, edge-flying cost Given a graph (vertex-city, edge-flying cost between two cities), the salesperson travels to between two cities), the salesperson travels to every city once and only once, and the cost should every city once and only once, and the cost should also be minimal.also be minimal.
Given a set of n planar pointsGiven a set of n planar pointsFind: A closed tour which includes all points Find: A closed tour which includes all points exactly once such that its total length is minimized.exactly once such that its total length is minimized.
This problem is NP-complete. This problem is NP-complete.
N5: Partition ProblemN5: Partition Problem
Given: A set of positive integers SGiven: A set of positive integers SFind: SFind: S11 and S and S22 such that S such that S11SS22==, S, S11SS22=S, =S, iiS1S1i=i=iiS2 S2 ii(partition into S(partition into S11 and S and S22 such that the sum of S such that the sum of S11 is is equal to Sequal to S22))
e.g. S={1, 7, 10, 9, 5, 8, 3, 13}e.g. S={1, 7, 10, 9, 5, 8, 3, 13}• SS11={1, 10, 9, 8}={1, 10, 9, 8}• SS22={7, 5, 3, 13}={7, 5, 3, 13}
This problem is NP-complete. This problem is NP-complete.
N6: Art Gallery ProblemN6: Art Gallery Problem Given an art gallery in the form of a polygon, Given an art gallery in the form of a polygon,
determine the minimum number of guards and their determine the minimum number of guards and their placements such that the entire art gallery can be placements such that the entire art gallery can be monitored by these guards.monitored by these guards.
P1: Minimal Spanning Tree ProblemP1: Minimal Spanning Tree Problem Given a graph, find a Given a graph, find a spanning tree spanning tree (a graph (a graph
without cycle) with the minimum total edges without cycle) with the minimum total edges length.length.
P1: Minimal Spanning Tree ProblemP1: Minimal Spanning Tree Problem
graph: greedy methodgraph: greedy method geometry(on a plane): divide-and-conquergeometry(on a plane): divide-and-conquer # of possible spanning trees for n points: # of possible spanning trees for n points:
nnn-2n-2
n=10→10n=10→1088, n=100→10, n=100→10196196
P1: Minimal Spanning Tree ProblemP1: Minimal Spanning Tree Problem
graph: greedy methodgraph: greedy method geometry(on a plane): divide-and-conquergeometry(on a plane): divide-and-conquer # of possible spanning trees for n points: n# of possible spanning trees for n points: n
n-2 n-2 (Cayley’s theorem)(Cayley’s theorem) n=10→10n=10→1088, n=100→10, n=100→10196196
P2: Convex Hull ProblemP2: Convex Hull Problem
Given a set of 2-dimensional points, find a Given a set of 2-dimensional points, find a smallest convex polygon to contain all of smallest convex polygon to contain all of these points.these points.
Convex Polygon: any line connecting any Convex Polygon: any line connecting any
two points inside the polygon must itself lie two points inside the polygon must itself lie inside the polygoninside the polygon..
P2: Convex Hull ProblemP2: Convex Hull Problem
It is not obvious to find a convex hull by It is not obvious to find a convex hull by examining all possible solutionsexamining all possible solutions
divide-and-conquer divide-and-conquer
P3: One-Center ProblemP3: One-Center Problem
Given a set of points, find a smallest circle Given a set of points, find a smallest circle covering all of these points.covering all of these points.
prune-and-search !!prune-and-search !!