import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
# G = nx.Graph(name= 'dispatch_topo')
# # print(G.graph)
# point = np.array([1,2,3,'a','b','c','m','n'])
# for i in point:
#     G.add_node(i)
# # print(G.graph)
# G.add_edge(1,'m',weight = 100)
# G.add_edge(2,'m',weight = 80)
# G.add_edge('a','m', weight = 120)
# G.add_edge('n','m',weight = 200)
# G.add_edge('b','m',weight = 200)
# G.add_edge('c','m',weight = 210)
# print(G.edges(data = True))
# nx.draw(G,node_color = 'red')
# plt.show()

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

G = nx.Graph()

# G.add_edge('a', 'b', weight=0.6)
# G.add_edge('a', 'c', weight=0.2)
# G.add_edge('c', 'd', weight=0.1)
# G.add_edge('c', 'e', weight=0.7)
# G.add_edge('c', 'f', weight=0.9)
# G.add_edge('a', 'd', weight=0.3)
G.add_edge(1,'m',length = 100)
G.add_edge(2,'m',length = 80)
G.add_edge('a','m', length = 120)
G.add_edge('n','m',length = 10)
G.add_edge('b','n',length = 100)
G.add_edge('h','n',length = 10)
G.add_edge('b','h',length = 80)
G.add_edge('c','n',length = 210)

# elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]
# esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]
#
# pos = nx.spring_layout(G)  # positions for all nodes
#
# # nodes
# nx.draw_networkx_nodes(G, pos, node_size=700)
#
# # edges
# nx.draw_networkx_edges(G, pos, edgelist=elarge,
#                        width=6)
# nx.draw_networkx_edges(G, pos, edgelist=esmall,
#                        width=6, alpha=0.5, edge_color='b', style='dashed')
#
# # labels
# nx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')
# plt.axis('off')
# plt.savefig("weight.jpg")
pos=nx.shell_layout(G)
nx.draw(G,pos,with_labels=True, node_color='red', edge_color='blue', font_size = 18,width = 5,node_size=600, alpha=0.5 )
# pylab.title('Self_Define Net',fontsize=15)
plt.show()

# print('dijkstra方法寻找最短路径：')
# path = nx.dijkstra_path(G, source='a', target='b', weight='length')
# print('节点a到b的路径：', path)
G_node = np.array(G.edges)
GG = G.number_of_edges()
print (G_node[0][1], type(G_node[0][1]))
print(GG,type(GG))

# G1 = nx.Graph()
# print(np.array(G1.nodes))



# G2 = nx.Graph()  # 创建：空的 无向图
# G2.add_weighted_edges_from([(1,2,2),(1,3,8),(1,4,1),
#                             (2,3,6),(2,5,1),
#                             (3,4,7),(3,5,5),(3,6,1),(3,7,2),
#                             (4,7,9),
#                             (5,6,3),(5,8,2),(5,9,9),
#                             (6,7,4),(6,9,6),
#                             (7,9,3),(7,10,1),
#                             (8,9,7),(8,11,9),
#                             (9,10,1),(9,11,2),
#                             (10,11,4)])  # 向图中添加多条赋权边: (node1,node2,weight)
#

