Unit 2 Algos
Unit 2 Algos
Alg: JobSequencing(jobs, n)
Sort jobs by profit (descending);
Initialize slots[] = {0}; totalProfit = 0;
Return totalProfit;
Explanation:
Return dist[];
Explanation:
Alg: Prim(graph, n)
Initialize dist[] = {∞}; dist[0] = 0;
Initialize visited[] = {false};
Return dist[];
Explanation:
4. Kruskal's Algorithm
Alg: Kruskal(edges, n)
Sort edges by weight;
Initialize parent[] = {i}; rank[] = {0};
Return MST;
Alg: Find(x)
If parent[x] ≠ x then:
parent[x] = Find(parent[x]);
Return parent[x];
Alg: Union(x, y)
rootX = Find(x); rootY = Find(y);
If rootX ≠ rootY then:
If rank[rootX] < rank[rootY] then:
parent[rootX] = rootY;
Else:
parent[rootY] = rootX;
Explanation: