0% found this document useful (0 votes)
37 views8 pages

FAssignment 6 - Jupyter Notebook

The document is a Jupyter Notebook containing assignments related to graph theory using the NetworkX library. It includes tasks such as drawing complete and cycle graphs, checking isomorphism between graphs, and visualizing the union, intersection, and Cartesian product of two graphs. The results show that K5 is not isomorphic to the complement of C5, while G1 and G2 are isomorphic.

Uploaded by

Great On
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views8 pages

FAssignment 6 - Jupyter Notebook

The document is a Jupyter Notebook containing assignments related to graph theory using the NetworkX library. It includes tasks such as drawing complete and cycle graphs, checking isomorphism between graphs, and visualizing the union, intersection, and Cartesian product of two graphs. The results show that K5 is not isomorphic to the complement of C5, while G1 and G2 are isomorphic.

Uploaded by

Great On
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [1]: 1 #Q.1)Draw K = Complete graph K5, H= complement of N5. Determine whether K


2 ​

localhost:8888/notebooks/fAssignment 6.ipynb 1/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [2]: 1 import networkx as nx


2 import matplotlib.pyplot as plt
3 ​
4 # Create the complete graph K5
5 K5 = nx.complete_graph(5)
6 ​
7 # Create the cycle graph C5
8 C5 = nx.cycle_graph(5)
9 ​
10 # Create the complement of C5
11 H = nx.complement(C5)
12 ​
13 # Check if K5 is isomorphic to H
14 is_isomorphic = nx.is_isomorphic(K5, H)
15 print(f"K5 is isomorphic to H (complement of C5): {is_isomorphic}")
16 ​
17 # Visualize both graphs
18 plt.figure(figsize=(12, 6))
19 ​
20 # Draw K5
21 plt.subplot(1, 2, 1)
22 pos_K5 = nx.spring_layout(K5) # positions for all nodes
23 nx.draw(K5, pos_K5, with_labels=True, node_color='lightblue', edge_color='
24 plt.title("Complete Graph K5")
25 ​
26 # Draw H (complement of C5)
27 plt.subplot(1, 2, 2)
28 pos_H = nx.spring_layout(H) # positions for all nodes
29 nx.draw(H, pos_H, with_labels=True, node_color='lightgreen', edge_color='b
30 plt.title("Complement of Cycle Graph C5 (H)")
31 ​
32 # Show the graphs
33 plt.show()
K5 is isomorphic to H (complement of C5): False

localhost:8888/notebooks/fAssignment 6.ipynb 2/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [3]: 1 #Q.2)Generate and draw any 2 graphs with names G1 and G2. Determine whethe
2 ​

localhost:8888/notebooks/fAssignment 6.ipynb 3/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [4]: 1 import networkx as nx


2 import matplotlib.pyplot as plt
3 ​
4 # Create the first graph G1
5 G1 = nx.Graph()
6 G1.add_edges_from([(1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
7 ​
8 # Create the second graph G2
9 G2 = nx.Graph()
10 G2.add_edges_from([(6, 7), (6, 8), (7, 9), (8, 9), (9, 10)])
11 ​
12 # Check if G1 is isomorphic to G2
13 is_isomorphic = nx.is_isomorphic(G1, G2)
14 print(f"G1 is isomorphic to G2: {is_isomorphic}")
15 ​
16 # Visualize both graphs
17 plt.figure(figsize=(12, 6))
18 ​
19 # Draw G1
20 plt.subplot(1, 2, 1)
21 pos_G1 = nx.spring_layout(G1) # positions for all nodes
22 nx.draw(G1, pos_G1, with_labels=True, node_color='lightblue', edge_color='
23 plt.title("Graph G1")
24 ​
25 # Draw G2
26 plt.subplot(1, 2, 2)
27 pos_G2 = nx.spring_layout(G2) # positions for all nodes
28 nx.draw(G2, pos_G2, with_labels=True, node_color='lightgreen', edge_color=
29 plt.title("Graph G2")
30 ​
31 # Show the graphs
32 plt.show()
G1 is isomorphic to G2: True

localhost:8888/notebooks/fAssignment 6.ipynb 4/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [5]: 1 #.Q.3)Draw union of graphs G1 and G2.


2 ​

In [6]: 1 import networkx as nx


2 import matplotlib.pyplot as plt
3 ​
4 # Create the first graph G1
5 G1 = nx.Graph()
6 G1.add_edges_from([(1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
7 ​
8 # Create the second graph G2
9 G2 = nx.Graph()
10 G2.add_edges_from([(6, 7), (6, 8), (7, 9), (8, 9), (9, 10)])
11 ​
12 # Compute the union of G1 and G2
13 G_union = nx.union(G1, G2)
14 ​
15 # Visualize the union graph
16 plt.figure(figsize=(8, 6))
17 pos_union = nx.spring_layout(G_union) # positions for all nodes
18 nx.draw(G_union, pos_union, with_labels=True, node_color='lightblue', edge
19 plt.title("Union of Graphs G1 and G2")
20 plt.show()

localhost:8888/notebooks/fAssignment 6.ipynb 5/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [7]: 1 # Q.4)Draw intersection of graphs G1 and G2.

In [8]: 1 import networkx as nx


2 import matplotlib.pyplot as plt
3 ​
4 # Create the first graph G1
5 G1 = nx.Graph()
6 G1.add_edges_from([(1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
7 ​
8 # Create the second graph G2
9 G2 = nx.Graph()
10 G2.add_edges_from([(6, 7), (6, 8), (7, 9), (8, 9), (9, 10)])
11 ​
12 # Compute the intersection of G1 and G2
13 G_intersection = nx.intersection(G1, G2)
14 ​
15 # Visualize the intersection graph
16 plt.figure(figsize=(8, 6))
17 pos_intersection = nx.spring_layout(G_intersection) # positions for all n
18 nx.draw(G_intersection, pos_intersection, with_labels=True, node_color='li
19 plt.title("Intersection of Graphs G1 and G2")
20 plt.show()

localhost:8888/notebooks/fAssignment 6.ipynb 6/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [9]: 1 # Q. 5)Draw product of graphs G1 and G2.

In [10]: 1 import networkx as nx


2 import matplotlib.pyplot as plt
3 ​
4 # Create the first graph G1
5 G1 = nx.Graph()
6 G1.add_edges_from([(1, 2), (1, 3), (2, 4), (3, 4), (4, 5)])
7 ​
8 # Create the second graph G2
9 G2 = nx.Graph()
10 G2.add_edges_from([(6, 7), (6, 8), (7, 9), (8, 9), (9, 10)])
11 ​
12 # Compute the Cartesian product of G1 and G2
13 G_product = nx.cartesian_product(G1, G2)
14 ​
15 # Visualize the product graph
16 plt.figure(figsize=(10, 8))
17 pos_product = nx.spring_layout(G_product) # positions for all nodes
18 nx.draw(G_product, pos_product, with_labels=True, node_color='lightblue',
19 plt.title("Cartesian Product of Graphs G1 and G2")
20 plt.show()

localhost:8888/notebooks/fAssignment 6.ipynb 7/8


1/14/25, 9:26 PM fAssignment 6 - Jupyter Notebook

In [ ]: 1 ​

localhost:8888/notebooks/fAssignment 6.ipynb 8/8

You might also like