Answers to CS302 Midterm Exam #2 - April 14, 2026

James S. Plank

The exam featured a lot of "bank" questions -- this answer guide is for the example exam, but has information about the other banks when appropriate.

Questions 1 and 2 are "what is your username" and "if you have comments, put them here." So we start with question 3.

Questions 3-12

In the answer, I have the reference material for where you find the answer explained. Grading: Each question was worth 2.5 points. If you gave me extraneous things in a Big-O equation, like O(V+E) for the DFS question, or O(V+ElogV) to the Dijkstra question, then you lost some fractional points.

Questions 13-15

Grading: 3 points per question. On the Insertion question, I gave 2.8 if you were within a few seconds of 324. Otherwise, I gave a point if your time was over 100. If it was linear, the time would be 84, so any answer around that value. received no credit.

On the quicksort question, I gave three points to any answer between 85 and 110. If you gave an answer between 80 and 84, you received one point, and if you answered greater than 110, you got a point.

On the DFS question, you got full credit for 64 seconds. I gave 0.5 points for values less than 10; 1 point for values between 10 and 19; 1.5 points for values between 20 and 30. 2 points for 32, and 1.5 points for 256.

Question 16

Grading: All questions but the pivot questions were worth three points. The pivot questions were worth two points. There was partial credit given.


Question 17

In the grading file, I keyed your exam with the adjacency list for node B. Here are the keys and answers:

  B{A,C,F,J}  |       B{H}     |      B{F}     |      B{J}      |   B{F,H,J}
      2       |        2       |       2       |       2        |      2
      4       |        4       |       4       |       4        |      4
     BJ       |       HF       |      EJ       |      JI        |     HE
     CD       |       DC       |      JI       |      GD        |     BF

Grading: Each of the three problems was worth 5 points.


Question 18

Since I have given you the order of processing the multimap, you only need to update the distance vector at each step, you only really need to look into when a node's distance is first set, and when it changes. Let's start with the unset ones: C, E, F and G: The order is C-47, F-141, G-97, E-110. You can flip the order of F and G if you want.

Ok -- so the first setting of each node is:

   A   B   C   D   E   F   G   H   I   J
   0  92  47  79 110 141  97  45  99  66
The nodes whose final distances are different from the above are B-89, F121, I-86 and J-62. I said I didn't care about order, so you don't need to figure out when they were changed.

In your grading, I keyed off the adjacency list for node A (yes, I gave you an adjacency matrix, but you can turn the line for A into a list):

Key                               A{92,79,45,99,66} 
1st 4 Questions (order matters)   C-47,G-97,F-141,E-110  (You can flip G and F)
2nd 4 Questions (any order)       B-89,F-121,I-86,J-62

Key                               A{37,21,17,12,80} 
1st 4 Questions (order matters)   I-23,B-82,D-38,C-91 (You can flip B and D )
2nd 4 Questions (any order)       B-68,C-60,J-78,J-46

Key                               A{24,66,85,30}
1st 4 Questions (order matters)   E-75,I-68,D-47,H-62 (You can flip E and I)
2nd 4 Questions (any order)       G-32,I-45,H-55,E-33

Key                               A{8,10,2,86,70}
1st 4 Questions (order matters)   D-3,E-5,F-43,J-74 (You can flip D and E)
2nd 4 Questions (any order)       I-63,I-20,J-65,H-81

Key                               A{48,9,84,2}
1st 4 Questions (order matters)   J-78,D-31,H-46,C-51 (You can flip D and H)
2nd 4 Questions (any order)       J-43,G-79,G-52,B-12

For grading, the first four questions were worth 8 points, as were the second four questions.


Question 19

Straightforward. This has a main() attached so you can test it:

#include <string>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

void dfs(int node, vector <int> &am, int &visited)
{
  int i;

  if (visited & (1 << node)) return;
  visited |= (1 << node);
  printf("%d\n", node);
  for (i = 0; i < am.size(); i++) {
    if (am[node] & (1 << i)) dfs(i, am, visited);
  }
}

int main()
{
  vector <int> am;
  int f, t, i, v;

  while (cin >> f >> t) {
    if (f >= am.size()) am.resize(f+1, 0);
    if (t >= am.size()) am.resize(t+1, 0);
    am[f] |= (1 << t);
    am[t] |= (1 << f);
  }

  for (i = 0; i < am.size(); i++) printf("AM[%d]: 0x%x\n", i, am[i]);

  v = 0;
  dfs(0, am, v);
  return 0;
}

Here we test it on the following graph:

  0 ----------> 1 --------> 3  
  |             |
  |             |
  |             |
  v             v
  2 --> 5 ----> 4

UNIX> ( echo 0 1 ; echo 0 2 ; echo 1 3 ; echo 1 4 ; echo 2 5 ; echo 5 4 ) | a.out
AM[0]: 0x6
AM[1]: 0x18
AM[2]: 0x20
AM[3]: 0x0
AM[4]: 0x0
AM[5]: 0x10
0
1
3
4
2
5
UNIX>
Grading: This was 11 points. Typically you started with 11 points and were deducted for things like: Some of you just wrote a DFS without any bit arithmetic. If it was correct, it was worth 5 points. If you skipped checking adjacency, then 4 points.