0% found this document useful (0 votes)
13 views

Algo Projects

The document discusses three algorithm projects: 1) Converting between labeled trees and Prüfer codes, 2) Calculating water trapped in 1D and 2D elevation maps, and 3) Solving the n-queens puzzle by finding the number of solutions and printing all solutions.

Uploaded by

08131019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Algo Projects

The document discusses three algorithm projects: 1) Converting between labeled trees and Prüfer codes, 2) Calculating water trapped in 1D and 2D elevation maps, and 3) Solving the n-queens puzzle by finding the number of solutions and printing all solutions.

Uploaded by

08131019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Algorithms (1122)

Projects

May, 2024

1. (3 people) Cayley’s formula states that for every positive integer n, the
number of labeled trees on n vertices is nn−2 . In 1918, H. Prüfer uses an
algorithm that associates to any tree T a name P(T ) (called the Prüfer
code) that characterizes the tree. For the vertices of Kn , we take the
ordered set V = {1, 2, . . . , n}. Given a spanning tree T in Kn , we let
T1 = T and generate a sequence of trees T1 , T2 , . . . , Tn−1 as follows: Given
the tree Ti with n − i + 1 vertices, 1 ≤ i ≤ n − 1, let xi be the least leaf
of Ti , and delete xi and its incident edge {xi , yi } from Ti to obtain a tree
Ti+1 on n − i vertices. Then the Prüfer code is to be

P(T ) = (y1 , y2 , . . . , yn−2 ).

For example, for the tree T shown in the following

we have (x1 , y1 ) = (3, 2), (x2 , y2 ) = (4, 2), . . . , (x9 , y9 ) = (9, 10). Hence,
these edges are the columns of the matrix
 
3 4 2 5 6 7 1 8 9
.
2 2 1 1 7 1 10 10 10

So, P(T ) = (2, 2, 1, 1, 7, 1, 10, 10). Don’t include y9 = 10. Show that the
mapping P is a bijection. Then, write algorithms to convert a labeled tree
T into a Prüfer code P(T ), and vice versa.

1
2. (3 people)
(a) Given n non-negative integers representing an elevation map where
the width of each bar is 1, compute how much water it can trap after
raining. For example,

Input: height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]


Output: 6

(b) Given an m × n integer matrix representing the height of each unit


cell in a 2D elevation map, return the volume of water it can trap
after raining. For example,

Input: heightM ap = [[1, 4, 3, 1, 3, 2], [3, 2, 1, 3, 2, 4], [2, 3, 3, 2, 3, 1]]


Output: 4

2
3. (2 people) The n-queens puzzle is the problem of placing n queens on an
n × n chessboard such that no two queens attack each other.
(a) Given a positive integer n, return the number of distinct solutions to
the n-queens puzzle.

Input: n = 4
Output: 2

(b) Print all distinct solutions in any order. Let ”Q” and ”.” indicate a
queen and an empty space, respectively.
Input: n = 4
Output: [[”.Q..”, ”...Q”, ”Q...”, ”..Q.”], [”..Q.”, ”Q...”, ”...Q”, ”.Q..”]]

You might also like