Ticket #257: random_graph.f90

File random_graph.f90, 2.3 kB (added by mwlinnem@IU.EDU, 4 months ago)

corrected version of fortran code

Line 
1       program random_graph
2 !   
3 !     It builds a random graph a la Erdoes-Renyi: the probability
4 !     that there is an edge between any pair of vertices is p
5 !
6 !   * degree is the array which stores the degree of each node,
7 !     which is printed out at the end in the file "degree.dat"
8
9
10       implicit none
11       integer*8 ibm
12       integer i,j,k,n_edges,iter,n_vert,n_edges_true,denom,denom0,k0
13       real*8 r,p
14       character*60 sn_vert,sp,sibm
15       character*25,allocatable,dimension(:):: attrN
16      
17       character*(25) addquotes
18
19 !     Reading of input parameters
20      
21       call GETARG(2,sn_vert)
22       call GETARG(4,sp)
23       call GETARG(6,sibm)
24
25       read(sn_vert,*)n_vert
26       read(sp,*)p
27       read(sibm,*)ibm
28
29 !     Array allocations
30
31 !     Opening of the file where the edges will be saved
32
33       allocate(attrN(1:n_vert))
34
35       denom=1000000000
36      
37       do i=1,n_vert
38          write(attrN(i),*) i
39          attrN(i) = addquotes(attrN(i))
40       enddo
41
42       open(21,file='network.nwb',status='unknown')
43       write(21,108)'# Erdoes-Renyi graph'
44       write(21,110)'# Linking probability ',p
45       write(21,103)'*Nodes ',n_vert
46       write(21,120)'id*int      label*string'
47       do i=1,n_vert
48          write(21,*)i,attrN(i)
49       enddo
50       write(21,109)'*UndirectedEdges'
51       write(21,121)'source*int      target*int'
52
53 !     The average degree of the graph is given by the product of the
54 !     linking probability p and the number of nodes n_vert
55
56       do i=1,n_vert
57          do j=1,i-1
58             ibm=ibm*16807                                       !  Here we create a random number
59             r=0.25d0*(ibm/2147483648.0d0/2147483648.0d0+2.0d0)  !  r between 0 and 1
60             if(r<p)then
61                write(21,*)i,j
62             endif
63          enddo
64       enddo
65
66       close(21)
67          
68 101   format(i10,8x,i10)
69 102   format(a34,i10)
70 103   format(a6,i10)
71 104   format(i10,9x,e15.6)
72 105   format(4x,e15.6,4x,e15.6)
73 108   format(a21)
74 109   format(a16)
75 110   format(a22,e15.6)
76 120   format(a24)
77 121   format(a26)
78
79       stop
80     end program random_graph
81    
82     character*(25) function addquotes(value)
83                 character*(*) value
84                 character*22 longname
85                
86                 if(len(TRIM(ADJUSTL(value))) >= 23) then
87                         longname = ADJUSTL(value)
88                         addquotes = '"' // longname // '"'
89                 else
90                         addquotes = '"' // TRIM(ADJUSTL(value)) // '"'
91                 endif
92                 return
93         end