Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving 101 DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. DEFN An algorithm is a group of.

Similar presentations


Presentation on theme: "Problem Solving 101 DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. DEFN An algorithm is a group of."— Presentation transcript:

1

2 Problem Solving 101 DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. example 1)Preheat oven to 350° F (175° C). 2)Beat in 2 eggs, then stir in vanilla (2t.). Dissolve 1t. of baking soda in hot water and add to mixture. Stir in salt (1/2t.), flour (3c.), chocolate chips (2c.), and chopped walnuts (1c.). 3)Bake in oven for 10 min. until edges are nicely browned another 1)From 16th St. walk straight east on State St. for ten blocks 2) Continuing, State St. turns to a northeasterly direction just before 5th Ave. Continue in this direction for three blocks after 5th Ave. 3)Turn left onto 2nd St. and walk south east for just under three blocks. The La Crosse Center is on your right.

3 Problem Solving 101 Divide & Conquer Strategy An algorithm poses a solution to a problem, but how is this solution discovered? The basic approach is a... Step #1 - Recognize your limitations! 7 ± 2 http://cogprints.org/730/

4 Divide and Conquer Basic approach: Divide a problem into smaller subproblems then solve (conquer) the subproblems. Example: outlining a theme: how did I spend my summer vacation? I. What I did in June. II. What I did in July. III. What I did in August.

5 Top-down Design Successively decompose algorithms into sub-algorithms and sub-algorithms into sub-sub-algorithms. Example: write a graphics program to draw the following image using lines, rectangles, and ovals

6 top level of design 1. Draw a top hat and head 2. Draw scarf 3. Draw middle body section 4. Lowest body section & surrounding snow 5. Draw trees in the background

7 next refinement 1. Draw a top hat and head 2. Draw scarf 3. Draw middle body section 4. Lowest body section & surrounding snow 5. Draw trees in the background 1.1 Draw hat 1.2 Draw purple hat ribbon 1.3 Draw head 1.4 Draw face 2.1 Draw scarf’s knot 2.2 Draw scarf fringes 2.3 Draw main portion of scarf 3.1 Draw center white body 3.2 Draw three buttons 3.3 Draw arms 4.1 Draw lowest body section 4.2 Draw surrounding snow 5.1 Draw trees to the left of snowman 5.2 Draw trees to the right of snowman

8 still more refinement 1. Draw a top hat 2. Draw scarf 3. Draw middle body section 1.1 Draw main section of hat 1.2 Draw purple hat ribbon 1.3 Draw head 1.4 Draw face 2.1 Draw scarf’s knot 2.2 Draw scarf fringes 2.3 Draw main portion of scarf 3.1 Draw center white body 3.2 Draw three buttons 3.3 Draw arms 3.2.1 Draw top green button (an oval) 3.2.2 Draw middle purple button (a square) 3.2.3 Draw botom blue button with snowflake on top Q: When do you stop refining? A: When the sub-tasks are simple enough to code. 1.4.1 Draw left eye 1.4.2 Draw right eye 1.4.3 Draw nose 1.4.4 Draw mouth from 3 ovals 1.4.5 Draw pink cheek ovals

9 Design by Prototype Software managers/customers like to see progress in software design. A second approach to software design is to use successive prototypes. A prototype is a partially-functional program. Successive prototyping is a staged design process moving from prototype to ever more capable prototype. Successive prototyping is another form of divide and conquer.

10 Successive Prototyping Example - creating new UW-L web pages

11 Successive Prototyping Example - creating new UW-L web pages prototype 1: A new UW-L home page with no working links Successive prototyping works well for modern graphical user interfaces. prototype 2: A UW-L home page with working links to a second tier of pages prototype 3: A complete set of hyperlinked web pages without interactive forms prototype 4: All prototype 3 functionality plus an online, searchable directory of students and staff prototype 5: All prototype 4 functionality plus a page to permit students and staff to communicate email securely

12 Divide and Conquer useful for software design strategies (examples: top-down design, successive prototyping) useful for software implementation approaches (examples: parallel supercomputers, distributed computing) useful for algorithms...

13 Binary Search Binary search is a classic divide and conquer algorithm. Suppose you are given an alphabetized stack of 1,000 transcripts. What is the most efficient way to look for your own transcript? Example: linear search algorithm 1) Examine the top transcript in the stack and remove it. 2) Repeat Step 1 until your transcript is found or stack is empty. How many transcripts do you expect to examine... if your transcript is not in the stack?... if your transcript is in the stack?

14 Binary Search Suppose you are given an alphabetized stack of 1,000 transcripts. What is the most efficient way to look for your own transcript? Example: binary search algorithm 1) Examine the middle transcript in the stack. 2) If your name alphabetically precedes the middle name from Step 1, then discard all transcripts after the middle. Otherwise, if your name alphabetically follows the middle, then discard all transcripts before the middle. Repeat the process on the remaining portion of the stack, until only one transcript remains. 1 - 500 transcripts remain 2 - 250 transcripts remain 3 - 125 transcripts remain 4 - 63 transcripts remain 5 - 32 transcripts remain 6 - 16 transcripts remain 7 - 8 transcripts remain 8 - 4 transcripts remain 9 - 2 transcripts remain 10 - 1 transcript remains

15 Greedy Algorithms A greedy algorithm is any algorithm based on selecting the “best” options first. “Pick the low-hanging fruit.” Example: Suppose you want to pack the trunk of your car. In order to make best use of your limited trunk space, which bags should you pack first? Another example:

16 Greedy Algorithms How should you schedule the order of network messages? Suppose the goal is to minimize the average time that a message must wait to be sent. An example scheduling algorithm: Service shorter messages first. The greedy algorithm:

17 ...service first messages first messagesizetime sent A2 MB 0 B10 MB 1 C2 MB 2 D3 MB 4 E 5 MB 6 Example collection of five messages: Assume that the capacity of the network is 1 MB/sec. 0246810121416182022 A B C D E first come, first served algorithm total wait time = 0 + 1 + 10 + 10 + 11 = 32 sec. average wait time = 32/5 = 6.4 sec.

18 0246810121416182022...service shorter messages first messagesizetime sent A2 MB 0 B10 MB 1 C2 MB 2 D3 MB 4 E 5 MB 6 Example collection of five messages: Assume that the capacity of the network is 1 MB/sec. A B C D E total wait time = 0 + 11 + 0 + 0 + 1 = 12 sec. average wait time = 12/5 = 2.4 sec. greedy algorithm


Download ppt "Problem Solving 101 DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. DEFN An algorithm is a group of."

Similar presentations


Ads by Google