Name(s) CSE Login Programming Language(s) Used Question Points Score 1 10 2 10 3 20 4 5 5 5 6 10 7 20 8 120 Total: 200 Graders Notes:
Instructions Follow instructions carefully, failure to do so may result in points being deducted. Clearly label each problem and submit the answers in order. Be sure to show sufficient work to justify your answer(s). If you are asked to prove something, you must give a formal, rigorous, and complete proof You must adhere to the CSE academic integrity policy: http://cse.unl.edu/academic-integrity-policy You are encouraged to typeset your homework using L A TEX; if your answers are not legible, you may be required to use L A TEX in future assignments. We recommend that you use the following packages: Algorithm2e package for pseudocode Minted for typesetting code Tikz/PGF for graphs and figures Hand in your written answers hardcopy, properly stapled, with a copy of the first page as a cover sheet. Hand in all programs softcopy using the CSE webhandin and be sure that they properly execute in the webgrader. Failure to do so will mean that your program is not graded. Partner Policy You may work in groups of at most two students. This is optional and you may work alone if you wish. If you opt to work in pairs, you must follow these guidelines: 1. You must work on all problems together. You may not simply partition the work between you. 2. You should not discuss problems with other groups or individuals beyond general questions. 3. Hand in only one hard copy (and possibly soft copy) under the first author s name/cse login. Programming Instructions: you are highly encouraged to solve the programming problems using a high level programming language that you are not already familiar with (C++, PHP, Python, C# etc.); however, you may not use Java. Your program will be required to compile and run on the CSE server from the command line. Because we are allowing a variety of languages, you are required to also provide a script file named run.sh that contains the commands to compile and execute your program passing any command line arguments on to your program. Several examples have been provided. Page 2 of 7
You are required to hand in all source file, test cases, and other artifacts necessary to compile and run your program through webhandin. Moreover, you are required to rigorously test your programs locally and ensure that they work. Your program is required to work through the course webgrader as well. If your program fails to run, you may receive a zero. Your programs will be evaluated based on the following: 10 pts You must provide at least 2 non-trivial test cases to show that you properly tested your program (test cases each include a plaintext input file and output file with the formatting and naming specified for each problem) The remainder of the points will be awarded based on the correctness of your program. We have provided numerous test cases (if you find an error with any, please notify us and you will be awarded bonus points). You should use these test cases to debug and test your programs. The correctness of your program, will be evaluated using these test cases as well as your classmates test cases. Points will be awarded based on the proportion of test cases you successfully pass. If your test case(s) are found to be in error, you will lose points for your test cases. Page 3 of 7
1. 10 points For each of the following sequences of keys, insert them into an initially empty AVL tree. Show the configuration of the tree after each insert. (a) 1, 2, 3, 4, 5, 6 (b) 6, 1, 2, 3, 4, 5 (c) 3, 6, 5, 1, 2, 4 2. 10 points For each of the following sequences of keys, insert them into an initially empty 2-3 tree. Show the configuration of the tree after each insert. (a) 1, 2, 3, 4, 5, 6 (b) 6, 1, 2, 3, 4, 5 (c) 3, 6, 5, 1, 2, 4 3. 20 points Suppose you have an AVL tree with tree nodes such that each tree node, u has the following pieces of data: u leftchild u rightchild u parent u key u balancef actor Now suppose that you are given, as input, a tree node u. (a) Write pseudocode to determine what kind of rotation (if any) should be performed on u to rebalance it (b) Write pseudocode to perform a left-rotation about u (c) Write pseudocode to perform a right-rotation about u (d) Write pseudocode to perform a left-right-rotation about u (e) Write pseudocode to perform a right-left-rotation about u 4. 5 points Each iteration of Kruskal s algorithm considers adding an edge e = (u, v) to the current minimum spanning tree T by checking whether or not its inclusion in T would induce a cycle. Gomer thinks he s found a better way: rather than checking for a cycle, just check if the end points of e are already in T : if they are, then do not include the edge as they would not add any vertex to the minimum spanning tree. If either end point (u or v) is outside the current tree then do add the edge as it would connect the tree further. Show that Gomer is wrong by providing an example of a tree where using this criteria instead would fail. Briefly explain why Gomer s criteria is wrong. Page 4 of 7
5. 5 points Suppose we run an MST algorithm on an undirected graph G = (V, E) and it produces a full spanning tree, T = (V, E ). Now consider the subgraph G = (V, E \ E ) (that is, the graph with all the edges in the spanning tree are removed). Prove or disprove: G must be acyclic. 6. 10 points A cycle graph, C n is a graph on n 3 vertices where the only edges present form a single simple cycle. Develop an algorithm to determine whether or not a given graph G = (V, E) is a cycle graph or not. 7. 20 points The girth of an undirected graph G = (V, E) is the length of the shortest cycle in G. In this exercise you will develop an algorithm to compute the girth of a graph. (a) Develop an algorithm that, given a graph G = (V, E) and an edge e = (u, v) E determines the length of the shortest cycle that includes e in it. (b) Use the algorithm from part (a) to solve the girth problem. 8. 120 points Program You will implement a suite of graph algorithms including DFS, BFS, MST (either Prim s or Kruskal s) and a minimum weighted-distance finding algorithm (either Floyd-Warshall or Dijsktra s). In particular, you will write a program to parse an input file (file name should be read from the command line) with data representing a weighted undirected graph as follows. The first line is an integer n, the number of vertices in the graph. By convention, vertices will be identified with the integers 0 thru n 1 inclusive. Each line after that contains two integers and a decimal number delimited by spaces. The first two are the vertices (since it is undirected, nothing should be inferred from their order) and the last is a weight. An example: 5 0 2 3.0 0 3 2.1 0 4 4.3 1 2 3.14 1 3 2.5 2 3 3.9 2 4 10.3 Once processed your program will run several graph algorithms on the input graph and output statistics associated with it. In particular, your output should display: A DFS traversal (visited order of vertices) starting at vertex 0 according to the following strategy: the next vertex should be the least numbered unvisited vertex A BFS traversal (visited order of vertices) starting at vertex 0 according to the following strategy: the next vertex should be an unvisited vertex with least edge weight. Page 5 of 7
The edges (pairs of vertices) that make up a minimum spanning tree of the graph along with the total weight of the MST A complete list of vertex pairs along with the weight of the shortest path between them An example output: Depth First Search Traversal: 0, 2, 1, 3, 4 Breadth First Search Traversal: 0, 3, 2, 4, 1 Minimum Spanning Tree: V = [0, 1, 2, 3, 4] E = (0, 3, 2.1), (1, 3, 2.5), (0, 2, 3.0), (0, 4, 4.3), Total Weight: 11.90 Shortest Paths: 0 -> 1 = (0, 3, 2.1) -> (1, 3, 2.5) Path Weight = 4.6 0 -> 2 = (0, 2, 3.0) Path Weight = 3.0 0 -> 3 = (0, 3, 2.1) Path Weight = 2.1 0 -> 4 = (0, 4, 4.3) Path Weight = 4.3 1 -> 2 = (1, 2, 3.14) Path Weight = 3.14 1 -> 3 = (1, 3, 2.5) Path Weight = 2.5 1 -> 4 = (1, 3, 2.5) -> (0, 3, 2.1) -> (0, 4, 4.3) Path Weight = 8.899999999999999 2 -> 3 = (2, 3, 3.9) Path Weight = 3.9 2 -> 4 = (0, 2, 3.0) -> (0, 4, 4.3) Path Weight = 7.3 3 -> 4 = (0, 3, 2.1) -> (0, 4, 4.3) Path Weight = 6.4 You may use a library to represent graphs in your language of choice if you wish. You may use basic methods on a graph (such as getting the vertex set or testing if two Page 6 of 7
vertices are connected, etc.). However, you may not use any graph algorithms within that library that would make the assignment trivial. Name your test case files input001.txt output001.txt input002.txt output002.txt respectively. Page 7 of 7