{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Week1 - CS130B DATA STRUCTURES AND ALGORITHMS II DISCUSSION...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS130B- DATA STRUCTURES AND ALGORITHMS II DISCUSSION SECTION WEEK 1 TAS Name: Emily Fujimoto Email: [email protected] Office Hours: TR 11am - 1pm F 9:30am - 11:30am Name: Fangqiu Han Email: [email protected] Office Hours: MW 3:30pm - 5:30pm T 9:30am - 11:30am Course Website: http://www.cs. ucsb.edu/~cs130b/ Class Email: [email protected] WRITTEN ASSIGNMENT 1 ● Due Friday, January 20th at 4pm ● Turn in to CS130b box in the mail room (HFH 2108) ● 4 problems PROBLEM 1- TOWER OF HANOI ● Move disks from one peg to another ● Larger disks can't go on top of smaller ones PROBLEM 1- TOWER OF HANOI ● In class solution: ○ Hanoi(n, A, B, C) = Hanoi(n-1, A, C, B) + Hanoi(1, A, B, C) + Hanoi(n-1, C, B, A) PROBLEM 1- TOWER OF HANOI ● Proposed Alternative 1: ○ Hanoi(n, A, B, C) = Hanoi(1, A, C, B) + Hanoi(n-1, A, B, C) + Hanoi(1, C, B, A) PROBLEM 1- TOWER OF HANOI ● Proposed Alternative 2: ○ Hanoi(n, A, B, C) = Hanoi(n/2, A, C, B) + Hanoi(n/2, A, B, C) + Hanoi(n/2, C, B, A) PROBLEM 2- TERNARY SEARCH Binary Search: Want to find 42 in a sorted array [1, 3, 5, 6, 9, 11, 14, 17, 21, 23, 27, 30, 42, 51, 55] PROBLEM 2- TERNARY SEARCH Binary Search: Want to find 42 in a sorted array [1, 3, 5, 6, 9, 11, 14, 17, 21, 23, 27, 30, 42, 51, 55] [21, 23, 27, 30, 42, 51, 55] PROBLEM 2- TERNARY SEARCH Binary Search: Want to find 42 in a sorted array [1, 3, 5, 6, 9, 11, 14, 17, 21, 23, 27, 30, 42, 51, 55] [21, 23, 27, 30, 42, 51, 55] [42, 51, 55] PROBLEM 2- TERNARY SEARCH Binary Search: Want to find 42 in a sorted array [1, 3, 5, 6, 9, 11, 14, 17, 21, 23, 27, 30, 42, 51, 55] [21, 23, 27, 30, 42, 51, 55] [42, 51, 55] [42] PROBLEM 2- TERNARY SEARCH Original: [1, 5, 6, 10, 11, 14, 17, 21, 23, 24, 27, 30] Ternary: [1, 5, 6, 10, 11, 14, 17, 21, 23, 24, 27, 30] PROBLEM 3- FINDING THE MEDIAN Median in a sorted array: [1, 2, 5, 7, 11, 15, 17, 21, 24] Median in an unsorted array: [42, 11, 32, 15, 1, 17, 7, 12, 37] much harder to find PROBLEM 3- FINDING THE MEDIAN 3a) Find a brute force solution. 3b) Find a Divide and Conquer Solution Hint: How can we use the Quicksort algorithm to help? PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap [15, 12, 4, 16, 7 , 11, 42] PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap [15, 12, 4, 16, 7 , 11, 42] [15, 12, 4, 16, 7 , 11, 42] Swap the values PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap [15, 12, 4, 16, 7 , 11, 42] [15, 12, 4, 16, 7 , 11, 42] Swap the values [15, 12, 4, 11, 7 , 16, 42] PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap [15, 12, 4, 16, 7 , 11, 42] [15, 12, 4, 16, 7 , 11, 42] Swap the values [15, 12, 4, 11, 7 , 16, 42] [15, 12, 4, 11, 7 , 16, 42] PROBLEM 3- FINDING THE MEDIAN Quicksort: [15, 12, 4, 16, 7 , 11, 42] Find the pivot [15, 12, 4, 16, 7 , 11, 42] Go through array until you find values to swap [15, 12, 4, 16, 7 , 11, 42] [15, 12, 4, 16, 7 , 11, 42] Swap the values [15, 12, 4, 11, 7 , 16, 42] [7, 12, 4, 11, 15 , 16, 42] PROBLEM 4- FIBONACCI GENERATION Matrix Multiplication a1,1*b1,2 + a1,2*b2,2 a3,1*b1,3 + a3,2*b2,3 Fibonacci Sequence: ● The first and second numbers are 1's ● Each subsequent number is the sum of the 2 previous ones ● 1, 1, 2, 3, 5, 8, 13, 21, ... IMAGE SOURCES ● http://pages.cs.brandeis. edu/~storer/JimPuzzles/MANIP/TowersOfHanoi/TowersO fHanoiFigure.jpg ● http://upload.wikimedia. org/wikipedia/en/e/eb/Matrix_multiplication_diagram_2. svg ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern