简体   繁体   English

在Matlab中动态地将节点和边添加到图数据结构中

[英]Adding nodes and edges to graph data structure dynamically in Matlab

I usually create graphs in matlab as follows: 我通常在matlab中创建图形如下:

g=sparse(5,5);
g(2,3)=1;

and so on. 等等。

This works very well if i have the graph created in advance and given to me figuratively so that i have to make a adjacency matrix representing that graph. 如果我预先创建了图形并以比喻的方式给我,这样就可以很好地工作,这样我就必须制作一个表示该图形的邻接矩阵。 But now i have a different kind of problem. 但现在我有一个不同的问题。 I have to create the graph from scratch dynamically. 我必须动态地从头开始创建图形。 I have a bunch of nodes, say 500 nodes. 我有一堆节点,比如500个节点。 Now i have to loop over these 500 nodes and if certain conditions are satisfied i will put some of the nodes, say 200 in my graph structure. 现在我必须遍历这500个节点,如果满足某些条件,我将放置一些节点,比如我的图形结构中的200个节点。 Then i will run a loop over different pairs of nodes in this pool of 200 nodes and if certain conditions are satisfied, then i will add an edge. 然后我将在这个200个节点的池中的不同节点对上运行一个循环,如果满足某些条件,那么我将添加一个边。

I am working in Matlab and one method i could figure was that i will just intialise an adjacency matrix of size 500x500. 我在Matlab工作,我可以想到的一个方法是,我将初始化一个大小为500x500的邻接矩阵。 And just add edges for the ones that pass the criterion. 只需为通过该标准的边添加边。 This is undesirable. 这是不可取的。 I don't want to include the 500 points in my graph structure at all. 我根本不想在我的图形结构中包含500个点。

Basically, I want to do this in Matlab. 基本上,我想在Matlab中做到这一点。 In pseudo-codes in research papers, it is told: 在研究论文中的伪代码中,它被告知:

Add vertex v0 to graph G.

How can i implement this? 我该如何实现呢?

And then later a pseudo-code tells 然后一个伪代码告诉

Add edge (v0,v1) to graph G.

And lastly if there is already an edge in the graph (v0,v1) and you are told: 最后,如果图中已经存在边缘(v0,v1)并且您被告知:

Delete edge (v0,v1)  % Not asking you to delete the nodes.
Add node v.
Add edges (v0,v) and (v,v1)

I want to do these steps efficiently. 我想有效地完成这些步骤。 I want to be able to create my graph dynamically. 我希望能够动态创建我的图表。 I don't want to create a huge adjacency matrix and then designate edges in it. 我不想创建一个巨大的邻接矩阵,然后在其中指定边。 I want to build my graph step-by-step if needed. 如果需要,我想逐步构建我的图表。 If this can be done in Matlab please let me know. 如果可以在Matlab中完成,请告诉我。 Otherwise i am open to python also. 否则我也对python开放。

Have you tried looking into MATLAB's new-ish graph class? 您是否尝试过研究MATLAB的新graph类? It was introduced in R2015b and has methods you can use to do these steps efficiently. 它是在R2015b中引入的,并且具有可用于有效执行这些步骤的方法。 Specifically, it has addnode , addedge , rmedge , etc. You can also extract the adjacency matrix from the graph object with adjacency . 具体地,它具有addnodeaddedgermedge等,还可以提取从邻接矩阵graph与对象adjacency

http://www.mathworks.com/help/matlab/graph-and-network-algorithms.html http://www.mathworks.com/help/matlab/graph-and-network-algorithms.html

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

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