0% found this document useful (0 votes)
2 views3 pages

Unit 2 Algos

Uploaded by

Arshad
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)
2 views3 pages

Unit 2 Algos

Uploaded by

Arshad
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/ 3

1.

Job Sequencing with Deadlines

Alg: JobSequencing(jobs, n)
Sort jobs by profit (descending);
Initialize slots[] = {0}; totalProfit = 0;

For each job in jobs do:


For j = job.deadline down to 1 do:
If slots[j] == 0 then:
Assign job to slot[j];
totalProfit += job.profit;
Break;

Return totalProfit;
Explanation:

1. Sort jobs by pro t.


2. For each job, try to nd the latest available slot before its deadline.
3. If a slot is found, assign the job and add its pro t.
4. Return the total pro t after scheduling all jobs.

2. Single Source Shortest Path (Dijkstra's Algorithm)

Alg: Dijkstra(graph, src, n)


Initialize dist[] = {∞}; dist[src] = 0;
Initialize visited[] = {false};

For i = 0 to n-1 do:


u = Select vertex with smallest dist[];
Mark u as visited;

For each neighbor v of u do:


If v is not visited and dist[u] + edge < dist[v]
then:
dist[v] = dist[u] + edge;

Return dist[];
Explanation:

1. Initialize all distances to in nity, except the source which is 0.


2. Repeatedly select the vertex with the smallest distance.
3. Update distances of its neighbors.
4. Repeat until all vertices are visited, then return the shortest distances.
fi
fi
fi
fi
fi
3. Prim's Algorithm

Alg: Prim(graph, n)
Initialize dist[] = {∞}; dist[0] = 0;
Initialize visited[] = {false};

For i = 0 to n-1 do:


u = Select vertex with smallest dist[];
Mark u as visited;

For each neighbor v of u do:


If v is not visited and weight(u, v) < dist[v]
then:
dist[v] = weight(u, v);

Return dist[];
Explanation:

1. Initialize distances for all vertices except the start vertex.


2. Use a loop to select the vertex with the smallest distance.
3. Update the distances of its neighbors.
4. Repeat until all vertices are included, and return the distance array.

4. Kruskal's Algorithm

Alg: Kruskal(edges, n)
Sort edges by weight;
Initialize parent[] = {i}; rank[] = {0};

For each edge (u, v) do:


If Find(u) ≠ Find(v) then:
Add edge to MST;
Union(u, v);

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:

1. Sort edges by weight.


2. For each edge, use Find to check if adding it forms a cycle.
3. If no cycle, add the edge to the MST and combine the sets using Union.
4. Return the MST once it contains the required number of edges.

You might also like