cs-404-01, Sum15, Exam 4 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 1 of 6.

. (–1 point for name missing on this page)

INSTRUCTIONS: For this whole exam, you may NOT call any methods UNLESS you define them! And
only primitive JAVA types are allowed—no importing (e.g., no ArrayLists).
________________________________________________________________________________________

Problem 1 a (10 pts) Using 𝑣𝑣3 as your starting point, trace through Prim’s Algorithm to find a minimum
spanning tree for the following graph:

1 2 3 4 5
1 0 ∞ 72 50 90
2 ∞ 0 71 70 73
3 72 71 0 ∞ 77
4 50 70 ∞ 0 60
5 90 73 77 60 0

Start with vertex 𝑣𝑣3 in the set, so initially we have:


1 2 3 4 5
nearest
distance

Iteration #1: vnear = ________


1 2 3 4 5
nearest
distance

Iteration #2: vnear = ________


1 2 3 4 5
nearest
distance

Iteration #3: vnear = ________


1 2 3 4 5
nearest
distance

Iteration #4: vnear = ________


1 2 3 4 5
nearest
distance

Part b) (3 pts) Give the set of edges that comprise the minimum spanning tree you found:

______________________________________________________________

Part c) (2 pts) What is the cost of the minimum spanning tree? ______________________
CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 2 of 6.
. (–1 point for name missing on this page)

Problem 2 a (10 pts) Using vertex 𝑣𝑣8 as your source, initialize touch and length, and then show the first
three iterations of the main loop of Dijkstra's algorithm:

W 1 2 3 4 5 6 7 8
1 0 15 40 88 70 ∞ 100 33
2 150 0 ∞ 25 800 100 ∞ 15
3 ∞ 8 0 10 65 55 ∞ 20
4 80 13 100 0 90 50 ∞ 10
5 10 2 ∞ 88 0 ∞ 22 70
6 ∞ 5 60 8 ∞ 0 50 10
7 92 15 20 ∞ 10 ∞ 0 25
8 200 60 ∞ 300 ∞ ∞ ∞ 0

1 2 3 4 5 6 7 8
touch
length

Show the first three iterations:

vnear = ______
1st 1 2 3 4 5 6 7 8
touch
length

vnear = ______
2nd 1 2 3 4 5 6 7 8
touch
length

vnear = ______
3rd 1 2 3 4 5 6 7 8
touch
length

Hint for Parts (b) and (c): You should not need to do any further iterations of Dijkstra’s algorithm.

Part b (3 pts) Give a path of minimum cost from 𝑣𝑣8 to 𝑣𝑣1: __________________________________

Part c (2 pts) What is the minimum cost of a path from 𝑣𝑣8 to 𝑣𝑣1: _______________________
CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 3 of 6.
. (–1 point for name missing on this page)

Problem 3 (15 pts) Write an algorithm to determine which vertex has the outgoing edge of highest cost.
The input W contains all edges—there are no ∞ symbols. For example, if W is the directed, weighted
graph below, your algorithm would return the integer 2 since 𝑣𝑣2 has an outgoing edge of cost 100, which
is higher than all other edge weights.

int vertexWithLargestOutgoingEdge(int n, int [][] W) {

}
CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 4 of 6.
. (–1 point for name missing on this page)

Problem 4 (15 pts) Write an algorithm to determine if an undirected, weighted graph W has two distinct
edges with the same weight (cost)—if so, return true; if not, return false. The input W contains all edges—
there are no ∞ symbols. W is undirected, so edge (𝑖𝑖, 𝑗𝑗) is the same edge as (𝑗𝑗, 𝑖𝑖).

int hasTwoDistinctEdgesOfSameWeight(int n, int [][] W) {

}
CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 5 of 6.
. (–1 point for name missing on this page)

Problem 5 (20 pts) Write an algorithm that takes an an array of n real numbers (doubles), and determines
how many distinct numbers are in the array. For example, if the array were:

0 1 2 3 4 5 6 7 8 9 10
3.42 9.9 3.42 -59 9.9 9.9 -1.1 0.5 -59 3.42 0.5

the algorithm would return 5, as it contains exactly 5 distinct values: 3.42, 9.9, −59, −1.1, and 0.5.

int howManyDistinctValues(int n, double [] A) {

}
CS-404, 7/20/15. Exam 4. Name______________________________________________________ p. 6 of 6.
. (–1 point for name missing on this page)

Problem 6 (20 pts) Write an algorithm that determines how many distinct edges a given nearest array has.
For example, with n = 8, the array below on the left has 7 distinct edges, whereas the one on the right has
only 3 edges: {2, 7}, {4, 5}, and {7, 5}. Note: {5, 4} is the same as edge {4, 5}, and {8, 8} is not an edge.

1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
1 3 4 8 6 1 6 7 1 7 3 5 4 6 5 8

int howManyDistinctEdges(int n, int [] nearest) {

You might also like