Created
December 28, 2017 13:56
-
-
Save kendhia/94fdcfb6ac66dd33123b7387c50a5d67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
G1=nx.DiGraph() | |
list_nodes = [0, 1, 2, 3, 4] | |
G1.add_nodes_from(list_nodes) | |
list_arcs = [(0,1,5.0) , (0,2,3.0) , (0,4,2.0) , (1,2,2.0) , (1,3,6.0) , (2,1,2.0) , (2,3,2.0) , (4,1,6.0) , (4,2,10.0), (4,3,4.0)] | |
G1.add_weighted_edges_from(list_arcs) | |
sp = nx.dijkstra_path(G1,source = 1, target = 6) | |
# We then set the coordinates of each node | |
G1.node[0]['pos'] = (0,0) | |
G1.node[1]['pos'] = (2,2) | |
G1.node[2]['pos'] = (2,-2) | |
G1.node[3]['pos'] = (5,2) | |
G1.node[4]['pos'] = (5,-2) | |
# The positions of each node are stored in a dictionary | |
node_pos=nx.get_node_attributes(G1,'pos') | |
# The edge weights of each arcs are stored in a dictionary | |
arc_weight=nx.get_edge_attributes(G1,'weight') | |
# Create a list of arcs in the shortest path using the zip command and store it in red edges | |
red_edges = list(zip(sp,sp[1:])) | |
# If the node is in the shortest path, set it to red, else set it to white color | |
node_col = ['white' if not node in sp else 'red' for node in G1.nodes()] | |
# If the edge is in the shortest path set it to red, else set it to white color | |
edge_col = ['black' if not edge in red_edges else 'red' for edge in G1.edges()] | |
# Draw the nodes | |
nx.draw_networkx(G1, node_pos,node_color= node_col, node_size=450) | |
# Draw the node labels | |
# nx.draw_networkx_labels(G1, node_pos,node_color= node_col) | |
# Draw the edges | |
nx.draw_networkx_edges(G1, node_pos,edge_color= edge_col) | |
# Draw the edge labels | |
nx.draw_networkx_edge_labels(G1, node_pos,edge_color= edge_col, edge_labels=arc_weight) | |
# Remove the axis | |
plt.axis('off') | |
# Show the plot | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment