0% found this document useful (0 votes)
54 views

Design and Analysis of Algorithm

This document discusses two algorithms: 1) The job sequencing with deadline algorithm takes as input jobs with deadlines and profits, and outputs a sequence of jobs that maximizes total profit while meeting all deadlines. It works by sorting jobs by profit, then greedily selecting the most profitable job possible at each step without violating any deadlines. 2) Dijkstra's algorithm finds the shortest path between all pairs of vertices in a graph with non-negative edge weights. It maintains a distance array showing the shortest path found so far to each vertex. It iteratively selects the unvisited vertex with the smallest distance, marks it visited, and updates distances to neighboring unvisited vertices if a shorter path is found through the

Uploaded by

Sk Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Design and Analysis of Algorithm

This document discusses two algorithms: 1) The job sequencing with deadline algorithm takes as input jobs with deadlines and profits, and outputs a sequence of jobs that maximizes total profit while meeting all deadlines. It works by sorting jobs by profit, then greedily selecting the most profitable job possible at each step without violating any deadlines. 2) Dijkstra's algorithm finds the shortest path between all pairs of vertices in a graph with non-negative edge weights. It maintains a distance array showing the shortest path found so far to each vertex. It iteratively selects the unvisited vertex with the smallest distance, marks it visited, and updates distances to neighboring unvisited vertices if a shorter path is found through the

Uploaded by

Sk Shetty
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q) Explain job sequencing with deadline algorithm

In job sequencing problem, the objective is to find a sequence of jobs, which is


completed within their deadlines and gives maximum profit.

Algorithm

Algorithm: Job-Sequencing-With-Deadline (D, J, n, k)


D(0) := J(0) := 0
k := 1
J(1) := 1 // means first job is selected
for i = 2 … n do
r := k
while D(J(r)) > D(i) and D(J(r)) ≠ r do
r := r – 1
if D(J(r)) ≤ D(i) and D(i) > r then
for l = k … r + 1 by -1 do
J(l + 1) := J(l)
J(r + 1) := i
k := k + 1

Analysis
In this algorithm, we are using two loops, one is within another. Hence, the
complexity of this algorithm is O(n2).

Example
We have to find a sequence of jobs, which will be completed within their deadlines
and will give maximum profit. Each job is associated with a deadline and profit.

To solve this problem, the given jobs are sorted according to their profit in a
descending order. Hence, after sorting, the jobs are ordered as shown in the following
table.
From this set of jobs, first we select J2, as it can be completed within its deadline and
contributes maximum profit.

Next, J1 is selected as it gives more profit compared to J4.

In the next clock, J4 cannot be selected as its deadline is over, hence J3 is selected as
it executes within its deadline.

The job J5 is discarded as it cannot be executed within its deadline.

Thus, the solution is the sequence of jobs (J2, J1, J3), which are being executed
within their deadline and gives maximum profit.

Total profit of this sequence is 100 + 60 + 20 = 180.

Dijkstra's Algorithm
It is a greedy algorithm that solves the single-source shortest path problem for a
directed graph G = (V, E) with nonnegative edge weights, i.e., w (u, v) ≥ 0 for each
edge (u, v) ∈ E.

Algorithm
AlgorithmDijkstra (v0, W, dist, n)
// v0 is the source vertex, W is the adjacency matrix to store the weights of the links,
dist[k] is the array
to store the shortest path to vertex k, n is the number of vertices//
{
for (i = 1 ; i < = n ; i ++){
S[i] = 0; // Initialize the set S to empty i.e. i is not inserted into the set//
dist[i] = w(v0,i) ; //Initialize the distance to each node
}
S[vo] = 1; dist[v0] = 0;
for (j = 2; j<= n; j++) {
choose a vertex u from those vertices not in S such that dist [u] is minimum.
S[u] = 1;
for (each z adjacent to u with S[z] = 0) {
if(dist[z] >dist [u] + w[u, z])
dist[z] = dist [u] + w[u, z];
}
}
}

Analysis: The running time of Dijkstra's algorithm on a graph with edges E and
vertices V can be expressed as a function of |E| and |V| using the Big - O notation.
The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an
ordinary linked list or array, and operation Extract - Min (Q) is simply a linear search
through all vertices in Q. In this case, the running time is O (|V2 |+|E|=O(V2 ).

Example

Use Dijkstras algorithm to find the shortest path from A to each of the other six
vertices in the graph:

You might also like