c-----------------------------------------------------------
c Chapter 25: Triangular Numbering in Partially Ordered 
c             Sets(p228)
c-----------------------------------------------------------
c   Name of subroutine: TRIANG
c
c   Algorithm:Discover consistent labeling of elements
c             of partially ordered set.
c
c   input:    
c   complier: f77 triang_2.f
c-----------------------------------------------------------

	parameter (n=10)
	integer zeta(n, n), sig(n)
	integer i,j

	write(6,*) "The input array is:"
	do 3 i=1,n
	read(5,*) (zeta(i,j),j=1,n)
3	continue

        write(6,20)
20      format('Enter sig numbers')
        read(5,*) (sig(i),i=1,n)
	call triang(n, zeta, sig)
	write(6,*) "The output zig is:"
		write(6,40) (sig(j), j=1,n)
40	format(10i3)
        
	end



c-----Subroutine begins here--------------------------------
      subroutine triang(n,zeta,sig)
      integer q,r,sig,t,zeta
      dimension sig(n), zeta(n,n)
10    m=0
      l=0
      do 11 i=1,n
11    sig(i)=0
20    m=m+1
30    if (sig(m).eq.0) go to 40
130   if (m.eq.n) return
      go to 20
40    t=m+1
50    r=t
60    if (r.gt.n) go to 100
70    if (sig(r).ne.0.or.zeta(r,m).eq.0) go to 90
80    sig(r)=m
      m=r
      go to 50
90    r=r+1
      go to 60
100   l=l+1
      q=sig(m)
      sig(m)=l
110   if (q.eq.0) go to 130
      r=m+1
120   m=q
      go to 60
      end



