c-----------------------------------------------------------
c Chapter 1: Next subset of an n-set(p19)
c-----------------------------------------------------------
c   Name of subroutine: LEXSUB
c
c   Algorithm:Generating subsets of {1,2,...,n} which
c             succeed input set, in lexicographical order, 
c             with optional jumps over supersets.     
c
c   input:    n=6
c   complier: f77 lexsub_2.f
c-----------------------------------------------------------

      parameter(n=6)
      logical jmp
      integer in(n) 
      ndim=3
      k=0
      jmp=.false.
    4 call lexsub(n,k,in,jmp,ndim)
      write(*,*), (in(i),i=1,k)        
      if (k.eq.ndim)jmp=.true.
      if (k.ne.ndim)jmp=.false.
      if (in(1).ne.n)goto 4
      stop
      end 

c-----Subroutine begins here-------------------------------   
      subroutine lexsub(n,k,in,jmp,ndim)
      dimension in(ndim)
      logical jmp
   10 if(k.ne.0)goto 40
   20 if(jmp)return
   30 is=0
  100 if(.not.jmp)k=k+1
  110 in(k)=is+1
      return
   40 if(in(k).eq.n)goto 50
   80 is=in(k)
   90 goto 100
   50 k=k-1
   60 if(k.eq.0)return
      is=in(k)
      goto 110
      end

