简体   繁体   English

获取具有给定数量的节点和边的所有可能的图形(使用图形工具)

[英]Get all possible graphs with a given number of nodes and edges (with graph-tools)

I am pretty new to graph and my knowledge is near to 0. But I need to build a model in order to get all possible graph with a given number of edges, given number of nodes and the maximum degree between each node.我对图形很陌生,我的知识接近于 0。但我需要构建一个 model 以获得具有给定边数、给定节点数和每个节点之间的最大度数的所有可能图形。

So for example i want to get all graphs possibility that have 8 nodes and exactly 3 connections (edges) between each node.因此,例如,我想获得所有具有 8 个节点和每个节点之间恰好有 3 个连接(边)的图形可能性。

I also would like to get all the possibilities as a dictionary, like so:我也想把所有的可能性都当作一本字典,就像这样:

{ 1 : [2,3,4],
  2 : [5,3,1],
  3 : [6,2,7]
... and so on

Of course an edge cannot be connected to itself.当然,一条边不能与其自身相连。

So far i tried to use the graph-tools library ( here )到目前为止,我尝试使用图形工具库( here

and what i tried is:我尝试的是:

from graph_tool.all import *

def degree () :
    return 3,3
g = random_graph(8, degree)
a = g.get_edges([g.edge_index])
print(a)

Which output me:其中 output 我:

[[ 0  7  0]
 [ 0  5  2]
 [ 0  2  1]
 [ 1  7 12]
 [ 1  5 14]
 [ 1  6 13]
 [ 2  3  9]
 [ 2  4 10]
 [ 2  1 11]
 [ 3  6 22]
 [ 3  0 23]
 [ 3  1 21]
 [ 4  3  3]
 [ 4  1  5]
 [ 4  0  4]
 [ 5  2 20]
 [ 5  0 19]
 [ 5  4 18]
 [ 6  7 15]
 [ 6  5 16]
 [ 6  4 17]
 [ 7  6  8]
 [ 7  3  7]
 [ 7  2  6]]

Can someone explain me what am i doing wrong here?有人可以解释一下我在这里做错了什么吗? (for example why the first list is 0,7,0 (what does it means...again i am totally new to this kind of stuff)) (例如为什么第一个列表是 0,7,0 (这是什么意思......我再次对这种东西完全陌生))

Why is there number greater than 7 if i defined only 8 nodes?如果我只定义了 8 个节点,为什么会有大于 7 的数字?

How can i get all the possibilities (all graphs of 8 nodes and exactly 3 connections between each nodes)?我怎样才能得到所有的可能性(8 个节点的所有图和每个节点之间的 3 个连接)?

I am not sure what do you try to achieve, but I wrote a code to solve a similar problem and generate data structure from random distinct graphs generated with graph tools library.我不确定您要实现什么目标,但我编写了一个代码来解决类似问题,并从使用图形工具库生成的随机不同图形生成数据结构。

It might helps you to get what you need.它可能会帮助您获得所需的东西。

If you got any question, let me know.如果您有任何问题,请告诉我。

from graph_tool.all import *
import json

def isDifferentList(list1, list2):
    return list1 != list2


def isDifferentGraph(graph1, graph2):
    for k in graph1.keys():
        if isDifferentList(graph1[k], graph2[k]):
            # if all lists of connection exists, return true and stop all iterations,
            # this mean that the graph exists, so need to generate a new one
            return True
    #the graph does not exist, we can continue with the current one
    return False


def graphExists(graph, all):
    for generated in all:
        if not isDifferentGraph(graph, generated):
            return True
    return False


def generate_output_data(gr):
    gGraph = {}
    for vertex in gr.get_edges():
        vertex0 = int(vertex[0]) + 1
        vertex1 = int(vertex[1]) + 1
        if int(vertex0) not in gGraph:
            gGraph[vertex0] = []
        if vertex1 not in gGraph:
            gGraph[vertex1] = []
        gGraph[vertex0].append(vertex1)
        gGraph[vertex1].append(vertex0)
    return gGraph


def getRandomGraph():
    return generate_output_data(random_graph(VERTICES, lambda: DEGREE, directed=False))


def defineDegreeAndvertexs(vertexs, in_degree):
    global VERTICES, DEGREE
    DEGREE = in_degree
    VERTICES = vertexs


def generateNgraph(in_vertices, in_degree, n_graphs):
    #first store inputs in globals variable
    defineDegreeAndvertexs(in_vertices, in_degree)

    #generate empty list of generated uniques graphs
    all_graphs = []
    for i in range(0, n_graphs):

        #generate a new random graph and get it as a desired output data struct
        g = getRandomGraph()

        # check if this graph already exists and generate a new one as long as it already been generated
        while graphExists(g, all_graphs):
            g = getRandomGraph()

        #Write the new graph in a text file
        with open("graphs.txt", 'a+') as f:
            f.write(json.dumps(g))
            f.write("\n")

        #append the new graph in the all graph list - this list will be the input for the graphExists function
        all_graphs.append(g)


if __name__ == '__main__':
    generateNgraph(8, 3, 1500)


Follow the main function here and its comments to understand the whole flow.按照这里的主要 function 及其评论了解整个流程。

Output: Output:

{"1": [2, 7, 5], "2": [1, 7, 5], "7": [1, 2, 6], "5": [1, 3, 2], "4": [6, 3, 8], "6": [4, 7, 8], "3": [4, 5, 8], "8": [6, 3, 4]}
{"1": [8, 7, 2], "8": [1, 2, 6], "7": [1, 4, 5], "2": [1, 6, 8], "6": [2, 3, 8], "4": [7, 3, 5], "3": [4, 5, 6], "5": [4, 3, 7]}
{"1": [8, 7, 5], "8": [1, 3, 2], "7": [1, 5, 6], "5": [1, 4, 7], "2": [3, 4, 8], "3": [2, 6, 8], "4": [6, 2, 5], "6": [4, 3, 7]}

Where the key of each dictionary is a named vertex and the value is a list that represents the connected vertices.其中每个字典的键是一个命名顶点,值是一个表示连接顶点的列表。

This is not a real optimized code since its complexity grows with the graphs generations.这不是一个真正的优化代码,因为它的复杂性随着图的生成而增长。 It will check each graph generated for each iteration so O(n) for the complexity.它将检查为每次迭代生成的每个图,因此复杂度为 O(n)。

Now for you first question: how many graphs can we output with given n edges and given degree, first it might be no solution if you want all edges strictly connected.现在为您提出第一个问题:在给定 n 条边和给定度数的情况下,我们 output 可以有多少个图,首先,如果您希望所有边严格连接,则可能没有解决方案。 Second, it is a very complex mathematics question in my opinion.其次,在我看来,这是一个非常复杂的数学问题。

This is what i've found so far but fo implementing this in code I let others specialists to answer this (cause I have no clue sorry).这是我到目前为止所发现的,但是为了在代码中实现它,我让其他专家来回答这个问题(因为我不知道对不起)。

https://math.stackexchange.com/questions/154941/how-to-calculate-the-number-of-possible-connected-simple-graphs-with-n-labelle https://math.stackexchange.com/questions/154941/how-to-calculate-the-number-of-possible-connected-simple-graphs-with-n-labelle

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

相关问题 graph-tools ValueError with graph_union for filtered graphs - graph-tools ValueError with graph_union for filtered graphs 给定节点数和边数,如何生成随机图? - How to generate a random graph given the number of nodes and edges? 在给定节点数和节点度的情况下,生成许多具有多条边的不同随机无向图的最快算法 - Fatest algorithm to generate many distinct random undirected graphs with multiple edges given number of nodes and node degree 获取链接到 networkx 图中给定节点的所有边 - Get all edges linked to a given node in a networkx graph 使用 networkx 图中的数据获取链接到给定节点的所有边 - Get all edges linked to given node with data in networkx graph 给定总节点数和每个节点的度数,是否可以构建图? - Given the number of total nodes and degrees of each node, is it possible to construct a graph? 包含图中所有节点和边的字典 - Dictionary containing all nodes and edges from graph 向节点,边和图添加属性 - Adding attributes to nodes,edges and graphs 给定一定数量的节点,如何获得所有树形图? 网络x - How can I get all the tree graphs given a certain numbers of nodes? Networkx 在具有给定位置坐标的节点图中设置边缘标签的问题 - Problem with setting edges labels in a graph of nodes with given position coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM