0% found this document useful (0 votes)
85 views5 pages

Theory Hw4

The document is a homework assignment on algorithms and data structures. It contains 10 questions testing knowledge of sorting algorithms like pancake sort, graph traversals including BFS and DFS, minimum spanning trees using Kruskal's and Prim's algorithms, and dynamic programming. Students are asked to analyze time complexities, provide implementations, trace algorithm steps, and solve other algorithmic problems.

Uploaded by

Sami Almasagedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views5 pages

Theory Hw4

The document is a homework assignment on algorithms and data structures. It contains 10 questions testing knowledge of sorting algorithms like pancake sort, graph traversals including BFS and DFS, minimum spanning trees using Kruskal's and Prim's algorithms, and dynamic programming. Students are asked to analyze time complexities, provide implementations, trace algorithm steps, and solve other algorithmic problems.

Uploaded by

Sami Almasagedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

COMS W3134, Theory Homework 4

Name & UNI: _______________________________ Date: __________________

Point values are assigned for each question. Points earned: ____ / 100, = ____ %

1. Joe Pancake has just invented what he thinks is a great sorting algorithm. Consider the following
code:

/**
* Returns index of the maximum element in array[0..n-1].
*/
int find_max(int array[], const int length) {
int max_index, i;
for (max_index = 0, i = 0; i < length; ++i) {
if (array[i] > array[max_index]) {
max_index = i;
}
}
return max_index;
}

/**
* Reverses array[0..limit].
*/
void flip(int array[], int limit) {
int temp, start = 0;
while (start < limit) {
temp = array[start];
array[start] = array[limit];
array[limit] = temp;
start++;
limit--;
}
}

void pancake_sort(int array[], const int length) {


for (int curr_size = length; curr_size > 1; --curr_size) {
int max_index = find_max(array, curr_size);
if (max_index != curr_size-1) {
flip(array, max_index);
flip(array, curr_size-1);
}
}
}
COMS W3134, Theory Homework 4

Show the array [2, 4, 1, 5, 3] after the for loop in pancake_sort executes once.
(5 points, one for each element in the correct position)

What is the best-case complexity of the pancake_sort algorithm above including helper functions?
(1 point for symbol, 1 for function)

What is the worst-case complexity of the pancake_sort algorithm above including helper functions?
(1 point for symbol, 1 for function)

Which of the elementary sorting algorithms discussed in class is closest to (but not exactly the same
as) what Joe has written? Use the complexities to guide your response. Circle your answer. (1 point)

bubble / selection / insertion

2. You are given a collection of bolts of different widths and corresponding nuts. You are allowed to try
a nut and bolt together, from which you can determine whether the nut is larger than the bolt,
smaller than the bolt, or matches the bolt exactly. However, there is no way to compare two nuts
together or two bolts together. The problem is to match each bolt to its nut.
Note: You cannot get credit for parts b and c if your answer to part a is incorrect.
a) Design an algorithm for this problem with efficient average-case complexity faster than
quadratic run time. (6 points)

b) What is the average-case complexity of your algorithm? (1 point for symbol, 1 for function)

c) What algorithm did we study this semester that is most similar to the algorithm you’ve
designed? (2 points)

3. List the order in which the vertices are visited with a breadth-first search starting with vertex 1. If
there are multiple vertices adjacent to a given vertex, visit the adjacent vertex with the lowest value
first. (10 points)
COMS W3134, Theory Homework 4

4. Using the same graph as in the previous question, list the order in which the vertices are visited with
a depth-first search starting with vertex 1. If there are multiple vertices adjacent to a given vertex,
visit the adjacent vertex with the lowest value first. (10 points)

5. On undirected graphs, does either of the two traversals, DFS or BFS, always find a cycle faster (i.e. in
less operations) than the other? If yes, indicate which of them is better and explain why it is the
case; if not, draw two graphs supporting your answer and explain the graphs. (10 points)

6. Consider the following graph:

List the order in which the vertices are visited with a topological sort, as shown in class. Always
process the vertex with the lowest number first if several vertices have indegree 0. (10 points)
COMS W3134, Theory Homework 4

7. Consider the following graph:

Apply Kruskal’s algorithm to find the minimum spanning tree. Edges are sorted first by length, and in
the event of a tie, by name, where the two letters are in alphabetical order. Use makeset(x),
find(x), and union(x, y) to determine if there are cycles.

a) Circle the edges that are part of the minimum spanning tree. (4 points)
AC, AD, AE, BC, BD, BE, DE

b) What is the weight of the minimum spanning tree? (1 point)

c) Draw the tree that results from applying the union-find algorithm for cycle detection. When
drawing the tree, put vertices with lower letters in the left subtrees so that all the vertices in a
level are sorted alphabetically from left to right. (5 points)

8. Using the same graph as in the previous question, use Prim’s algorithm to find the minimum
spanning tree starting with vertex A. Fill in the table below, as shown in lecture. (10 points)

Tree vertices Remaining vertices

9. In a weighted graph, assume that the shortest path from a source 'S' to a destination 'T' is correctly
calculated using Dijkstra’s shortest path algorithm. If we increase weight of every edge by 1, does
the shortest path always remain the same? If so, prove it. If not, give a counterexample. (10 points)
COMS W3134, Theory Homework 4

10. Suppose you have coins in a row with the following monetary value in cents: 7 1 3 2 1 7 5
You want to take coins such that you maximize the value of collection subject to the constraint that
no two adjacent coins may be taken. Use dynamic programming to fill in the table. (7 points)

Index 0 1 2 3 4 5 6 7
C 0 7 1 3 2 1 7 5
F 0
S 0

What is the maximum amount of money you can take? (1 point)

What coins did you take? List the indices of the coins taken from lowest to highest, assuming that
first 7-cent coin is in index 1. (2 points)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy