Lab 4
Lab 4
Lab 4
LAB - 4
1. For the graph given below use Kruskal's algorithm to find a minimum spanning tree.
2. For the following graph use Prim's algorithm to find a minimum spanning tree.
Hint: You can use the tool at https://algorithms.discrete.ma.tum.de/graph-algorithms/mst-
prim/index_en.html
3. HA30 dataset describes the airline distances, in hundreds of miles, between 30 international
cities. Using the Python code below find the shortest airway system that connects all the
cities, using only straight paths from one city to another.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from google.colab import drive
drive.mount('/content/drive')
CME4422 - INTRODUCTION TO GRAPH THEORY
LAB - 4
from google.colab import files
#dataset from https://people.sc.fsu.edu/~jburkardt/datasets/cities/
cities.html
#https://people.sc.fsu.edu/~jburkardt/datasets/cities/ha30_dist.txt
distanceMatrix = np.loadtxt('/content/drive/My Drive/Colab Notebooks/
CME4422/ha30_dist.txt')
#https://people.sc.fsu.edu/~jburkardt/datasets/cities/ha30_xyz.txt
positions = np.loadtxt('/content/drive/My Drive/Colab Notebooks/
CME4422/ha30_xyz.txt')
#https://people.sc.fsu.edu/~jburkardt/datasets/cities/ha30_name.txt
text_file = open('/content/drive/My Drive/Colab Notebooks/CME4422/
ha30_name.txt', "r")
cityList = text_file.readlines()
G = nx.Graph()
for i in range(len(cityList)):
G.add_node(cityList[i], pos=(positions[i,0], positions[i,1]))
for i in range(G.number_of_nodes()):
for j in range(i + 1, G.number_of_nodes()):
G.add_edge(cityList[i], cityList[j], weight=distanceMatrix[i][j])
pos=nx.get_node_attributes(G,'pos')
nx.draw(G, pos, with_labels=True)
plt.show()
T = nx.minimum_spanning_tree(G)
pos=nx.get_node_attributes(T,'pos')
nx.draw_networkx(T, pos, with_labels=True)
plt.show()
for u,v,a in T.edges(data=True):
print (u,v,a)
print("Total", sum([T[i][j]['weight'] for (i,j) in T.edges()]))
CME4422 - INTRODUCTION TO GRAPH THEORY
LAB - 4
Azores
London
{'weight': 16.0}
Azores
Montreal
{'weight': 24.0}
Baghdad
Cairo
{'weight': 8.0}
Baghdad
Bombay
{'weight': 20.0}
Berlin
Paris
{'weight': 5.0}
Berlin
Rome
{'weight': 7.0}
Berlin
Moscow
{'weight': 10.0}
Bombay
Shanghai
{'weight': 31.0}
Buenos Aires
Santiago
{'weight': 7.0}
Buenos Aires
Rio de Janeiro
{'weight': 12.0}
Cairo
Istanbul
{'weight': 8.0}
Capetown
CME4422 - INTRODUCTION TO GRAPH THEORY
LAB - 4
Rio de Janeiro
{'weight': 38.0}
Chicago
New York
{'weight': 7.0}
Chicago
New Orleans
{'weight': 8.0}
Chicago
Seattle
{'weight': 17.0}
Guam
Manila
{'weight': 16.0}
Guam
Sydney
{'weight': 33.0}
Honolulu
San Francisco
{'weight': 24.0}
Istanbul
Rome
{'weight': 9.0}
Juneau
Seattle
{'weight': 9.0}
London
Paris
{'weight': 2.0}
Manila
Shanghai
{'weight': 12.0}
Melbourne
Sydney
{'weight': 4.0}
Mexico City
New Orleans
{'weight': 9.0}
Mexico City
Panama City
{'weight': 15.0}
Montreal
New York
{'weight': 3.0}
Panama City
Santiago
{'weight': 31.0}
San Francisco
Seattle
{'weight': 7.0}
Shanghai
Tokyo
{'weight': 11.0}
Total 403.0
CME4422 - INTRODUCTION TO GRAPH THEORY
LAB - 4
4. Find the non-isomorphic tree count of order 7 by using the Python code given below.
import networkx as nx
import matplotlib.pyplot as plt
order = 4
GList = nx.nonisomorphic_trees(order, create='graph')
#print("Non-isomorphic tree count of order ", order, " is ", len(list(
GList)))
for G in GList:
nx.draw_networkx(G)
plt.show()
CME4422 - INTRODUCTION TO GRAPH THEORY
LAB - 4
5. How many different isomers exist for the chemical formula CnH2n+2 when n is 7.
Therefore, for C7H16, the maximum number of possible isomers is 2^7 = 128.