Skip to content

Commit

Permalink
Outreachy tasks networkx#3 and networkx#4
Browse files Browse the repository at this point in the history
  • Loading branch information
navyagarwal committed Mar 8, 2023
1 parent 568410e commit 05244a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2023-round-1/navyagarwal/nx_pull_requests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1. https://github.com/networkx/networkx/pull/6474
49 changes: 49 additions & 0 deletions 2023-round-1/navyagarwal/nx_tutorial_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# imports
import networkx as nx
import matplotlib.pyplot as plt

# DiGraph -> Direted graph that can contain self-loops but not parallel edges
G = nx.DiGraph()

# various methods to add nodes of different data types
G.add_node(1) # integer
G.add_node('navya') # string
G.add_node((3, 4)) # tuple
G.add_nodes_from(['a', 'b']) # from list
G.add_nodes_from(range(9, 11)) # in range

X = nx.path_graph(2)
Y = nx.complete_graph(3)

G.add_nodes_from(X) # from graph X
G.add_node(Y) # graph Y



# can add individual or multiple edges to graph
G.add_edge(1, 'navya')
G.add_edges_from([
('a', 'b'),
('navya', 1),
(X, Y),
(9, X),
(Y, 'navya'),
('b', 10),
(10, Y),
('navya', 9),
((3, 4), 0),
('a', 9),
(0, 'b'),
(9, 10)])
G.add_edges_from(X.edges)
G.add_edges_from(Y.edges)


nx.draw(G, with_labels = True, node_color = 'yellow')


# printing shortest path between all pairs of nodes if it exists
for source in G:
for dest in G:
if(nx.has_path(G, source, dest)):
print(source, " -> ", dest," : ", nx.shortest_path(G, source, dest))

0 comments on commit 05244a2

Please sign in to comment.