[BACK]Return to ts_subsets.adb CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / PHC / Ada / Schubert

File: [local] / OpenXM_contrib / PHC / Ada / Schubert / ts_subsets.adb (download)

Revision 1.1.1.1 (vendor branch), Sun Oct 29 17:45:32 2000 UTC (23 years, 6 months ago) by maekawa
Branch: PHC, MAIN
CVS Tags: v2, maekawa-ipv6, RELEASE_1_2_3, RELEASE_1_2_2_KNOPPIX_b, RELEASE_1_2_2_KNOPPIX, RELEASE_1_2_2, RELEASE_1_2_1, HEAD
Changes since 1.1: +0 -0 lines

Import the second public release of PHCpack.

OKed by Jan Verschelde.

with text_io,integer_io;                 use text_io,integer_io;
with Standard_Integer_Vectors;           use Standard_Integer_Vectors;
with Standard_Integer_Vectors_io;        use Standard_Integer_Vectors_io;

procedure ts_subsets is

-- DESCRIPTION :
--   Generates all subsets of k elements from a set of n elements.

  k,n : natural;

  function Complement ( n : natural; v : Vector ) return Vector is

  -- DESCRIPTION :
  --   Returns the complement of the vector w.r.t. the set 1..n.

    res : Vector(1..n-v'length);
    cnt : natural := 0;
    found : boolean;

  begin
    for i in 1..n loop
      found := false;
      for j in v'range loop
        if v(j) = i
         then found := true;
              exit;
        end if;
      end loop;
      if not found 
       then cnt := cnt + 1;
            res(cnt) := i;
      end if;
    end loop;
    return res;
  end Complement;

  procedure Enumerate ( start,i,n : in natural; accu : in out Vector ) is

  -- DESCRIPTION :
  --   Enumerates all subsets of 1..n, of size accu'length, starting to
  --   fill up accu(i) with entries in start..n.

  begin
    if i > accu'last
     then put("Subset : "); put(accu);
          put("  Complement : "); put(Complement(n,accu)); new_line;
     else for l in start..n loop
            accu(i) := l;
            Enumerate(l+1,i+1,n,accu);
          end loop;
    end if;
  end Enumerate;

begin
  put("Give the cardinality of whole set : "); get(n);
  put("Give the cardinality of subset : "); get(k);
  declare
    acc : Vector(1..k);
  begin
    Enumerate(1,1,n,acc);
  end;
end ts_subsets;