11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22 Chap. 1 <strong>D<strong>at</strong>a</strong> <strong>Structures</strong> <strong>and</strong> <strong>Algorithm</strong>sFor example, the string “((())())()” contains properly nested pairs of parentheses,but the string “)()(” does not; <strong>and</strong> the string “())” does not containproperly m<strong>at</strong>ching parentheses.(a) Give an algorithm th<strong>at</strong> returns true if a string contains properly nested<strong>and</strong> balanced parentheses, <strong>and</strong> false if otherwise. Hint: At no timewhile scanning a legal string from left to right will you have encounteredmore right parentheses than left parentheses.(b) Give an algorithm th<strong>at</strong> returns the position in the string of the first offendingparenthesis if the string is not properly nested <strong>and</strong> balanced.Th<strong>at</strong> is, if an excess right parenthesis is found, return its position; ifthere are too many left parentheses, return the position of the first excessleft parenthesis. Return −1 if the string is properly balanced <strong>and</strong>nested.1.16 A graph consists of a set of objects (called vertices) <strong>and</strong> a set of edges, whereeach edge connects two vertices. Any given pair of vertices can be connectedby only one edge. Describe <strong>at</strong> least two different ways to represent the connectionsdefined by the vertices <strong>and</strong> edges of a graph.1.17 Imagine th<strong>at</strong> you are a shipping clerk for a large company. You have justbeen h<strong>and</strong>ed about 1000 invoices, each of which is a single sheet of paperwith a large number in the upper right corner. The invoices must be sorted bythis number, in order from lowest to highest. Write down as many differentapproaches to sorting the invoices as you can think of.1.18 How would you sort an array of about 1000 integers from lowest value tohighest value? Write down <strong>at</strong> least five approaches to sorting the array. Donot write algorithms in Java or pseudocode. Just write a sentence or two foreach approach to describe how it would work.1.19 Think of an algorithm to find the maximum value in an (unsorted) array.Now, think of an algorithm to find the second largest value in the array.Which is harder to implement? Which takes more time to run (as measuredby the number of comparisons performed)? Now, think of an algorithm tofind the third largest value. Finally, think of an algorithm to find the middlevalue. Which is the most difficult of these problems to solve?1.20 An unsorted list allows for constant-time insert by adding a new element <strong>at</strong>the end of the list. Unfortun<strong>at</strong>ely, searching for the element with key value Xrequires a sequential search through the unsorted list until X is found, whichon average requires looking <strong>at</strong> half the list element. On the other h<strong>and</strong>, asorted array-based list of n elements can be searched in log n time with abinary search. Unfortun<strong>at</strong>ely, inserting a new element requires a lot of timebecause many elements might be shifted in the array if we want to keep itsorted. How might d<strong>at</strong>a be organized to support both insertion <strong>and</strong> search inlog n time?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!