22
Algorithmic Problem Solving COMP 101 Computational Thinking and Design Thursday, September 4, 2014 Carolyn Seaman Susan Martin University of Maryland, Baltimore County

Algorithmic Problem Solving COMP 101 Computational Thinking and Design Thursday, September 4, 2014 Carolyn Seaman Susan Martin University of Maryland,

Embed Size (px)

Citation preview

Algorithmic Problem Solving

COMP 101Computational Thinking and Design

Thursday, September 4, 2014

Carolyn SeamanSusan Martin

University of Maryland, Baltimore County

Coding.org

http://www.youtube.com/watch?v=nKIu9yen5nc

Important ProblemsHunger

Poverty

Disease

Inequality

Traffic

War

Climate change

Population growth

Education

Lack of technology

Dependence on technology

Racial conflict

Homelessness

Pollution

Energy

A Kidney StoryKidney disease affects 50,000 new Americans a

year

A great example of computational abstraction! Pairs Cycles Chains

Two cycle Three cycle

A Big Kidney Story

What about really big chains?

How do you come up with an optimal series of swaps?

Exercise taken from:http://www.nsf.gov/cise/csbytes/newsletter/vol1/vol1i6.html

NY Times, Feb 19, 2012

Kidney Exchange 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 did you use to solve this problem? How would your technique scale if there were ten donors and patients? 100? Thousands?

Alternative Representation A graph data structure can capture the important relationships

among patients and donors  In the graph, there is an edge from one node to

another if and only if the first node can receive a kidney from the second node.  

This representation is an abstraction of the matching problem In reality, the problem is more

complex – edges are labeled with weights that represent the degree/quality of the match

A legal exchange is a path that never visits any vertex twice A cycle returns to the starting vertex; a chain does not If every node was visited (every patient received a kidney and

every donor donated one), this would be called a Hamiltonian cycle

Discovering a Hamiltonian cycle is a famous NP-complete (i.e., computationally hard) problem

What is the longest legal chain in this graph? Longest legal cycle?

AlgorithmsAn algorithm is an ordered set of

unambiguous steps that describes a process

Examples from real life:RecipesProject directions - chemistry lab, writing prompt Instruction manual (IKEA)Construction/building anythingScientific methodDeciding what you want for lunchKrebbs cycle (breathing/photosynthesis)Medical protocols

Algorithms Express Solutions to Computational Problems

Algorithms are expressed and implemented using languages Possibilities: natural language, pseudocode, visual and textual

programming languages Different languages may be more suitable for solving different

problems The choice of language can affect clarity or readability, but not

whether a solution exists

Algorithms can solve many, but not all, problems Many problems can be solved in a reasonable time Some require heuristic (approximate) approaches to solve them

in a reasonable time Some problems cannot be solved using any algorithm

Algorithms are evaluated analytically and empirically. Many possible criteria (e.g., efficiency, correctness, usability, ...) Different algorithms for the same problem can have different

measurements for these criteria

Friendship

Sheldon’s Friendship Algorithmhttp://www.youtube.com/watch?v=k0xgjUhEG3U

Your Examples

How to make a sandwichHow to change a tireGetting ready in the morningGetting to classMake up tutorialsGPSInstalling a videogame

Algorithms:Syntax and Semantics

Before you can write an algorithm, you have to have a language in which to express it

Basic components of every algorithmic language:“Primitive” actionsConditionals: if <condition> then <actions>Loops: repeat <actions> until <condition>Variables: places to store informationInput and output: ways to get information into

the algorithm and to return information to the invoker

Functions: ways to group instructions into a “macro-action” or “subgoal”

7 1/2 T all purpose flour1 1/2 C milk1 1/2 C butter1 1/2 C granulated sugar3/8 t salt3 t vanilla

Blend flour and milk until smooth. Cook over medium heat

until very thick. Stir constantly. Cool. Cream butter at

medium speed. Add sugar gradually, 1/4 C at a time. Add

milk mixture. Beat until smooth and fluffy. Add salt and

vanilla. Beat well.

Whipped Butter Frosting

Is this a reasonable primitive action? (for Julia Child? for a 5-year-old?)

Here’s a loop termination condition...

What’s the termination condition??

Is this a reasonable primitive action?Maybe it should be written as a loop...

Peanut Butter and Jelly

In your group, write an algorithm to make a peanut butter and jelly sandwich.

What could possibly go wrong?

ProcessingLanguage for programming graphical and

interactive computations

Processing Demonstration“My Crazy Squares” by Marcella Todaro

http://www.openprocessing.org/sketch/49183CrazySquares.pde

Reminders