G.add_node("node 1")
G.add_nodes_from(["node 2", "node 3"])
G.add_nodes_from([("node 4", {"abc": 123}), ("node 5", {"abc": 0})])
print(G.nodes)
print(G.nodes["node 4"]["abc"]) # accessed like a dictionary
G.add_edge("node 1", "node 2")
G.add_edge("node 1", "node 6")
G.add_edges_from([("node 1", "node 3"),
("node 3", "node 4")])
G.add_edges_from([("node 1", "node 5", {"weight" : 3}),
("node 2", "node 4", {"weight" : 5})])
print(G.nodes)
print(G.edges)
print(G["node 1"])
print(G["node 1"]["node 5"])
nx.draw(G)
weights = [1 if G[u][v] == {} else G[u][v]['weight'] for u,v in G.edges()]
nx.draw(G, width=weights)
characters = ["R2-D2",
"CHEWBACCA",
"C-3PO",
"LUKE",
"DARTH VADER",
"CAMIE",
"BIGGS",
"LEIA",
"BERU",
"OWEN",
"OBI-WAN",
"MOTTI",
"TARKIN",
"HAN",
"DODONNA",
"GOLD LEADER",
"WEDGE",
"RED LEADER",
"RED TEN"]
edges = [("CHEWBACCA", "R2-D2"),
("C-3PO", "R2-D2"),
("BERU", "R2-D2"),
("LUKE", "R2-D2"),
("OWEN", "R2-D2"),
("OBI-WAN", "R2-D2"),
("LEIA", "R2-D2"),
("BIGGS", "R2-D2"),
("HAN", "R2-D2"),
("CHEWBACCA", "OBI-WAN"),
("C-3PO", "CHEWBACCA"),
("CHEWBACCA", "LUKE"),
("CHEWBACCA", "HAN"),
("CHEWBACCA", "LEIA"),
("CAMIE", "LUKE"),
("BIGGS", "CAMIE"),
("BIGGS", "LUKE"),
("DARTH VADER", "LEIA"),
("BERU", "LUKE"),
("BERU", "OWEN"),
("BERU", "C-3PO"),
("LUKE", "OWEN"),
("C-3PO", "LUKE"),
("C-3PO", "OWEN"),
("C-3PO", "LEIA"),
("LEIA", "LUKE"),
("BERU", "LEIA"),
("LUKE", "OBI-WAN"),
("C-3PO", "OBI-WAN"),
("LEIA", "OBI-WAN"),
("MOTTI", "TARKIN"),
("DARTH VADER", "MOTTI"),
("DARTH VADER", "TARKIN"),
("HAN", "OBI-WAN"),
("HAN", "LUKE"),
("C-3PO", "HAN"),
("LEIA", "MOTTI"),
("LEIA", "TARKIN"),
("HAN", "LEIA"),
("DARTH VADER", "OBI-WAN"),
("DODONNA", "GOLD LEADER"),
("DODONNA", "WEDGE"),
("DODONNA", "LUKE"),
("GOLD LEADER", "WEDGE"),
("GOLD LEADER", "LUKE"),
("LUKE", "WEDGE"),
("BIGGS", "LEIA"),
("LEIA", "RED LEADER"),
("LUKE", "RED LEADER"),
("BIGGS", "RED LEADER"),
("BIGGS", "C-3PO"),
("C-3PO", "RED LEADER"),
("RED LEADER", "WEDGE"),
("GOLD LEADER", "RED LEADER"),
("BIGGS", "WEDGE"),
("RED LEADER", "RED TEN"),
("BIGGS", "GOLD LEADER"),
("LUKE", "RED TEN")]
G_starWars = nx.Graph()
G_starWars.add_nodes_from(characters)
G_starWars.add_edges_from(edges)
nx.draw(G_starWars, with_labels = True)
pos = nx.circular_layout(G_starWars)
nx.draw(G_starWars, pos=pos, with_labels = True)
nx.draw_kamada_kawai(G_starWars, with_labels = True)
print(G_starWars.degree["LUKE"])
print(G_starWars.degree)
print(sorted(G_starWars.degree, key=lambda x: x[1], reverse=True))
pageranks = nx.pagerank(G_starWars) # A dictionary
print(pageranks["LUKE"])
print(sorted(pageranks, key=lambda x: x[1], reverse=True))
from networkx.algorithms.community.label_propagation import label_propagation_communities
communities = label_propagation_communities(G_starWars)
print([community for community in communities])