python 5 P
python 5 P
Practical No 1:
Name of Student :-
Q 1) Generate graph G with vertex(node) set {1,2,3,4,5} And the edge set
{(1,5),(1,4),(5,2),(2,1),(2,3) ,(4,3),(4,5)}Draw the graph G.
Solution:
import networkx as nx
import matplotlib.pyplot as plt
G2 = nx.Graph()
G2.add_nodes_from([1, 2, 3, 4, 5])
G2.add_edges_from([(1, 5), (1, 4),(5,2),(2,1),(2,3) ,(4,3),(4,5)])
nx.draw(G2, with_labels=True, node_color='lightblue', font_weight='bold',
font_size=10)
plt.show()
OUTPUT:
Q2) Generate G2 with vertex set {1,2,3,4,5} & edge set {(4, 5), (5, 3), (2, 2),
(2, 3), (2, 4), (3, 4), (1, 5)}. Draw G2 with vertex in red colour and edge in
green.
Solution:
import matplotlib.pyplot as
G2 = nx.Graph()
G2.add_nodes_from([1, 2, 3, 4, 5])
G2.add_edges_from([(4, 5), (5, 3), (2, 2), (2, 3), (2, 4), (3, 4), (1, 5)])
pos = nx.spring_layout(G2)
plt.show()
num_vertices=G2.number_of_nodes()
num_edges=G2.number_of_edges()
print("number of vertices:",num_vertices)
print("number of edges:",num_edges)
OUTPUT:
Q3) Find the number of vertex, number of edge in above graph.
SOLUTION :
import matplotlib.pyplot as plt
import networkx as nx
G2 = nx.Graph()
G2.add_nodes_from([1, 2, 3, 4, 5])
G2.add_edges_from([(4, 5), (5, 3), (2, 2), (2, 3), (2, 4), (3, 4), (1, 5)])
pos = nx.spring_layout(G2)
nx.draw(G2, pos, with_labels=True, node_color='red', edge_color='green',
node_size=1000, font_size=15)
plt.show()
num_vertices=G2.number_of_nodes()
num_edges=G2.number_of_edges()
print("number of vertices:",num_vertices)S
print("number of edges:",num_edges)
OUTPUT:
Q4) Generate any graph.
SOLUTION:
import matplotlib.pyplot as plt
import networkx as nx
G2 = nx.Graph()
G2.add_nodes_from([1,2,3,4,5])
G2.add_edges_from([(1,5),(1,2),(2,3),(4,5)])
nx.draw(G2, with_labels=True, node_color='blue',font_weight
='bold',node_size=100)
plt.show()
OUTPUT:
Mula Education Society’s
Practical No 2:
Name of Student :-
Practical No 3:
Name of Student :-
Q1) Draw directed graph PI with vertex set v{1,2,3,4,5} and directed edge set
E{(1,4),(2,3),(1,2),(5,3),(5,),(4,1),(3,2),(5,2),(5,4)}. Draw underlying graph of PI.
Find in degrees and out degree of all vertices in PI.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
D1 = nx.DiGraph()
edges = [(1,4),(2,3),(1,2),(5,3),(5,1),(4,1),(3,2),(5,2),(5,4)]
D1.add_edges_from(edges)
plt.figure(figsize=(8,6))
nx.draw(D1,
with_labels=True,node_color='orange',font_weight='bold',node_size=2000,arr
owsize=20)
plt.title('Directed Grap D1')
plt.show()
underlying_graph = D1.to_undirected()
plt.figure(figuresize=(8,6))
nx.draw(underlying_graph,with_labels+True,node_color='lightgreen',font_wei
ght='bold',node_size=2000)
plt.title('Undirected Grap D1')
plt.show()
in_degrees = D1.in_degree()
out_degrees =
D1.out_degree() print("In-
degree:")
for node, degree in in_degrees:
print(f"vertex {node}: {degree}")
print("\nnOut-degree:")
for node, degree in out_degree:
print(f"vertex {node}: {degree}")
OUTPUT:
Q2) Draw symmetric directed graph on given number of vertices.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
import random
def draw_symmetric_directed_graph(num_vertices):
G = nx.DiGraph()
G.add_nodes_from(range(num_vertices))
for i in range(num_vertices):
for j in range(i+1,num_vertices):
if
random.choice([True,False]):
G.add_edge(i,j)
G.add_edge(j,i)
pos = nx.spring_layout(G)
nx.draw(G,pos,with_labels=True,node_color='orange',node_size=500,arrowsiz
e=15)
plt.show()
draw_symmetric_directed_graph(5)
OUTPUT:
Q3) Draw asymmetric directed graph on given number of vertices.
SOLUTION :
import networkx as nx
import random
def generate_asymmetric_directed_graph(num_vertices):
G=nx.DiGraph()
G.add_nodes_from(range(num_vertices))
for i in range(num_vertices):
for j in range(num_vertices):
if i!=j:
if random.choice([True,False]):
G.add_edge(i,j)
else:
G.add_edge(j,i)
return G
num_vertices =6
G = generate_asymmetric_directed_graph(num_vertices)
pos=nx.spring_layout(G)
plt.figure(figsize=(8,6))
nx.draw(G,pos,with_labels=True,node_size=700,node_color='orange',font_size=12,arrows=True)
plt.show()
Mula Education Society’s
Practical No 4:
Name of Student :-
Q1) Create a simple graph G. Draw graph G with nodes & edges.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_nodes_from([1,2,3,4,5])
G.add_edges_from([(1,2),(2,3),(3,4),(4,5),(1,5)])
nx.draw(G,node_color='green',node_size=100,edge_color='red',width=2,font_
size=12,font_weight='bold',font_color='black')
plt.show()
OUTPUT:
Q2) Determine where G is bipartite or not bipartite.
SOLUTION:
import networkx as nx
G=nx.Graph()
G.add_edges_from([(2,2),(3,5),(1,3),(1,2)])
if nx.is_bipartite(G):
print("The graph is bipartite.")
else:
print("The graph is not bipartite.")
OUTPUT:
Q2)Find the number of component in the graph G.
SOLUION:
import networkx as nx
import matplotlib.pyplot as plt
G2 = nx.Graph()
G2.add_nodes_from([1,2,3,4,5,6])
G2.add_edges_from([(1,2),(2,3),(3,4)])
pos = nx.spring_layout(G2)
nx.draw(G2,
pos,with_labels=True,node_color='orange',edge_color='green',node_size=100
0,font_size=15)
plt.show()
num_components=nx.number_connected_components(G2)
print('no of connected component = ',num_components)
OUTPUT:
Q4) Determine whether G is connected.
SOLUTION:
import networkx as nx
G = nx.Graph()
G.add_edges_from([(1,4),(2,1),(3,4)])
is_connected_G = nx.is_connected(G)
print(f"Graph G is connected: {is_connected_G}")
OUTPUT:
Mula Education Society’s
Practical No 5:
Name of Student :-
Q1) In the graph G add some vertices and add some edges.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(1,4)
nx.draw(G,with_labels=True,node_color='skyblue',node_size=2000,f
ont_size=15,font_weight='bold',edge_color='gray')
plt.show()
Q2) From the G delete some vertices & delete some edges.
SOLUTION:
import networkx as nx
G = nx.Graph()
G.add_nodes_from([1,2,3,4,5])
G.add_edges_from([(1,3),(2,3),(4,5),(3,4)])
print("Original graph:")
print("Nodes:",G.nodes())
print("Edges:",G.edges())
nodes_to_remove = [2]
G.remove_nodes_from(nodes_to_remove)
edges_to_remove =[(1,2)]
G.remove_edges_from(edges_to_remove)
print("\nModified Graph:")
print("Nodes:",G.nodes())
print("Edges:",G.edges())
OUTPUT:
Q3) Determine whether G is connected graph.
SOLUTION:
import networkx as nx
G = nx.Graph()
G.add_edges_from([(2,3),(4,5),(1,4)])
if nx.is_connected(G):
print("The grapg is connected.")
else:
print("The graph is not connected.")
OUTPUT: