0% found this document useful (0 votes)
5 views4 pages

DAA1314

The document contains two algorithms: Dijkstra's Algorithm for finding the shortest path in a graph and the Fractional Knapsack Problem for maximizing value within a weight limit. The Dijkstra's implementation includes functions for calculating minimum distances and printing results, while the Fractional Knapsack implementation sorts items by value-to-weight ratio and calculates the maximum value obtainable. Both algorithms are implemented in C++ with sample input and output instructions.

Uploaded by

shekhar22302
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)
5 views4 pages

DAA1314

The document contains two algorithms: Dijkstra's Algorithm for finding the shortest path in a graph and the Fractional Knapsack Problem for maximizing value within a weight limit. The Dijkstra's implementation includes functions for calculating minimum distances and printing results, while the Fractional Knapsack implementation sorts items by value-to-weight ratio and calculates the maximum value obtainable. Both algorithms are implemented in C++ with sample input and output instructions.

Uploaded by

shekhar22302
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/ 4

Shekhar Krishna 2100320130157

Dijkstra’s Algorithm

Input:-
#include <iostream>
using namespace std;
#include <limits.h>

#define V 9

int minDistance(int dist[], bool sptSet[])


{

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)


if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

void printSolution(int dist[])


{
cout << "Vertex \t Distance from Source" << endl;
for (int i = 0; i < V; i++)
cout << i << " \t\t\t\t" << dist[i] << endl;
}

void dijkstra(int graph[V][V], int src)


{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;

for (int count = 0; count < V - 1; count++) {


int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v]
&& dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}
Output:-
Shekhar Krishna 2100320130157
Fractional Knapsack Problem

Input:-
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;


bool compare(pair <float, int> p1, pair <float, int> p2)
{
return p1.first > p2.first;
}

int fractional_knapsack(vector <int> weights, vector <int> values, int capacity)


{
int len = weights.size();
int total_value = 0;
vector <pair <float, int>> ratio(len, make_pair(0.0, 0));

for(int i = 0; i < len; i++)


ratio[i] = make_pair(values[i]/weights[i], i);

sort(ratio.begin(), ratio.end(), compare);


for(int i = 0; i < len; i++)
{
if(capacity == 0)
break;

int index = ratio[i].second;

if(weights[index] <= capacity)


{
capacity -= weights[index];
total_value += values[index];
}
else
{

int value_to_consider = values[index] * (float(capacity)/float(weights[index]));


total_value += value_to_consider;
capacity = 0;
}
}

return total_value;
}

int main()
{
cout << "Enter the weights of the items, press -1 to stop" << endl;

vector <int> weights;

while(true)
{
int weight;
cin >> weight;
if(weight == -1)
break;

weights.push_back(weight);
}

cout << "Enter the values of each item, press -1 to stop" << endl;

vector <int> values;

while(true)
{
int value;
cin >> value;

if(value == -1)
break;

values.push_back(value);
}

cout << "Enter the capacity of the knapsack" << endl;

int capacity;
cin >> capacity;

cout << "The maximum value possible based on current list is: " << fractional_knapsack(weights, values, capacity)
<< endl;
}
Output:-

You might also like