-
Notifications
You must be signed in to change notification settings - Fork 28
Backup
Jinho D. Choi edited this page Nov 9, 2016
·
8 revisions
- Create a package
cs323.queue
. - Create an abstract class
AbstractPriorityQueue
underqueue
. - Make a generic type
T extends Comparable<T>
]. - Declare an abstract method
add
. -
@param key
a comparable key to be added. - Declare an abstract method
removeMax
. -
@return
the key with the highest priority. -
@throws
[NoSuchElementException
] (http://docs.oracle.com/javase/8/docs/api/java/util/NoSuchElementException.html) if the queue is empty. - Declare an abstract method
size
. -
@return
the number of keys in this queue. - Define a method
isEmpty
. -
@return
true if the queue is empty; otherwise, false.
- Create a class
EagerPriorityQueue
underqueue
. - Extend the
AbstractPriorityQueue
class. - Make a generic type
T extends
[Comparable<T>
]. -
List<T> l_keys
- contains all keys. - Override the
add
method. - Insert the key to
l_keys
such thatl_keys
maintains to be in ascending order. - Override the
removeMax
method. - Remove and return the last key in
l_keys
. - Override the
size
method. -
@return
the size ofl_keys
.
- Create a class
BinaryHeap
under thequeue
package. - Extend the
AbstractPriorityQueue
class. -
List<T> l_keys
- contains all keys, including a dummy key at the front. -
int n_size
- counts the number of keys in the heap. - Override the
add
method. - Add the key to the end of
l_keys
, and increasen_size
by 1. - Perform
swim
(bottom-up reheapify). - Override the
removeMax
method. - Swap the last and the first keys in
l_keys
. - Remove and keep the last key in
l_keys
; decreasen_size
by 1. - Perform
sink
(top-down reheapify). - Override the
size
method. -
@return
n_size
.
- Assess
EagerPriorityQueue
andBinaryHeap
using: -
PriorityQueueTest#testAccuracy()
. - Compare the speeds of
EagerPriorityQueue
andBinaryHeap
using: -
PriorityQueueTest#testSpeed()
.
- Create a class
LastnameHeap
underqueue
(e.g.,ChoiHeap
). - Extend the
AbstractPriorityQueue
class. - Override the
add
,removeMax
, andsize
methods such that each node takes 3 children instead of 2 (so it becomes a ternary instead of binary tree). You code should look very similar toBinaryHeap
. - Put
TernaryHeap.java
undercs323/quiz1
.
- Create a package
sort
. - Put the
AbstractSort
class undersort
. - Create classes
SelectionSort
,InsertionSort
,HeapSort
,ShellSort
,MergeSort
,QuickSort
undersort
. - Extend the
AbstractSort
class. - Make a generic type
T extends Comparable<T>
. - Override the
sort
method in each class by implementing selection sort, insertion sort, heap sort, shell sort, merge sort, and quick sort. - Make sure you use the
compareTo
,assign
andswap
methods for all comparisons, array assignments, and swaps, respectively.
- Assess all sorting algorithms using:
-
SortTest#testAccuracy()
. - Compare the speeds of all sorting algorithms using:
-
SortTest#compareSpeeds()
. - Compare the assignments of all sorting algorithms using:
-
SortTest#countOperations()
.
- Create a class
ShellSortHibbard
undersort
. - Extend the
AbstractSort
class. - Override the
sort
method. - Implement the shell sort algorithm.
- Use the
Hibbard
sequence for the gaps. - Compare the performance between
ShellSort
andShellSortHibbard
using lists of 100, 200, ..., 1000 random keys for speeds, comparison counts, and assignment counts.
- Create an abstract class
AbstractBucketSort
under thesort
package. - Inherit the
AbstractSort
class. - Make a generic type
T extends
Comparable<T>
. -
List<T>[] g_buckets
: the list of buckets. -
boolean b_sort
: iftrue
, sort each bucket. - Define a constructor.
-
@param bucketSize
: the total number of buckets. -
@param sort
: iftrue
, sort each bucket. - Initialize
g_buckets
with empty lists (useDSUtils#createEmptyListArray
). - Initialize the global instance
b_sort
withsort
. - Declare an abstract method
getBucketIndex
. -
@param key
a comparable key. -
@return
the index of the bucket thatkey
should be inserted. - Override the
sort
method. - Add all keys in
array
tog_buckets
using thegetBucketIndex
method. - If
b_sort
istrue
, sort each bucket in ascending order. - Put all keys in each bucket back to
array
.
- Create a class
IntegerBucketSort
under thesort
package. - Inherit the
AbstractBucketSort<Integer>
class. - Define a constructor.
-
@param min
the minimum integer (inclusive). -
@param max
the maximum integer (exclusive). - Call
super
with an appropriate bucket size givenmin
andmax
, andsort = false
. - Override the
getBucketIndex
method. -
@return
the index of the bucket thatkey
∈[min
,max
] should be inserted.
- Create a class
LSDRadixSort
under thesort
package. - Inherit the
AbstractBucketSort<Integer>
class. - Define a constructor.
-
@param maxDigit
the maximum number of digits. - Call
super
withbucketSize = 10
andsort = false
. - Override the
getBucketIndex
method. -
@return
the index of the bucket, wherekey
≤ 10maxDigit
.
- Create a class
ProbabilityBucketSort
under thesort
package. - Inherit the
AbstractBucketSort<Double>
class. - Define a constructor.
- Call
super
with an appropriate bucket size given the input range of [0, 1), andsort = true
. - Override the
getBucketIndex
method. -
@return
the index of the bucket thatkey
∈[0, 1) should be inserted. - Create a class
ProbabilityBucketSortTest
under thesort
package, and define a methodtest
to evaluate the accuracy of your algorithm using 10 different lists of 100 random double values between 0 and 1. - See
SortTest#testAccuracy
andRandom#nextDouble
.
- Create a package
cs323.tree
. - Download the
AbstractBinaryNode
class and put it under thetree
package. - Create a class
BinaryNode
under thetree
package. - Inherit the
AbstractBinaryNode
class. - Make a generic type
T extends
Comparable<T>
. - Define a constructor.
-
@param key
the key of this node.
- Download the
AbstractBinarySearchTree
class and put it under thetree
package. - Create a class
BinarySearchTree
under thetree
package. - Inherit the
AbstractBinarySearchTree
class. - Make a generic type
T extends
Comparable<T>
. - Override the
createNode
method.-
@param key
the key of this node. -
@return
aBinaryNode
with the specific key.
-
- Create a package
cs323.tree.balanced
. - Create a class
AbstractBalancedBinarySearchTree
under thebalanced
package. - Declare an abstract method
balance
. -
@param node
the node to be balanced. - Override the
add
andremove
methods. -
@param key
the key of the node to be added and removed. - Call the
balance
method to keep the tree in balance. - Define methods
rotateLeft
androtateRight
. -
@param node
the node to be rotated. - Rotate the specific node to left and right.
- Create a class
AVLNode
under thebalanced
package. - Inherit the
AbstractBinaryNode
class and setAVLNode
as the node type. - Make a generic type
T extends
Comparable<T>
. -
int i_height
the height of this node. - Create a constructor.
-
@param key
the key of this node. - Intialize
i_height
to1
. - Define a method
getBalanceFactor
. -
@return
height(left-subtree) - height(right-subtree). - Define a method
resetHeights
. - Reset the heights of this node and its ancestors, recursively.
- Override the
setLeftChild
andsetRightChild
methods. -
@param node
the child node. - Call the
resetHeights
method to update the heights.
- Create a class
AVLTree
under thebalanced
package. - Inherit the
AbstractBalancedBinarySearchTree
class and setAVLNode
as the node type. - Make a generic type
T extends
Comparable<T>
. - Override the
createNode
method.-
@return
aAVLNode
with the specific key.
-
- Override the
rotateLeft
androtateRight
methods. -
@param node
the node to be rotated. - Call the
node.resetHeights
method to update the heights. - Override the
balance
method. -
@param node
the node to be balanced. - Read AVL tree.
- Create a class
RedBlackNode
under thebalanced
package. - Inherit
AbstractBinaryNode
and setRedBlackNode
as the node type. - Make a generic type
T extends
Comparable<T>
. -
boolean b_red
true
if this node is red; otherwise,false
. - Create a constructor.
-
@param key
the key of this node. - Intialize
b_red
totrue
. - Define methods
isRed
andisBlack
. -
@return
true
if this node is red and black. - Define methods
setToRed
andsetToBlack
. - Set this node to red and black.
- Create a class
RedBlackTree
under thebalanced
package. - Inherit
AbstractBalancedBinarySearchTree
and setRedBlackNode
as the node type. - Make a generic type
T extends
Comparable<T>
. - Override the
createNode
method.-
@return
aRedBlackNode
with the specific key.
-
- Override the
balance
method. -
@param node
the node to be balanced. - Read Red-Black tree.
- Create a package
cs323.dynamic.fibonacci
. - Download the
AbstractFibonacci
class and put it underfibonacci
. - Create classes
RFibonacci
,LFibonacci
,DFibonacci
underfibonacci
. - Extend
AbstractFibonacci
. - Override the
get2p
method underRFibonacci
,LFibonacci
,DFibonacci
using recursion, dynamic programming, and a loop, respectively. -
@param k
must be greater than 1. -
@return
thek
'th Fibonacci number.
- Create a package
cs323.dynamic.hanoi
. - Download the
AbstractHanoi
class and put it underhanoi
. - Create classes
RHanoi
andDHanoi
underhanoi
. - These classes extend
AbstractHanoi
. - Override the
solve
method underRHanoi
andDHanoi
using recursion and dynamic programming, respectively. -
@param n
the total number of rings. -
@param source
the source tower. -
@param intermediate
the intermediate tower. -
@param destination
the destination tower. -
@return
a list of steps to move all rings fromsource
todestination
.
- Create a package
cs323.dynamic.lcs
. - Download the
AbstractLCS
class and put it underlcs
. - Create classes
RLCS
andDLCS
underlcs
. - These classes extend
AbstractLCS
. - Override the
solve
method underRLCS
andDLCS
using recursion and dynamic programming, respectively. -
@param c
the first array of characters. -
@param d
the second array of characters. -
@param i
the index ofc
to be compared. -
@param j
the index ofd
to be compared. -
@return
a longest common sequence of the specific stringsc
andd
.
- Create a package
cs323.dynamic.knapsack
. - Download the
AbstractKnapsack
class and put it underlcs
. - Create classes
RKnapsack
andDKnapsack
underknapsack
. - These classes extend
AbstractKnapsack
. - Override the
solve
method underRKnapsack
andDKnapsack
using recursion and dynamic programming, respectively. -
@param items
items to be entered into the knapsack. -
@param maxWeight
the maximum weight that the knapsack can hold. -
@return
a list of items maximizing the total value givenitems
andmaxWeight
.
- For each k, how many times are the following methods called recursively?
-
RHanoi#solve(List<String> int, char, char, char)
. -
DHanoi#solve(List<String>, int, char, char, char, Map<String,int[]>)
. - Is there any clear pattern between k and the number of the method calls in each class? Explain the pattern if there is one.
- Create a package
cs323.graph
. - Download the
Graph
andEdge
classes and put it undergraph
. - Create a package
cs323.graph.span
. - Download the
SpanningTree
class and put it underspan
. - Download the
MSTAlgorithm
interface and put it underspan
.
- Create classes
MSTPrim
,MSTKruskal
, andMSTEdmonds
underspan
. - Inherit the
MSTAlgorithm
interface. - Override the
getMinimumSpanningTree
method in each class by implementing Prim's, Kruskal's, and Chu–Liu-Edmonds' algorithms, respectively. -
@param graph
a graph containing at least one spanning tree. -
@return
a minimum spanning tree.
- Pick a vertex.
- Put the picked vertex to a
set
and add all its incoming edges to apriority queue
using their weights. - Remove the minimum edge in the
queue
and check if theset
contains the source of this edge. - If the set contains the source, add the edge to a
tree
. - If all edges in the
tree
form a spanning tree, return thetree
. - Otherwise, go to #2 by picking the source of the edge.
- Go to #3 unless the
queue
is empty.
- Create a
forest
in which eachset
consists of each vertex in the graph. - Create a
priority queue
containing all edges in the graph. - Remove the minimum edge in the
queue
and check if the source and the target belong to the sameset
in theforest
. - If the source and the target do not belong to the same
set
, add the edge to atree
and merge the source and the target sets. - If all edges in the
tree
form a spanning tree, return thetree
. - Go to #3 unless the
queue
is empty.
- Initially, every vertex is considered a tree.
- For each tree, keep 1 incoming edge with the minimum weight.
- If there is no cycle, go to #5.
- If there is a cycle, merge trees with the cycle into one and update scores for all incoming edges to this tree, and goto #2.
- For each vertex in the tree, add the weight of its outgoing edge to its incoming edges not in the tree. Break all cycles by removing edges that cause multiple parents.
- Give a graph where Prim's and Kruskal's algorithms generate different kinds of minimum spanning trees. Explain how these trees are generated. If you cannot find an example, explain why these algorithms always generate the same minimum spanning tree given any graph.
- What is the worst-case complexity of Chu–Liu-Edmonds’ Algorithm when
V
is the number of vertices andE
is the number of edges in a graph? Explain how you come up with this complexity.
- Create a package
graph.flow
. - Download the
MaxFlow
andMFAlgorithm
classes. - Create a class
FordFulkerson
undergraph.flow
. - Inherit the
MFAlgorithm
interface. - Override the
getMaximumFlow
method underFordFulkerson
. -
@param graph
a graph. -
@param source
the source vertex. -
@param target
the target vertex. -
@return
the maximum flow from the source to the target vertices. - Find each augmenting path using depth-first search.
- Ford-Fulkerson.
- Prove the maximum flow is equal to the minimum cut capacity.
Copyright © 2014-2017 Emory University - All Rights Reserved.