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(n1, A, C, B)
+ Hanoi(1, A, B, C) + Hanoi(n1, C, B,
A) PROBLEM 1 TOWER OF HANOI ● Proposed Alternative 1:
○ Hanoi(n, A, B, C) = Hanoi(1, A, C, B) +
Hanoi(n1, 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
 Spring '12
 Drexel
 Algorithms, Data Structures, Harshad number, Divide and conquer algorithm, FINDING THE MEDIAN

Click to edit the document details