CS 411 Fall 2025  >  Assignment 2 (A Problem, Part 1)


CS 411 Fall 2025
Assignment 2 (A Problem, Part 1)

Assignment 2 is due at 5 pm on Thursday, September 25. It is worth 85 points.

Procedures

This assignment is to be done individually.

Turn in your answer to the exercise below on the UA Canvas site, under Assignment 2 for this class.

Exercises (A only, 85 pts total)

Exercise A — Exhaustive Search

Purpose

In this exercise, you will write a package that finds an optimal solution to a problem via exhaustive search.

Background

A river flows from north to south. There are cities on the west bank and cities on the east bank. We wish to build bridges across the river. Each bridge will join a west-bank city with an east-bank city. We wish to choose the bridges we build in such a way as to maximize the tolls we collect from people crossing them.

There are two restrictions on the bridges we build. First, no city can have more than one bridge reaching it. Second, no two bridges can cross each other.

Concerning the second condition: the cities on each bank are numbered. There are w cities on the west bank, numbered 0, 1, … ,w−1 (starting at zero, because we are computer programmers). And there are e cities on the east bank, numbered 0, 1, … ,w−1. Suppose one bridge joins city a on the west to city b on the east. Another bridge joins city c on the west to city d on the east. If a < c and b > d, then these two bridges cross each other, and we are not allowed to build both.

For each pair of cities we might build a bridge between, we know the toll we will collect from it. Building a bridge never affects the toll collected from another bridge.

Examples

Example 1. Suppose w = 3 and e = 3. Each line below describes a bridge we might build; the three numbers are west-bank city, east-bank city, and toll, respectively.

  1. 0, 1, 3.
  2. 1, 1, 5.
  3. 1, 2, 4.
  4. 2, 0, 8.

Answer. We build bridge D, for a total toll of 8.

Note that bridge D crosses every other bridge, so we cannot build a second bridge if we build D.

We could build two bridges: A and C do not cross. But the total toll from that is 3 + 4 = 7, so D is a better option.

Example 2. Suppose w = 3 and e = 3. Each line below describes a bridge we might build, as before.

  1. 0, 1, 3.
  2. 1, 1, 5.
  3. 1, 2, 4.
  4. 2, 0, 8.
  5. 2, 2, 6.

Answer. We build bridges B and E, for a total toll of 11.

Data Storage

Each single data item (city index, toll) will be represented by an int value.

The description of a bridge will be stored as a vector<int> of size 3. The 3 items represent, in order:

So you might want to do something like this:

[C++]

using Bridge = vector<int>;

Instructions

Write a C++ function bridges, prototyped in the file bridges.hpp and implemented in the file bridges.cpp. Function bridges is given a list of possible bridges; it determines the maximum toll that can be collected.

Be sure to follow the Coding Standards. Function bridges is prototyped as follows (assuming the above using).

[C++]

int bridges(int w,
            int e,
            const vector<Bridge> & bridges);

Function bridges takes the number of cities on the west and east banks, respectively, and a vector of descriptions of bridges. It returns the maximum total toll that can be collected from a legal set of bridges.

Other requirements:

Test Program

A test program is available in the Git repository: bridges_test.cpp. If you compile and run this program (unmodified!) with your code, then it will test whether your code works properly.

Do not turn in the test program.

Notes