21.03.2013 Views

CSC 165 H1F Tutorial # 8 Winter 2012 1. Find a tight bound on the ...

CSC 165 H1F Tutorial # 8 Winter 2012 1. Find a tight bound on the ...

CSC 165 H1F Tutorial # 8 Winter 2012 1. Find a tight bound on the ...

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.

<str<strong>on</strong>g>CSC</str<strong>on</strong>g> <str<strong>on</strong>g>165</str<strong>on</strong>g> <str<strong>on</strong>g>H1F</str<strong>on</strong>g> <str<strong>on</strong>g>Tutorial</str<strong>on</strong>g> # 8 <str<strong>on</strong>g>Winter</str<strong>on</strong>g> <str<strong>on</strong>g>2012</str<strong>on</strong>g><br />

<str<strong>on</strong>g>1.</str<strong>on</strong>g> <str<strong>on</strong>g>Find</str<strong>on</strong>g> a <str<strong>on</strong>g>tight</str<strong>on</strong>g> <str<strong>on</strong>g>bound</str<strong>on</strong>g> <strong>on</strong> <strong>the</strong> worst-case running time of <strong>the</strong> following algorithm.<br />

# Prec<strong>on</strong>diti<strong>on</strong>: L is a list that c<strong>on</strong>tains n > 0 real numbers.<br />

<str<strong>on</strong>g>1.</str<strong>on</strong>g> max = 0<br />

2. for i = 0, 1, . . . , n − 1:<br />

3. for j = i, i + 1, . . . , n − 1:<br />

4. sum = 0<br />

5. for k = i, i + 1, . . . , j:<br />

6. sum = sum + L[k]<br />

7. if sum > max:<br />

8. max = sum<br />

Dept. of Computer Science, University of Tor<strong>on</strong>to, St. George Campus Page 1 of 2


<str<strong>on</strong>g>CSC</str<strong>on</strong>g> <str<strong>on</strong>g>165</str<strong>on</strong>g> <str<strong>on</strong>g>H1F</str<strong>on</strong>g> <str<strong>on</strong>g>Tutorial</str<strong>on</strong>g> # 8 <str<strong>on</strong>g>Winter</str<strong>on</strong>g> <str<strong>on</strong>g>2012</str<strong>on</strong>g><br />

2. Prove that TBFT(n) ∈ Θ(n 2 ), where BFT is <strong>the</strong> algorithm below.<br />

BFT(E, n):<br />

<str<strong>on</strong>g>1.</str<strong>on</strong>g> i = n − 1<br />

2. while i > 0:<br />

3. P [i] = −1<br />

4. Q[i] = −1<br />

5. i = i − 1<br />

6. P [0] = n<br />

7. Q[0] = 0<br />

8. t = 0<br />

9. h = 0<br />

10. while h t:<br />

1<str<strong>on</strong>g>1.</str<strong>on</strong>g> i = 0<br />

12. while i < n:<br />

13. if E[Q[h]][i] = 0 and P [i] < 0:<br />

14. P [i] = Q[h]<br />

15. t = t + 1<br />

16. Q[t] = i<br />

17. i = i + 1<br />

18. h = h + 1<br />

(Although this is not directly relevant to <strong>the</strong> questi<strong>on</strong>, this algorithm carries out a breadth-first traversal of<br />

<strong>the</strong> graph <strong>on</strong> n vertices whose adjacency matrix is stored in E.)<br />

Dept. of Computer Science, University of Tor<strong>on</strong>to, St. George Campus Page 2 of 2

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

Saved successfully!

Ooh no, something went wrong!