Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving - Divide & Conquer

Similar presentations


Presentation on theme: "Problem Solving - Divide & Conquer"— Presentation transcript:

1 Problem Solving - Divide & Conquer

2 Problem Solving 101 example another Preheat oven to 350° F (175° C).
DEFN An algorithm is a group of instructions (often expressed informally) for performing some task. example Preheat oven to 350° F (175° C). 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 The first example is a recipe for making chocolate chip cookies. The second is directions from Morris Hall to the La Crosse Center. Of course, the algorithms become more complicated for things like a recipe for a Thanksgiving Feast for 200 or the directions from GMH to the Louvre in Paris. This leads to the need for problem solving skills... another 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. 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 7 ± 2 Divide & Conquer Strategy
An algorithm poses a solution to a problem, but how is this solution discovered? Step #1 - Recognize your limitations! 7 ± 2 George Miller (1956) “The Magical Number Seven, Plus or Minus Two: Some Limits on Our Capacity for Processing Information” The basic approach is a... Divide & Conquer Strategy

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. This is the point of the Aesop fable.

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 1.1 Draw hat
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 1.1 Draw main section of 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 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 Q: When do you stop refining? A: When the sub-tasks are simple enough to code. 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

9 Design by Prototype A prototype is a partially-functional program.
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. Top-down design fails to demonstrate sufficient progress for many customers. Consider Ford Motor Co. who uses a small clay model, followed by a full-sized mockup, followed by a concept car taken to a Car Show, followed by the real car.

10 Successive Prototyping
Example - creating new UW-L web pages Note how the customer sees progress at each stage. Note how the customer has considerable opportunity to provide input.

11 Successive Prototyping
Example - creating new UW-L web pages prototype 1: A new UW-L home page with no working links 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 securely Note how the customer sees progress at each stage. Note how the customer has considerable opportunity to provide input. Successive prototyping works well for modern graphical user interfaces.

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... In 1998 DES Cracker (a $250K parallel computer) could crack DES keys in 3 days. In 1999 DES Cracker + 100,000 desktop computers crack DES in one day. Talk about distributed computations of PI.

13 Binary Search Binary search is a classic divide and conquer algorithm.
Example: Suppose you are given an alphabetized stack of 1,000 transcripts. What is the most efficient way to look for your own transcript? 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 be certain to emphasize the importance of alphabetization. Also point out that you don’t know anything about the stack, except alphabetization - no info on distribution. If your transcript is in the stack then we could expect it to be halfway through. ... if your transcript is not in the stack? ... if your transcript is in the stack?

14 Binary Search Example:
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. transcripts remain transcripts remain transcripts remain transcripts remain transcripts remain transcripts remain 7 - 8 transcripts remain 8 - 4 transcripts remain 9 - 2 transcripts remain transcript remains Log1000 base 2 is 9.99 = 10 Also, note that with binary search if the stack is twice the size it only take one additional probe. So the next time you look for a word in a dictionary ... START IN THE MIDDLE! There are similar D&C algorithms for sorting, etc., but you will need to study CS to see these.

15 Greedy Algorithms Example: “Pick the low-hanging fruit.”
A greedy algorithm is any algorithm based on selecting the “best” options first. Example: “Pick the low-hanging fruit.” Another 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? Greedy divides the alternatives based on an “easiest first” strategy. The low hanging fruit maximizes the number of apples you can pick in a certain amount of time.

16 Greedy Algorithms An example scheduling algorithm:
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. The greedy algorithm: Service shorter messages first. Scheduling algorithms, like searching algorithms, are common in CS.

17 ...service first messages first
Example collection of five messages: message size time sent A 2 MB B 10 MB C 2 MB D 3 MB E MB Assume that the capacity of the network is 1 MB/sec. first come, first served algorithm 2 4 6 8 10 12 14 16 18 20 22 A B  C  D  Scheduling algorithms, like searching algorithms, are common in CS. E  total wait time = = 32 sec. average wait time = 32/5 = 6.4 sec.

18 ...service shorter messages first
Example collection of five messages: message size time sent A 2 MB B 10 MB C 2 MB D 3 MB E MB Assume that the capacity of the network is 1 MB/sec. greedy algorithm 2 4 6 8 10 12 14 16 18 20 22 A B  C D E  total wait time = = 12 sec. average wait time = 12/5 = 2.4 sec.


Download ppt "Problem Solving - Divide & Conquer"

Similar presentations


Ads by Google