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.

Answers (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)
G =
graph with properties: Edges: [27×2 table] Nodes: [15×0 table]
plot(G, 'EdgeLabel', G.Edges.Weight)
adj = full(adjacency(G, 'weighted'))
adj = 15×15
0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 3 0 0 0 0 -5 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 5 0 0 0 0 0 0 4 5 0 0 -5 0 0 0 0 0 0 0 -4 0 0 -4 5 0 0 0 -4 0 0 0 0 5 0 0 0 0 -2 0 0 0 9 5 0 0 0 0 0 0 -2 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 5 0 0 0 0 0 0 -7 0 -5 0 4 0 0 0 0 0 0 0 0 6 0 3 5 0

3 Comments

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.
Thank you , by signed graph the meaning is not weighted graph just some nodes are connected by positive edges and others by negative edges. Can you help to acheive that
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.

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Asked:

on 7 Jan 2022

Commented:

on 7 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!