我正在尝试使用python中的networkx
库进行2D环形圆环互连。我在matlab中使用以下代码实现了这一点。
%..points for grid....%
S=4; % Side length of grid
N=S^2; % Number of nodes in network
v1=[1:S];
[c1 c2]=meshgrid(v1,v1);
points(:,1)=2*c1(:);
points(:,2)=2*c2(:);
%...end of points for grid...%
D=squareform(pdist(points)); % Caluclate distances between points
D1=D+eye(N);
Dbin=D<RI; % Deciding conflicting nodes
%......Extra edges for torus.....%
for i=1:S
Dbin(i,i+(S-1)*S)=1;
Dbin(i+(S-1)*S,i)=1;
Dbin(S*i,S*(i-1)+1)=1;
Dbin(S*(i-1)+1,S*i)=1;
end
%......Extra edges for torus.....%
我可以使用numpy在python中做类似的事情,但是我想使用networkx
库。我不知道该怎么做。
编辑:
我使用以下代码创建了一个方形格子。但是必须弄清楚如何连接外围边缘,如下图所示。
import networkx as nx
G = nx.grid_graph(dim = [4,4])
有没有办法将其推广到具有外围连接的nxn网格。
答案 0 :(得分:0)
我不知道如何在networkx中制作该图,但是我使用乳胶和tikz创建了一个类似的图。似乎效率低下,但完成了工作。
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,chains,positioning,scopes,quotes,bending,calc,intersections}
\tikzset{
block/.style={draw,minimum width=1em,minimum height=1em,align=center,fill=blue!30},
arrow/.style={->},
line/.style={-}
}
\begin{document}
\begin{tikzpicture}[>=stealth',node distance=0.5cm]
% Creating rows of blocks
{[start chain]
\node[on chain] (s0) {};
\node[on chain] (s1) {};
\node[on chain] (s2) {};
\node[on chain] (s3) {};
\node[on chain] (s4) {};
}
{[start chain]
\node[block,on chain, below = 0.15 cm of s0] (A0) {};
\node[block,on chain, join =by {line}] (A1) {};
\node[block,on chain, join =by {line}] (A2) {};
\node[block,on chain, join =by {line}] (A3) {};
\node[block,on chain, join =by {line}] (A4) {};
}
{[start chain]
\node[block,on chain, below = of A0] (B0) {};
\node[block,on chain, join =by {line}] (B1) {};
\node[block,on chain, join =by {line}] (B2) {};
\node[block,on chain, join =by {line}] (B3) {};
\node[block,on chain, join =by {line}] (B4) {};
}
{[start chain]
\node[block,on chain, below = of B0] (C0) {};
\node[block,on chain, join =by {line}] (C1) {};
\node[block,on chain, join =by {line}] (C2) {};
\node[block,on chain, join =by {line}] (C3) {};
\node[block,on chain, join =by {line}] (C4) {};
}
{[start chain]
\node[block,on chain, below = of C0] (D0) {};
\node[block,on chain, join =by {line}] (D1) {};
\node[block,on chain, join =by {line}] (D2) {};
\node[block,on chain, join =by {line}] (D3) {};
\node[block,on chain, join =by {line}] (D4) {};
}
% Drawing vertical lines
\draw (A0) -- (B0) -- (C0) -- (D0); % -- (E0);
\draw (A1) -- (B1) -- (C1) -- (D1); % -- (E1);
\draw (A2) -- (B2) -- (C2) -- (D2); % -- (E2);
\draw (A3) -- (B3) -- (C3) -- (D3); % -- (E3);
\draw (A4) -- (B4) -- (C4) -- (D4); % -- (E4);
% Drawing loop backs horizontal
\draw (A0.west) -- ($(A0.west) - (0.15, 0)$);
\draw ($(A0.west) - (0.15, 0)$) -- ($(A0.west) - (0.15, 0)+(0,0.5)$);
\draw ($(A0.west) - (0.15, 0)+(0,0.5)$) -- ($(A0.west) +(4,0.5)$);
\draw ($(A0.west) +(4,0.5)$) |- (A4.east);
% \draw (A0.north) |- (s2.north east) -| (A4.north);
% B row
\draw (B0.west) -- ($(B0.west) - (0.15, 0)$);
\draw ($(B0.west) - (0.15, 0)$) -- ($(B0.west) - (0.15, 0)+(0,0.5)$);
\draw ($(B0.west) - (0.15, 0)+(0,0.5)$) -- ($(B0.west) +(4,0.5)$);
\draw ($(B0.west) +(4,0.5)$) |- (B4.east);
% C row
\draw (C0.west) -- ($(C0.west) - (0.15, 0)$);
\draw ($(C0.west) - (0.15, 0)$) -- ($(C0.west) - (0.15, 0)+(0,0.5)$);
\draw ($(C0.west) - (0.15, 0)+(0,0.5)$) -- ($(C0.west) +(4,0.5)$);
\draw ($(C0.west) +(4,0.5)$) |- (C4.east);
% D row
\draw (D0.west) -- ($(D0.west) - (0.15, 0)$);
\draw ($(D0.west) - (0.15, 0)$) -- ($(D0.west) - (0.15, 0)+(0,0.5)$);
\draw ($(D0.west) - (0.15, 0)+(0,0.5)$) -- ($(D0.west) +(4,0.5)$);
\draw ($(D0.west) +(4,0.5)$) |- (D4.east);
% Vertical Loopbacks
% 0 column
\draw (A0.north) -- ($(A0.north) + (0.0, 0.15)$);
\draw ($(A0.north) + (0, 0.15)$) -- ($(A0.north) + (0, 0.15)+(-0.5,0)$);
\draw ($(A0.north) + (0, 0.15)+(-0.5,0)$) -- ($(D0.north) +(-0.5,-0.65)$);
\draw ($(D0.north) +(-0.5,-0.65)$) -| (D0.south);
% 1 column
\draw (A1.north) -- ($(A1.north) + (0.0, 0.15)$);
\draw ($(A1.north) + (0, 0.15)$) -- ($(A1.north) + (0, 0.15)+(-0.5,0)$);
\draw ($(A1.north) + (0, 0.15)+(-0.5,0)$) -- ($(D1.north) +(-0.5,-0.65)$);
\draw ($(D1.north) +(-0.5,-0.65)$) -| (D1.south);
% 2 column
\draw (A2.north) -- ($(A2.north) + (0.0, 0.15)$);
\draw ($(A2.north) + (0, 0.15)$) -- ($(A2.north) + (0, 0.15)+(-0.5,0)$);
\draw ($(A2.north) + (0, 0.15)+(-0.5,0)$) -- ($(D2.north) +(-0.5,-0.65)$);
\draw ($(D2.north) +(-0.5,-0.65)$) -| (D2.south);
% 3 column
\draw (A3.north) -- ($(A3.north) + (0.0, 0.15)$);
\draw ($(A3.north) + (0, 0.15)$) -- ($(A3.north) + (0, 0.15)+(-0.5,0)$);
\draw ($(A3.north) + (0, 0.15)+(-0.5,0)$) -- ($(D3.north) +(-0.5,-0.65)$);
\draw ($(D3.north) +(-0.5,-0.65)$) -| (D3.south);
% 4 column
\draw (A4.north) -- ($(A4.north) + (0.0, 0.15)$);
\draw ($(A4.north) + (0, 0.15)$) -- ($(A4.north) + (0, 0.15)+(-0.5,0)$);
\draw ($(A4.north) + (0, 0.15)+(-0.5,0)$) -- ($(D4.north) +(-0.5,-0.65)$);
\draw ($(D4.north) +(-0.5,-0.65)$) -| (D4.south);
\end{tikzpicture}
\end{document}