!pip install networkx
import networkx as nx
# create an undirected graph (network)
G = nx.Graph()
G.name = "The first simple graph"
G
G.name
# adding one node at a time
G.add_node(1)
G.add_node(2)
# adding several nodes at once
G.add_nodes_from([3,4,5,6])
G.nodes
sdklfj sskdfj skadjf aůgkfj
dsgfdsagsdfg
sdfg dsgfdsagsdfgsadg dsfgd
s fgshdfhfdgh fdghfdh dfgh
dfghd fghf
dfgh dfghdfgh
# adding one node at a time
G.add_node(1)
G.add_node(2)
# adding several nodes at once
G.add_nodes_from([3,4,5,6])
G.nodes
# ading edeges one at a time
G.add_edge(1,2)
e = (2,3)
G.add_edge(*e) # unpack the tuple e
# adding several edges at once
G.add_edges_from([(3,4),(4,5),(5,6),(6,1)])
G.edges
G.add_edge(1,2,weight=2.1)
G.add_edges_from([(2,3,{'weight':1.0}), (3,4,{'weight': 3.9}) ])
G.edges
# adding one node at a time
G.add_node(1)
G.add_node(2)
# adding several nodes at once
G.add_nodes_from([3,4,5,6])
G.nodes
# adding one node at a time
G.add_node(1)
G.add_node(2)
# adding several nodes at once
G.add_nodes_from([3,4,5,6])
G.nodes
G.nodes[1]['name'] = "A"
G.nodes[2]['name'] = "B"
G.add_node(3,name = "C")
G.nodes
list(G.nodes)
G.nodes[1]
G.nodes.data()
G.remove_edge(1,6)
G.remove_edges_from([(4,5),(5,6)])
G.remove_node(4)
G.remove_nodes_from([5,6])
print(list(G.nodes))
print(list(G.edges))
G.clear() # clear everything from G
G.add_edges_from([(1,2,{'weight':1.0}), (2,3,{'weight':2.0}), (1,3,{'weight':1.5}),
(3,4,{'weight':2.0})])
print('Nodes:',list(G.nodes))
print('Edges:',list(G.edges))
G.edges
G[1] # this is a view on the neighbors of node 1
G.adj
G.adj[1]
G[1][2]
G.edges[1,2]
G.edges[1,2]['color'] = "blue"
G[2][3]['color'] = "red"
G[2]
list(G.adjacency())
for (u,v,w) in G.edges.data('weight'):
print('{}, {}, {}'.format(u,v,w))
G = nx.Graph(time = '1pm')
G.graph
G.graph['time'] = '2pm'
print(G.graph)
G.graph['event'] = 'tournament'
G.graph
G.add_node(1, name='Ethan')
G.add_nodes_from([3,4], team='Bulls')
G.nodes[1]
G.nodes[1]['court'] = 4
G.nodes.data()
G.add_edge(1, 2, round=1 )
G.add_edges_from([(3, 4), (4, 5)], round=2)
G.add_edges_from([(1, 2, {'round': 3}), (2, 3, {'time': '3pm'})])
G[1][2]['time'] = '2:30pm'
G.edges[3, 4]['time'] = '4pm'
list(G.adjacency())
G[1][3]['weight'] = 2.3
G.add_edge(1,2,weight=3.2)
G.add_weighted_edges_from([(2,3,1.2), (3,1,0.5)])
print(G.adj[2])
G[1][2]['weight'] = 2
print(G.adj[2])
DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
DG.out_degree(1, weight='weight')
DG.degree(1, weight='weight')
node = 1
print('Node {} \n\thas successors {}\n\t predecessors {}'.format(
node, list(DG.successors(node)), list(DG.predecessors(node)))
)
print('Neighbors of node {} are {}'.format(
node, list(DG.neighbors(node))) )
import matplotlib.pyplot as plt
G = nx.petersen_graph()
plt.subplot(121)
#nx.draw(G, with_labels=True, font_weight='bold')
nx.draw(G)
plt.subplot(122)
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold')
%matplotlib inline
G = nx.petersen_graph()
plt.subplot(121)
#nx.draw(G, with_labels=True, font_weight='bold')
nx.draw(G)
plt.subplot(122)
nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold',
node_color="#00AAAA")
def compute_diameter(G):
# returns the diameter of graph G
pass
def compute_degrees(G):
# returns list of degrees of nodes in G
pass
def avg_degree(G):
# return the average degree of nodes in G
pass
def degree_histogram(G):
# plot histogram of degrees of nodes in graph G
pass
def centers(G):
# return the list of all centers of graph G
pass
def max_diameter_graph(Nodes):
# return a graph with the set of nodes Nodes and maximal possible diameter
pass
def min_diameter_graph(Nodes):
# return a graph with the set of nodes Nodes and minimal diameter
pass
def binomial_graph(N,prob):
# generate graph with N nodes 1,...,N where the probability that a pair
# of nodes is connected by an edge is prob
pass
def describe_graph(G):
# print description of graph G. The description should list
# as many properties as possible. E.g., the number of nodes and edges,
# average degree, standard deviation from the average, number and names of different
# attributes of nodes, of edges, etc.
pass
G = nx.karate_club