Maximization and Minimization Problems CSE 1310 Introduction to
Maximization and Minimization Problems CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1
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. 2
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. 3
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 max. Consecutive. Char that takes a string as an argument, and returns the character that occurs the most consecutive times in that string. • For example: – max. Consecutive. Char("hello") returns 'l'. – max. Consecutive. Char("ab. CCCababab") returns 'C'. 4
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. next. Line(); if (text. to. Lower. Case(). equals("q")) { break; } if (text. length() == 0) { continue; } char c = max. Consecutive. Char(text); System. out. printf("Result: %c. nn", c); } System. out. println("Exiting. . . "); } 5
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(i); } } return max_char; } • Note: this function calls an auxiliary function, called count. Consecutive, defined in the next slide. 6
count. Consecutive Function public static int count. Consecutive(String str, int position) { int counter = 0; for (int i = position; i < str. length(); i++) { char c 1 = str. char. At(position); char c 2 = str. char. At(i); if (c 1 == c 2) { counter++; } else { break; } } return counter; } 7
A Closer Look at max. Consecutive. Char • Function max. Consecutive. Char 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. 8
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(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. 9
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(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. 10
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(i); } } return max_char; } • Step 3: do a loop, to go through all our data. 11
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(i); } } return max_char; } • Step 4: measure the current value. 12
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(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 13 max value AND the max object.
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(i); } } return max_char; } • Step 5 a: if the current value is the largest value we have seen so far, update the max value variable. 14
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(i); } } return max_char; } • Step 5 b: if the current value is the largest value we have seen so far, update the max object variable. 15
max. Consecutive. Char Function public static char max. Consecutive. Char(String str) { int max_counter = 0; char max_char = '1'; for (int i = 0; i < str. length(); i++) { int times = count. Consecutive(str, i); if (times > max_counter) { max_counter = times; max_char = str. char. At(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 16 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). 17
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. 18
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). 19
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. 20
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. 21
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? 22
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 23 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. next. Line(); if (text. to. Lower. Case(). equals("q")) { break; } if (text. length() == 0) If the data is empty, we do not { call max. Consecutive. Char. continue; } char c = max. Consecutive. Char(text); System. out. printf("Result: %c. nn", c); } System. out. println("Exiting. . . "); 24 }
- Slides: 24