Assaingment 3 0901CS201007

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

import networkx as nx

import matplotlib.pyplot as plt


import random as rd

Q1. Take 10 students from your class and create a friendship network.
Follow the given steps:
1. Initialize an empty graph using NetworkX.
1. Add nodes with IDs as the name of the students.
2. Add edges connecting nodes (students who are friends with each other).
3. Visualize the graph using NetworkX's drawing functions.
4. Determine if the graph is connected.

G = nx.Graph()
students = ['Yashvardhan', 'Aryaman', 'Kartikey', 'Shipra', 'Praveen',
'Yash', 'Prajwal', 'Aditya', 'Sonal', 'Akarshi']

for student in students:


G.add_node(student)

G.add_edge('Yashvardhan', 'Aryaman')
G.add_edge('Yashvardhan', 'Kartikey')
G.add_edge('Yashvardhan', 'Praveen')
G.add_edge('Aryaman', 'Akarshi')
G.add_edge('Aryaman', 'Aditya')
G.add_edge('Praveen', 'Shipra')
G.add_edge('Praveen', 'Prajwal')
G.add_edge('Shipra', 'Sonal')
G.add_edge('Shipra', 'Aditya')
G.add_edge('Aditya', 'Prajwal')
G.add_edge('Kartikey', 'Yash')
G.add_edge('Akarshi', 'Aditya')

nx.draw(G, with_labels = 1)
plt.show()
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels = 1)
plt.show()
print(nx.is_connected(G))

True

Q2. Create a random friendship graph with 10 nodes (students) and


15 edges (friendships) using
NetworkX. Analyze the following properties:
1. Calculate and print the number of nodes and edges in the graph.
1. Determine if the graph is connected.
2. Find and print the shortest path between different nodes.
3. Calculate and print the length of the shortest path between them.

G = nx.Graph()
students = ['Yashvardhan', 'Aryaman', 'Kartikey', 'Shipra', 'Praveen',
'Yash', 'Prajwal', 'Aditya', 'Sonal', 'Akarshi']

for student in students:


G.add_node(student)

nx.draw(G, with_labels = 1)
plt.show()

print(G. number_of_nodes())
10

distance = [rd.randint(50, 1000) for x in range(10) ]


print (distance)
print (len(distance))

[843, 756, 355, 235, 777, 364, 493, 597, 177, 598]
10

#Addding 15 edges to the graph


while(G.number_of_edges() != 15):
student1 = rd.choice(students)
student2 = rd.choice(students)
wt = rd.choice(distance) #selecting random distance
if student1 != student2 and G.has_edge(student1, student2) == 0:
G.add_edge(student1, student2, weight = wt)

print (G.edges())
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=1)
plt.show()

[('Yashvardhan', 'Akarshi'), ('Yashvardhan', 'Sonal'), ('Aryaman', 'Shipra'),


('Kartikey', 'Praveen'), ('Kartikey', 'Sonal'), ('Kartikey', 'Shipra'),
('Kartikey', 'Prajwal'), ('Kartikey', 'Aditya'), ('Praveen', 'Yash'),
('Praveen', 'Aditya'), ('Yash', 'Sonal'), ('Yash', 'Prajwal'), ('Yash',
'Akarshi'), ('Yash', 'Aditya'), ('Aditya', 'Akarshi')]
print("Number of Nodes: ", G.number_of_nodes())
print("Number of Edges: ", G.number_of_edges())

Number of Nodes: 10
Number of Edges: 15

print(nx.is_connected(G))

True

for i in students:
for j in students:
print (nx.dijkstra_path(G, i, j))
print (nx.dijkstra_path_length(G, i, j))

['Yashvardhan']
0
['Yashvardhan', 'Sonal', 'Kartikey', 'Shipra', 'Aryaman']
1438
['Yashvardhan', 'Sonal', 'Kartikey']
590
['Yashvardhan', 'Sonal', 'Kartikey', 'Shipra']
1083
['Yashvardhan', 'Akarshi', 'Yash', 'Praveen']
1318
['Yashvardhan', 'Akarshi', 'Yash']
541
['Yashvardhan', 'Sonal', 'Kartikey', 'Prajwal']
1083
['Yashvardhan', 'Sonal', 'Kartikey', 'Aditya']
954
['Yashvardhan', 'Sonal']
235
['Yashvardhan', 'Akarshi']
177
['Aryaman', 'Shipra', 'Kartikey', 'Sonal', 'Yashvardhan']
1438
['Aryaman']
0
['Aryaman', 'Shipra', 'Kartikey']
848
['Aryaman', 'Shipra']
355
['Aryaman', 'Shipra', 'Kartikey', 'Praveen']
1625
['Aryaman', 'Shipra', 'Kartikey', 'Sonal', 'Yash']
1567
['Aryaman', 'Shipra', 'Kartikey', 'Prajwal']
1341
['Aryaman', 'Shipra', 'Kartikey', 'Aditya']
1212
['Aryaman', 'Shipra', 'Kartikey', 'Sonal']
1203
['Aryaman', 'Shipra', 'Kartikey', 'Sonal', 'Yashvardhan', 'Akarshi']
1615
['Kartikey', 'Sonal', 'Yashvardhan']
590
['Kartikey', 'Shipra', 'Aryaman']
848
['Kartikey']
0
['Kartikey', 'Shipra']
493
['Kartikey', 'Praveen']
777
['Kartikey', 'Sonal', 'Yash']
719
['Kartikey', 'Prajwal']
493
['Kartikey', 'Aditya']
364
['Kartikey', 'Sonal']
355
['Kartikey', 'Sonal', 'Yashvardhan', 'Akarshi']
767
['Shipra', 'Kartikey', 'Sonal', 'Yashvardhan']
1083
['Shipra', 'Aryaman']
355
['Shipra', 'Kartikey']
493
['Shipra']
0
['Shipra', 'Kartikey', 'Praveen']
1270
['Shipra', 'Kartikey', 'Sonal', 'Yash']
1212
['Shipra', 'Kartikey', 'Prajwal']
986
['Shipra', 'Kartikey', 'Aditya']
857
['Shipra', 'Kartikey', 'Sonal']
848
['Shipra', 'Kartikey', 'Sonal', 'Yashvardhan', 'Akarshi']
1260
['Praveen', 'Yash', 'Akarshi', 'Yashvardhan']
1318
['Praveen', 'Kartikey', 'Shipra', 'Aryaman']
1625
['Praveen', 'Kartikey']
777
['Praveen', 'Kartikey', 'Shipra']
1270
['Praveen']
0
['Praveen', 'Yash']
777
['Praveen', 'Kartikey', 'Prajwal']
1270
['Praveen', 'Aditya']
493
['Praveen', 'Kartikey', 'Sonal']
1132
['Praveen', 'Yash', 'Akarshi']
1141
['Yash', 'Akarshi', 'Yashvardhan']
541
['Yash', 'Sonal', 'Kartikey', 'Shipra', 'Aryaman']
1567
['Yash', 'Sonal', 'Kartikey']
719
['Yash', 'Sonal', 'Kartikey', 'Shipra']
1212
['Yash', 'Praveen']
777
['Yash']
0
['Yash', 'Prajwal']
597
['Yash', 'Aditya']
493
['Yash', 'Sonal']
364
['Yash', 'Akarshi']
364
['Prajwal', 'Kartikey', 'Sonal', 'Yashvardhan']
1083
['Prajwal', 'Kartikey', 'Shipra', 'Aryaman']
1341
['Prajwal', 'Kartikey']
493
['Prajwal', 'Kartikey', 'Shipra']
986
['Prajwal', 'Kartikey', 'Praveen']
1270
['Prajwal', 'Yash']
597
['Prajwal']
0
['Prajwal', 'Kartikey', 'Aditya']
857
['Prajwal', 'Kartikey', 'Sonal']
848
['Prajwal', 'Yash', 'Akarshi']
961
['Aditya', 'Kartikey', 'Sonal', 'Yashvardhan']
954
['Aditya', 'Kartikey', 'Shipra', 'Aryaman']
1212
['Aditya', 'Kartikey']
364
['Aditya', 'Kartikey', 'Shipra']
857
['Aditya', 'Praveen']
493
['Aditya', 'Yash']
493
['Aditya', 'Kartikey', 'Prajwal']
857
['Aditya']
0
['Aditya', 'Kartikey', 'Sonal']
719
['Aditya', 'Akarshi']
843
['Sonal', 'Yashvardhan']
235
['Sonal', 'Kartikey', 'Shipra', 'Aryaman']
1203
['Sonal', 'Kartikey']
355
['Sonal', 'Kartikey', 'Shipra']
848
['Sonal', 'Kartikey', 'Praveen']
1132
['Sonal', 'Yash']
364
['Sonal', 'Kartikey', 'Prajwal']
848
['Sonal', 'Kartikey', 'Aditya']
719
['Sonal']
0
['Sonal', 'Yashvardhan', 'Akarshi']
412
['Akarshi', 'Yashvardhan']
177
['Akarshi', 'Yashvardhan', 'Sonal', 'Kartikey', 'Shipra', 'Aryaman']
1615
['Akarshi', 'Yashvardhan', 'Sonal', 'Kartikey']
767
['Akarshi', 'Yashvardhan', 'Sonal', 'Kartikey', 'Shipra']
1260
['Akarshi', 'Yash', 'Praveen']
1141
['Akarshi', 'Yash']
364
['Akarshi', 'Yash', 'Prajwal']
961
['Akarshi', 'Aditya']
843
['Akarshi', 'Yashvardhan', 'Sonal']
412
['Akarshi']
0

You might also like