Algorithms ADA
Algorithms ADA
Fractional Knapsack:
1. Store jobs as pairs of (Profit, Deadline): Since we need to prioritize jobs with higher
profits, we pair the profit and deadline together.
2. Sort Jobs Based on Profit: We sort the jobs array in descending order of profit so that
we prioritize scheduling the most profitable jobs first.
3. Create a Slot Array: We create a slot [] array of size n (equal to the number of jobs)
initialized with zeroes. This array will help track which time slots are occupied.
4. Iterate Over Each Job and Try to Schedule It:
a. For each job, check if it can be placed in an available time slot.
b. The job should be scheduled as late as possible but before its deadline.
c. If an empty slot is found, schedule the job there, increment the job count,
and add its profit to the total.
d. After processing all jobs, return the number of jobs completed and the total
profit earned.
Prim's Algorithm
Step 1: Determine an arbitrary vertex as the starting vertex of the MST. We pick 0 in the
below diagram.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in the MST (known as
fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step 5: Add the chosen edge to the MST. Since we consider only the edges that connect
fringe vertices with the rest, we never get a cycle.
Kruskal's Algorithm:
2. Initialize a forest (a set of trees where each vertex is its own tree)
b. Otherwise, discard it
4. Repeat until there are (V-1) edges in the MST (where V is number of vertices)
1. Divide the search space into two halves by finding the middle index "mid".
2. Compare the middle element of the search space with the key.
3. If the key is found at middle element, the process is terminated.
4. If the key is not found at middle element, choose which half will be used as the next
search space.
5. If the key is smaller than the middle element, then the left side is used for next
search.
6. If the key is larger than the middle element, then the right side is used for next
search.
7. This process is continued until the key is found or the total search space is
exhausted.
Multistage Graph:
1. Input: A weighted multistage graph G with s and t as source and target vertices,
respectively.
2. Output: The shortest path from s to t in G.
3. Set d(t) = 0 and d(v) = ? for all other vertices v in G.
4. For i = k-1 to 1:
5. a. For each vertex v in stage i:
6. i. Set d(v) = min(w(v, u) + d(u)) for all vertices u in stage i+1.
7. Return d(s) as the shortest path from s to t.
Graph Coloring:
1. Create a recursive function that takes the graph, current index, number of vertices,
and color array.
2. If the current index is equal to the number of vertices. Print the color configuration
in the color array.
3. Assign a color to a vertex from the range (1 to m).
4. For every assigned color, check if the configuration is safe, (i.e. check if the
adjacent vertices do not have the same color) and recursively call the function with
the next index and number of vertices else return false
5. If any recursive function returns true then break the loop and return true
6. If no recursive function returns true then return false
Hamiltonian Cycle:
3. Prune branches where the remaining elements cannot possibly reach the target
sum