Docsity

Input Parsing and Tokenizing Techniques for Data Processing - Prof. Sugih Jamin, Assignments of Algorithms and Programming

Partial preview of the text

Download Input Parsing and Tokenizing Techniques for Data Processing - Prof. Sugih Jamin and more Assignments Algorithms and Programming in PDF only on Docsity!

Discussion slides for week of November 26, 2007

The University of Michigan

Agenda^ z

Any Questions? z HW4 (Due coming Tuesday) z PA3 Questions/discussion

z^

Last time

−^

Branch & Bound

•^

Today

−^

Input parsing, “tokenizing”

−^

PA3 review (BnB)

−^

2-approximation

−^

k-Optimization, getrusage

−^

Anytime algorithms

z^

The C strtok() function

char str[] = “1,2,3,40,5,12” ;char *tmp;printf ("Splitting string "%s" into tokens:\n",str);tmp = strtok (str,",");while (tmp != NULL) {

printf ("%s\n",tmp);tmp = strtok (NULL, ","); }

Options for Tokenizing

z^

Use string library functions^ string tmp;string delims = “ ,”;int j = 0;int i = s.find_first_not_of(delims,j);while (i != string::npos) {

j = s.find(delims, i);tmp = s.substr(i, j-i);I = s.find_first_not_of(delims,j);// process tmp }

Options for Tokenizing

z^

Use a stringstream (if the tokens are spaces)

int i;stringstream a(“1 2 3 40 5 12”)while (a>>i) {

// process i }

Options for Tokenizing

double getcputime(void) {

struct timeval tmpTime;struct rusage ru;getrusage(RUSAGE_SELF, &ru);tmpTime = ru.ru_utime;double t = (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;tim = ru.ru_stime;t += (double)tim.tv_sec + (double)tim.tv_usec / 1000000.0;return t; }

getrusage

z^

In many applications, we need an answer, even if it is not the bestanswer^ −

Available time may be unknown − Solution times can vary

widely

for different instances

z^

We want an algorithm that will always return the best answer it can, giventhe time we allow it^ −

“Real time” or “anytime” computation

Anytime algorithms

:^

z^

For PA3, you must return the best solution you can on 60 seconds. Howcould you approach this?

PA3 Anytime Algorithm

z^

Always store the best known solution to return when time is up (and foruse as a bound) z^

First, compute quick solutions that may not be very good^ −

Random − Heuristic solution (e.g. greedy) − 2-approximation z^

Local search for improvements (k-opt) z^

Compute the optimal solution (BnB)

General Strategy