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

Similar documents
Common Mistakes with Functions. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Introduction to Programming (Java) 4/12

Arrays and Array Lists. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington

Introduction to Software Development (ISD) Week 3

Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Array Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Question: Total Points: Score:

Loops. CSE 114, Computer Science 1 Stony Brook University

Last Class. While loops Infinite loops Loop counters Iterations

Loop - 3 & Class. Cheng, Wei COMP May 22, Title

Chapter 7 User-Defined Methods. Chapter Objectives

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Object Oriented Programming. Java-Lecture 6 - Arrays

Chapter Goals. Contents LOOPS

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Repetition. Chapter 6

Repetition. Chapter 6

Chapter 6. Repetition. Asserting Java. Rick Mercer

Name CIS 201 Midterm II: Chapters 1-8

a data type is Types

Variables, Types, Operations on Numbers

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSE 114 Computer Science I

Fundamentals of Programming Data Types & Methods

Conditional Execution

Array. Array Declaration:

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Example Program. public class ComputeArea {

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

1 Short Answer (5 Points Each)

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CSC 1051 Data Structures and Algorithms I

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Chapter 4: Control Structures I

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Handout 4 Conditionals. Boolean Expressions.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Part (II): In the body method main : Write the call to method formatname and have the return value stored in the variable formatted.

Binary and Hexadecimal Numbers. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Darrell Bethea May 20, 2011

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Chapter Goals. Chapter 5 - Iteration. Calculating the Growth of an Investment

CS141 Programming Assignment #5

CSE142 Sample Midterm Spring Name UW NetId (e.g. whitab) Section (e.g., AA) TA

SELECTION. (Chapter 2)

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Arrays: An array is a data structure that stores a sequence of values of the same type. The data type can be any of Java s primitive types:

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

1 Short Answer (15 Points Each)

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

CSC142, Computer Science II, Project 5 (2 nd submission)

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CSIS 10A Assignment 9 Solutions


Chapter 6. Repetition. Asserting Java. Rick Mercer

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Top-Down Program Development

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

1 Short Answer (10 Points Each)

CSC 1051 Data Structures and Algorithms I

Name: Checked: Preparation: Response time experiment accessing the system clock

MIDTERM REVIEW. midterminformation.htm

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Question: Total Points: Score:

/** * Utility class to support string reassembly from fragments. * Emily McIntyre * <pre> * * OVERLAPS ( * s1: string of

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Activity 9: Object-Oriented

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CSE 113. Announcements

CSE 143 Sample Midterm Exam #4

Following is the general form of a typical decision making structure found in most of the programming languages:

( &% class MyClass { }

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

ITERATION WEEK 4: EXMAPLES IN CLASS

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

McGill University School of Computer Science COMP-202A Introduction to Computing 1

In this chapter, you will:

Practice Midterm 2 CMPS 12A Fall 2017

1. Code Trace: 2. Program Logic (0.5 each) X <= y y > z x == 1 A S N S N S A A N S S S S A S POINT A POINT B POINT C POINT D POINT E

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Question: Total Points: Score:

Lab 6. Name: Checked:

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Transcription:

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

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

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

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

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

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. 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 4: measure the current value. 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 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. 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 5a: if the current value is the largest value we have seen so far, update the max value variable. 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 5b: if the current value is the largest value we have seen so far, update the max object 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 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 case (see next slide). 23

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