OI-style programming Gary Wong For any questions, please ask via MSN:

Slides:



Advertisements
Similar presentations
Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
Advertisements

Contest format 5 hours, around 8-12 problems One computer running (likely)Linux, plus printer 3 people on one machine No cell phones, calculators, USB.
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
MS 101: Algorithms Instructor Neelima Gupta
HST 952 Computing for Biomedical Scientists Lecture 10.
An Introduction to Programming Concepts and OI-programming …from abstract theory to dirty tricks…
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
The Complexity of Algorithms and the Lower Bounds of Problems
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
1 Complexity Lecture Ref. Handout p
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
An Introduction to Programming Concepts and OI-programming …from abstract theory to dirty tricks…
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Sorting HKOI Training Team (Advanced)
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Course Web Page Most information about the course (including the syllabus) will be posted on the course wiki:
Recursion, Complexity, and Sorting By Andrew Zeng.
Searching and Sorting Gary Wong.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Introduction to HKOI. Self Introduction Ice Breaking Game.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Fall 2002CMSC Discrete Structures1 Enough Mathematical Appetizers! Let us look at something more interesting: Algorithms.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
MS 101: Algorithms Instructor Neelima Gupta
Data Structure Introduction.
New Mexico Computer Science For All Algorithm Analysis Maureen Psaila-Dombrowski.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
HKOI 2005 Training Introduction to Algorithms Alan, Tam Siu Lung.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Reactive and Output-Only HKOI Training Team 2006 Liu Chi Man (cx) 11 Feb 2006.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
HKOI Programming HKOI Training Team (Intermediate) Alan, Tam Siu Lung Unu, Tse Chi Yung.
Introduction to HKOI Gary Wong. Ice Breaking and bond forming…
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Introduction to Algorithms. Algorithms Algorithms are ways of solving problems. There is a technical definition that basically says an algorithm is a.
1 The Role of Algorithms in Computing. 2 Computational problems A computational problem specifies an input-output relationship  What does the.
CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Design and Analysis of Algorithms Faculty Name : Ruhi Fatima Course Description This course provides techniques to prove.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Introduction to Algorithms
Enough Mathematical Appetizers!
The Complexity of Algorithms and the Lower Bounds of Problems
Objective of This Course
Applied Discrete Mathematics Week 6: Computation
Introduction to Algorithms
Enough Mathematical Appetizers!
Enough Mathematical Appetizers!
Presentation transcript:

OI-style programming Gary Wong For any questions, please ask via MSN:

Before the training… I never assume you knowing a lot It is okay to interrupt me whenever you think of any question to ask

Contents OI-oriented skills Algorithms and data structures Concept of “complexity”

OI-oriented skills Use one word to describe the aim of a contestant in a competition. WIN!

OI-oriented skills How to win? –Number of tasks attempted? –Using the most elegant solution? –Using the least time to “finish”? –Highest score! Let us quickly review the scoring method used in OI competitions…

OI-oriented skills Scoring –A set of test data is fed into your program –Get the score when it outputs correctly –ALWAYS remember: judging systems never score you by reading the codes!

OI-oriented skills Recommended steps for solving problems in OI: 1. Reading the problems 2. Choosing a problem 3. Reading the problem 4. Thinking 5. Coding 6. Testing (and debugging) 7. Finalizing the program

OI-oriented skills Reading the problemS –Have a quick look –You should at least look at: Length of problem statement Input/Output format Constraints –Range of variables –Time limit

OI-oriented skills Choosing a problem –Problem setters might not expect contestants to finish the whole paper –Usually from easy to difficult

OI-oriented skills Reading the problem –My own advice: read every single word! Underline keywords if possible –NEVER make assumptions yourself Ask if you are not sure

OI-oriented skills Thinking –Classify the problem into certain type(s) –Rough works –Special cases, boundary cases –No idea? Give up first, do it later. Spend time for other problems.

OI-oriented skills Thinking –Make sure you know what you are doing before coding –Points to note: Expected running time of your program? How much memory will be used? Coding difficulties?

OI-oriented skills Coding –Short variable names Use i, j, m, n instead of no_of_schools, name_of_students, etc. –No comments needed –As long as YOU understand YOUR code, okay to ignore all “appropriate“ coding practices My opinion: poor coding style will eventually kill you once in your life

OI-oriented skills Coding –Edsger Wyber Dijkstra A famous Dutch computer scientist One of his great work is “Dijkstra’s algorithm” for finding shortest paths He hates “spaghetti codes” a lot!

OI-oriented skills Testing –Sample Input/Output “A problem has sample output for two reasons: 1.To make you understand what the correct output format is 2.To make you believe that your incorrect solution has solved the problem correctly ” –Always create your own test data Manually Using a program –Test for ALL possible cases (including tricky cases)

OI-oriented skills Testing –If time allows, cross check your “efficient” program with a “slower” program

OI-oriented skills Debugging –Easiest method: writeln/printf/cout It is so-called “Debug message” –Use of debuggers: FreePascal IDE debugger gdb debugger

OI-oriented skills Finalizing –Make sure that the output format is EXACTLY the same as in the problem –Remember to delete all “debug messages” –Is your submitted code the most updated version? –Try to allocate ~5 mins at the end for finalizing

Tricks Solve for simple cases –50% (e.g. slower solution, brute force) –Special cases (smallest, largest, etc) –Incorrect greedy algorithmS –Very often, slow and correct solutions get higher scores than fast but wrong solutions

Tricks Hard Code –“No solution” –Stupid Hardcode: begin writeln(random(100)); end. –Naïve hardcode: “if input is x, output hc(x)” –More “intelligent” hardcode (sometimes not possible): pre-compute the values, and only save some of them

Common pitfalls Even experienced contestants might have such problems! –Misunderstanding the problem –Not familiar with competition environment –Output format –Using complex algorithms unnecessarily –Choosing the hardest problem first –Too confident with himself/herself

Contents OI-oriented skills Algorithms and data structures Concept of “complexity”

Algorithms “Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output.” [CLRS] N.B.: CLRS = a book called “Introduction to algorithms”

Algorithms In other words, a series of procedures to solve a problem Example: –Bubble Sort, Merge Sort, Quick Sort –Dijkstra’s Algorithm, Bellman Ford’s Algorithm Common misconceptions: –Algorithm = Program –Confusion between “algorithms” and “methods to design algorithms” E.g. “recursion” is NOT an algorithm

Data structures Briefly speaking, the way to organize data Examples: –Binary Search Tree –Hash Table –Segment Tree Different data structures have different properties –Efficiency –Amount of memory used Different algorithms use different data structures

Contents OI-oriented skills Algorithms and data structures Concept of “complexity”

Complexity We want to know how well an algorithm “scales” in terms of amount of data –In BOTH time and space Only consider the proportionality to number of basic operations performed –A reasonable implementation can pass –Minor improvements usually cannot help

Complexity Big-O notation Definition We say that f(x) is in O(g(x)) if and only if there exist numbers x 0 and M such that |f(x)| ≤ M |g(x)| for x > x 0 You do not need to know this

Complexity Example: Bubble Sort For i := 1 to n do For j := 2 to i do if a[j] > a[j-1] then swap(a[j], a[j-1]); Worst case number of swaps = n(n-1)/2 Time Complexity=O(n(n-1)/2)=O(n 2 /2–n/2)=O(n 2 ) Total space needed = size of array + space of variables Space Complexity=32*n +32*3=O(n)+O(1)=O(n)

Complexity Another example: Binary search While a<=b do m=(a+b)/2 If a[m]=key, Then return m If a[m]<key, Then a=m+1 If a[m]>key, Then b=m-1 In worst case, –number of iterations = lg n [lg means log 2 ] Time Complexity = O(lg n) Total space needed = size of array + space of variables Space Complexity = O(n)

Complexity What if… –An algorithm using bubble sort, followed by binary search? –O(f) + O(g) = max(O(f), O(g)) –Take the “maximum” one only, ignore the “smaller” one –Answer: O(n 2 )

Complexity Points to note: –Speed of algorithm is machine-dependent –Use suitable algorithms to solve problems E.g., if n=1000 and runtime limit is 1s, would you use: –O(n 2 )? –O(n!)? –O(n 3 )? –Constant hidden by Big-O notation –Testing is required!

Any question?

Let’s know each other!