l work with Networkx to generate some class of graphs.
Now l would like to permute nodes and rotate the graph with (80°, 90°,120° degree)
How can l apply permutation and rotation on graphs with NetworkX ?
Edit_1:
Given an adjacency matrix of a graph, l would like to rotate the graph in the way that it preserves the edges and vertices link. The only thing that changes is the position of nodes.
What l would like to do is to rotate my graph with 90 degree.
Input :
Adjacency matrix of graph G
process :
Apply rotation on G with 90 degree
Output :
Rotated adjacency matrix
It means, the graph preserves its topology and just the index of adjacency matrix that changes position.
For example nodes 1 at index 0 after rotation will be at index 4 for instance.
What l have tried ?
1)l looked after numpy.random.permutation()
but it does't seem to accept the rotation parameter.
2) In networkX l didn't find any function that allows to do rotation.
EDIT2 Given an adjacency matrix of 5*5 (5 nodes:
adj=[[0,1,0,0,1],
[1,0,1,1,0],
[0,0,0,1,1],
[0,0,1,0,1],
[1,1,1,1,0]
]
l would like to permute between indexes . Say that node 1 takes the place of node 3 , node 3 takes the place of nodes 4 and node 4 takes the place of node 1.
It's just the permutation of nodes (preserving their edges).
l would like to keep in a dictionary the mapping between original index and the new index after permutation.
Secondly, l would like to apply permutation or rotation of this adjacency matrix with an angle of 90°. (It's like apply rotation on an image). I'm not sure how it can be done.
Take a look at the networkx command relabel_nodes
.
Given a graph G
, if we want to relabel node 0
as 1
, 1
as 3
, and 3
as 0
[so a permutation of the nodes, leaving 2
in place], we create the dict mapping = {0:1, 1:3, 3:0}
. Then we do
H = nx.relabel_nodes(G, mapping)
And H
is now the permuted graph.
import networkx as nx
G = nx.path_graph(4) #0-1-2-3
mapping = {0:1, 1:3, 3:0}
H = nx.relabel_nodes(G, mapping) #1-3-2-0
#check G's adjacency matrix
print(nx.to_numpy_matrix(G,nodelist=[0,1,2,3]))
> [[ 0. 1. 0. 0.]
[ 1. 0. 1. 0.]
[ 0. 1. 0. 1.]
[ 0. 0. 1. 0.]]
#check H's adjacency matrix
print(nx.to_numpy_matrix(H,nodelist=[0,1,2,3]))
> [[ 0. 0. 1. 0.]
[ 0. 0. 0. 1.]
[ 1. 0. 0. 1.]
[ 0. 1. 1. 0.]]