0% found this document useful (0 votes)
4 views

python 5 P

The document outlines a series of practical exercises conducted by students at Shri Dnyaneshwar Mahavidyalaya, focusing on graph generation and analysis using Python's NetworkX library. It includes tasks such as creating various types of graphs (e.g., directed, bipartite, complete), drawing them, and calculating properties like the number of vertices and edges. Each exercise is accompanied by code snippets demonstrating the implementation of the graph-related tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

python 5 P

The document outlines a series of practical exercises conducted by students at Shri Dnyaneshwar Mahavidyalaya, focusing on graph generation and analysis using Python's NetworkX library. It includes tasks such as creating various types of graphs (e.g., directed, bipartite, complete), drawing them, and calculating properties like the number of vertices and edges. Each exercise is accompanied by code snippets demonstrating the implementation of the graph-related tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa

Department Of BSC (CS)

Practical No 1:

Name of Student :-

Seat No.:- Remark :-

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

plt import networkx as n

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)

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

Shri Dnyaneshwar Mahavidyalaya Newasa

Department Of BSC (CS)

Practical No 2:

Name of Student :-

Seat No.:- Remark :-

Q1) Draw regular graph on 6 vertices with 2 degree.


SOLUTION :
import networkx as nx
import matplotlib.pyplot as plt
G = nx.random_regular_graph(2, 6)
nx.draw(G, with_labels=True, node_color='yellow', node_size=2000,
font_size=16, font_weight='bold', edge_color='orange')
plt.show()
OUTPUT:
Q2) Draw the null graph N9,.
SOLUTION:
import matplotlib.pyplot as plt
import networkx as nx
# Function to create and draw a null graph for N vertices
def draw_null_graph(N):
G = nx.Graph()
G.add_nodes_from(range(N))
nx.draw(G, with_labels=True, node_color="skyblue",
node_size=200,font_size=15, font_weight="bold")
plt.title(f"Null Graph with {N} vertices")
plt.show()
draw_null_graph(9)
OUTPUT:
Q3) Draw the cycle graph c14
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
# Create a cycle graph with 8 nodes (C8)
G = nx.cycle_graph(14)
# Draw the graph
nx.draw(G, with_labels=True, node_color='yellow', node_size=500,
font_size=16, font_weight='bold', edge_color='orange')
# Show the plot
plt.title("Cycle Graph C14")
plt.show()
OUTPUT:
Q4) Draw the complete graph k19.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.complete_graph(19)
nx.draw(G, with_labels=True, node_color='lightblue', node_size=2000,
font_size=15, font_weight='bold')
plt.title("Complete Graph K19")
plt.show()
OUTPUT:
Q5) Draw the wheel graph w20.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.wheel_graph(20)
nx.draw(G, with_labels=True, node_color='orange', font_weight='bold',
node_size=500)
plt.show()
OUTPUT:
Q6) Draw the complete bipartite graph k60.
SOLUTION:
import networkx as nx
import matplotlib.pyplot as plt
set1_size = 5
set2_size = 3
B = nx.complete_bipartite_graph(set1_size, set2_size)
pos = nx.bipartite_layout(B, nodes=range(set1_size))
nx.draw(B, pos, with_labels=True, node_size=500, node_color='skyblue',
font_weight='bold')
plt.show()
OUTPUT:
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa

Department Of BSC (CS)

Practical No 3:

Name of Student :-

Seat No.:- Remark :-

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 matplotlib.pyplot as plt

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.title(f"Asymmetric directed Graph with {num_vertices}vertices")

plt.show()
Mula Education Society’s

Shri Dnyaneshwar Mahavidyalaya Newasa

Department Of BSC (CS)

Practical No 4:

Name of Student :-

Seat No.:- Remark :-

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

Shri Dnyaneshwar Mahavidyalaya Newasa

Department Of BSC (CS)

Practical No 5:

Name of Student :-

Seat No.:- Remark :-

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:

You might also like