首页 > 编程语言 >R语言代做编程辅导ASSIGNMENT FOUR - RANDOM GRAPHS(附答案)

R语言代做编程辅导ASSIGNMENT FOUR - RANDOM GRAPHS(附答案)

时间:2023-07-11 16:05:18浏览次数:55  
标签:... matrix GRAPHS column ASSIGNMENT RANDOM number person edges

全文链接:https://tecdat.cn/?p=33183

PROBLEM 1) Creating Random Adjacency Matrices

Script Name: adjMatrix Input: n... The number of vertices in the graph p... Probablity two vertices are connected plot... whether or not the matrix should be plotted as a graph Output: The nxn matrix of zero and ones Error Checking: The dimension is postive (else return NULL)

Description: The matrix is related to a simple, undirected graph of n vertices. In the graph is Vertex i and Vectex j are joined by an edge, then in the matrix A[i,j] = 1, if no edge exists then A[i,j]=0. There are differenct ways of handling the diagonal. We will require the diagonal elements to be NA.

The matrix must be symmetric. That is A[i,j]=A[j,1]

Whether or not an entry in the matrix is 0 or 1, is determined by drawing numbers from the binomial distribution. This distribution will yield a 1 with probability p and a o with probability (1-p). In R it will work like this, in the ith column (vertex i) , we require A[i,i] to be NA. This leaves n-1 entries to be determined. We use the command rbinom(n-1,1,p) to general these entries all at once, or rbinom(1,1,p) to generate them one at a time.

Plot: Using igraph create a simple plot. Use a color easy on the eyes such as something found in the Brewer color palette (discussed in class)

 

requires(igraph)

adjMatrix<-function(n,p,plot=FALSE)
{m=matrix(1,n,n)
  for(i in 1:n){
    for(j in 1:n){
      if(i<j){
        
        m[i,j]=ifelse(i==j,NA,rbinom(1,1,p) )
        m[j,i]=m[i,j]
      }
    }
  }
  colpal<-brewer.pal(8,"Dark2")  
  
  gA <- graph.adjacency(m, mode="undirected",diag=FALSE )
  V(gA)$color<-colpal[1]   You choose 1 to 8... try some others
  plot(gA, layout=layout.auto,main="Random Graph using Brewer Color Palette")
  s
}

PROBLEM 2) Person with the most friends in the network

Script Name: vowelMax Input: A... An adjacency matrix plot... Plot the graph with the "friends" network highlighted Output: The number of the vertex with the most edge connections Error Checking: None

Description: The number of "friends" a person has in a network is the number of edges connected to that vertex. The ith person in the network, will be connected to the jth person if A[i,j]=1. So the number of 1's in the ith column reports how many edges connect person i to others In mathematical terms, this is called the "degree" of vertex i.

So you must determine the number of ones in each column and then find which column has the most ones.

Hint: There is a quick trick to use here. Since a column has only 0 or 1 in it, the sum of all of the numbers in the column is the same as the number of 1's in the column. Prove that to yourself.

Plot: Use igraph to plot the graph. Use three colors for the vertices: one for the most friendly person, another for his/her immediate friends, and another for the remaining vertices. Color red the edges from the most friendly person to his/her friends. All remaining edges are black.

 
mostFriends<-function(A,plot=FALSE)
{
  A[is.na(A)]=0
  s=which(apply(A,2,sum)==max(apply(A,2,sum)))
  n=which(A[,s]==1)
  pastel<-brewer.pal(8,"Pastel2")   Try a different palette  	
  gA <- graph.adjacency(A, mode="undirected",diag=FALSE )
  V(gA)[n]$color<-pastel[1]
  V(gA)[s]$color<-pastel[2]   changes the 5 vertex to the 4th color of the palette
  V(gA)[n]$color<-pastel[3]
  E(gA)[s%--%n]$color<-"red"
  plot(gA, layout=layout.auto,main="Random Graph With A Range of Vertices Colored")
  s
}

PROBLEM 3) Find a lonely person

Script Name: noFriends Input: A... An adjacency matrix Output: The number of the vertex with the no edge connections Error Checking: None

Description: The number of "friends" a person has in a network is the number of edges connected to that vertex. The ith person in the network, will be connected to the jth person if A[i,j]=1. So the number of 1's in the ith column reports how many edges connect person i to others In mathematical terms, this is called the "degree" of vertex i.

So you must determine the number of ones in each column and then find which column has the most ones.

Hint: There is a quick trick to use here. Since a column has only 0 or 1 in it, the sum of all of the numbers in the column is the same as the number of 1's in the column. Prove that to yourself.

Plot: Create a plot of the graph using igraph. Make the "lonely" person/people a different color

 
noFriends<-function(A,plot=FALSE)
{
   s=which(apply(A,2,sum)==0))

PROBLEM 4) Number of friendship relationships in the network

Script Name: numFriendships Input: A... An adjacency matrix Output: The number of uniqe edges in the graph

Description: The number of friendships in the network is the number of edges in the graph. Basically, count the ones in the adjacency matrix, but be careful. (Remember, an edge from i to j is the same as an edge from j to i)

 
numFriendships<-function(A)
{

num
}

PROBLEM 5) Add a person to the friendship network

Script Name: addPerson Input: A... An adjacency matrix p... Probability a relationship forms plot... Show the new person and relationships Output: New adjacency matrix

Description: As in creating an adjacency matrix, a new column is added and the binomail distribution is used to determine the 1 entries.

Plot: Plot the graph using igraph. Use three colors for the vertices: one for the newly added person, another for his/her immediate friends, and another for the remaining vertices. Color red the edges from the new person to his/her friends. All remaining edges are black.

 
addPerson<-function(A,p,plot)
{
	
 n=nrow(A)
  new=rep(1,n)
  for(i in 1:n){
    new[i]=rbinom(1,1,p)
  }
  A=cbind(A,new)

PROBLEM 6) Add several people to the friendship network

Script Name: growNetwork Input: A... An adjacency matrix p... Probability a relationship forms n... the number of people to add plot... Show the new person and relationships Output: New adjacency matrix

Plot: Plot the graph using igraph. Use three colors for the vertices: one for the newly added person, another for his/her immediate friends, and another for the remaining vertices. Color red the edges from the new person to his/her friends. All remaining edges are black.

 

growNetwork<-function(A,n,p,plot=FALSE)
{row=nrow(A)
  for(ii in 1:n){
    nn=nrow(A)
    new=rep(1,nn)
  for(i in 1:nn){
    new[i]=rbinom(1,1,p)
  }

标签:...,matrix,GRAPHS,column,ASSIGNMENT,RANDOM,number,person,edges
From: https://www.cnblogs.com/tecdat/p/17544955.html

相关文章

  • 判断语句+ random的应用-剪刀石头布游戏
    1'''2需求:31.通过人机交换实现您的出拳(input函数的应用)42.通过伪随机数模块random实现模拟对手出拳53.然后进行数据处理,得出结果64.输入数字非0、1、2退出7'''89importrandom#导入随机数模块random1011whileTrue:12#人机交换:pla......
  • random模块
    说明1.要知道什么是伪随机数?什么是随机数的种子?2.伪随机数random的学习:2.1生成一个伪随机数randomint(start,end)、random()    2.2对序列处理:洗牌(shuffle)、随机选1个(choice)、生成序列(randrange) 示例1'''2伪随机数random的学习3random模块生......
  • 关于 TypeScript 的变量声明和解构赋值(Destructuring Assignment)
    看下面这段代码:const{queryParams,fragment}=this.router.parseUrl(url);const[,path]=url.match(this.URL_SPLIT)??[,''];这段TypeScript代码虽然较短,但仍然展示了许多TypeScript的特性和语法。以下是对这段代码的分析,涵盖了相关的TypeScript特性和语法。......
  • Spectrum Random Masking for Generalization in Image-based Reinforcement Learning
    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! ......
  • CodeForces 1842G Tenzing and Random Operations
    洛谷传送门CF传送门原来还不会这种拆期望的套路设\(b_j\)为第\(j\)次操作中选择的\(i\),所求即为\(E(\prod\limits_{i=1}^n(a_i+\sum\limits_{j=1}^m[b_j\lei]\timesv))\)。乘法也可以考虑拆期望。我们有最基础的性质\(E((a+b)\times(c+d))=E(ac)......
  • numpy中的np.random用法
    转载自:https://blog.csdn.net/Candyerer/article/details/111300215一、np.random.rand():生成指定维度的[0,1)间的随机数np.random.rand(4,3);///生成4行3列的数组,数组中内一个元素都是[0,1)间的随机数二、np.random.random():生成指定维度的[0,1)间的随机数np.random.rando......
  • random.sample()和random.choices()、random.choice()区别
    random.sample()和random.choices()、random.choice()区别 返回列表(1-k个值)random.sample(data,3)random.sample(data,k=3)data可以是字符串元组list从一个数据源中随机获取k个数据不重复取(取过的index不会在被取) 返回列表(1-k个值)random.choices(data,weights=[10,1,......
  • Python random模块
    Pythonrandom模块random模块用于生成随机数importrandomprint(random.random())print(random.randint(1,100))print(random.randrange(1,100))输出结果:0.182467957909153044666randint和randrange的区别##########randint##########defrandint(self,......
  • Stochastic 与 Random 异同: 都有“随机”的意思
    常用词:Stochastic:StochasticProcess,AcademicWordsRandom:RandomVariable/Memory,OALD3000/OxfordCollocationsAcademicDictionary(OALD,OxfordAdvancedLearnersDictionary):Stochastic:https://www.oxfordlearnersdictionaries.com/definition/academic/stoc......
  • 执行cnpm install 时报错:randomUUID is not a function
    啊,熟悉的气息! TypeError:randomUUIDisnotafunction搜了一下得知:npm.taobao.org和registry.npm.taobao.org将在2022.06.30号正式下线和停止DNS解析。新域名切换规则:npm.taobao.org=>npmmirror.comregistry.npm.taobao.org=>registry.npmmirror.com ......