CS61B Summer 2000, Final Exam TA Submitted Review Problems This will be updated as TA's submit their questions. 1) The n-queens problem asks how n queens can be placed on an n x n chessboard so that no two queens can attack one another. (Queens can move horizontally, vertically, and diagonally.) Design a solution to the n-queens problem. Think depth first search and consider using a stack. This is the function prototype. int [][] nqueens(int n) The 2-d int array you return should be n x n, and it should contain 1 if the location of a queen, and 0 where there is no piece. 2) Devise an algorithm to find the second shortest spanning tree in a connected weighted graph. 3) Write a function in C that, given a pointer to a null terminated sequence of characters, determines whether the {,[,and ( in the sequence are appropriately matched. The function returns false if the delimiters are matched, and a pointer to the first offending delimiter otherwise. braceAndParensNotMatched( 4) Prove or disprove: A shortest paths set of edges in a graph G contains at least one edge in common with any Minimum Spanning Tree in G. 5) Implement a D-ary Heap class. class dheap{ ~ //Some number of variables. //Fill this in. public dheap(int somed){ public dheap(int somed, Comparable[] cosas){ public void toss(Comparable x){ public void insert(Comparable x){ public Comparable deleteMax(){ private void fixheap(){ private void percolateDown(int hole){ } 6) What does the follow C code do? int* foo (int x, int y) { int *z; z=(int *)malloc(x*sizeof(int)); for (int i=z; i /* Contains declaration of malloc. */ #include /* Declaration of printf. */ struct A { int x; double y; }; int main () { /* Each line is problematic. */ A structVariable; A structPointer *; structVariable->x = 13; &structPointer = malloc (sizeof (struct A)); *(structPointer.y) = 13.2; structPointer = *structVariable; free (structPointer); /* The return is fine. */ return 0; } 13. This problem is taken from "Algorithms in C++", written by Robert Sedgewick. Write a non-recursive quicksort algorithm using a stack. Assume that you will be sorting integers and that you can use the java Stack class. Also assume the existence of a partitioning function which takes an array of ints, an upper and lower bound, and returns the final index of the partitioned element. 14. Prove the correctness of Kruskal's algorithm. 15. In the lecture notes, Mike talked about using BFS to find the shortest path between nodes on an unweighted graph. However, we can in fact use BFS to find the shortest path between nodes on a WEIGHTED path. Explain how this can be done. How does Dijkstra's algorithm speed up your algorithm?