This document outlines an introduction to competitive programming and problem solving algorithms. It discusses that competitive programming involves writing programs to solve well-known computer science problems quickly. To be successful requires coding quickly, identifying problem types, analyzing time complexity, and extensive practice. The document then covers four basic problem solving paradigms - complete search, divide and conquer, greedy algorithms, and dynamic programming. It provides details on complete search, including that it involves searching the entire solution space and is useful when no clever algorithm exists or the input size is small.
Competitive programming involves writing computer programs to solve problems according to provided specifications in a competition format. It tests algorithmic skills, programming skills, speed, creativity, debugging skills, and more. To get started, participants should understand complexity, choose a programming language, learn data structures, and brush up on basics like number theory. Problem properties include absolute correctness checks, background stories to hide the actual problem, and examples of input/output formats. Practice is important and can be done on various online judge systems like CodeChef and Hackerrank.
Competitive programming involves writing programs to solve problems within a limited time period, typically ranging from hours to days, either individually or in teams. These contests test algorithm skills, basic math skills, programming skills, speed, creativity, and debugging abilities. Some prestigious contests include the ACM ICPC, Google Code Jam, TopCoder Open, and Facebook Hacker Cup. Competitive programming improves logical and analytical skills, looks good on a CV, and is fun, while also preparing participants for coding rounds during job interviews at many companies. Sites like Topcoder, SPOJ, Codeforces, Codechef, and Hackerrank allow programmers to practice for competitions.
This document introduces competitive programming competitions like ACM ICPC and Google Code Jam. It discusses training platforms such as Codeforces and Kattis that contain practice problems. It categorizes common problem types like ad hoc, complete search, divide and conquer, greedy, and dynamic programming to help determine a problem's difficulty and appropriate solving algorithm. Key problem types are defined, with ad hoc being the easiest and complete search involving brute force searching the entire search space.
The document provides an introduction to programming concepts including:
- A computer program tells a computer step-by-step how to solve a problem. An algorithm is the sequence of steps to solve a problem.
- Programming languages allow communication between users and computers. The program design process involves problem solving, designing algorithms and flowcharts, coding, testing and debugging.
- Problem solving for programming breaks problems into subproblems, specifies requirements, analyzes the problem, designs and tests solutions, and maintains programs. This process is used to systematically solve any type of problem.
This document discusses competitive programming and provides tips for getting started. It defines competitive programming as solving well-defined problems by writing computer programs within time and memory limits. It outlines some merits like fun and practice, and demrits like potential addiction. Tips are provided such as learning a programming language, practicing on online judges, and learning data structures and algorithms. Finally, it lists some online and onsite programming contests as well as university courses relevant to competitive programming.
The document provides an introduction to competitive programming, which involves solving algorithm and data structure problems quickly under time and memory constraints. It discusses what competitive programming tests, how to get started, problem properties, examples, where to practice, tips for practicing, reasons for doing competitive programming, drawbacks, prestigious contests, regular contests, and how KIIT students are performing. The high-level goal of competitive programming is improving programming and problem-solving skills through regular practice and competition. It is recognized by major tech companies and helps build useful everyday skills.
Competitive programming involves writing computer programs to solve problems according to provided specifications in a competition format. It tests algorithmic skills, programming skills, speed, creativity, debugging skills, and more. To get started, participants should understand complexity, choose a programming language, learn data structures, and brush up on basics like number theory. Problem properties include absolute correctness checks, background stories to hide the actual problem, and examples of input/output formats. Practice is important and can be done on various online judge systems like CodeChef and Hackerrank.
Competitive programming involves writing programs to solve problems within a limited time period, typically ranging from hours to days, either individually or in teams. These contests test algorithm skills, basic math skills, programming skills, speed, creativity, and debugging abilities. Some prestigious contests include the ACM ICPC, Google Code Jam, TopCoder Open, and Facebook Hacker Cup. Competitive programming improves logical and analytical skills, looks good on a CV, and is fun, while also preparing participants for coding rounds during job interviews at many companies. Sites like Topcoder, SPOJ, Codeforces, Codechef, and Hackerrank allow programmers to practice for competitions.
This document introduces competitive programming competitions like ACM ICPC and Google Code Jam. It discusses training platforms such as Codeforces and Kattis that contain practice problems. It categorizes common problem types like ad hoc, complete search, divide and conquer, greedy, and dynamic programming to help determine a problem's difficulty and appropriate solving algorithm. Key problem types are defined, with ad hoc being the easiest and complete search involving brute force searching the entire search space.
The document provides an introduction to programming concepts including:
- A computer program tells a computer step-by-step how to solve a problem. An algorithm is the sequence of steps to solve a problem.
- Programming languages allow communication between users and computers. The program design process involves problem solving, designing algorithms and flowcharts, coding, testing and debugging.
- Problem solving for programming breaks problems into subproblems, specifies requirements, analyzes the problem, designs and tests solutions, and maintains programs. This process is used to systematically solve any type of problem.
This document discusses competitive programming and provides tips for getting started. It defines competitive programming as solving well-defined problems by writing computer programs within time and memory limits. It outlines some merits like fun and practice, and demrits like potential addiction. Tips are provided such as learning a programming language, practicing on online judges, and learning data structures and algorithms. Finally, it lists some online and onsite programming contests as well as university courses relevant to competitive programming.
The document provides an introduction to competitive programming, which involves solving algorithm and data structure problems quickly under time and memory constraints. It discusses what competitive programming tests, how to get started, problem properties, examples, where to practice, tips for practicing, reasons for doing competitive programming, drawbacks, prestigious contests, regular contests, and how KIIT students are performing. The high-level goal of competitive programming is improving programming and problem-solving skills through regular practice and competition. It is recognized by major tech companies and helps build useful everyday skills.
The document analyzes the performance of different bin-packing heuristics. It finds that the Next-Fit heuristic is the fastest, taking O(n) time, while First-Fit and First-Fit-Decreasing are the slowest at O(n^2) time. The Max-Rest heuristic can be optimized to O(n log n) time by using a priority queue. Various optimizations are discussed that improve the performance of heuristics like using mapping/lookup tables or sorting objects by size.
This document discusses competitive programming (CP). It defines CP as a "mind sport" involving problem solving under constraints, not a computer engineering field. The goal of CP is to increase logical ability and write code for challenging situations. Reasons to do CP include it being trendy now and to improve problem solving and data structures and algorithms (DSA) skills. The roadmap involves learning time/space complexity, selecting a language, learning DSA, and practicing problems. Benefits include helping with coding interviews, improving problem solving, adding skills to one's resume, and demonstrating abilities publicly. To be a CP master requires practice.
This document discusses implementing a brute force algorithm to solve the travelling salesman problem (TSP) using GPUs. TSP involves finding the shortest route to visit each city once and return to the origin city. The author details dividing the problem across GPU blocks, threads, and permutations to calculate all routes within memory limits. Shared memory is used to find the shortest path within each block, while global memory tracks the overall shortest path across blocks. Testing showed GPUs can efficiently solve large TSP problems due to parallelizing many small route calculations. The key challenge was dividing the problem suitably for massive parallel GPU processing.
This document discusses the merge sort algorithm for sorting a sequence of numbers. It begins by introducing the divide and conquer approach, which merge sort uses. It then provides an example of how merge sort works, dividing the sequence into halves, sorting the halves recursively, and then merging the sorted halves together. The document proceeds to provide pseudocode for the merge sort and merge algorithms. It analyzes the running time of merge sort using recursion trees, determining that it runs in O(n log n) time. Finally, it covers techniques for solving recurrence relations that arise in algorithms like divide and conquer approaches.
This document discusses formal methods in software engineering. It defines formal methods as mathematically-based techniques for specifying, developing, and verifying computer systems in a systematic way. Formal methods use formal logic to model systems and prove that models satisfy requirements. The document provides an example of applying formal methods to specify a water tank system that refills when low. Key steps include: 1) defining types and functions, 2) stating properties as logical statements, and 3) constructing proofs to show properties hold for all cases.
1) The document presents information on the topic of undecidability, including definitions of decision problems, undecidable problems, and examples like the halting problem and tiling problem.
2) It proves that the halting problem is undecidable by contradiction, showing that assuming a program could solve the halting problem leads to a logical paradox.
3) It also proves the totality problem is undecidable by reducing it to the halting problem - if the totality problem could be solved, it could be used to solve the halting problem.
This document provides an introduction and overview of the Python programming language. It begins with a brief history of Python and discusses its growing popularity. The document then outlines the main topics that will be covered, including programming languages, the Python environment, basic data types, functions, conditions, loops, and common errors. It provides examples of representing different data types in bits and how Python encodes numbers, text, and other objects. The document also demonstrates how to set up a Python environment, write basic programs, use variables and built-in functions, and implement functions, conditions, and other core concepts.
Test design techniques: Structured and Experienced-based techniquesKhuong Nguyen
This document discusses different types of software testing techniques, including structured-based techniques like cyclomatic complexity and statement/decision coverage, as well as experience-based techniques like error guessing and exploratory testing. It explains how to calculate cyclomatic complexity and coverage percentages. Choosing the appropriate testing technique depends on factors like system type, standards, requirements, risk level, documentation, tester knowledge, time and budget. Testing usually involves combining different techniques.
(1) Dynamic programming is an algorithm design technique that solves problems by breaking them down into smaller subproblems and storing the results of already solved subproblems. (2) It is applicable to problems where subproblems overlap and solving them recursively would result in redundant computations. (3) The key steps of a dynamic programming algorithm are to characterize the optimal structure, define the problem recursively in terms of optimal substructures, and compute the optimal solution bottom-up by solving subproblems only once.
P, NP, NP-Complete, and NP-Hard
Reductionism in Algorithms
NP-Completeness and Cooks Theorem
NP-Complete and NP-Hard Problems
Travelling Salesman Problem (TSP)
Travelling Salesman Problem (TSP) - Approximation Algorithms
PRIMES is in P - (A hope for NP problems in P)
Millennium Problems
Conclusions
A birthday attack is a type of cryptographic attack that exploits the mathematics behind the birthday problem in probability theory. This attack can be used to abuse communication between two or more parties. The attack depends on the higher likelihood of collisions found between random attack attempts and a fixed degree of permutations
Formal verification involves proving the correctness of algorithms or systems with respect to a formal specification using mathematical techniques. It can be done by formally modeling a system and using theorem proving or model checking to verify that the model satisfies given properties. Theorem proving uses logical deduction to prove properties, while model checking automatically checks all possible states of a finite model against temporal logic properties. Both approaches have advantages and limitations, but formal verification can help find bugs and prove correctness of systems.
Exception handling is always an important topic. This presentation will provide couple of good practices to deal with Errors & Exceptions happing in Node.js.
Keep those in mind, you will write a better code for a more stable software.
Happy Coding!
1. The document discusses the equivalence between different models of regular languages including finite state automata (FSA), regular grammars, and regular expressions.
2. It explains how to convert between an FSA and regular grammar by establishing a one-to-one correspondence between the states of the FSA and symbols in the grammar.
3. Regular languages are closed under operations like concatenation - the concatenation of two regular languages is also regular.
Lexical analysis is the process of converting a sequence of characters from a source program into a sequence of tokens. It involves reading the source program, scanning characters, grouping them into lexemes and producing tokens as output. The lexical analyzer also enters tokens into a symbol table, strips whitespace and comments, correlates error messages with line numbers, and expands macros. Lexical analysis produces tokens through scanning and tokenization and helps simplify compiler design and improve efficiency. It identifies tokens like keywords, constants, identifiers, numbers, operators and punctuation through patterns and deals with issues like lookahead and ambiguities.
The document discusses algorithms and their properties. It defines an algorithm as a finite sequence of steps to solve a specific problem. Algorithms must have a defined input and output, and can solve the same problem in different ways. Common algorithm types include recursive, dynamic programming, backtracking, divide and conquer, greedy, brute force, and heuristic algorithms. Efficiency is measured by time and space complexity. Variables are introduced as a way to store input, intermediate results, and output values in algorithms.
The document analyzes the performance of different bin-packing heuristics. It finds that the Next-Fit heuristic is the fastest, taking O(n) time, while First-Fit and First-Fit-Decreasing are the slowest at O(n^2) time. The Max-Rest heuristic can be optimized to O(n log n) time by using a priority queue. Various optimizations are discussed that improve the performance of heuristics like using mapping/lookup tables or sorting objects by size.
This document discusses competitive programming (CP). It defines CP as a "mind sport" involving problem solving under constraints, not a computer engineering field. The goal of CP is to increase logical ability and write code for challenging situations. Reasons to do CP include it being trendy now and to improve problem solving and data structures and algorithms (DSA) skills. The roadmap involves learning time/space complexity, selecting a language, learning DSA, and practicing problems. Benefits include helping with coding interviews, improving problem solving, adding skills to one's resume, and demonstrating abilities publicly. To be a CP master requires practice.
This document discusses implementing a brute force algorithm to solve the travelling salesman problem (TSP) using GPUs. TSP involves finding the shortest route to visit each city once and return to the origin city. The author details dividing the problem across GPU blocks, threads, and permutations to calculate all routes within memory limits. Shared memory is used to find the shortest path within each block, while global memory tracks the overall shortest path across blocks. Testing showed GPUs can efficiently solve large TSP problems due to parallelizing many small route calculations. The key challenge was dividing the problem suitably for massive parallel GPU processing.
This document discusses the merge sort algorithm for sorting a sequence of numbers. It begins by introducing the divide and conquer approach, which merge sort uses. It then provides an example of how merge sort works, dividing the sequence into halves, sorting the halves recursively, and then merging the sorted halves together. The document proceeds to provide pseudocode for the merge sort and merge algorithms. It analyzes the running time of merge sort using recursion trees, determining that it runs in O(n log n) time. Finally, it covers techniques for solving recurrence relations that arise in algorithms like divide and conquer approaches.
This document discusses formal methods in software engineering. It defines formal methods as mathematically-based techniques for specifying, developing, and verifying computer systems in a systematic way. Formal methods use formal logic to model systems and prove that models satisfy requirements. The document provides an example of applying formal methods to specify a water tank system that refills when low. Key steps include: 1) defining types and functions, 2) stating properties as logical statements, and 3) constructing proofs to show properties hold for all cases.
1) The document presents information on the topic of undecidability, including definitions of decision problems, undecidable problems, and examples like the halting problem and tiling problem.
2) It proves that the halting problem is undecidable by contradiction, showing that assuming a program could solve the halting problem leads to a logical paradox.
3) It also proves the totality problem is undecidable by reducing it to the halting problem - if the totality problem could be solved, it could be used to solve the halting problem.
This document provides an introduction and overview of the Python programming language. It begins with a brief history of Python and discusses its growing popularity. The document then outlines the main topics that will be covered, including programming languages, the Python environment, basic data types, functions, conditions, loops, and common errors. It provides examples of representing different data types in bits and how Python encodes numbers, text, and other objects. The document also demonstrates how to set up a Python environment, write basic programs, use variables and built-in functions, and implement functions, conditions, and other core concepts.
Test design techniques: Structured and Experienced-based techniquesKhuong Nguyen
This document discusses different types of software testing techniques, including structured-based techniques like cyclomatic complexity and statement/decision coverage, as well as experience-based techniques like error guessing and exploratory testing. It explains how to calculate cyclomatic complexity and coverage percentages. Choosing the appropriate testing technique depends on factors like system type, standards, requirements, risk level, documentation, tester knowledge, time and budget. Testing usually involves combining different techniques.
(1) Dynamic programming is an algorithm design technique that solves problems by breaking them down into smaller subproblems and storing the results of already solved subproblems. (2) It is applicable to problems where subproblems overlap and solving them recursively would result in redundant computations. (3) The key steps of a dynamic programming algorithm are to characterize the optimal structure, define the problem recursively in terms of optimal substructures, and compute the optimal solution bottom-up by solving subproblems only once.
P, NP, NP-Complete, and NP-Hard
Reductionism in Algorithms
NP-Completeness and Cooks Theorem
NP-Complete and NP-Hard Problems
Travelling Salesman Problem (TSP)
Travelling Salesman Problem (TSP) - Approximation Algorithms
PRIMES is in P - (A hope for NP problems in P)
Millennium Problems
Conclusions
A birthday attack is a type of cryptographic attack that exploits the mathematics behind the birthday problem in probability theory. This attack can be used to abuse communication between two or more parties. The attack depends on the higher likelihood of collisions found between random attack attempts and a fixed degree of permutations
Formal verification involves proving the correctness of algorithms or systems with respect to a formal specification using mathematical techniques. It can be done by formally modeling a system and using theorem proving or model checking to verify that the model satisfies given properties. Theorem proving uses logical deduction to prove properties, while model checking automatically checks all possible states of a finite model against temporal logic properties. Both approaches have advantages and limitations, but formal verification can help find bugs and prove correctness of systems.
Exception handling is always an important topic. This presentation will provide couple of good practices to deal with Errors & Exceptions happing in Node.js.
Keep those in mind, you will write a better code for a more stable software.
Happy Coding!
1. The document discusses the equivalence between different models of regular languages including finite state automata (FSA), regular grammars, and regular expressions.
2. It explains how to convert between an FSA and regular grammar by establishing a one-to-one correspondence between the states of the FSA and symbols in the grammar.
3. Regular languages are closed under operations like concatenation - the concatenation of two regular languages is also regular.
Lexical analysis is the process of converting a sequence of characters from a source program into a sequence of tokens. It involves reading the source program, scanning characters, grouping them into lexemes and producing tokens as output. The lexical analyzer also enters tokens into a symbol table, strips whitespace and comments, correlates error messages with line numbers, and expands macros. Lexical analysis produces tokens through scanning and tokenization and helps simplify compiler design and improve efficiency. It identifies tokens like keywords, constants, identifiers, numbers, operators and punctuation through patterns and deals with issues like lookahead and ambiguities.
The document discusses algorithms and their properties. It defines an algorithm as a finite sequence of steps to solve a specific problem. Algorithms must have a defined input and output, and can solve the same problem in different ways. Common algorithm types include recursive, dynamic programming, backtracking, divide and conquer, greedy, brute force, and heuristic algorithms. Efficiency is measured by time and space complexity. Variables are introduced as a way to store input, intermediate results, and output values in algorithms.
This document provides an introduction to the analysis of algorithms. It discusses algorithm specification, performance analysis frameworks, and asymptotic notations used to analyze algorithms. Key aspects covered include time complexity, space complexity, worst-case analysis, and average-case analysis. Common algorithms like sorting and searching are also mentioned. The document outlines algorithm design techniques such as greedy methods, divide and conquer, and dynamic programming. It distinguishes between recursive and non-recursive algorithms and provides examples of complexity analysis for non-recursive algorithms.
The document discusses various algorithms design approaches and patterns including divide and conquer, greedy algorithms, dynamic programming, backtracking, and branch and bound. It provides examples of each along with pseudocode. Specific algorithms discussed include binary search, merge sort, knapsack problem, shortest path problems, and the traveling salesman problem. The document is authored by Ashwin Shiv, a second year computer science student at NIT Delhi.
Production model lifecycle management 2016 09Greg Makowski
This talk covers going over the various stages of building data mining models, putting them into production and eventually replacing them. A common theme throughout are three attributes of predictive models: accuracy, generalization and description. I assert you can have it all, and having all three is important for managing the lifecycle. A subtle point is that this is a step to developing embedded, automated data mining systems which can figure out themselves when they need to be updated.
The document provides an introduction to problem solving and programming fundamentals. It defines a computer program as a set of instructions that directs a computer to solve a problem. The document outlines a four-step problem solving process of understanding the problem, devising a plan, carrying out the plan, and looking back. It also discusses analyzing a problem by identifying inputs, outputs, processing, and storage. The design phase involves developing an algorithm to solve the problem. Debugging addresses fixing syntax, runtime, and semantic errors in a program.
Dynamic programming is an algorithm design technique for optimization problems that reduces time by increasing space usage. It works by breaking problems down into overlapping subproblems and storing the solutions to subproblems, rather than recomputing them, to build up the optimal solution. The key aspects are identifying the optimal substructure of problems and handling overlapping subproblems in a bottom-up manner using tables. Examples that can be solved with dynamic programming include the knapsack problem, shortest paths, and matrix chain multiplication.
This document provides information about an algorithms course, including the course syllabus and topics that will be covered. The course topics include introduction to algorithms, analysis of algorithms, algorithm design techniques like divide and conquer, greedy algorithms, dynamic programming, backtracking, and branch and bound. It also covers NP-hard and NP-complete problems. The syllabus outlines 5 units that will analyze performance, teach algorithm design methods, and solve problems using techniques like divide and conquer, dynamic programming, and backtracking. It aims to help students choose appropriate algorithms and data structures for applications and understand how algorithm design impacts program performance.
This document provides a lecture plan for teaching Unit 1 of the Design and Analysis of Algorithms course. The unit covers introduction topics including the notion of algorithms, algorithmic problem solving, analysis of algorithm efficiency, and asymptotic notations. The lecture plan lists 9 topics to be taught over 9 class periods using modes of delivery like PPT and blended learning. It also includes examples of activity based learning like flashcards, programming skills tests, and a crossword puzzle to reinforce the topics.
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or inputs. The importance of analysis is also covered.
This document discusses the design and analysis of algorithms. It begins with defining what an algorithm is - a well-defined computational procedure that takes inputs and produces outputs. It describes analyzing algorithms to determine their efficiency and comparing different algorithms that solve the same problem. The document outlines steps for designing algorithms, including understanding the problem, deciding a solution approach, designing the algorithm, proving correctness, and analyzing and coding it. It discusses using mathematical techniques like asymptotic analysis and Big O notation to analyze algorithms independently of implementations or data. The importance of analyzing algorithms and techniques like divide-and-conquer are also covered.
The document provides information about computers and computer science. It defines a computer as an electronic machine that processes data and instructions to perform computations and make logical decisions. Computer programs are sets of instructions that computers process data according to. Computer hardware is the physical devices that make up a computer system. Computer software are programs that run on computers and can be categorized into system software, middleware, and application software. The document also discusses the history of computers, what computer science entails, and how computers are used in various applications today. It defines computer science as the study of theoretical foundations of information and computation as well as practical techniques for implementing computer systems.
Solving Industrial Scheduling Problems with Constraint ProgrammingPhilippe Laborie
Scheduling problems represent an important class of application for Constraint Programming (CP) in the industry. For more than 20 years, our team at ILOG (now IBM) has been developing CP technology and applying it to solve our customers' scheduling problems. In the first part of the talk, we will present some extensions of CP (interval variables, functions, sequences) that capture the temporal dimension of scheduling and make it possible/easier to design efficient models for complex problems. But a powerful modeling language is not sufficient: in an industrial context, one must also simplify the complete process of model design (connection to data, data processing and consistency checking, model debugging, etc.) and problem resolution (robust automatic search algorithm, warm start, model chaining, etc.), this will be the topic of the second part of the talk. The presentation will be illustrated with examples using IBM ILOG CP Optimizer.
The document provides an overview of problem solving techniques in computer science. It discusses:
- Algorithms and programs as solutions to problems expressed in programming languages.
- Key aspects of problem solving including problem definition, getting started, working backwards, and utilizing past experiences.
- General problem solving strategies such as "divide and conquer" and dynamic programming.
- Algorithm design techniques like top-down design and choosing appropriate data structures.
- Implementing algorithms through modularization, variable naming, documentation, debugging, and testing.
- Verifying algorithms through input/output assertions, symbolic execution, and proving program segments.
- Analyzing algorithm efficiency by reducing redundant computations, early termination detection, and trading storage for
This document provides an introduction to the CS-701 Advanced Analysis of Algorithms course. It includes the course objectives, which are to design and analyze modern algorithms and evaluate their efficiency. The instructor and contact information are provided. The document outlines the course contents, including topics like sorting algorithms, graph algorithms, and complexity theory. It also discusses what algorithms are, how to represent them, and examples of algorithm applications. Common algorithm design techniques like greedy algorithms and heuristics are introduced.
The document discusses an algorithms analysis and design course. The major objectives are to design and analyze modern algorithms, compare their efficiencies, and solve real-world problems. Students will learn to prove algorithm correctness, analyze running times, and apply techniques like dynamic programming and graph algorithms. While algorithms can differ in efficiency, even on faster hardware, the computational model used makes reasonable assumptions for comparing algorithms asymptotically.
This document provides an introduction to the analysis of algorithms. It defines an algorithm and lists key properties including being finite, definite, and able to produce the correct output for any valid input. Common computational problems and basic algorithm design strategies are outlined. Asymptotic notations for analyzing time and space efficiency are introduced. Examples of algorithms for calculating the greatest common divisor and determining if a number is prime are provided and analyzed. Fundamental data structures and techniques for analyzing recursive algorithms are also discussed.
This document discusses the problem solving process in programming. It describes the three main phases: analyzing the problem, designing an algorithm to solve it, and implementing the algorithm in code. In the analysis phase, the problem and requirements are outlined, and a flowchart or pseudocode is used to design the algorithm. In the implementation phase, the algorithm is coded into a program. Finally, the program is tested to verify it works correctly and identify any errors. The key steps of each phase are explained in detail with examples.
Ds03 part i algorithms by jyoti lakhanijyoti_lakhani
The document defines an algorithm as a finite sequence of unambiguous instructions to solve a problem and produce an output from given inputs. It notes that algorithms have advantages like being easy to plan, implement, understand and debug. The key aspects of algorithms discussed are that they must be step-by-step, unambiguous and able to solve problems in a finite amount of time. The document also discusses analyzing and choosing between algorithms, as well as designing, proving, coding and analyzing algorithms.
LF Energy Webinar - Unveiling OpenEEMeter 4.0DanBrown980551
LF Energy OpenEEmeter measures the energy impacts of demand-side interventions in buildings. OpenEEmeter 4.0 provides enhanced performance of the daily model with dramatically reduced seasonal and weekend/weekday bias, along with increased computational efficiency.
This webinar explores how OpenEEMeter:
-Reduces seasonal bias in the daily model by 84%
-Reduces weekend/weekday bias in the daily model by 95%
-Runs up to 100x faster with monthly data, and 2 - 10x faster with daily data
-Along with this release, the OpenEEMeter community is also publishing a detailed 4.0 model specification and results of thorough testing conducted across residential and commercial sectors and gas and electric fuels.
Speakers:
-Adam Scheer, Vice President of Applied Data Science, Recurve
-Travis Sikes, Data Science Manager, Recurve
-Jason Chulock, Lead Engineer, Recurve
Similar to Basic Problems and Solving Algorithms (20)
How Barcodes Can Be Leveraged Within Odoo 17Celine George
In this presentation, we will explore how barcodes can be leveraged within Odoo 17 to streamline our manufacturing processes. We will cover the configuration steps, how to utilize barcodes in different manufacturing scenarios, and the overall benefits of implementing this technology.
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...indexPub
The recent surge in pro-Palestine student activism has prompted significant responses from universities, ranging from negotiations and divestment commitments to increased transparency about investments in companies supporting the war on Gaza. This activism has led to the cessation of student encampments but also highlighted the substantial sacrifices made by students, including academic disruptions and personal risks. The primary drivers of these protests are poor university administration, lack of transparency, and inadequate communication between officials and students. This study examines the profound emotional, psychological, and professional impacts on students engaged in pro-Palestine protests, focusing on Generation Z's (Gen-Z) activism dynamics. This paper explores the significant sacrifices made by these students and even the professors supporting the pro-Palestine movement, with a focus on recent global movements. Through an in-depth analysis of printed and electronic media, the study examines the impacts of these sacrifices on the academic and personal lives of those involved. The paper highlights examples from various universities, demonstrating student activism's long-term and short-term effects, including disciplinary actions, social backlash, and career implications. The researchers also explore the broader implications of student sacrifices. The findings reveal that these sacrifices are driven by a profound commitment to justice and human rights, and are influenced by the increasing availability of information, peer interactions, and personal convictions. The study also discusses the broader implications of this activism, comparing it to historical precedents and assessing its potential to influence policy and public opinion. The emotional and psychological toll on student activists is significant, but their sense of purpose and community support mitigates some of these challenges. However, the researchers call for acknowledging the broader Impact of these sacrifices on the future global movement of FreePalestine.
Temple of Asclepius in Thrace. Excavation resultsKrassimira Luka
The temple and the sanctuary around were dedicated to Asklepios Zmidrenus. This name has been known since 1875 when an inscription dedicated to him was discovered in Rome. The inscription is dated in 227 AD and was left by soldiers originating from the city of Philippopolis (modern Plovdiv).
3. Introduction
Fact:
“ACM-‐ICPC
is
a
competitive
programming
contest.”
“Given
a
set
of
well-‐known
problems
in
Computer
Science
your
job
is
to
write
programs
to
solve them
as
quickly as
possible.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 3
4. Introduction
Fact:
“Given
problems
are
already
solved.”
“At
least
the
problem
setter
have
solved
these
problems
before.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 4
5. Introduction
Fact:
“Your
program
is
accepted
if
it
produces
the
same
output
as
the
problem
setter
using
their
secret input
data.”
“The
sample
input
and
output
will
be
given
for
every
single
problem
.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 5
6. Introduction
Fact:
“In
competitive
programming,
your
program
must
also
quickly solve
a
problem
within
the
given
time
limit.”
“The
time
limit
will
always
be
provided
for
every
single
problem.”
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 6
7. Introduction
To
be
a
competitive
programmer
◦ Code
faster
◦ Quickly
identify
problem
types
◦ Do
algorithm
analysis
(time
complexity)
◦ Master
programming
languages
(C/C++
or
Java)
◦ Master
at
testing
and
debugging
code
◦ Practice
and
more
practice
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 7
8. Introduction
Keys
to
success
in
identifying
problem
types
◦ Know
problem
solving
paradigms.
◦ Be
familiar
with
basic
algorithms,
their
time
complexities,
and
their
implementations.
◦ Understand
and
know
how
to
use
basic
data
structures.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 8
10. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 10
11. Problem
Solving
Paradigms
– Complete
Search
Complete
Search
◦A
method
for
solving
a
problem
by
searching
(up
to)
the
entire
search
space to
obtain
the
desired
solution.
◦Also
known
as
brute
force/tree
pruning
or recursive
backtracking
techniques.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 11
12. Problem
Solving
Paradigms
– Complete
Search
Keys
to
develop
a
Complete
Search
solution
1. Identify
the
search
space
which
contains
feasible
solutions.
2. Identify
the
requirement
of
the
desired
solution.
3. Construct
a
feasible
solution.
4. Check
if
a
feasible
solution
matches
the
requirement.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 12
13. Problem
Solving
Paradigms
– Complete
Search
Example
of
Complete
Search
(Permutation
Sort)
◦In
the
problem
of
sorting
n integers
ØSearch
space
is
all
possible
permutations
of
n integers.
ØThe
desired
solution
is
a
permutation
a1a2…an such
that
a1 ≤
a2 ≤
… ≤
an.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 13
14. Problem
Solving
Paradigms
– Complete
Search
Example
of
Complete
Search
(Permutation
Sort)
◦In
the
problem
of
sorting
n integers
ØA
complete
search
solution
can
be
constructed
by
enumerating
all
permutations
of
n integers
and
then
outputting
the
permutation
in
sorted
order.
ØClearly,
this
solution
has
𝑂 𝑛
! time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 14
15. Problem
Solving
Paradigms
– Complete
Search
Pros
•Usually
easy
to
come
up
with.
•A
(bug-‐free)
Complete
Search
solution
will
never
cause
Wrong
Answer
(WA)
response
(since
it
explores
the
entire
search
space).
Cons
•A
(naïve)
Complete
Search
solution
may
cause
Time
Limit
Exceeded
(TLE)
response (extremely
slow,
at
least
exponential
running
time).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 15
16. Problem
Solving
Paradigms
– Complete
Search
When
to
use
Complete
Search?
◦Use
when
there
is
no
clever
algorithm
available.
ØEX: The
problem
of
generating
all
possible
permutations.
◦Use
when
the
input
size
trends
to
be
very
small
(so
that
it
can
pass
the
time
limit).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 16
17. Practice
Problem
UVa #725:
Division
Problem
statement
Find
and
display
all
pairs
of
5-‐digit
numbers
that
between
them
use
the
digits
0
through
9
once
each,
such
that
the
first
number
divided
by
the
second
is
equal
to
an
integer
N,
where
2
≤
N
≤
79.
That
is,
abcde /
fghij =
N,
where
each
letter
represents
a
different
digit.
The
first
digit
of
one
of
the
numerals
is
allowed
to
be
zero,
e.g.
79546
/
01283
=
62;
94736
/
01528
=
62.
Time
limit
:
3s
Iterative
Complete
Search
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 17
18. Practice
Problem
UVa #725:
Division
Quick
Analysis
§fghij can
only
be
from
01234
to
98765
(≈100k
possibilities).
§For
each
tried
fghij,
abcde is
computed
from
fghij*N
and
then
check
if
all
digits
are
different.
§Since
100k
operations
are
small,
iterative
complete
search
may
pass
the
time
limit.
Time
limit
:
3s
Iterative
Complete
Search
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 18
19. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 19
20. Problem
Solving
Paradigms
– Divide
and
Conquer
Divide
and
Conquer
(D&C)
◦A
method
for
solving
a
problem
where
we
try
to
make
the
problem
simpler
by
dividing
it
into
smaller
parts
(smaller
sizes)
and
conquering
them.
◦Frequently
used
D&C
algorithms
include
Quick
Sort,
Merge
Sort,
Heap
Sort,
and
Binary
Search.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 20
21. Problem
Solving
Paradigms
– Divide
and
Conquer
Keys
to
develop
a
D&C
solution
1. Divide
the
original
problem
into
sub-‐problems
(usually
by
half).
2. Find
sub-‐solutions
for
each
of
these
sub-‐problems.
3. If
required,
combine
sub-‐solutions
to
produce
a
complete
solution
to
the
original
problem.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 21
22. Problem
Solving
Paradigms
– Divide
and
Conquer
Example
of
D&C
(Merge
Sort)
◦In
the
problem
of
sorting
n integers
ØDivide
the
problem
into
2
sub-‐problems
each
of
which
sorting
n/2 integers.
ØRecursively
solve
each
of
these
two
sub-‐problems
until
the
sub-‐problem
of
sorting
n/2
=
1
integers
is
encountered.
ØCombine
a
sorted
sequence
from
the
sub-‐solutions.
Ø𝑂 𝑛
log
𝑛 time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 22
23. Problem
Solving
Paradigms
– Divide
and
Conquer
Pros
•Quite
easy
to
come
up
with
•For
some
problems,
if
applicable,
a
D&C
solution
can
be
pretty
fast
as
it
is
(ideally)
expected
to
reduce
the
size
of
the
problem
by
half
each
time.
Cons
•Time
complexity
is
not
always
good.
•Availability
for
efficient
D&C
solutions
is
hard
to
tell.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 23
24. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 24
25. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(D&C
Solution)
§Solve
Fi by
recursively
solving
Fi-‐1 and
Fi-‐2.
§If
i =
0,
then
return
Fi =
0.
§If
i =
1,
then
return
Fi =
1.
§Combine
Fi
from
Fi-‐1
+ Fi-‐2.
The
D&C
solution
is
extremely
slow.
The
time
complexity
is
about
𝑂 𝑛
! . It
cannot
pass
the
time
limit
definitely
!!
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 25
26. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 26
27. Problem
Solving
Paradigms
– Greedy
Greedy
◦A
method
for
solving
a
problem
by
picking
locally
optimal
choice
at
each
step
with
the
hope
of
producing
the
optimal
solution.
◦Frequently
used
greedy
algorithms
include
Kruskal’s MST,
Prim’s
MST,
Dijkstra’s SSSPs.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 27
28. Problem
Solving
Paradigms
– Greedy
A
problem
that
admits
a
Greedy
solution
must
exhibit
two
properties:
1. Optimal
sub-‐structure:
Ø Optimal
solution
to
the
problem
contains
within
optimal
solutions
to
the
sub-‐problems.
2. Greedy-‐choice
property:
Ø Pick
a
choice
that
seems
best
at
the
moment
and
solve
the
remaining
sub-‐problems.
No
need
to
reconsider
previous
choices.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 28
29. Problem
Solving
Paradigms
– Greedy
Keys
to
develop
a
Greedy
solution
1. Determine
the
optimal
sub-‐structure
of
the
problem.
2. Prove
the
greedy-‐choice
property.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 29
30. Problem
Solving
Paradigms
– Greedy
Example
of
Greedy
(Selection
Sort)
◦In
the
problem
of
sorting
n integers
ØOptimal
sub-‐structure:
o A’
=
a1a2…an
such
that
a1 ≤
a2 ≤
… ≤
an.
o A’’
=
remove(A’,
ai)
is
still
in
sorted
order.
ØGreedy-‐choice
property:
o Consider
a*
=
min(A’)
as
a
greedy
choice.
o add(remove(A’,
a*),
a*)
is
not
in
sorted
order.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 30
31. Problem
Solving
Paradigms
– Greedy
Example
of
Greedy
(Selection
Sort)
◦In
the
problem
of
sorting
n integers
ØIterative
Greedy
Sort(A):
1. A’
=
[]
2. for
i =
1
to
n:
3. a*
=
min(A)
4. add(A’,
a*)
5. remove(A,
a*)
Ø𝑂 𝑛 + 𝑂 𝑛 − 1 + ⋯ 𝑂 1 =
𝑂(𝑛3
) time
complexity.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 31
32. Problem
Solving
Paradigms
– Greedy
Pros
•For
some
problems,
if
applicable,
a
Greedy
solution
is
usually
compact
and
fast.
Cons
•Time-‐consuming
to
prove
the
greedy-‐choice
property.
•(Very)
hard
to
come
up
with.
•A
non-‐proven
Greedy
solution
may
easily
cause
Wrong
Answer
(WA)
response.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 32
33. Problem
Solving
Paradigms
– Greedy
When
to
use
Greedy?
◦Use
when
the
problem
exhibits
the
two
properties.
◦Use
when
we
know
for
sure
that
the
input
size
is
too
large
for
our
best
Complete
Search
(we
greed!!).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 33
34. Practice
Problem
UVa #410:
Station
Balance
Problem
statement
Given
1
≤
C
≤
5
chambers
which
can
store
0,
1,
or
2
specimens,
1
≤
S
≤
2C
specimens,
and
M:
a
list
of
mass
of
the
S
specimens,
determine
in
which
chamber
we
should
store
each
specimen
in
order
to
minimize
IMBALANCE.
𝐴 = ∑ 𝑀8
9
8:; /𝐶;
A
is
the
average
of
all
mass
over
C
chambers.
𝐼𝑀𝐵𝐴𝐿𝐴𝑁𝐶𝐸 =
(∑ |𝑋E − 𝐴|F
8:; )
;
Time
limit
:
3s
Greedy
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 34
35. Practice
Problem
UVa #410:
Station
Balance
Illustration
Source:
Competitive
Programming
Book
by
Steven
Halim
Time
limit
:
3s
Greedy
IMBALANCE = C
i=1 |Xi − A|, i.e. sum of differences between the mass i
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balan
This problem can be solved using a greedy algorithm. But first, we have
IMBALANCE = C
i=1 |Xi − A|, i.e. sum of differences between the mass i
where Xi is the total mass of specimens in chamber i.
Figure 3.3: Visualization of UVa 410 - Station Balan
This problem can be solved using a greedy algorithm. But first, we have8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 35
36. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Observation
1
:
If
there
exists
an
empty
chamber,
at
least
one
chamber
with
2
specimens
must
be
moved
to
this
empty
chamber!
Otherwise
the
empty
chambers
contribute
too
much
to
IMBALANCE.
Time
limit
:
3s
Greedy
tions. If there exists an empty chamber, at least one chamber with 2 specim
this empty chamber! Otherwise the empty chambers contribute too much
Figure 3.4.
Figure 3.4: UVa 410 - Observation 1
Next observation: If S > C, then S −C specimens must be paired with one
in some chambers. The Pigeonhole principle! See Figure 3.5.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 36
37. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Observation
2 :
If
S
>
C,
then
S−C
specimens
must
be
paired
with
one
other
specimen
already
in
some
chambers
(the
Pigeonhole
principle).
Time
limit
:
3s
Greedy
Next observation: If S > C, then S −C specimens must be paired with one other sp
in some chambers. The Pigeonhole principle! See Figure 3.5.
Figure 3.5: UVa 410 - Observation 2
Now, the key insight that can simplify the problem is this: If S < 2C, add dummy 2
with mass 0. For example, C = 3, S = 4, M = {5, 1, 2, 7} → C = 3, S = 6, M =
Then, sort these specimens based on their mass such that M1 ≤ M2 ≤ . . . ≤ M2
this example, M = {5, 1, 2, 7, 0, 0} → {0, 0, 1, 2, 5, 7}.
368/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 37
38. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Tricks:
§ If
S
<
2C,
add
dummy
2C−S
specimens
with
mass
0.
EX: C
=
3,
S
=
4,
M
=
{5,1,2,7}
→
C
=
3,S
=
6,M
=
{5,1,2,7,0,0}.
§ Then,
sort
these
specimens
based
on
their
mass
such
that
M1 ≤
M2 ≤
...
≤
M2C−1
≤
M2C.
EX: M
=
{5,1,2,7,0,0}
→
{0,0,1,2,5,7}.
Time
limit
:
3s
Greedy
Pair the specimens with masses M1&M2C and put them in chamber 1, th
Pair the specimens with masses M2&M2C−1 and put them in chamber 2,
This greedy algorithm – known as ‘Load Balancing’ – works! See Figure 3
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained f
from this example: If no obvious greedy strategy seen, try to sort the dat
tweaks and see if a greedy strategy emerges.8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 38
39. Practice
Problem
UVa #410:
Station
Balance
Quick
Analysis
Source:
Competitive
Programming
Book
by
Steven
Halim
Greedy
solution
(Load
Balancing):
§ Pair
the
specimens
with
masses
M1 and
M2C and
put
them
in
chamber
1.
§ Then,
pair
the
specimens
with
masses
M2 and
M2C−1 and
put
them
in
chamber
2,
and
so
on.
Time
limit
:
3s
Greedy
Figure 3.6: UVa 410 - Greedy Solution
To come up with this way of thinking is hard to teach but can be gained from exp
from this example: If no obvious greedy strategy seen, try to sort the data first o
tweaks and see if a greedy strategy emerges.
3.3.3 Remarks About Greedy Algorithm in Programming Conte
Using Greedy solutions in programming contests is usually risky. A greedy solut8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 39
40. Problem
Solving
Paradigms
Four
Basic
Problem
Solving
Paradigms
◦ Complete
Search
◦ Divide
and
Conquer
◦ Greedy
◦ Dynamic
Programming
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 40
41. Problem
Solving
Paradigms
– Dynamic
Programming
Dynamic
Programming
(DP)
◦ A
method
for
solving
a
problem
by
combining
the
solutions
to
sub-‐problems
(like
D&C).
◦ Advanced
problem
solving
paradigm
(improvised,
subtleties).
◦ Most
frequently
appearing
in
programming
contest.
◦ Frequently
used
DP
algorithms
include
LCS,
Matrix
Chain
Multiplication,
Knapsack,
Floyd
Warshall’s APSPs,
Kadene’s
Maximum
Contiguous
Sub-‐array.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 41
42. Problem
Solving
Paradigms
– Dynamic
Programming
Elements
of
DP
1. Optimal
sub-‐structure:
Ø Optimal
solution
to
the
problem
contains
within
optimal
solutions
to
the
sub-‐problems.
2. Overlapping
sub-‐problems:
Ø The
same
problem
is
revisited
over
and
over
(contrast
to
D&C).
3. Memoization :
Ø Caching
lookup
table
(contrast
to
Complete
Search).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 42
43. Problem
Solving
Paradigms
– Dynamic
Programming
Keys
to
develop
a
DP
solution
1. Determine
the
optimal
sub-‐structure
of
the
problem.
2. Identify
if
the
problem
has
overlapping
sub-‐
problems.
3. Choose
Top-‐Down
or
Bottom-‐Up.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 43
44. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 44
45. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(D&C
Solution)
§Solve
Fi by
recursively
solving
Fi-‐1 and
Fi-‐2.
§If
i =
0,
then
return
Fi =
0.
§If
i =
1,
then
return
Fi =
1.
§Combine
Fi
from
Fi-‐1
+ Fi-‐2.
The
D&C
solution
is
extremely
slow.
The
time
complexity
is
about
𝑂 𝑛
! . It
cannot
pass
the
time
limit
definitely
!!
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 45
46. Problem
Solving
Paradigms
– Dynamic
Programming
Example
of
DP
(Fibonacci
Sequence)
◦In
the
Fibonacci
Freeze
problem
ØOptimal
sub-‐structure:
o Fi =
Fi-‐1 +
Fi-‐2
ØOverlapping
sub-‐problems:
o EX: F6 =
F5 +
F4,
F5 =
F4 +
F3,
F4 =
F3 +
F2 ,
F3 =
F2 +
F1
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 46
47. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Top-‐Down
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§Solve
Fi =
Fi-‐1
+
Fi-‐2
by
oIf
A[i-‐1]
is
null,
then
recursively
solve Fi-‐1;
Otherwise,
return
A[i-‐1].
oif
A[i-‐2]
is
null,
then
recursively
solve Fi-‐2;
Otherwise,
return
A[i-‐2].
The
time
complexity
is
𝑂 𝑛 ,
as
no
duplicate
sub-‐
problems
recomputed.
Note:
recursive
calls
overhead
as
many
sub-‐problems
are
still
revisited.
The
solution
might
not
be
good
enough
to
pass
the
time
limit.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 47
48. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Bottom-‐Up
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§For
each
2
≤
j
≤
i,
compute
A[j]
=
A[j-‐1]+ A[j-‐
2].
§Return
A[i].
The
time
complexity
is
also
𝑂 𝑛 ,
as
no
duplicate
sub-‐problems
recomputed.
Obviously,
the
Bottom-‐up
version
is
faster.
Yes,
it
can
likely
pass
the
time
limit.
But…
wait
for
it!!!
We
will
come
back
later.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 48
49. Problem
Solving
Paradigms
– Dynamic
Programming
Pros
of
DP
Top-‐Down
•Natural
transformation
from
recursive
Complete
Search.
•Sub-‐problems
are
computed
when
necessary.
Cons
of
DP
Top-‐Down
•Slower
if
many
sub-‐
problems
are
revisited.
•Caching
table
of
very
large
size
may
cause
Memory
Limit
Exceeded
(MLE).
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 49
50. Problem
Solving
Paradigms
– Dynamic
Programming
Pros
of
DP
Bottom-‐Up
•Faster
if
many
sub-‐
problems
are
revisited.
•Save
memory
space.
Cons
of
DP
Bottom-‐Up
•May
not
be
intuitive.
•Every
states
of
caching
table
needed
to
be
filled.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 50
51. Problem
Solving
Paradigms
– Dynamic
Programming
When
to
use
DP?
◦Use
when
Complete
Search
receives
TLE.
◦Use
when
Greedy
receives
WA.
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 51
57. Basic
Algorithms – Sorting
Often
used
sorting
algorithms
◦ Insertion
sort
-‐ 𝑂(𝑛3
) time
in
the
worst
case
◦ Merge
sort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
worst
case
◦ Heap
sort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
worst
case
◦ Quicksort
-‐ 𝑂 𝑛
log
𝑛 time
in
the
average
case
◦ Counting
sort
-‐ 𝑂 𝑛 + 𝑘 time
in
the
worst
case
◦ Stable
sorting
property
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 57
59. Basic
Algorithms
– Binary
Search
Binary
Search
algorithm
◦ Input
Ø Sorted
array
:
A[1
..
n]
Ø Target
value
:
a
◦ Output
Ø Report
the
position
of
a in
A, if
a is
found
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 59
60. Basic
Algorithms
– Binary
Search
Built-‐in
Binary
Search
algorithm
with
𝑂 log
𝑛 worst-‐
case
time
complexity
◦ C/C++
Ø std::binary_search(A.begin(),
A.end(),
a);
◦ JAVA
Ø Java.util.Arrays.binarySearch(int[]
A,
int a);
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 60
61. Basic
Algorithms – Graph
Often
used
graph
algorithms
◦ Graph
Representations
– Adjacency
list
&
matrix
◦ Graph
Traversal
– DFS,
BFS,
CC,
SCC
◦ Maximum
Spanning
Tree
◦ Shortest
Paths
– SSSPs,
APSPs
◦ Maximum
Flow
◦ Maximum
Matching
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 61
63. Basic
Algorithms – Mathematics
Topics
often
related
as
Ad-‐Hoc
problems
◦ Number
Theory
– GCD
&
LCS,
Prime,
Fibonacci,
Modulo
Arithmetic,
Factorial
◦ java.util.BigInteger Class
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 63
64. Practice
Problem
UVa #495:
Fibonacci
Freeze
Problem
statement
The
Fibonacci
numbers
(0,
1,
1,
2,
3,
5,
8,
13,
21,
34,
55,
…)
are
defined
by
the
recurrence:
F0 =
0
F1 =
1
Fi =
Fi-‐1 +
Fi-‐2 ,
for
all
𝑖 ≥ 2
Compute
Fi,
where
𝑖 ≤ 5000.
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 64
65. Practice
Problem
UVa #495:
Fibonacci
Freeze
Quick
Analysis
(DP
Bottom-‐Up
Solution)
§Initialize
array
A[0..i],
A[0]
=
0, A[1]
=
1.
Intuitively,
we
will
have
A[i]
=
Fi
at
the
end.
§For
each
2
≤
j
≤
i,
compute
A[j]
=
A[j-‐1]+
A[j-‐2].
§Return
A[i].
The
real
problem
is
no
primitive
data
types
supporting
the
1045
digits
number.
Exit
way:
java.math.BigInteger class
Time
limit
:
3s
First
trial
:
Divide
and
Conquer
Second
trial
:
Dynamic
Programming
(Top-‐Down)
Second
trial
:
Dynamic
Programming
(Bottom-‐Up)
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 65
66. Basic
Algorithms – String
Processing
Often
used
algorithms
in
string
processing
◦ LCS
◦ Suffix
Tree
&
Array
◦ Palindrome
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 66
70. Tips
To
be
a
competitive
programmer
◦ Code
faster
◦ Quickly
identify
problem
types
◦ Do
algorithm
analysis
(time
complexity)
◦ Master
programming
languages
(C/C++
or
Java)
◦ Master
at
testing
and
debugging
code
◦ Practice
and
more
practice
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 70
72. Tips
Resources
•Online
Judges
Ø University
of
Valladolid
:
https://uva.onlinejudge.org/
Ø Peking
University:
http://poj.org/
Ø USA
Computing
Olympiad:
http://train.usaco.org/usacogate/
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 72
73. References
Figure 1.5: Some Reference Books that Inspired the Authors to Write This Book
This and subsequent chapters are supported by many text books (see Figure 1.5) and Internet
resources. Tip 1 is an adaptation from introduction text in USACO training gateway [18]. More
details about Tip 2 can be found in many CS books, e.g. Chapter 1-5, 17 of [4]. Reference for
Tip 3 are http://www.cppreference.com, http://www.sgi.com/tech/stl/ for C++ STL and
http://java.sun.com/javase/6/docs/api for Java API. For more insights to do better testing
(Tip 4), a little detour to software engineering books may be worth trying. There are many other
Online Judges than those mentioned in Tip 5, e.g.
SPOJ http://www.spoj.pl,
POJ http://acm.pku.edu.cn/JudgeOnline,
TOJ http://acm.tju.edu.cn/toj,
ZOJ http://acm.zju.edu.cn/onlinejudge/,
Ural/Timus OJ http://acm.timus.ru, etc.
There are approximately 34 programming exercises discussed in this chapter.
c⃝ Steven & Felix, NUS
(c) Create tricky test cases and find the bug.
3. Follow up question (see question 2 above): What if maximum N is 100.000?
1.2.5 Tip 5: Practice and More Practice
Competitive programmers, like real athletes, must train themselves regularly and keep themselves
‘programming-fit’. Thus in our last tip, we give a list of websites that can help you improve your
problem solving skill. Success is a continuous journey!
University of Valladolid (from Spain) Online Judge [17] contains past years ACM contest prob-
lems (usually local or regional) plus problems from another sources, including their own contest
problems. You can solve these problems and submit your solutions to this Online Judge. The
correctness of your program will be reported as soon as possible. Try solving the problems men-
tioned in this book and see your name on the top-500 authors rank list someday :-). At the point
of writing (9 August 2010), Steven is ranked 121 (for solving 857 problems) while Felix is ranked
70 (for solving 1089 problems) from ≈ 100386 UVa users and 2718 problems.
Figure 1.1: University of Valladolid (UVa) Online Judge, a.k.a Spanish OJ [17]
UVa ‘sister’ online judge is the ACM ICPC Live Archive that contains recent ACM ICPC Regionals
and World Finals problem sets since year 2000. Train here if you want to do well in future ICPCs.
Figure 1.2: ACM ICPC Live Archive [11]
INTRODUCTION
TO
ALGORITHMS
2nd ED
CLRS
Art
of
Programming
Contest
2ED
Arefin
Competitive
Programming
1ST ED
Steven
Halim
and
Felix
Halim
University
of
Valladolid
(Spain)
Online
Judge
8/29/15 ACM-‐ICPC
2015:
BASIC
PROBLEMS
&
SOLVING
ALGORITHMS 73