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

### 1. Job Sequencing with Deadline and knapsack

The document discusses various optimization problems including Job Sequencing with Deadlines, Fractional Knapsack Problem, 0/1 Knapsack Problem, and Dijkstra's Algorithm for shortest paths. It outlines the steps to maximize profits in job scheduling, calculate maximum values in knapsack problems, and find shortest paths in graphs. Each section provides a structured approach to solving these problems using greedy and dynamic programming methods.

Uploaded by

megh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

### 1. Job Sequencing with Deadline and knapsack

The document discusses various optimization problems including Job Sequencing with Deadlines, Fractional Knapsack Problem, 0/1 Knapsack Problem, and Dijkstra's Algorithm for shortest paths. It outlines the steps to maximize profits in job scheduling, calculate maximum values in knapsack problems, and find shortest paths in graphs. Each section provides a structured approach to solving these problems using greedy and dynamic programming methods.

Uploaded by

megh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

### 1.

**Job Sequencing with Deadlines**

We are given a set of jobs, each with a deadline and a profit, and the objective is
to maximize the total profit by scheduling these jobs within their deadlines. The
jobs are:

| Job | Deadline | Profit |


|------|----------|--------|
| A | 2 | 100 |
| B | 1 | 19 |
| C | 2 | 27 |
| D | 1 | 25 |
| E | 3 | 15 |

#### Steps:
1. **Sort Jobs by Profit**: To maximize profit, we first sort the jobs in
descending order of profit.

| Job | Deadline | Profit |


|------|----------|--------|
| A | 2 | 100 |
| D | 1 | 25 |
| C | 2 | 27 |
| B | 1 | 19 |
| E | 3 | 15 |

2. **Schedule Jobs**: Now, schedule each job based on its deadline, checking the
latest available slot.

- **Job A**: Deadline = 2 → Schedule at time slot 2.


- **Job D**: Deadline = 1 → Schedule at time slot 1.
- **Job C**: Deadline = 2 → Slot 2 is taken, so Job C cannot be scheduled.
- **Job B**: Deadline = 1 → Slot 1 is taken, so Job B cannot be scheduled.
- **Job E**: Deadline = 3 → Schedule at time slot 3.

#### Final Schedule:


- Time Slot 1: Job D (Profit = 25)
- Time Slot 2: Job A (Profit = 100)
- Time Slot 3: Job E (Profit = 15)

#### Maximum Profit:


Total profit = \( 100 + 25 + 15 = 140 \)

### 2. **Fractional Knapsack Problem**

We are given a knapsack with a capacity of 50 kg and items with the following
weights and values:

| Item | Weight | Value |


|------|--------|-------|
| 1 | 10 | 60 |
| 2 | 20 | 100 |
| 3 | 30 | 120 |

#### Steps:
1. **Calculate Value per Unit Weight**: To maximize the value, we should consider
the items with the highest value per weight first.

| Item | Weight | Value | Value/Weight |


|------|--------|-------|--------------|
| 1 | 10 | 60 | 6.0 |
| 2 | 20 | 100 | 5.0 |
| 3 | 30 | 120 | 4.0 |

2. **Sort by Value per Unit Weight**: The items are already sorted.

3. **Fill the Knapsack**:


- Take all of Item 1 (10 kg), total value = \( 60 \).
- Take all of Item 2 (20 kg), total value = \( 100 \). Now the knapsack holds 30
kg.
- Take \( \frac{20}{30} = \frac{2}{3} \) of Item 3 (20 kg), total value = \( \
frac{2}{3} \times 120 = 80 \).

#### Maximum Value:


Total value = \( 60 + 100 + 80 = 240 \).

### 3. **0/1 Knapsack Problem**

We have a knapsack with a capacity of 10 kg, and the following items:

| Item | Weight | Value |


|------|--------|-------|
| 1 | 2 | 12 |
| 2 | 1 | 10 |
| 3 | 3 | 20 |
| 4 | 2 | 15 |

#### Greedy Approach:


The Greedy method in the 0/1 knapsack problem typically fails because it doesn't
always give the optimal solution. It selects the items based on the highest value
per unit weight:

| Item | Weight | Value | Value/Weight |


|------|--------|-------|--------------|
| 2 | 1 | 10 | 10.0 |
| 1 | 2 | 12 | 6.0 |
| 4 | 2 | 15 | 7.5 |
| 3 | 3 | 20 | 6.67 |

**Greedy Selection**:
- Select Item 2 (1 kg), total value = 10.
- Select Item 4 (2 kg), total value = 15. Now the knapsack holds 3 kg.
- Select Item 3 (3 kg), total value = 20. Now the knapsack holds 6 kg.
- Select Item 1 (2 kg), total value = 12. Now the knapsack holds 8 kg.

Total weight = 8 kg, total value = \( 10 + 15 + 20 + 12 = 57 \).

However, the optimal solution is obtained using dynamic programming, not the greedy
method.

#### Why Greedy Fails:


The greedy approach may not always yield the best result because it doesn't
consider combinations of items. The dynamic programming approach is better suited
for 0/1 knapsack problems because it explores all possible combinations to find the
maximum value.

### 4. **0/1 Knapsack Problem with 4 Objects**


We have a knapsack with a capacity of 15 kg and the following items:

| Item | Profit | Weight |


|------|--------|--------|
| 1 | 10 | 3 |
| 2 | 5 | 4 |
| 3 | 7 | 3 |
| 4 | 11 | 5 |

#### Steps:
1. **Dynamic Programming Approach**:
- Create a DP table where the rows represent the items and the columns represent
the capacity from 0 to 15.
- Fill the table by considering two possibilities for each item:
- Exclude the item.
- Include the item if its weight is less than or equal to the current
capacity.

#### Optimal Solution:


The optimal solution includes Items 1, 3, and 4, with a total weight of 11 kg and a
total profit of \( 10 + 7 + 11 = 28 \).

### 5. **Single-Source Shortest Path Algorithm (Dijkstra’s Algorithm)**

For a given graph, we can use Dijkstra’s algorithm to find the shortest path from a
source vertex to all other vertices. Here’s the general process:

1. **Initialization**: Set the distance of the source vertex to 0 and all other
vertices to infinity.
2. **Update Distances**: For each vertex, update the distances of its adjacent
vertices by adding the edge weight to the current distance.
3. **Choose Minimum Distance**: Select the vertex with the minimum distance that
hasn’t been visited yet, and repeat the process until all vertices are visited.

#### Directed and Undirected Graphs:


- **Directed Graph**: Dijkstra’s algorithm works similarly, but only considers
edges that go from one vertex to another (not bidirectional).
- **Undirected Graph**: All edges can be traversed in both directions, so each edge
is considered bidirectionally.

### 6. **Dijkstra's Algorithm to Find the Shortest Path from Vertex 1 to Vertex 7**

We are asked to find the shortest path from vertex 1 to vertex 7 using Dijkstra's
algorithm. Assuming the graph is provided, the following steps would be performed:

1. **Initialize Distances**: Set the distance of vertex 1 to 0 and all other


vertices to infinity.
2. **Update**: From vertex 1, find the vertex with the smallest distance (initially
the direct neighbors).
3. **Iterate**: Repeat this process by visiting the vertex with the smallest known
distance until vertex 7 is reached.

#### Example:

If the graph has the following edge weights:

- \( 1 \rightarrow 2 = 5 \)
- \( 2 \rightarrow 3 = 7 \)
- \( 3 \rightarrow 7 = 9 \)
- \( 1 \rightarrow 4 = 10 \)
- \( 4 \rightarrow 7 = 15 \)

Dijkstra’s algorithm will explore the shortest path from vertex 1 to vertex 7,
which in this case may follow the path \( 1 \rightarrow 2 \rightarrow 3 \rightarrow
7 \).

You might also like