作业:生成图表
我需要模拟一个离散事件模拟器,为此我需要生成一个由 30 个节点组成的网络,然后检查生成的图是否有向。谁能指导我如何从这里开始。我不应该使用 boost 库来执行此操作。是的,这是一项任务,我需要一个建议来开始。我只需要一些指示即可继续。
#define MAXNODES 30
struct {
int p;
int *incoming;
int *outgoing;
} NODE[MAXNODES]
//The struct defines each node and the neighbors to it.
上面的结构体定义正确吗?
I need to simulate a discrete event simulator and for that I need to generate a network consisting of 30 nodes and then check if the generated graph is directed or not. Can anyone guide me on how to start with this. I should not be using the boost library to do this. Yes this is an assignment, I need an advice to start with. I just need few pointers to go ahead.
#define MAXNODES 30
struct {
int p;
int *incoming;
int *outgoing;
} NODE[MAXNODES]
//The struct defines each node and the neighbors to it.
Is the above struct definition correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您可以生成任何随机图。我还假设您熟悉图的邻接矩阵表示。
如果是这种情况,我将使用图的 邻接矩阵 表示。您可以使用二维数组来表示这一点。
因此,您的图表将被定义为:
您的图表是未加权还是加权?如果未加权,则矩阵的每个元素(例如
graph[3][7]
)将具有 0 或 1。如果是 0,则没有边连接节点 3 和 7(在本例中),如果有 1,则确实存在一条边。如果它是加权的,那么 0 仍然意味着没有边缘,但是数字(1、9、234,任何数字)表示该边缘的权重。
因此,您可以使用循环为每个数组元素填充一个数字 - 因此,遍历每对节点并随机分配一个权重(0 表示没有边,如果有边则为某个数字,根据加权与-未加权。)
因此,要回答您的问题,检查“定向性”很容易。如果图是有向图,则 graph[3][7] 和 graph[7][3] 将具有相同的值。因此,您可以检查每一对(graph[i][j] 和 graph[j][i])以查看值是否相等。您正在查看矩阵是否对称。
如果它不是对称的(因此 [3][7] 为 0,但 [7][3] 为 1),则只有一个方向上的边 - 使其成为有向的。如果每对都有两个值 ([3][7] = 5, [7][3] = 21),则该图是有向的,因为权重会根据您行进的方向而变化。
I am assuming that you may generate any random graph. I'm also assuming that you're familiar with the adjacency matrix representation of a graph.
If that is the case, I'd use an adjacency matrix representation of a graph. You'd use a 2D array to represent this.
So your graph would be defined as:
Is your graph unweighted or weighted? If it is unweighted, then each element of your matrix (
graph[3][7]
, for example) will have either a 0 or 1. If it is a 0, then there is no edge connecting nodes 3 and 7 (in this example), and if there is a 1, then there is indeed an edge.If its weighted, then a 0 still means there is no edge, but a number (1, 9, 234, anything) indicates the weight of that edge.
So you can use a loop to fill in a number for each array element - so, go through each pair of nodes and randomly assign a weight (0 for no edge, or some number if there is an edge, as per weighted-vs-unweighted.)
So to answer your question, checking for "directedness" is easy. If a graph is directed, then graph[3][7] and graph[7][3] will have the same value. So you can check for every pair (graph[i][j] and graph[j][i]) to see if the values are equal. You are seeing if the matrix is symmetric.
If it is not symmetric (so [3][7] has 0, but [7][3] has 1) then there is only an edge in one direction - making it directed. And if each pair has two values ([3][7] = 5, [7][3] = 21) then the graph is directed, since the weight changes depending on the direction you're traveling.
我认为你需要首先定义你的特定版本的“定向性”。决定一个节点是否先于另一个节点的依据是什么?例如,您是否只是要为每个节点分配一个随机数?如果是这样,您的结构似乎不完整。它至少需要一个额外的数据元素来保存该节点的位置值......
I think you need to define your particular version of 'directedness' first. What is the basis for deciding whether one node precedes another? Are you just going to, for example, assign a random number to each node? If so, your struct seems incomplete. It would at least need an extra data element to hold that node's positional value...