PAlocationmodel <- function(startconf = list(degree = c(1,1), location = c(1/3,2/3)), size = 100000, alpha = -3/4, locprofile = c(0,1,0)){
#Samples the Preferential attachment model with location-based choice
#Parameters:
#startconf - list of vertices of the initial graph G_0, containing their initial degree and their location in arrays.
# To ensure, that the initial graph is a tree, check the sufficient and necessary condition that the sum of the degrees is equal to 2*(number of vertices - 1)
#size - number of vertices added to the graph
#alpha - parameter controlling the importance of the degree in the preferential attachment mechanism.
#locprofile - probability vector which describes the connection mechanism to one of the selected vertices
#Generating location for all vertices
startnumber = length(startconf$degree);
startdegree = sum(startconf$degree);
vertices = list(degree = c(startconf$degree,rep(0,size)), location = c(startconf$location, runif(size)))
#Connecting new vertices to the model
for (i in 1:size){
#Selecting vertices due to Preferential Attachment
sumdegree = startdegree + alpha*startnumber + (2+alpha)*(i-1); #Calculating the normalizing constant of Psi. Assuming the startconfiguration has a tree structure: sumdegree = (2+alpha)*((i-1)+startnumber) - 2
sampleid = sample(1:((i-1)+startnumber), size = length(locprofile), replace = TRUE, prob = (vertices$degree[1:((i-1)+startnumber)] + alpha)/sumdegree )
samplevert = list(name = sampleid, degree = vertices$degree[sampleid], location = vertices$location[sampleid]);
#Order sample with respect to the location of the vertex
sortind = sort(samplevert$location, index.return = TRUE)
samplevert = list(name = samplevert$name[sortind$ix], degree = samplevert$degree[sortind$ix], location = samplevert$location[sortind$ix])
#Choose one vertex of the ordered sample according to the given probability vector and connect to this one
connectid = sample(samplevert$name, size = 1, prob = locprofile)
vertices$degree[i + startnumber] = 1;
vertices$degree[connectid] = vertices$degree[connectid] + 1;
if(i %% 100000 == 0){print(i)}
}
return(vertices)
}