Maximization and Minimization Problems

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
Advertisements

1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 2: Algorithm Discovery and Design
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Program State and Program Execution Version 2 – Discussing Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University.
Program State and Program Execution CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Chapter 4 Unordered List.
Strings CSE 1310 – Introduction to Computers and Programming
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Boolean Expressions and Conditionals (If Statements)
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Formatted Output (printf)
Exceptions and User Input Validation
What’s cheating and what is not?
Chapter 7 Top-Down Development
Chapter 5: Control Structures II
Lecture 10: More on Methods and Scope
Repetition-Counter control Loop
Repetition.
SELECTION STATEMENTS (1)
First Programs CSE 1310 – Introduction to Computers and Programming
Something about Java Introduction to Problem Solving and Programming 1.
הרצאה 7: מחרוזות וחתימה של פונקציה
Common Mistakes with Functions
Chapter 4 Unordered List.
Building Java Programs
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
SELECTION STATEMENTS (2)
CMSC 202 Lesson 22 Templates I.
Variables, Types, Operations on Numbers
Array Lists CSE 1310 – Introduction to Computers and Programming
Scope of variables class scopeofvars {
Templates I CMSC 202.
Strings CSE 1310 – Introduction to Computers and Programming
Building Java Programs
Classes, Objects and Methods
Names of variables, functions, classes
Arrays, Part 1 of 2 Topics Definition of a Data Structure
More on iterations using
Presentation transcript:

Maximization and Minimization Problems CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Maximization Problems Given a set of data, we want to find the object for which some criterion is maximized. Examples: Given a string, find the character that occurs the most times. Given grade information for many students, find the student with the highest GPA. Given fuel consumption information for many car models, find the car model with the highest mileage per gallon.

Minimization Problems Similarly, in many other cases, given a set of data, we want to find the object for which some criterion is minimized. Examples: Given information about many flights from DFW to Paris, find the cheapest ticket (minimize the price). Given temperature information for multiple locations, find the location with the lowest temperature.

An Example Maximization/Minimization Problem Let’s solve a specific maximization problem: Given a string, find the character that occurs the most consecutive times in that string. More specifically: Let’s write a function maxConsecutiveChar that takes a string as an argument, and returns the character that occurs the most consecutive times in that string. For example: maxConsecutiveChar("hello") returns 'l'. maxConsecutiveChar("abCCCababab") returns 'C'.

Main Function public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) System.out.printf("Please enter some text, or q to quit: "); String text = in.nextLine(); if (text.toLowerCase().equals("q")) break; } if (text.length() == 0) continue; char c = maxConsecutiveChar(text); System.out.printf("Result: %c.\n\n", c); System.out.println("Exiting...");

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Note: this function calls an auxiliary function, called countConsecutive, defined in the next slide.

countConsecutive Function public static int countConsecutive(String str, int position) { int counter = 0; for (int i = position; i < str.length(); i++) char c1 = str.charAt(position); char c2 = str.charAt(i); if (c1 == c2) counter++; } else break; return counter;

A Closer Look at maxConsecutiveChar Function maxConsecutiveChar is the function that solves the maximization problem. This function follows some specific steps, which are common to all maximization/minimization problems. Let's look at these steps, one by one.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 1: initialize a variable that will keep track of the max value that we find as we go through the data.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 2: initialize a variable that will keep track of the max object, i.e., the object corresponding to the max value that we find as we go through the data.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 3: do a loop, to go through all our data.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 4: measure the current value.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 5: if the current value is larger than the max value we have seen so far, update the variables for BOTH the max value AND the max object.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 5a: if the current value is the largest value we have seen so far, update the max value variable.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 5b: if the current value is the largest value we have seen so far, update the max object variable.

maxConsecutiveChar Function public static char maxConsecutiveChar(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str.length(); i++) int times = countConsecutive(str, i); if (times > max_counter) max_counter = times; max_char = str.charAt(i); } return max_char; Step 6: AFTER we have searched all the data, return the variable for the max object (may or may not be the max value itself, depending on the problem).

Maximization: Summary of All Steps Step 1: initialize a variable that will keep track of the max value that we find as we go through the data. Step 2: initialize a variable that will keep track of the max object, i.e., the object corresponding to the max value that we find as we go through the data. Step 3: do a loop, to go through all our data. Step 4: measure the current value. Step 5: if the current value is larger than the max value we have seen so far, update the variables for BOTH the max value AND the max object. Step 6: AFTER we have searched all the data, return the variable for the max object (may or may not be the max value itself, depending on the problem).

Maximization vs. Minimization For minimization problems, you follow the exact same steps. You just need to make obvious changes, since now you are searching for the minimum value, not the maximum value.

Minimization: Summary of All Steps Step 1: initialize a variable that will keep track of the min value that we find as we go through the data. Step 2: initialize a variable that will keep track of the min object, i.e., the object corresponding to the min value that we find as we go through the data. Step 3: do a loop, to go through all our data. Step 4: measure the current value. Step 5: if the current value is smaller than the min value we have seen so far, update the variables for BOTH the min value AND the min object. Step 6: AFTER we have searched all the data, return the variable for the min object (may or may not be the min value itself, depending on the problem).

A Closer Look at Step 1 Step 1: initialize a variable that will keep track of the max value that we find as we go through the data. What is a good initial value? You have to choose that value very carefully, otherwise your solution may not work.

A Closer Look at Step 1 Step 1: initialize a variable that will keep track of the max value that we find as we go through the data. What is a good initial value? You have to choose that value very carefully, otherwise your solution may not work. You must pick an initial value that is guaranteed to be lower than any actual value that you find in your data. The right value depends on the problem. For the max-consecutive problem, 0 is a good choice. No character will appear fewer than 0 consecutive times. -1 (or any other negative number) would also be a good choice.

A Closer Look at Step 2 Step 2: initialize a variable that will keep track of the max object, i.e., the object corresponding to the max value that we find as we go through the data. What is a good initial value?

A Closer Look at Step 2 Step 2: initialize a variable that will keep track of the max object, i.e., the object corresponding to the max value that we find as we go through the data. What is a good initial value? As long as you did step 1 right, the initial value for step 2 does not matter. This initial value will be replaced immediately, when you start looking at the data. Special case: what if you have no data? Make sure that your code does something reasonable in that case. In our max-consecutive code, the main function prevents that case (see next slide).

Checking for Empty Data public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) System.out.printf("Please enter some text, or q to quit: "); String text = in.nextLine(); if (text.toLowerCase().equals("q")) break; } if (text.length() == 0) continue; char c = maxConsecutiveChar(text); System.out.printf("Result: %c.\n\n", c); System.out.println("Exiting..."); If the data is empty, we do not call maxConsecutiveChar.