Note:  Below is the CS 1501 Final Exam from the Spring 2004 Term.  Your exam will cover much of the same material, but perhaps not all.  For details on the exact material for your (Summer 2005) exam, see the Online Syllabus.  I recommend trying this exam in a real, timed environment (allow 105 minutes).  Only after trying it should you look at the solutions.  Write down any questions you have and either email me or make an appointment to see me about them.

 

CS 1501 Algorithm Implementation

Spring 2004

Exam 2

 

 

Problem

Possible Pts

Pts Received

1

20

 

2

10

 

3

12

 

4

8

 

5

16

 

6

8

 

7

8

 

8

8

 

9

10

 

Total

100

 

 


For all questions, be sure to show your work.  Answers without work will not receive full credit.

1)      (20 points – 2 points each) Fill in the Blanks. Complete the statements with the MOST APPROPRIATE word(s) and/or phrase(s).

a)      For a variable-length codeword compression scheme such as Huffman compression to be valid, it must satisfy the prefix property, which means __________________________________________________.

b)      An important difference between BFS and PFS is how the fringe is implemented.  In BFS, the fringe is a(n) ____________________________ and in PFS the fringe is a(n) _____________________________

c)      DFS on a graph with V vertices and E edges runs in time ___________________________ with an adjacency list and in time __________________________ with an adjacency matrix.

d)      A graph G is said to be biconnected if _____________________________________________________.

e)      The algorithm to find articulation points that we discussed in lecture was a modification of the _________________________________________________ algorithm.

f)       A sequence of N Inserts followed by N DeleteMins on a min-heap will have a total run-time of Theta(_____________________________).

g)      One rule for a valid flow in a network graph is: For All u in [V – {s,t}],  sum(flow on edges incident upon u) == 0.  This means ____________________________________________________________ __________________________________________________________________.

h)      Given edge (u, v) with capacity 75 and flow 50, and using PFS to find an augmenting path, the priority value for edge (u, v) would be _________________________________ and the priority value for edge (v, u) would be _________________________________.

i)        The triangle inequality as applied to TSP states that _________________________________________ ______________________________________________________.

j)        When considering a neighbor of a TSP tour for the 2-opt heuristic, given two edges in the original tour, (U, V) and (W, X), I remove them and replace them with ________________________________.

 

2)      (10 points – 2 points each) Indicate if each of the following statements is TRUE or FALSE.  For FALSE statements, INDICATE WHY THEY ARE FALSE.

 

a)      Huffman compression is an example of a lossy compression scheme.

b)      In LZW compression, once all available codewords have been "used" (i.e. placed into the dictionary), the input file from that point on will not be compressed.

c)      An advantage of an adjacency matrix representation of a graph is that all neighbors of a vertex can be found in time Theta(V) (where V is the number of vertices in the graph).

d)      NP-Complete problems are problems that are known to require exponential run-times.

e)      If I discover a true polynomial algorithm that solves the Traveling Salesman Problem, I will have proved that P = NP.


3)      (12 points – 6 + 6) Consider the Huffman and LZW compression algorithms that we discussed in lecture. Consider a file with 1G (230) letter As in it (no other characters, except for the end of file character, which we will ignore, so its original size is 1GB).  Also consider each of the 5 compression ratios: 1/2, 1/8, 1/32, 1/128, 1/k where k > 128.

a)      Which of the compression ratios best approximates the compression ratio achieved by Huffman compression of the file?  Thoroughly justify your answer.  The correct answer without justification will get minimal credit.

 

 

 

 

 

 

 

 

 

 

 

b)      Which of the compression ratios best approximates the compression ratio achieved by LZW compression of the file?  Assume a fixed codeword size of 16 bits is used.  Thoroughly justify your answer.  The correct answer without justification will get minimal credit.

 

 

 

 

 

 

 

 

 

 

 

 

4)      (8 points) Consider a file containing the following ASCII string:

ABACABABA

Trace the LZW encoding process for the file (in the same way done in handout lzw.txt).  Assume that the extended ASCII set will use codewords 0-255.  For each step in the decoding, be sure to show all of the information indicated below, in the form shown in handout lzw.txt.  Note: The ASCII value for 'A' is 65.

 

STEP     PREFIX MATCH      CODEWORD OUTPUT   (STRING, CODE) ADDED TO DICTIONARY

----     ------------      ---------------   ----------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5)      (16 points – 8 + 8)  An array representation of a min-heap data structure of integers is shown below. 

a)      Draw the resulting array after the operation  DeleteMin(). Show your work.

1

2

3

4

5

6

7

8

9

10

11

15

20

30

25

50

40

35

60

45

90

80

 

 

 

 

 

 

 

 

 

 

b)      Write Java code (or C++) to do the Insert() method for this heap.  Assume the following instance variables:

data: the heap itself, which is an array of integers

size: an int indicating how many integers are currently in the heap (assume size is much less than the actual array size, so an insert will not cause an out of bounds exception)

Use the header below:

public void Insert(int newKey)

 

 

 

 

 

 

 

 

 

 

 


6)      (8 points)  Consider the graph below with the edge weights shown.  Assume the vertices are stored in alphabetical order, using an adjacency matrix.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


                                                                                            

 

Complete the table below, as it would look after a Minimum Spanning Tree (starting from vertex A) were created for the graph, assuming Prim's Algorithm is used.  val[] is the edge weight associated with the vertex, and dad[] is the parent vertex in the MST.  Show your work above or in the space below the table for partial credit.

 

 

A

B

C

D

E

F

G

H

I

val

 

 

 

 

 

 

 

 

 

dad

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7)      (8 points)  Consider the PFS algorithm discussed in lecture, implemented using a) an adjacency list and b) using an adjacency matrix.  When, if ever, would each implementation be preferred?   Thoroughly justify your answers by giving the Theta run-times of each version in different relevant situations.


8)      (8 points) Consider the weighted graph below. The numbers are the edge capacities. S is the source vertex and T is the sink vertex.

 

 

 

 

 

 

 

 

 

 

 

 


Using the BREADTH FIRST SEARCH implementation of the Ford-Fulkerson algorithm, show EACH AUGMENTING PATH generated (in the correct order that the paths are generated), the amount of flow for each path, and the Maximum Flow for the graph.  Assume that an adjacency matrix implementation is used, and that the vertices are listed in the adjacency matrix in the following order: S, A, B, C, D, E, T.   To ensure partial credit, be sure to SHOW YOUR WORK.

 

 

 

 

9)      (10 points) Consider an unweighted graph that may or may not be connected.  Your goal is the determine the number of connected components in the graph using the DFS algorithm for adjacency matrices.  For example, for the graph below your output would be:

The graph contains 3 connected components

Note that you should not have any other output.

 

 

 

 

 

 

 

 


Below is some (modified) code from the text with comments that you should use as a starting point.

 

Data that is already initialized for you to use:

a[][] – adjacency matrix for the graph

val[] – array to determine if a vertex has been visited or not

V – number of vertices

E – number of edges

 

void search()  // Main search function

{

    id = 0;    // Assume id is a global variable

    for (int k = 1; k <= V; k++) 

           val[k] = unseen;       // Initialize all vertices to unseen

 

 

 

 

 

 

 

 

 

 

 

 

}

 

int visit(int k)        // Recursive DFS

{                      

   

                  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}