Download presentation
Presentation is loading. Please wait.
1
Maximization and Minimization Problems
CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington
2
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.
3
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.
4
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'.
5
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...");
6
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.
7
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;
8
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.
9
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.
10
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.
11
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.
12
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.
13
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.
14
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.
15
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.
16
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).
17
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).
18
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.
19
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).
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.
21
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.
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?
23
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).
24
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.