# Maximization and Minimization Problems - PowerPoint Presentation

#### Maximization and Minimization Problems - Description

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 ID: 734329 Download Presentation

### Tags

char max int str max char str int times counter step maxconsecutivechar data object find string function variable return

Embed / Share - Maximization and Minimization Problems

## Presentation on theme: "Maximization and Minimization Problems"— Presentation transcript

Slide1

Maximization and Minimization Problems

CSE 1310 – Introduction to Computers and ProgrammingVassilis AthitsosUniversity of Texas at Arlington

1Slide2

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.2Slide3

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.3Slide4

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'.4Slide5

Main Function

5 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...");

}Slide6

maxConsecutiveChar Function

6 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 functio

n calls an auxiliary function, called

countConsecutive

, defined in the next slide.Slide7

countConsecutive Function

7 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; }Slide8

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.8Slide9

maxConsecutiveChar Function

9 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.Slide10

maxConsecutiveChar Function

10 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. Slide11

maxConsecutiveChar Function

11 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.Slide12

maxConsecutiveChar Function

12 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.Slide13

maxConsecutiveChar Function

13 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.Slide14

maxConsecutiveChar Function

14 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

.Slide15

maxConsecutiveChar Function

15 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

.Slide16

maxConsecutiveChar Function

16 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).Slide17

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).17Slide18

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.18Slide19

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).19Slide20

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.20Slide21

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.21Slide22

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?22Slide23

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).23Slide24

Checking for Empty Data

24 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.

Shom More....
By: tatiana-dople
Views: 7
Type: Public