Pascal Permutation Matrix: Find Number of Columns

In summary, the programmer attempted to find a more elegant solution to the homework problem and is unsure how to best check if a column is the given permutation. The code he wrote works, but there may be a more elegant solution.
  • #1
tawi
33
0

Homework Statement


I should write a program that determines how many columns of a given matrix are a permutation. The user will input some number N <=20 which will be the size of our matrix NxN. Then he will input the individual elements of the matrix by rows. I need to find out how many columns are the permuation of 1,...,N (column can only contain those numbers in random order).

Example:
Input:
3
1 2 2
3 1 1
2 3 1
Output:
2

Homework Equations

The Attempt at a Solution


I have written a code that works and should be usable. However I believe there should be more elegant solution to this.. Probably by using a function that will check each column of the matrix and determine if it is the desired permutation. If it is we will increase some variable "result" by one. I am not quite sure how to best check if the column is the given permutation.. probably by having some array prepared and always tick off the number we find in our column and if the numbers match the numbers 1,...,N we know it is a permutation? Anyway I am not quite sure if that is the best way to do it and I would appreciate your help. What do you think of the solution below and how can I actually write such a function? I have never really worked with functions and arrays.
Thanks.

Code:
program columnPerm;
var A:array[1..20,1..20] of integer;
var N,C,R,i,differ,count,total:integer;
var correct:boolean;

begin
   read(N);
   for R:=1 to N do
   begin
      for C:=1 to N do
      begin
         read(A[R,C]);
      end;
      readln;
   end;

   for C:=1 to N do
   begin
      count:=0;
      if A[1,C]< (N+1) then
         if A[1,C]>0
            then count:=(count+1);
               for R:=2 to N do
               begin
                  correct:=true;
                  differ:=0;
                  for i:=1 to (R-1) do
                  begin
                     if A[R,C] <> A[i,C] then
                        differ:=(differ+1);
                  end;

                  if differ <> (R-1) then correct:=false;
                     if (A[R,C] > N) or (A[R,C] < 1) then correct:=false;
                        if correct then count:=(count+1);
               end;

   if count=N then total:=(total+1);
   end;

write(total);

end.
 
Physics news on Phys.org
  • #2
If you start by saving your input matrix as a sorted (within columns) copy of itself, then you can do a 1 to 1 comparison of the columns, since you will have removed any permutations.

A second option might be to use the fact that your ideal column will have only one of each number. You could do something to isolate the maximum occurrence of any number in a column. If this is not 1, then it cannot be a permutation of 1...N.
 
  • #3
Is there an easy way to sort the columns? Can't think of any. I feel like if I wanted to do that the code would be just as long as the one I have already written.
 
Last edited:

FAQ: Pascal Permutation Matrix: Find Number of Columns

1. What is a Pascal Permutation Matrix?

A Pascal Permutation Matrix is a square matrix in which each row and column contains a permutation of the numbers 1 through n, where n is the size of the matrix. It is named after the mathematician Blaise Pascal.

2. How do you find the number of columns in a Pascal Permutation Matrix?

To find the number of columns in a Pascal Permutation Matrix, you can use the formula n!/(n-k)! where n is the size of the matrix and k is the number of rows. This formula is based on the number of possible permutations for k elements from a set of n elements.

3. How is a Pascal Permutation Matrix related to the Pascal's Triangle?

Pascal's Triangle is a triangular array of numbers that is used to calculate the coefficients of the binomial expansion. The values in a Pascal Permutation Matrix can be derived from Pascal's Triangle by selecting the appropriate row and column.

4. Can a Pascal Permutation Matrix have repeating numbers in a row or column?

No, a Pascal Permutation Matrix must contain a unique permutation of numbers in each row and column. This is a property of permutation matrices in general.

5. What are some applications of Pascal Permutation Matrices?

Pascal Permutation Matrices have various applications in mathematics, computer science, and engineering. They are used in combinatorics, coding theory, and cryptography. They can also be applied in data analysis and image processing.

Similar threads

Back
Top