This title is orange because of Halloween. It is totally not because it’s always orange.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CHAPTER 7 Greedy Algorithms.
Greedy Algorithms.
Introduction to Algorithms Greedy Algorithms
BackTracking Algorithms
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Intro to USACO Strategy
I couldn’t think of anything to put here. So have a cow. Or two.
Semester 10 Time sure flies.. PotW Solution One possible solution is to randomly search the grid: o At each point in your search, look at the (up to four)
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Greedy Algorithms. Announcements Exam #1 ◦ See me for 2 extra points if you got #2(a) wrong. Lab Attendance ◦ 12 Labs, so if anyone needs to miss a lab.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
CSC 2300 Data Structures & Algorithms April 17, 2007 Chapter 9. Graph Algorithms.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
TA: Nouf Al-Harbi NoufNaief.net :::
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Dave Risch. Project Specifications There is a “knapsack” that you want to fill with the most valuable items that are available to you. Each item has a.
9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Guest lecturer: Martin Furer Algorithm Design and Analysis L ECTURE.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Lecture 23. Greedy Algorithms
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Moooooo Or: How I learned to stop worrying and love USACO.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Binary Search From solving a problem to verifying an answer.
Public void main What do you call something that’s not static?
Algorithm Paradigms High Level Approach To solving a Class of Problems.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Click to add title Click to add lame programming pun.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Not a Math Honor Society meeting. Or: How I learned to do math again, this time with cows.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
Greedy Algorithms Prof. Kharat P. V. Department of Information Technology.
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Greedy Algorithms – Chapter 5
Greedy function greedy { S <- S0 //Initialization
TK1114 Computer Programming
Short paths and spanning trees
Minimum-Cost Spanning Tree
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Greedy Algorithms.
Algorithms: Design and Analysis
CSC 380: Design and Analysis of Algorithms
Building Java Programs
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Prim’s algorithm for minimum spanning trees
More on iterations using
Minimum-Cost Spanning Tree
Presentation transcript:

This title is orange because of Halloween. It is totally not because it’s always orange.

Last three weeks’ PotW! BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringBuffer s = new StringBuffer(in.readLine()); int count = 0; for ( ; s.length() > 1; count++) { if (s.charAt(s.length() - 1) == '0') // if even s.deleteCharAt(s.length() - 1); // remove last char else // if odd { int last0 = s.lastIndexOf("0"); if (last0 == -1) // no '0' remaining { System.out.println(count + s.length() + 1); // shortcut return; } // continued on next slide

PotW solution (cont.) else // there are still '0's remaining { s.setCharAt(last0, '1'); // replace last '0' with 1 last0++; // replace trailing '1's with '0' for ( ; last0 < s.length(); last0++) s.setCharAt(last0, '0'); } } // end else (if odd) } // end for loop if (s.charAt(0) == '0') count++; System.out.println(count);

PotW Solution (hax ver.) import java.util.Scanner; public class YoungBetsy { public static void main(String[] args) { Scanner s = new Scanner(System.in); String n = s.next(); int loc = n.lastIndexOf('1'); if (loc != 0) System.out.println(n.split("0", -1).length + loc + 1); else System.out.println(n.length() - 1); }

Greedy Algorithms All hail Johnny. Allow him to enlighten you on this fascinating topic.

At each step of the algorithm, make the locally optimal choice This only works for a fairly limited set of problems Only use greedy algorithms if you can prove their validity Examples: o Change making o Segment covering o Dijkstra's algorithm for shortest paths o Kruskal's and Prim's algorithms for minimum spanning tree Greedy Algorithms Source: Wikipedia

Change Making (working case) Change making: o Try to make change for a certain amount using the minimum number of coins/bills At each step of the algorithm, use the highest denomination that fits under current amount o Guaranteed to work if each denomination is divisible by the next highest o Bills: $1, $2, $4 o Amount: $11 to $7 to $3 to $1 to $0 o {$1,$2,$4,$4} o Works!

Change Making (fail case) Doesn't always work! o Bills: $1, $3, $4 o Amount: $6 o $6 to $2 to $1 to $0 o {$1,$1,$4} o Not as good as {$3,$3}! You would have to use a knapsack algorithm in this case

Segment Covering Find maximum number of non-intersecting segments o e.g. try to attend largest number of activites Sort all segments by endpoint Go through segments one by one and check if they can be added to the current segment set without intersections o If so, add it! o Note that you only have to maintain the latest endpoint in the current set - you don't need arrays or anything Source: Topcoder

Partyin’, Partyin’ (yeah!) Partyin’, Partyin’ (yeah!) fun, fun, fun, fun looking forward to the weekend It's Monday, and Halloween, so naturally, the cows are out partying. Bessie has a list of N<100,000 parties and their start/end times (in 24 hr format), and she wants to maximize the number of parties she attends, k. She must attend the entirety of each party. Note that the parties are sorted by end time (VERY important). Sample Input: 4 0:00 0:05 0:00 0:10 0:05 0:15 0:15 0:20 Sample Output: (print out k on a single line) 3 This is worth 20 pts, but for 15 extra pts, also print out the maximum amount of time she can spend attending k parties (fairly difficult).