简体   繁体   English

生成大型无向加权图

[英]Generating a large undirected weighted graph

A random large connected undirected graph in JAVA with 5K vertices and D density is to be generated. 将生成JAVA中具有5K顶点和D密度的随机大连通无向图。 But generating an edge between random V1 and a random V2 is taking a lot of time. 但是在随机V1和随机V2之间生成边沿需要花费大量时间。 I tried eliminating a created edge from the list of all possible edges and then selecting a random one from the remaining edges, but again it is taking a lot of time. 我尝试从所有可能的边缘列表中删除创建的边缘,然后从其余边缘中随机选择一个边缘,但是这又要花费很多时间。

What do you think will be a fast way to generate the edges for the huge connected graph in a random fashion? 您认为将以一种随机方式为巨大的连通图生成边的快速方法是什么?

You have N = 5k number of vertixes. 您有N = 5k个顶点。 You need M = number of edges. 您需要M =边数。 Its fixed or random? 它是固定的还是随机的? If random it's a number between 1 and N*N. 如果是随机数,则为1到N * N之间的数字。 Just create a random number from randomizer. 只需从randomizer创建一个随机数。

Then in a look generate M pairs of random numbers each 0<=P1 and P2<=N. 然后在外观中生成M对随机数,每对随机数为0 <= P1和P2 <= N。 Each pair is an edge. 每对都是一条边。 Then just add weight to the pair. 然后只是增加重量对。

The easiest solution i can think of (might be cheating), would be to loop through each vertex and create an edge from itself to the next vertex in the list. 我能想到的最简单的解决方案(可能是作弊)是遍历每个顶点,并创建一条从自身到列表中下一个顶点的边。 This would meet the criteria of it being connected, then you could generate random edges until you meet the required density by generating a random number between 0 and the length of the list of vertices for the start and end of the edge and creating it. 这将满足连接的条件,然后可以生成随机边缘,直到生成所需边缘的密度为止,方法是生成一个介于0和边缘起点和终点的顶点列表长度之间的随机数,然后创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM