Implement Greedy Search Algorithm for Selection Sort.
import sys, heapq
from collections import defaultdict
from math import inf
from rich import print
def selectionSort(A):
U = [Link]()
for i in range(len(A)):
min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]:
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i]
print(f'Selection Sort:\nUnsorted array: {U}\nSorted array: {A}')
def jobScheduling(arr):
n = len(arr)
[Link](key=lambda x: x[1])
result = []
maxHeap = []
for i in range(n - 1, -1, -1):
if i == 0:
slots_available = arr[i][1]
else:
slots_available = arr[i][1] - arr[i - 1][1]
[Link](maxHeap, (-arr[i][2], arr[i][1], arr[i][0]))
while slots_available and maxHeap:
profit, deadline, job_id = [Link](maxHeap)
slots_available -= 1
[Link]([job_id, deadline])
[Link](key=lambda x: x[1])
print(f'\n\nJob Scheduling Problem:\nFollowing is maximum profit sequence of
jobs: {result}')
class PGraph:
def __init__(self, vertices, graph):
self.V = vertices
[Link] = graph
def minKey(self, key, mstSet):
min = [Link]
for v in range(self.V):
if key[v] < min and mstSet[v] == False:
min = key[v]
min_index = v
return min_index
def primMST(self):
key = [[Link]] * self.V
parent = [None] * self.V
key[0] = 0
mstSet = [False] * self.V
parent[0] = -1
for cout in range(self.V):
u = [Link](key, mstSet)
mstSet[u] = True
for v in range(self.V):
if [Link][u][v] > 0 and mstSet[v] == False and key[v] >
[Link][u][v]:
key[v] = [Link][u][v]
parent[v] = u
print(f'\n\nPrim’s Minimum Spanning Tree:\nEdge \tWeight')
minimumCost = 0
for i in range(1, self.V):
print(f'{parent[i]} -- {i} == {[Link][i][parent[i]]}')
minimumCost += [Link][i][parent[i]]
print(f'Minimum cost = {minimumCost}')
class KGraph:
def __init__(self, vertices, graph):
self.V = vertices
[Link] = []
for i in range(self.V):
for j in range(i, self.V):
if graph[i][j] != 0:
[Link]([i, j, graph[i][j]])
def find(self, parent, i):
if parent[i] == i:
return i
return [Link](parent, parent[i])
def union(self, parent, rank, x, y):
xroot = [Link](parent, x)
yroot = [Link](parent, y)
if rank[xroot] < rank[yroot]:
parent[xroot] = yroot
elif rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else:
parent[yroot] = xroot
rank[xroot] += 1
def KruskalMST(self):
result = []
i = 0
e = 0
[Link] = sorted([Link], key = lambda item: item[2])
parent = []
rank = []
for node in range(self.V):
[Link](node)
[Link](0)
while e < self.V - 1:
u, v, w = [Link][i]
i = i + 1
x = [Link](parent, u)
y = [Link](parent, v)
if x != y:
e = e + 1
[Link]([u, v, w])
[Link](parent, rank, x, y)
minimumCost = 0
print(f'\n\nKruskal’s Minimum Spanning Tree:\nEdge \tWeight')
for u, v, weight in result:
minimumCost += weight
print(f'{u} -- {v} == {weight}')
print(f'Minimum cost = {minimumCost}')
def dijkstra(inp: list, source: str, dest: str) -> int:
graph = defaultdict(list)
weight = {}
path = [dest]
for i in range(len(inp)):
for j in range(i, len(inp)):
if inp[i][j] != 0:
s, d, c = i, j, inp[i][j]
graph[s].append(d)
weight[f'{s} {d}'] = int(c)
graph[d].append(s)
weight[f'{d} {s}'] = int(c)
Q = list([Link]())
A, d, p = [], {}, defaultdict(list)
for v in Q:
d[v] = inf
p[v] = []
d[source] = 0
while Q:
u = min(Q, key=lambda x: d[x])
[Link](u)
[Link](u)
for v in set(graph[u]).intersection(Q):
alt = d[u] + weight[f'{u} {v}']
if d[v] > alt:
d[v] = alt
p[v].append(u)
if u == dest:
break
key = dest
while p[key]:
[Link](p[key][-1])
key = p[key][-1]
[Link]()
print(f'\n\nDijkstra Single-Source Shortest Path:\nPath: {path}\nMinimum Cost:
{d[dest]}\n\n')
if __name__ == '__main__':
A = [64, 25, 12, 22, 11]
selectionSort(A)
A = [
['A', 2, 100],
['B', 1, 19],
['C', 2, 27],
['D', 1, 25],
['E', 3, 15]
]
jobScheduling(A)
graph = [
[0, 4, 0, 0, 0, 0, 0, 8, 0],
[4, 0, 8, 0, 0, 0, 0,11, 0],
[0, 8, 0, 7, 0, 4, 0, 0, 2],
[0, 0, 7, 0, 9,14, 0, 0, 0],
[0, 0, 0, 9, 0,10, 0, 0, 0],
[0, 0, 4,14,10, 0, 2, 0, 0],
[0, 0, 0, 0, 0, 2, 0, 1, 6],
[8,11, 0, 0, 0, 0, 1, 0, 7],
[0, 0, 2, 0, 0, 0, 6, 7, 0]
]
g = PGraph(9, graph)
[Link]()
g = KGraph(9, graph)
[Link]()
dijkstra(graph, 0, 4)