CS 1501

Here are some sample problems for Exam 2. Note that these are by no means the only types of problems that will be on the exam, nor do they cover all of the material that will be on the exam (you should review everything from the entire term and study in detail everything from EXAM 2 MATERIAL STARTS HERE on the Online Syllabus). Try not to look at the solutions until after you have given each problem a good try.

________________________________________________________________________________

1) Consider the following array that depicts a Max-Heap (highest priority is largest value) implementation of a Priority Queue:

Index

1

2

3

4

5

6

7

8

9

10

Value

100

60

80

20

50

70

30

5

15

40

a) Show the resulting array after a single call of the remove() (deletemax) function.

b) State two algorithms that we have discussed in which a priority queue figures prominently. Explain its role in each of the algorithms.

________________________________________________________________________________

2)

I can represent a graph using either an adjacency list or an adjacency matrix. Explain what each is and when (if at all) each is preferable.

________________________________________________________________________________

3)

The text uses Priority First Search (PFS) for both the MST and Shortest Path algorithms (using an adjacency list). Explain the difference between the algorithms, and why PFS can be used for both.

_________________________________________________________________________

4)

In the Karp-Edmonds algorithms for finding network flow, once an augmenting path is found, the flow values of the edges are updated to reflect the additional flow. Write the C++ or Java code segment that does this update. Assume that the graph has V vertices, that flow[x][y] represents the current flow from vertex x to vertex y, that the val[] array contains the amount that the flow will be augmented, that the dad[] array contains the path from the sink back to the source, and that V is the sink vertex.


 

5)

 

Consider LZW compression and, in particular, the lzw.c implementation that we discussed in lecture and that you modified in Assignment 5.  Consider the parallel arrays shown below that indicate a portion of the current dictionary of (string, codeword) pairs used during the compression process. Recall that this representation is used to save space.  The index values (determined by hashing) are not relevant here so are not shown.  Note: The ASCII value for the letter "T" is 84.

 

code_value

prefix_code

append_character

 

 

 

350

320

T

 

 

 

270

84

H

 

 

 

 

 

 

380

291

Y

 

 

 

291

270

E

 

 

 

 

 

 

340

291

N

 

 

 

320

270

A

 

 

 

 

List all of the (string, codeword) pairs that are shown in the portion of the dictionary above (excluding all single character strings).  Show your work for partial credit.

 


6) Consider a file containing the following text data:

AAABBBAAB

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

 

               LONGEST

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

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


7) Write the code for BFS using an adjacency list representation of the graph.  Use the partial code below as a starting point.

void search()

  {

    int k;

    for (k = 1; k <= V; k++) val[k] = unseen;

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

      if (val[k] == unseen) visit(k);

  }

 

Queue queue(maxV);

void visit(int k) // BFS, adjacency lists

  {

 

 

 

 

 

 

 

 

 

  }


8)

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

 

 

 

 

 

 

 

 

 

 

 


Using the Priority First Search implementation of the Ford-Fulkerson algorithm, show EACH AUGMENTING PATH generated, the amount of flow for each path, and the Maximum Flow for the graph.


9)

Fill in the blank

a) The N in NP-complete stands for _____________________.

b) If an NP-complete problem can be solved in deterministic polynomial time, it means that ___________________.

c) The Ford-Fulkerson technique to finding maximum flow involves improving a previous flow value by finding a(n) _____________________________ to the flow.

d) The simple exhaustive search Traveling Salesman Problem algorithm must consider all _____________________ of the vertices in the graph, since each represents a different possible tour.

e) Given a TSP tour ABCDEA, the 2-opt heuristic will "swap" edges (A, B) and (C, D) by actually removing (A, B) and (C, D) from the tour and replacing them with _______________ and __________________.