forked from networkx/outreachy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Outreachy tasks networkx#3 and networkx#4
- Loading branch information
1 parent
568410e
commit 05244a2
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1. https://github.com/networkx/networkx/pull/6474 |
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
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)) |