CS 1501

Solutions to the practice questions for Exam 2

_____________________________________________________________________________

1) a)

Index

1

2

3

4

5

6

7

8

9

10

Value

80

60

70

20

50

40

30

5

15

-

Trace: The last leaf (40) was copied into location 1 and then downheap was performed.  In this process, 40 (index 1) was compared with its two children, 60 and 80 (index 2 and index 3).  The largest value (80) was swapped with 40.  This process was repeated from index 3, swapping 40 this time with 70 in location 6.  One more test saw no more change and the final array is as shown above.

b) We used a priority queue for the priority first search versions of minimum spanning tree, shortest path, and network flow. See the text and notes for details on these algorithms.

______________________________________________________________________________

2)

See notes and textbook for details on adjacency list and adjacency matrix. The adjacency matrix allows direct access to each edge, and is simple to use. However, it requires Theta(V2) memory and time minimum and is thus only effective for dense graphs (E ~= V2). The adjacency list only keeps nodes for edges which exist, and thus has Theta(E) memory and time minimum, so it is good for sparse (E ~= V) graphs. However, due to the overhead of dynamic memory and the length of the lists, it becomes less effective as E approaches Theta(V2), or in dense graphs.

_____________________________________________________________________________________

3)

Vertices are selected in both the MST and Shortest Path algorithms by smallest (highest) priority value. The only difference in the algorithms is the value used for priority. In the MST algorithm, for each vertex in the fringe, the priority is the smallest edge weight connecting that vertex to any vertex already in the tree. In the Shortest Path algorithm, for each vertex in the fringe, the priority is the smallest distance from the root to that vertex through any path in the tree. Thus, the MST algorithm considers only single edge weights for priority, which the Shortest Path algorithm may have the sum of many edges as a priority.

_____________________________________________________________________________________

4)

 
y = V; x = dad[V];
while (x != 0)
{
     flow[x][y] = flow[x][y] + val[V];
     flow[y][x] = -flow[x][y];
     y = x; x = dad[y];
}

_____________________________________________________________________________________

5)

Starting from the top of the table:

(THAT, 350)
(TH, 270)
(THEY, 380)
(THE, 291)
(THEN, 340)
(THA, 320)

Recall that to determine the strings, we must substitute back for the prefix_code until we get to a code value that is under 256 (meaning it is simply an ASCII code)


6) 

               LONGEST

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

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

  1            A                 65                  (AA, 256)

  2            AA                256                 (AAB, 257)

  3            B                 66                  (BB, 258)

  4            BB                258                 (BBA, 259)

  5            AAB               257                    --


7)

Queue queue(maxV);

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

  {

    struct node *t;

    queue.put(k);

    while (!queue.empty())

      {

        k = queue.get(); val[k] = ++id;

        for (t = adj[k]; t != z; t = t->next)

          if (val[t->v] == unseen)

            { queue.put(t->v); val[t->v] = -1; }

      }

  }


 8)

The paths are as follows:

SBCT – flow of 60
SACBT – flow of 30 (note that there is a back edge in this path – see notes from lecture for details)
SBT – flow of 20

MAXIMUM FLOW: 110

Recall that PFS for finding augmenting paths finds a maximum spanning tree until the sink is reached.  Therefore, the augmenting paths found are in order by highest augment to lowest augment.


9)

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

b) If an NP-complete problem can be solved in deterministic polynomial time, it means that __P = NP (or they all can)__.

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

d) The simple exhaustive search Traveling Salesman Problem algorithm must consider all ___permutations______ 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 ___(A, C)_____ and ____(B, D)________.