Greedy Technique - Dijlstra'S Algorithm, Huffman Trees and Codes
Greedy Technique - Dijlstra'S Algorithm, Huffman Trees and Codes
AIM :
1. Dijkstra’s Algorithm: To find the shortest path from a source node to all other nodes in a weighted
graph.
2. Huffman Coding: To create an optimal prefix code based on the frequencies of characters in a
given string.
ALGORITHM:
Dijkstra’s Algorithm
Algorithm:
1. Initialize distances from the source to all vertices as infinite and to the source itself as 0.
3. For each neighbor of the current vertex, update the distance if the current path is shorter.
Algorithm:
o Create a new internal node with these two nodes as children and with a frequency
equal to the sum of their frequencies.
Huffman Coding:
Character Codes:
h: 00
e: 010
l: 11
o: 011
f: 100
u: 101
m: 110
n: 111
RESULT :
1. Dijkstra’s Algorithm: Successfully calculated the shortest distances from the source vertex to all
other vertices in the graph.
2. Huffman Coding: Successfully generated optimal prefix codes for the characters in the input string.
EX.NO :07
ITERATIVE IMPROVEMENT – SIMPLEX METHOD
DATE:
AIM :
ALGORITHM :
1. Initialization: Start with a feasible solution.
2.Formulate the Simplex tableau: Create the tableau representation of the linear program.
3.Identify Pivot Column: Choose the entering variable (most negative coefficient in the objective
row).
4.Identify Pivot Row: Choose the leaving variable using the minimum ratio test.
5.Pivot: Update the tableau by performing row operations to form a new tableau.
6.Repeat: Go back to step 3 until no more negative coefficients exist in the objective row.
7.Extract Solution: Read off the values of the variables from the tableau.
PROGARAM :
#include <iostream>
#include <vector>
#include <iomanip>
#include <limits>
using namespace std;
void printTableau(const vector<vector<double>>& tableau) {
for (const auto& row : tableau) {
for (double val : row) {
cout << setw(10) << setprecision(4) << val << " ";
}
cout << endl;
}
cout << endl;
}
void simplexMethod(vector<vector<double>> tableau) {
int m = tableau.size();
int n = tableau[0].size(); // Number of variables + slack variables
while (true) {
int pivotCol = -1;
double mostNegative = 0;
for (int j = 0; j < n - 1; j++) {
if (tableau[m - 1][j] < mostNegative) {
mostNegative = tableau[m - 1][j];
pivotCol = j;
}
}
if (pivotCol == -1) break;
int pivotRow = -1;
double minRatio = numeric_limits<double>::max();
for (int i = 0; i < m - 1; i++) {
if (tableau[i][pivotCol] > 0) {
double ratio = tableau[i][n - 1] / tableau[i][pivotCol];
if (ratio < minRatio) {
minRatio = ratio;
pivotRow = i;
}
}
}
double pivotValue = tableau[pivotRow][pivotCol];
for (int j = 0; j < n; j++) {
tableau[pivotRow][j] /= pivotValue;
}
for (int i = 0; i < m; i++) {
if (i != pivotRow) {
double factor = tableau[i][pivotCol];
for (int j = 0; j < n; j++) {
tableau[i][j] -= factor * tableau[pivotRow][j];
}
}
}
cout << "Current tableau:" << endl;
printTableau(tableau);
}
cout << "Optimal Solution:" << endl;
for (int j = 0; j < n - 1; j++) {
cout << "x" << j + 1 << " = ";
double value = 0;
for (int i = 0; i < m - 1; i++) {
if (tableau[i][j] == 1) {
value = tableau[i][n - 1];
break;
}
}
cout << value << endl;
}
cout << "Optimal Value of Objective Function: " << tableau[m - 1][n - 1] << endl;
}
int main() {
vector<vector<double>> tableau = {
{2, 1, 1, 0, 4}, // Constraint 1
{1, 2, 0, 1, 3}, // Constraint 2
{-3, -2, 0, 0, 0} // Objective Function (last row)
};
cout << "Initial tableau:" << endl;
printTableau(tableau);
simplexMethod(tableau);
return 0;
}
RESULT :
Initial tableau:
2.0000 1.0000 1.0000 0.0000 4.0000
1.0000 2.0000 0.0000 1.0000 3.0000
-3.0000 -2.0000 0.0000 0.0000 0.0000
Current tableau:
1.0000 0.0000 0.5000 -0.5000 2.0000
0.0000 1.0000 -0.5000 0.5000 1.0000
0.0000 -0.0000 1.5000 1.5000 6.0000
Optimal Solution:
x1 = 2
x2 = 1
Optimal Value of Objective Function: 6
RESULT :
Thus, the above program was successfully executed and output was verified.