27
HKOI 2005 Training Introduction to Algorithms Alan, Tam Siu Lung

HKOI 2005 Training

  • Upload
    jackie

  • View
    38

  • Download
    3

Embed Size (px)

DESCRIPTION

HKOI 2005 Training. Introduction to Algorithms Alan, Tam Siu Lung. What is Algorithm?. Algorithm A set of data manipulations instructions to solve a problem (informatics problem) Problem A mapping from each input (in a specific format) to an output (in a specific format) Pseudo-Code - PowerPoint PPT Presentation

Citation preview

Page 1: HKOI 2005 Training

HKOI 2005 TrainingIntroduction to Algorithms

Alan, Tam Siu Lung

Page 2: HKOI 2005 Training

What is Algorithm?• Algorithm

– A set of data manipulations instructions to solve a problem (informatics problem)

• Problem– A mapping from each input (in a specific format) to an

output (in a specific format)• Pseudo-Code

– Realization of Algorithm in the current domain• Mapping from problem input to algorithm input• Mapping from algorithm output to problem output

• Program– Realization of Pseudo-Code given

• A compiler and the language supported• Data in plain text format and the IO routines supported

Page 3: HKOI 2005 Training

Visualization of Program

A BAlgorithm1

Coding Pseudo-Code

Source-Codeint main() {

A a = Read();

B b = Algorithm1(a);

C c = Algorithm2(b);

D d = Algorithm3(c);

Write(d);

}

B CAlgorithm2

C DAlgorithm3

Page 4: HKOI 2005 Training

Algorithm Example• Sorting Problem

– Given an array of N comparable elements with comparable keys

– Output a permutation of the elements– such that no element is has a key greater

than that of the one there before• Note

– Array = a sequence of elements– Element = a key-value pair– Permutation = ...– Greater than = …

Page 5: HKOI 2005 Training

Realization Example• To print in sorted order a set of points in a

cetacean plane by the distance from origin:• Realization contains

– Key = distance from origin– Value = coordinates = (x, y)– Comparison of 2 elements = comparison of the

numerical values of distances• Pseudo-Code contains

– Store the set in an array– Sort them– Output the array in order

Page 6: HKOI 2005 Training

Why Algorithms?

• Computational Biology• Database• Cryptography• Quantitative Analysis• Artificial Intelligence• Scheduling

Page 7: HKOI 2005 Training

Example – Multiple Sequence Alignment

• Input– GCTAGCTAAC– AACTAGC– AGCGCTAGCTA– TAACGACTAT

• Find a string with minimum which contains all these sub-strings

Page 8: HKOI 2005 Training

Example – Multiple Sequence Alignment

• Find a string with minimum which contains all these sub-strings

• OutputAACTAGCGCTAGCTAACGACTAT GCTAGCTAACAACTAGC AGCGCTAGCTA TAACGACTAT

Page 9: HKOI 2005 Training

Example – Sorting By Reversal

• Given a sequence of integers 1..N• Every step you can reverse a

continuous sub-subsequence• Find the minimum number of steps

to sort it

Page 10: HKOI 2005 Training

Example – Sorting By Reversal

• Input– 4 3 1 5 6 2

• Output– 4 3 1 5 6 2 – 1 3 4 5 6 2– 1 6 5 4 3 2– 1 2 3 4 5 6

Page 11: HKOI 2005 Training

To know what algorithms are

• Mathematics– Symbols and notations– Methods

• Data Structures– Specification of input and output– Internal storage during processing

• You can recite them.• It is science. You can also derive

them.

Page 12: HKOI 2005 Training

To know how to use algorithms

• Techniques– Common ways that algorithms are built

upon• Formulations

– How to map the problem domain into entities in algorithms

• It is art. We have no way to teach you!

• That’s why you need exercises.

Page 13: HKOI 2005 Training

Techniques• Recursion

– Reduction into sub-problems• Divide and Conquer

– Reduction into >1 sub-problems– Collect the results

• Exhaustion– Expand all reductions into sub-problems

• Greedy– Use only 1 reduction into sub-problem(s)

• Dynamic Programming– Try all and remember the one

Page 14: HKOI 2005 Training

To know why algorithms work

• Complexity Analysis• Proof of correctness• Mathematics-oriented• Out of our scope, but …• To “invent” new algorithms …

Page 15: HKOI 2005 Training

HKOI concerns• All basics and techniques specified

above– Mathematics, Data Structures– Recursion, Exhaustion, Greedy,

• Algorithms which implement the techniques– Sorting, Searching, DFS, BFS, Prim, Kruskal– Dijkstra, Bellman-Ford, Warshall-Floyd

• Famous IO models used by algorithms– Tree, Graph– Search Space (Graph Searching)– State Space (Constraint Satisfaction)

Page 16: HKOI 2005 Training

Introduction to Algorithms

Questions?

Page 17: HKOI 2005 Training

HKOI also concerns

• Modifications to algorithms– To accommodate the situation

• Multi-state BFS

– For speed• Branch & Bound

– For memory• Bit-wise States

• Other paradigms– Open test data (known input)– Interactive (hidden input)

Page 18: HKOI 2005 Training

Complexity

• An approximation to the runtime and memory requirement of a program.

• Note that there are– Best-case complexity– Average-case complexity– Worst-case complexity

• In most cases, we concern runtime only.

Page 19: HKOI 2005 Training

Measuring Runtime• Definition

– An operation– Problem size

• Consider bubble sort:for i := N - 1 downto 1 dofor j := 1 to i doif A[j] > A[j+1] thenswap(A[j], A[j+1]);

• In worst case how many “if” operations?“swap” operations? “for j” operations?“for j” operations? Integer comparisons?

Page 20: HKOI 2005 Training

Complexity

• Give a name to a group of functions with similar growth property, e.g. use O(n2) to mean n2 + n, 2n2 – 10n, 10n2, …

• Since the constant won’t be too big practically, it is a good indicator of expected runtime.

• We uses “worst case runtime complexity” (a.k.a. order) extensively to describe runtime of a program

Page 21: HKOI 2005 Training

Graph

1

2

3

4

5

76

8

Page 22: HKOI 2005 Training

Graph

WA

NT

SA

Q

NSW

V

T

Page 23: HKOI 2005 Training

Graph

• A directed graph G=(V,E), where– V = the set of Node/Vertex– E = the set of Edge/Arc, where

• E=(v1,v2) where v1 and v2 are in V

• Extreme: |E| ≤ |V|2

• So we have real algorithms with order:– O(|V|3)– O(|V| + |E|)– O(|V| log |V| + |E|)– O(|E| log(log* |E| – log* |E|/|V|))

Page 24: HKOI 2005 Training

Difficulty of Problem• Definitions (INCORRECT)

– A problem with order being a polynomial is called polynomial-time solvable (P)

– A problem whose solution is verified in polynomial time is said to be polynomial-time verifiable (NP)

– A problem with no known polynomial-time solution to date is called NP-hard

• Difficulty of problems are roughly classified as:– Easy: in P (of course all P problems are also in NP)– Hard: in NP but not in P (NP-complete)– Very Hard: not even in NP

Page 25: HKOI 2005 Training

Complexity

• When you have a problem, given input (output) constraints and runtime constraints, you can guess whether an algorithm works or not.

• But sometimes the stated order of an algorithm depends on some tricky data structures, which we won’t have skills or time to implement!

Page 26: HKOI 2005 Training

IOI/NOI Problems Requires

• Understanding of basic algorithms• Innovation on ways to realize the

algorithms• Technical capability of the

realization• Remember: Different people care

different things in different situations

Page 27: HKOI 2005 Training

Finally

Information, What

Knowledge, How

Understanding, Why InductionDed

uction