Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Since we didnt have enough time to properly discuss about problems 34.41 and 34.58, I will
upload brief explanations for these problems.
34.41
It might be difficult to even understand the problem without carefully reading the proof of
Theorem 34.9 of t
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #12
You will not need to submit homework next week. Keep up the good work with your
programming assignment and prepare yourself for the finals!
Problems[34.41, 34.45, 34.51, 34.58] will be discussed in the class.
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #8 (due 11/10 23:59PM)
Submit your answers for the selected problems [22.24, 24.36, 24.37]
Problems[22.26, 22.28, 24.33, 24.35] will be discussed in the class.
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #11 (due 12/11 11:59 PM)
Submit your answers for the selected problems [2, 4]
Problems [1, 3, 5, 6] will be discussed in the class.
Problem1
Problem2
Problem3
Suppose that someone gives you a polynomialtime algorithm to decide formula sati
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #10 (Due: 12/1 11:59 PM)
Submit your answers for the selected problems : [2, 3]
Problems [1, 4, 5, 6] will be discussed in the class
1.
Minimum negative cycle
For given graph G, give an efficient algorithm to find the length(number of edges
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #7 (due 11/03 11:59 PM)
Submit your answers for the selected problems [2,22.17]
Problems [1,3,4,22.15] will be discussed in the class.
1.
Suppose that we have a set of activities to schedule among a large number of lecture halls,
where an
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #9 (Due: 11/17 11:59PM)
Submit your answers for the selected problems: [246]
Problems [1, 2, 24.16, 243] will be discussed in the class.
1.
A mixed graph is graph in which some of the edges are directed and some of the edges are
undirect
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #3 (due 9/29 23:59PM)
Submit your answers for the selected problems [7.14, 7.22, 7.32]
Problems[7.25, 7.44, 7.46] will be discussed in the class.
n
Note: E[T(n)] = E[T(p)] + E[T(n p)] + (n) 2E [T ( )] + (n)
2
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #4 (Due: 10/6 11:59PM)
Submit your answers for the selected problems: 6.26, 8.24, 8.44
Problems [8.13, 8.34, 83] will be discussed in the class.
6.26. Show that the worstcase running time of MAXHEAPIFY on a heap of size n is (lg ).
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #2 (due 9/22 12:55 PM)
Submit your answers for the selected problems [1,2,4]
Problems [3,5,6] will be discussed in the class.
1.
Use the divideandconquer approach to write an algorithm that finds the largest item in a
list of items. Analy
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
2014 FALL CS300
HW1 Solutions
2.13 linear search
pseudocode example:
Linear( A, n, v )
1
index < NIL
2
i < 1
3
while ( i <= n ) do
4
if ( A[i] = v ) then
5
index < i
6
end if
7
i < i+1
8
end while
9
return index
loop invariant: if A[1,i1] has v, the
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #8 Solutions
22.24
The correctness proof for the BFS algorithm shows that d[u] = (s, u), and the algorithm doesnt
assume that the adjacency lists are in any particular order. Therefore, d[u] is independent of the
order in which the vertice
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Solutions of CS300 Homework #9
1.
A mixed graph is graph in which some of the edges are directed and some of the edges are
undirected. If a given mixed graph G has no directed cycle, then it is always possible to orient
the remaining undirected edges so t
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #4 Solution
6.26.
Consider a heap of n nodes where the root node has been changed to contain the smallest
value of all the nodes. Now when we call MAXHEAPIFY on the root, the value will have to
be exchanged down with its child at every le
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Homework #10 Solution
1.
Minimum negative cycle
Let D[i][ j][k] be shortest path from vertex i to vertex j with k edges at most. Perform this algorithm :
(initially every elements in D is INF)
For k 1 to V:
For every edges E(u,v):
D[w][v][k] = D[w][v][k
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Homework 6 solution
15.52 (skip)
Log cutting
Let the left end point be L0 , and Lk+1 = . Then we build a table M[0k+1, 0k+1] of size O(k 2 ),
which of each element M[i, j] contains the minimum cost of cutting the sublog, starting from Li
to Lj . Then,
M
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Homework#5 Solution!
9.11
We can compare the elements in a tournament fashion  we split them into pairs, compare each
pair and then proceed to compare the winners in the same fashion. We need to keep track of
each "match" the potential winners have part
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
Modify a line of Partition(A, p, r)
if A[y] x into if A[y] x
During the operation of Partition, values not greater then x are in the first partition, values greater
then x are in the second partition. Since all elements have the same value, which means no
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
1.
Use the divideandconquer approach to write an algorithm that finds the largest item in a
list of items. Analyze your algorithm, and show the results in order notation.
Answer) If you sort the list first, then you get ( ), which is worse than sequenti
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #5 (due 10/13 23:59PM)
Submit your answers for the selected problems [9.11, 9.12, 9.31]
Problems [9.12, 9.33, 9.38, 9.39] will be discussed in the class
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Fall 2014
CS300 Homework #1 (due 9/13 12:55 PM)
Submit your answers for the selected problems:
2.33, 34, 4.54
Problems [2.13, 2.36, 34, 4.43] will be discussed in the class.
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Spring 2010
Multiplication
Problem : multiply two nbit numbers.
ex) 41 42 (in binary, 101001 101010).
Add 41 to itself 42 times :
Multiplication
Problem : multiply two nbit numbers.
ex) 41 42 (in binary, 101001 101010).
Add 41 to itself 42 times :
(2n) additio
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Spring 2010
5. Heap Sort
1 2 7 4 3 6 8 1
level 0
9 5 2
1 1
level 1
1 0
level 2 level 3
Defn : A heap is said to be a complete binary tree with the property that the value of each node is at least as large as its child nodes( if any).
11
i
ki
( i, ki ) value, ki, i =
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Spring 2010
3. Greedy Algorithm
c 25 quarter c 10 dime 5 c 1 c
nickel
penny
How can you minimize the number of coins in returning c 63 ? = 2 quarters 13 = 1 dime = 3 pennies 3 0
6 coins This is the optimal solution !
11
Basic Strategy
Choose the option which is local
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Spring 2010
#include
#include
#include
#include
"LinkedListAndBST.h"
<stdlib.h>
<string.h>
<stdio.h>
int makeBST(BSTree *pBST)cfw_ /* fungsi makeBST dengan 2 parameter yaitu BSTree dan
pointer pBST */
pBST>head = NULL; /* inisialisasi nilai awal member head dengan N
Korea Advanced Institute of Science and Technology
Algorithms
CS 300

Spring 2010
/* Author : Wanda Ichsanul Isra
Date
: September 27, 2012
Program : Mengelompokkan kata dari file teks dengan struktur data BST dan
linked list */
#include
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<ctype.h> /* include ctype.h untuk penggun