#1 Selection Sort # Changed To Positive Infinity For Correct Comparison
#1 Selection Sort # Changed To Positive Infinity For Correct Comparison
In [5]: #2
import sys
graph = {}
n = int(input("Enter the number of edges: "))
for i in range(n):
edge = input("Enter the edge (source destination weight): ").split()
source, destination, weight = edge[0], edge[1], int(edge[2])
if source not in graph:
graph[source] = {}
graph[source][destination] = weight
In [7]: #3
class Graph:
def __init__(self, vertices):
self.V = vertices
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 1/7
5/5/24, 10:24 PM Assgn3
self.graph = []
def KruskalMST(self):
result = []
i=0
e=0
self.graph = sorted(self.graph, key=lambda item: item[2])
parent = [i for i in range(self.V)]
rank = [0] * self.V
minimumCost = 0
print("Edges in the constructed MST:")
for u, v, weight in result:
minimumCost += weight
print("%d -- %d == %d" % (u, v, weight))
print("Minimum Spanning Tree:", minimumCost)
if __name__ == '__main__':
vertices = int(input("Enter the number of vertices: "))
g = Graph(vertices)
edges = int(input("Enter the number of edges: "))
print("Enter the edges and their weights:")
for i in range(edges):
u, v, w = map(int, input().split())
g.addEdge(u, v, w)
g.KruskalMST()
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 2/7
5/5/24, 10:24 PM Assgn3
In [8]: #4
import sys
class Graph():
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)] for row in range(vertices)]
def primMST(self):
key = [sys.maxsize] * self.V
parent = [None] * self.V
key[0] = 0
mstSet = [False] * self.V
parent[0] = -1
for cout in range(self.V):
u = self.minKey(key, mstSet)
mstSet[u] = True
for v in range(self.V):
if self.graph[u][v] > 0 and mstSet[v] == False and key[v] > self.graph[u][v]:
key[v] = self.graph[u][v]
parent[v] = u
self.printMST(parent)
if __name__ == '__main__':
vertices = int(input("Enter the number of vertices: "))
g = Graph(vertices)
print("Enter the graph matrix:")
for i in range(vertices):
row = list(map(int, input().split()))
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 3/7
5/5/24, 10:24 PM Assgn3
for j in range(vertices):
g.graph[i][j] = row[j]
source_vertex = int(input("Enter the source vertex: "))
g.primMST()
Enter the number of vertices: 5
Enter the graph matrix:
02060
20358
03007
68009
05790
Enter the source vertex: 0
Edge Weight
0-1 2
1-2 3
1-3 8
2-4 7
In [10]: #5
import sys
class Graph():
def __init__(self, vertices):
self.V = vertices
self.graph = [[0 for column in range(vertices)] for row in range(vertices)]
if __name__ == "__main__":
vertices = int(input("Enter the number of vertices: "))
graph = []
print("Enter the graph matrix:")
for i in range(vertices):
row = list(map(int, input().split()))
graph.append(row)
g = Graph(vertices)
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 4/7
5/5/24, 10:24 PM Assgn3
g.graph = graph
src = int(input("Enter the source vertex: "))
g.dijkstra(src)
In [11]: #6
profit = []
jobs = []
deadline = []
for i in range(n):
job = profitNJobs[i]
for j in range(job[2], 0, -1):
if slot[j] == 0:
ans[j] = job[1]
total_profit += job[0]
slot[j] = 1
break
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 5/7
5/5/24, 10:24 PM Assgn3
while unvisited:
current_node = min(unvisited, key=lambda node: distances[node])
unvisited.remove(current_node)
return distances
graph = {}
n = int(input("Enter the number of edges: "))
for i in range(n):
edge = input("Enter the edge (source destination weight): ").split()
source, destination, weight = edge[0], edge[1], int(edge[2])
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 6/7
5/5/24, 10:24 PM Assgn3
In [ ]:
localhost:8889/nbconvert/html/LP2/Assgn3.ipynb?download=false 7/7