Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School
Computational Problem Solving Problems Problem Solving Approaches Computational Problems Solutions for Computational Problems Impacts of Computational Problem Solving What is an Algorithm? Representation of Algorithms – Flow Chart Representation of Algorithms – Pseudo Code Algorithms of Skyscrapers Project
Problems
Problems are obstacles, difficulties, or challenges which invite solutions
Problems are EVERYWHERE
Problems Healthcare Education Transportation Internet Biology Economic
Problem Solving Approaches
3 Problem Solving Approaches in Science and Engineering Analytical Experimental Computational
Problem Solving Example How can we build a projectile intercepting system?
Problem Solving Approaches Example 3 Problem Solving Approaches (3 Types of Models) Analytical Experimental Computational
Problem Solving Example Analytical (Mathematical) Model
Problem Solving Example Experimental Model
Problem Solving Example Computational (Simulation) Model
Problem Solving Example 3 Problem Solving Approaches (3 Types of Models) Analytical Experimental Computational
Computational Problems
Decision Problems Function Problems Search Problems Sort Problems Counting Problems Optimization Problems
Decision Problems A computational problem where the answer for every instance is either yes or no. Example: The primality testing - "Given a positive integer n, determine if n is prime." A decision problem is typically represented as the set of all instances for which the answer is yes.
Function Problems A computational problem where a single output of a function is expected for every input, but the output is more complex than that of a decision problem, that is, it isn't just YES or NO. Example: The integer factorization problem, which asks for the list of factors.
Sort Problems A computational problem where elements of a list need to be put in a certain order. The most-used orders are numerical order and lexicographical order. Example: Order the students in a class according to their ages from older to younger.
Search Problems A computational problem where it searches an element from a given list of elements. Example: Find students with first name Peter in Elro classlist.
Counting Problems A computational problem where it counts the number of occurrences of a type of elements in a set of elements. Example: A counting problem associated with factoring is "Given a positive integer n, count the number of nontrivial prime factors of n.”
Optimization Problems A computational problem where it finds the "best possible" solution among the set of all possible solutions. Example: Use a Google Map to find the shortest path of driving.
Solutions for Computational Problems
Problem Solving Process 4-Step Problem Solving Process Algorithm Programming Testing Specification
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible US bills and coins US monetary system: $100, $20, $10, $5, $1, 25¢, 10¢, 5¢, 1¢ Example: To make $56.39, what should you do?
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible US bills and coins US monetary system: $100, $20, $10, $5, $1, 25¢, 10¢, 5¢, 1¢ Example: To make $56.39, what should you do? – At each step, take the largest possible bill or coin that does not overshoot
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible US bills and coins US monetary system: $100, $20, $10, $5, $1, 25¢, 10¢, 5¢, 1¢ Example: To make $56.39, what should you do? – At each step, take the largest possible bill or coin that does not overshoot two $20 bills, to make $40 a $10 bill, to make $50 a $5 bill, to make $55 a $1 bill, to make $56 a 25¢ coin, to make $56.25 a 10¢ coin, to make $56.35 four 1¢ coins, to make $56.39
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible US bills and coins US monetary system: $100, $20, $10, $5, $1, 25¢, 10¢, 5¢, 1¢ Example: To make $56.39, what should you do? – At each step, take the largest possible bill or coin that does not overshoot two $20 bills, to make $40 a $10 bill, to make $50 a $5 bill, to make $55 a $1 bill, to make $56 a 25¢ coin, to make $56.25 a 10¢ coin, to make $56.35 four 1¢ coins, to make $56.39 – For US money, this algorithm always gives the optimum solution
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible coins Fictional monetary system: $12, $8, $1 (coins) Example: To make $17, what should you do?
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible coins Fictional monetary system: $12, $8, $1 (coins) Example: To make $17, what should you do? – At each step, take the largest possible bill or coin that does not overshoot a $12 coin, to make $12 five $1 coins, to make $17
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible coins Fictional monetary system: $12, $8, $1 (coins) Example: To make $17, what should you do? – At each step, take the largest possible bill or coin that does not overshoot a $12 coin, to make $12 five $1 coins, to make $17 – This algorithm needs six coins and is NOT the optimum solution
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible coins Fictional monetary system: $12, $8, $1 (coins) Example: To make $17, what should you do? – At each step, take the largest possible bill or coin that does not overshoot a $12 coin, to make $12 five $1 coins, to make $17 – This algorithm needs six coins and is NOT the optimum solution – The better way is to use
Problem Solving Example: Counting Money Suppose you want to count out a certain amount of money, using the fewest possible coins Fictional monetary system: $12, $8, $1 (coins) Example: To make $17, what should you do? – At each step, take the largest possible bill or coin that does not overshoot a $12 coin, to make $12 five $1 coins, to make $17 – This algorithm needs six coins and is NOT the optimum solution – The better way is to use two $8 coins, to make $16 a $1 coin, to make $17 – This way needs ONLY three coins and IS the optimum solution
Impacts of Computational Problem Solving
Problem Solving Example: A Kidney Story Kidney disease affects 50,000 new Americans a year. Kidney Exchanges can save lives! Kidney transplants are often their best option for saving their life. The demand for kidneys far outstrips the supply from deceased donors. For patients with kidney disease, the best option is to find a living donor — a healthy person willing to donate one of their two kidneys. In a live donation, a potential donor and the intended recipient must be compatible, which can be quite rare. In order for patients to obtain a compatible donor, they can swap donors. A pool of incompatible patient-donor pairs where one tries to find swaps is called a kidney exchange.
Problem Solving Example: A Kidney Story 2-Cycle Exchange 3-Cycle Exchange In the simplest exchange, you have two patients each with an available donor kidney, and both patients are compatible with the other’s donor kidney. When compatibility doesn't match, looking for a third person can make the exchange work —this is called a "3-cycle exchange”. Longer cycles can yield more matches, but are often difficult to manage.
Problem Solving Example: A Kidney Story Consider the exchange below. A patient is connected to a donor if they are biologically compatible. A donor will only donate a kidney if his or her friend also receives a kidney. What is the optimal exchange for this situation? Why? What technique will you use to solve this problem? How would your technique scale if there were ten donors and patients? 100? Thousands?
Problem Solving Example: A Kidney Story 60 Lives, 30 Kidneys, All Linked (New York Time article: February 18, 2012) Prof. Tuomas Sandholm and his research group from CMU developed an algorithm that can compute the optimal matching for a U.S.-wide kidney exchange (~10,000 patients) in about an hour. The optimal matching is the one that results in the largest number of exchanges.
Problem Solving Example: A Kidney Story Matching algorithms can be applied to a variety of other problems that require similar barter exchanges. The National Odd Shoe Exchange lets people with differently sized feet agree to swap shoes to avoid having to buy two pairs each ( Read It Swap It lets people swap books ( Intervac allows people to swap vacation houses ( homeexchange.com), and Netcycler allows people to swap, give away, and get items for free (
What is an Algorithm?
An Algorithm is a finite set of well-defined instructions for effectively carrying out a procedure or solving a problem.
An Algorithm is a finite set of well-defined instructions for effectively carrying out a procedure or solving a problem.
Examples of Algorithms
Representations of Algorithms
Show the logic of how the problem is solved - not how it is implemented. Readily reveal the flow of the algorithm. Be expandable and collapsible. Lend itself to implementation of the algorithm.
Representations of Algorithms Flowcharts –Graphical representation of control flow Pseudocode –A sequence of statements, more precise notation –Not a programming language, no formal syntax
Representation of Algorithms – Flow Chart
Start or stop Process Input or output Connector Decision Flow line Off-page connector Representation - Flowcharts Elements of flowcharts
Examples of Flowcharts An algorithm for finding the area of a circle of radius r.
Review of Flowchart Elements
Start or stop Connector Elements of flowcharts
Process Input or output Elements of flowcharts
Decision Off-page connector Elements of flowcharts
Flow lines Elements of flowcharts
Process Decision
Off-page connector Elements of flowcharts Connector
Input or output Elements of flowcharts Start or stop
Representation of Algorithms – Pseudocode
Representation - Pseudocode Conventions of pseudocode 1.Give a valid name for the pseudo-code procedure 2.Use the line numbers for each line of code. 3.Use proper Indentation for every statement in a block structure. 4.For a flow control statements use if-else. Always end an if statement with an end-if. Both if, else and end-if should be aligned vertically in same line. Ex: If (conditional expression) statements else statements end-if
Representation - Pseudocode Conventions of pseudocode (Continued) 5.Use “=” or “ ” operator for assignment statements. Ex: i = j or I j n = 2 to length[A] or n 2 to length[A] 6.Array elements can be represented by specifying the array name followed by the index in square brackets. For example, A[i] indicates the ith element of the array A. 7.For looping or iteration use for or while statements. Always end a for loop with end-for and a while with end-while. 8.The conditional expression of for or while can be written as shown in rule (4). You can separate two or more conditions with an “and” or an “or”.
Examples of Pseudocode An algorithm for calculating the class average of test scores
Examples of Pseudocode An algorithm for calculating the class average of test scores 1. Prompt “Enter the number of tests” and store in testCount 2. Let counter = 1 3. Let totalScore = 0 4. While counter is less than or equal to testCount 5. Prompt “enter test score” and store into testScore 6. If testScore is numeric then 7. totalScore = testScore + totalScores 8. counter = counter else 10. display “error: test score must be numeric” 11. end-if 12. end-while 13. Average = totalScore / testCount 14. display “Average is :” average
64 Building Blocks of Algorithms Three building blocks of algorithms: Sequences Conditionals Loops
A sequence of instructions that are executed in the precise order they are written in: statement block 1 statement block 2 statement block 3 Building Blocks - Sequences statement block 1 statement block 2 statement block 3
Select between alternate courses of action depending upon the evaluation of a condition If ( condition = true ) statement block 1 Else statement block 2 End if Building Blocks - Conditionals statement block 1 condition TrueFalse statement block 2
Loop through a set of statements as long as a condition is true Loop while ( condition = true ) statement block End Loop Building Blocks - Loops condition True False statement block
Examples of Flowchart Make a robot trace the boundary of a square.
Examples of Flowchart Calculate n 2
Examples of Pseudocode Calculate n 2
Examples of Pseudocode Calculate n 2 1. Input n 2. Sum = 0 3. i = 1 4. While i <= n 5. sq = I * i 6. Sum = Sum + sq 7. i = I End-While 9. Output Sum
Examples of Pseudocode Fibonacci numbers or Fibonacci series are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. Write an algorithm to find the Nth Fibonacci number.
Examples of Pseudocode An algorithm for finding the Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ……) Fibonacci () 1. Input N 2.Let result0 = 0 3.Let result1 = 1 4.If N = 1 or N = 2 5. result = N else 7. for loop counter = 2 to N – 1 8. result = result1 + result0 9. result0 = result1 10. result1 = result 11. end-for loop 12.end-if 13. output result
Algorithms of Skyscrapers Project
Rules: Using prefabricated-modular blocks of 100 meters long, 100 meters wide, and 5 meters tall. The blocks interlock on top and bottom (like Legos), and they cannot be stacked sideways. Using special lifters, putting stacks of blocks at the same height on ground or on top of another set of equal-height stacks takes one week regardless of how tall the stacks are or how many stacks are lifted. The prefabrication time of the blocks doesn’t count since they are already in stock. No resource/budget limitations (i.e., you can have as many stacks as possible at the same time, however, you don’t waste materials.)
Algorithms of Skyscrapers Project Problem: Part 1: If a client wants to build a 100-meter long, 100-meter wide, and 1250-meter high tower as quickly as possible, what is the shortest amount of time that it will take to build the tower? Show your algorithm in both flowchart and pseudocode forms. Part 2: Develop a general algorithm for skyscrapers of 100-meter long, 100-meter wide, and N-meter high (where N is a multiple of 5) in both flowchart and pseudocode forms.
Q & A