Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the tutorial script unna97/nx_dev_tutorial_script.py #45

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions 2022-round-1/unna97/nx_tutorial_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

print(nx.__version__)


def highlight_path_given(path_highlight, position, color="yellow", alpha=0.8, width=5):

nx.draw_networkx_edges(
example_directed_graph,
edgelist=path_highlight,
pos=position,
edge_color=color,
alpha=alpha,
ax=ax,
width=width,
)

nx.draw(example_directed_graph, pos=position, ax=ax, with_labels=True)


def update_fig(i):
ax.clear()
print(i)
highlight_path_given(path_edges[i], position)
fig.suptitle(f"Path {i + 1} of {len(path_edges)}")


def create_animation_highlight_paths(path_edges):

ani = animation.FuncAnimation(
fig,
update_fig,
frames=len(path_edges),
)

gif_file_path = r"shortest_paths.gif"
writergif = animation.PillowWriter(fps=1)
ani.save(gif_file_path, writer=writergif)
return ani


if __name__ == "__main__":
### Intializing the graph:
example_directed_graph = nx.DiGraph()

### Adding nodes from list:
random_nodes = ["node_string", 10, 10, 12, (1, 22), "red", "green", 36, "blue"]
example_directed_graph.add_nodes_from(random_nodes)

### Only unique node_values will be added
print("nodes added:", example_directed_graph.nodes())

### Adding random edges between nodes:
for i in range(10):
example_directed_graph.add_edge(
random.choice(random_nodes), random.choice(random_nodes)
)

### Edges will be also added only once, above will create at max 10:
print("edges added:", example_directed_graph.edges())
print("number of edges added:", example_directed_graph.number_of_edges())

### Finding shortest paths between nodes in an unweighted directed graph:
###In case of multiple shortest paths between two pair of nodes, only one is returned in methods belows:

### Method 1:
print(
"Method 1 shortest path between nodes:",
nx.shortest_path(example_directed_graph),
)

### Method 2: Get the generator of all pairs shortest paths:
print(
"Method 2 shortest path between nodes:",
dict(nx.all_pairs_shortest_path(example_directed_graph)),
)

### Method 3: Get the generator of single source shortest paths (for all nodes):
for node in example_directed_graph.nodes():
print(
f"Method 3 shortest path between {node} and other reachable nodes",
dict(nx.single_source_shortest_path(example_directed_graph, node)),
)

### Plot the graph:
nx.draw(
example_directed_graph, with_labels=True, node_size=100, alpha=1, linewidths=10
)
plt.show()

### Create a gif, highlighting the shortest paths:
position = nx.spring_layout(example_directed_graph)
fig, ax = plt.subplots(figsize=(10, 10))
plt.close()
paths = nx.shortest_path(example_directed_graph)
path_edges = []

for source_node in paths:
for target_node in paths[source_node]:
path = paths[source_node][target_node]
path_edge = list(zip(path, path[1:]))
path_edges.append(path_edge)
print(path_edges)

create_animation_highlight_paths(path_edges)

### All shortest paths between pairs of node:
print("All shortest paths between all pairs of nodes:\n")

for source_node in paths:
for target_node in paths[source_node]:
print(
"source_node:",
source_node,
"target_node:",
target_node,
"\npaths:",
list(
nx.all_shortest_paths(
example_directed_graph, source=source_node, target=target_node
)
),
)