Generating a random signed graph using MATLAB
显示 更早的评论
I want to generate a random signed graph and display its adjancency matrix and plot the graph. Then I want to define 2 network games between the any 2 nodes connected by positive and negative edges and check if it is converging to pure strategy nash equilibrium using a BR update rule.
回答(1 个)
nodes = 15;
num_edge = 27;
cost_range = [-9 9];
st = zeros(0,2);
while size(st,1) < num_edge
%no self-loops
cs = randi(nodes-1); ct = cs + randi(nodes-cs);
if ~ismember([cs, ct], st, 'rows')
st(end+1,:) = [cs, ct];
end
end
w = randi(cost_range, num_edge, 1);
G = graph(st(:,1), st(:,2), w, nodes)
plot(G, 'EdgeLabel', G.Edges.Weight)
adj = full(adjacency(G, 'weighted'))
3 个评论
Walter Roberson
2022-1-7
Note: the above code makes no attempt to enforce that all nodes will be connected to another node, or to ensure that the graph has no isolated cycles.
DINESH PATRA
2022-1-7
Walter Roberson
2022-1-7
What is the difference for your purposes between an edge having a "weight" and an edge being "positive" or "negative" ?
If you do not need a particular value of negative or positive, just to know whether it is negative or positive, then you could potentially use weights of -1 and +1.
Unless, that is, you need to use shortestpath() -- but in that case you could use the 'unweighted' option.
类别
在 帮助中心 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
