Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

77
Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School

Transcript of Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Page 1: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Computational Problem Solving

Chin-Sung Lin

Eleanor Roosevelt High School

Page 2: 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

Page 3: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problems

Page 4: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problems are

obstacles, difficulties, or challenges

which invite solutions

Page 5: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problems are

EVERYWHERE

Page 6: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problems

Healthcare Education

Transportation Internet Biology

Economic

Page 7: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Approaches

Page 8: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Approaches

3 Problem Solving Approaches in Science and Engineering

Analytical Experimental Computational

Page 9: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Example

How can we build a projectile intercepting system?

Page 10: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Approaches Example

3 Problem Solving Approaches (3 Types of Models)

Analytical Experimental Computational

Page 11: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Example

Analytical (Mathematical) Model

Page 12: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Example

Experimental Model

Page 13: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Example

Computational (Simulation) Model

Page 14: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Example

3 Problem Solving Approaches (3 Types of Models)

Analytical Experimental Computational

Page 15: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Computational Problems

Page 16: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Computational Problems

• Decision Problems

• Function Problems

• Search Problems

• Sort Problems

• Counting Problems

• Optimization Problems

Page 17: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 18: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 19: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 20: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 21: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.”

Page 22: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 23: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Solutions for Computational Problems

Page 24: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Problem Solving Process

4-Step Problem Solving Process

Algorithm

Programming

Testing

Specification

Page 25: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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?

Page 26: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 27: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 28: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 29: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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?

Page 30: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 31: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 32: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 33: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 34: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Impacts ofComputational Problem Solving

Page 35: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 36: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 37: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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?

Page 38: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

• http://www.nsf.gov/cise/csbytes/newsletter/vol1/vol1i6.html

Page 39: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

(http://www.oddshoe.org),

• Read It Swap It lets people swap books

(http://www.readitswapit.co.uk//TheLibrary.aspxx),

• Intervac allows people to swap vacation houses (http://www.intervac-

homeexchange.com), and

• Netcycler allows people to swap, give away, and get items for free

(www.netcycler.com).

Page 40: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

What is an Algorithm?

Page 41: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

An Algorithm

is a finite set of well-defined instructions for

effectively carrying out a procedure or solving a problem.

Page 42: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

An Algorithm

is a finite set of well-defined instructions for

effectively carrying out a procedure or solving a problem.

Page 43: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Algorithms

Page 44: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Algorithms

Page 45: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Representations of Algorithms

Page 46: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 47: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Representations of Algorithms

• Flowcharts

– Graphical representation of control flow

• Pseudocode

– A sequence of statements, more precise notation

– Not a programming language, no formal syntax

Page 48: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Representation of Algorithms

– Flow Chart

Page 49: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Start or stop

Process

Input or output

Connector

Decision

Flow line

Off-page connector

Representation - Flowcharts

Elements of flowcharts

Page 50: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Flowcharts

An algorithm for finding the area of a circle of radius r.

Page 51: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Review of

Flowchart Elements

Page 52: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Start or stopConnector

Elements of flowcharts

Page 53: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Process Input or output

Elements of flowcharts

Page 54: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Decision Off-page connector

Elements of flowcharts

Page 55: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Flow lines

Elements of flowcharts

Page 56: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Elements of flowcharts

ProcessDecision

Page 57: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Off-page connector

Elements of flowcharts

Connector

Page 58: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Input or output

Elements of flowcharts

Start or stop

Page 59: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Representation of Algorithms

– Pseudocode

Page 60: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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

Page 61: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Representation - PseudocodeConventions 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”.

Page 62: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of PseudocodeAn algorithm for calculating the class average of test scores

Page 63: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of PseudocodeAn 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 = 04. 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 + 1 9. else 10. display “error: test score must be numeric” 11. end-if 12. end-while 13. Average = totalScore / testCount 14. display “Average is :” average

Page 64: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

64

Building Blocks of Algorithms

Three building blocks of algorithms:

• Sequences

• Conditionals

• Loops

Page 65: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

A sequence of instructions that are executed in the precise order they are written in:

statement block 1statement block 2statement block 3

Building Blocks - Sequences

statement block 1

statement block 2

statement block 3

Page 66: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Select between alternate courses of action depending upon the evaluation of a condition

If ( condition = true )statement block 1

Elsestatement block 2

End if

Building Blocks - Conditionals

statementblock 1

conditionTrue False

statementblock 2

Page 67: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Loop through a set of statements as long as a condition is true

Loop while ( condition = true )statement block

End Loop

Building Blocks - Loops

conditionTrue

False

statementblock

Page 68: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Flowchart

Make a robot trace the boundary of a square.

Page 69: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Flowchart

Calculate 12 + 22 + ... + n2

Page 70: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Pseudocode

Calculate 12 + 22 + ... + n2

Page 71: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of Pseudocode

Calculate 12 + 22 + ... + n2

1. Input n

2. Sum = 0

3. i = 1

4. While i <= n

5. sq = I * i

6. Sum = Sum + sq

7. i = I + 1

8. End-While

9. Output Sum

Page 72: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 73: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Examples of PseudocodeAn 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 = 25. result = N - 16. else7. for loop counter = 2 to N – 1 8. result = result1 + result09. result0 = result110. result1 = result11. end-for loop12. end-if13. output result

Page 74: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms of Skyscrapers Project

Page 75: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.)

Page 76: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

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.

Page 77: Computational Problem Solving Chin-Sung Lin Eleanor Roosevelt High School.

Q & A