      program bs
c 
c Program implementing Bak-Sneppen model
c
      parameter(l=1000,mtime=100000,idum=4711)
c
      dimension fitnes(l),ir(l),il(l)
c
c Heating up random number generator
c
      ibm=idum
      rinv=0.5/(2.**31-1.)
      do i=1,1000
      ibm=ibm*16807
      enddo
c
c Implementing periodic boundary conditions
c
      do i=1,l
      ir(i)=i+1
      il(i)=i-1
      enddo
      ir(l)=1
      il(1)=l
c
c Initializing fitness vector
c
      do i=1,l
      ibm=ibm*16807
      fitnes(i)=(ibm*rinv+0.5)
      enddo
c
c Loop over time
c 
      do itime=1,mtime
c
c finding smallest fitness factor
c
      is=0
      fs=2.0   
      do i=1,l
      if(fitnes(i).le.fs) then
      is=i
      fs=fitnes(i)
      endif
      enddo 
c
c Updating element with smallest fitness and neighbors
c
      ibm=ibm*16807
      fitnes(   is )=(ibm*rinv+0.5)
      ibm=ibm*16807
      fitnes(ir(is))=(ibm*rinv+0.5)
      ibm=ibm*16807
      fitnes(il(is))=(ibm*rinv+0.5)
c
c Writing position of least fit element
c
      write(*,*) is,itime
c
      enddo
c
      end

