0% found this document useful (0 votes)
3 views8 pages

Lab9 193

DAA lab

Uploaded by

vikaspant990
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)
3 views8 pages

Lab9 193

DAA lab

Uploaded by

vikaspant990
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/ 8

LAB 9

Name: Vikas Pant

Roll number: 2200290110193

Sem-Sec-Group: 5 C 2

AIM: Write a program to implement Dijkstra Algorithm.

Dijkstra Algorithm :

Source Code :

#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

// Function to find the vertex with the smallest distance


int findMinDistanceVertex(vector<int> &distance, vector<bool> &visited, int V) {
int minDistance = INT_MAX;
int minVertex = -1;
for (int i = 0; i < V; ++i) {
if (!visited[i] && distance[i] < minDistance) {
minDistance = distance[i];
minVertex = i;
}
}

return minVertex;
}

// Dijkstra's Algorithm implementation


void dijkstra(vector<vector<int>> &graph, int source) {
int V = graph.size();
vector<int> distance(V, INT_MAX); // Stores the shortest distance from source to each
vertex
vector<bool> visited(V, false); // Tracks visited vertices

distance[source] = 0; // Distance to the source is 0

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


int u = findMinDistanceVertex(distance, visited, V);
visited[u] = true;

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


if (graph[u][v] && !visited[v] && distance[u] != INT_MAX &&
distance[u] + graph[u][v] < distance[v]) {
distance[v] = distance[u] + graph[u][v];
}
}
}

// Print the shortest distances


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

int main() {
int V, E, source;
cout << "Enter the number of vertices: ";
cin >> V;
cout << "Enter the number of edges: ";
cin >> E;

vector<vector<int>> graph(V, vector<int>(V, 0));

cout << "Enter the edges (source, destination, weight):" << endl;
for (int i = 0; i < E; ++i) {
int u, v, w;
cin >> u >> v >> w;
graph[u][v] = w;
graph[v][u] = w; // Uncomment this line for undirected graph
}

cout << "Enter the source vertex: ";


cin >> source;

dijkstra(graph, source);

return 0;
}
Output:
AIM: Write a program to implement Greedy Knapsack Problem.

Greedy Knapsack Problem :

Source Code :
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// A structure to represent an item with value, weight, and value-to-weight ratio


struct Item {
int value, weight;

// Constructor
Item(int v, int w) : value(v), weight(w) {}
};

// Comparator function to sort items by value-to-weight ratio in descending order


bool compare(Item a, Item b) {
double r1 = (double)a.value / a.weight;
double r2 = (double)b.value / b.weight;
return r1 > r2;
}

// Function to solve the fractional knapsack problem


double fractionalKnapsack(int W, vector<Item> &items) {
// Sort items based on value-to-weight ratio
sort(items.begin(), items.end(), compare);

double totalValue = 0.0; // Maximum value in the knapsack


int currentWeight = 0; // Current weight of the knapsack

for (auto &item : items) {


if (currentWeight + item.weight <= W) {
// If the item can be fully added
currentWeight += item.weight;
totalValue += item.value;
} else {
// If the item can only be added partially
int remainingWeight = W - currentWeight;
totalValue += item.value * ((double)remainingWeight / item.weight);
break;
}
}

return totalValue;
}

int main() {
int n, W;

cout << "Enter the number of items: ";


cin >> n;

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


cin >> W;

vector<Item> items;

cout << "Enter the value and weight of each item:" << endl;
for (int i = 0; i < n; ++i) {
int value, weight;
cin >> value >> weight;
items.emplace_back(value, weight);
}

double maxValue = fractionalKnapsack(W, items);


cout << "Maximum value in the knapsack: " << maxValue << endl;

return 0;
}
Output:

You might also like