CPSC448: Final exam preparation questionsThese are the questions that did not make it to the final exam. QuestionsQuestion 1You have an 8x8-cell chessboard and a chess knight on cell S on the board. There are other pieces on the board; they do not move. What is the shortest number of moves the knight will need to get to cell T? Two pieces can not share a cell. The knight always moves in the following way:
Each of the knight's moves is a jump - other pieces never obstruct its motion unless the piece is occupying the cell to which the knight is jumping. Write at algorithm that will determine the smallest number of jumps required or will print "Impossible". Question 2Write an algorithm that, given a graph G, will print ALL the possible minimum spanning trees of G. It does not need to be efficient. Question 3Write an algorithm that, given an undirected graph G, will determine whether G is a tree. Question 4[Deleted] Question 5Given a collection of N primes and a number M, what is the largest integer smaller than M that can be constructed as a product of some of the N primes, if each prime can be used at most once? at most twice? You can assume M is at most a million. Question 6Given a string of decimal digits, determine the smallest base in which this string represents a perfect cube. If no base smaller than 100 works, print "Impossible". Question 7Given a black-and-white picture (as a table of pixels), count the number of black spots in it. A black spot is a collection of black pixels such that from any pixel in the spot there is a black path to any other pixel in the spot. The path can not go diagonally. Question 8Given a directed, acyclic graph, print, for each pair of vertices, the number of different paths between them. The algorithm has to be O(n^4) in the number of vertices. Question 9Rate these problems in the order of computational difficulty.
Question 10Write a RECURSIVE routine that, given a word, will print all of it's letter-permutations alphabetically, for example, given "loon", it will print lnoo lono loon nloo nolo nool olno olon onlo onol ooln oonl Question 11Write an algorithm that, given a set of integers, will find the subset with the largest even total sum. Some (or all) of the given integers can be negative. Scroll down to see the answersAnswersQuestion 1The question is asking for a shortest path of some sort. If we consider each cell as a vertex, then the edges will be the possible knight moves. For example, the cell (2,3) would have an edge to (and from) the cell (4,4), assuming that there are no pieces occupying either cell. This graph is unweighted, so the most natural algorithm to use is BFS. Having realized this, the graph itself is not necessary anymore; we can just use the board directly. S is the source, and is the first cell/vertex added to the BFS queue. At each step, at most 8 more vertices will be added to the queue. BFS will stop when T is found or when the queue is empty (no path to T was found). Here is the algorithm.
Question 2The first question to ask yourself is, "How can a graph have more than one minimum spanning tree?" Think of Kruskal's algorithm. If there are n vertices, it will pick the n-1 smallest edges and call that the minimum spanning tree. That means that if G's edges all have different weights, there is only one minimum spanning tree. The only place where there is any ambiguity is when the algorithm is picking its next edge. If there are two edges with the same weight, Kruskal's algorithm does not specify which one should be picked - either one will do the job. [The rest of solution deleted. It was wrong. This question is too hard.]Question 3By definition, a tree is a connected acyclic graph, so there are two things to check - that G is connected and that G is acyclic. Both can be verified simultaneously by a run of DFS. The graph is connected if and only if DFS reaches all the vertices. The graph is acyclic if and only if DFS does not find any back edges. To detect back edges, we need to use three colours for each vertex - white vertices have not been reached yet, gray vertices are being processed (are on the DFS stack now), and black vertices have been dealt with (all of their neighbours have been visited). Because G is undirected, each edge corresponds to two entries in the adjacency matrix. dfs() has to make sure it does not call itself recursively on the same edge is has just traversed. This is the purpose of the 'pred' variable in the code below. Here is the algorithm in detail.
Question 5If you replace multiplication with addition and "primes" with "numbers", this is exactly the coins/partition problem we discussed in class. Create a bool table of size M. Set all of its entries to false. Set entry number 1 to true. Take the primes one at a time and for each one update the table right-to-left possibly setting some entries true.
To deal with the case when each prime can be used twice, simply repeat the inner 'for' loop twice. Question 6This is almost precisely problem Y from assignment 7 (Squares). The idea is to try all bases from 2 to 100, compute the number represented by the digits in that base and check whether it is a cube. One caveat to watch out for is that no number in base b can have a digit that is larger than b-1 See the solution to problem Y on how to use Horner's rule to evaluate the numbers. Checking whether n is a cube can be done like this.
Problem 7This is much like problem J from assignment 3 (The Seasonal War). Look throught the image; when you find a black pixel, use DFS to colour it (and its neighbours) white and count this as one black spot.
Question 8One (not the most efficient) way of doing this is to notice that "the number of paths" could be viewed as "the number of paths of length 0", plus "the number of paths of length 1", plus "the number of paths of length 2", etc. Since the graph is acyclic, the longest path will have length "number of vertices minus one". The number of paths of length k between u and v is precicely the [u][v]'th entry of the k'th power of the graph's adjacency matrix. Start with the identity matrix (which gives all the paths of length zero). Multiply it by G's adjacency matrix n-1 times (n is the number of vertices in G), and add up the [u][v]'th entries in all of these matrix powers. This will give the total number of paths between u and v in G.
Since matrix multiplication is O(n3), this algorithm is O(n4). With a bit of linear algebra knowledge, we could notice that since we only care about the u'th row of the matrices, we can just keep that row and multiply it by the adjacency matrix. This will improve the algorithm's performance to O(n3). Question 9
Question 10The problem is to generate all permutations of the letters while avoiding duplicates. Here is an algorithm that almost works.
This does generate all the permutations and it does avoid duplicate words, but it does not print them alphabetically. (try running it on the string "abcd"). To ensure the strings are printed alphabetically, some extra swapping will be required, and the code starts getting trickier. This is why this question did not make it to the final exam. ;-) Question 11There is a greedy algorithm that will do the job. We will build the set incrementally. First, add all the positive even numbers because they can never make the sum odd. Next, add all the positive odd numbers in pairs because a pair of odd numbers adds up to an even number. What if there are an odd number of positive odd numbers? We'll have to discard one. It makes sense to discard the smallest one because it is contributing the least to the total sum. This is it. We have the answer. In practice, it is easier to go from the other side. Take all the positive numbers. If there is an odd number of odd ones in there, throw away the smallest odd one and return the rest. This is what the algorithm below does.
|