Skip to content

Commit

Permalink
Code_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sansh2356 committed Mar 11, 2024
0 parents commit 68777c8
Show file tree
Hide file tree
Showing 17 changed files with 994 additions and 0 deletions.
90 changes: 90 additions & 0 deletions ADA/Activity_selection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class activity
{
public:
int start;
int finish;
int duration;

activity(int start, int finish)
{
this->start = start;
this->finish = finish;
this->duration = finish - start;
}
};

vector<activity> generateRandomActivities(int numActivities)
{
vector<activity> activities;

for (int i = 0; i < numActivities; ++i)
{
int start = rand() % 10;
int duration = rand() % (10 - start) + 1; // Ensure duration is at least 1q
activities.push_back(activity(start, start + duration));
}

return activities;
}
bool cmpDuration(activity a, activity b)
{
return b.duration > a.duration;
}

bool cmpStart(activity a, activity b)
{
return b.start > a.start;
}

bool cmpEnd(activity a, activity b)
{
return b.finish > a.finish;
}

vector<int> greedyActivitySelector(vector<activity> &activities)
{
vector<int> solution;
solution.push_back(0);
int k = 0;

for (int i = 1; i < activities.size(); i++)
{
if (activities[k].finish <= activities[i].start)
{
solution.push_back(i);
k = i;
}
}

return solution;
}

int sortFinish(vector<activity> &activities)
{
sort(activities.begin(), activities.end(), cmpEnd);
vector<int> finish = greedyActivitySelector(activities);
return finish.size();
}

int sortDuration(vector<activity> &activities)
{
sort(activities.begin(), activities.end(), cmpDuration);
vector<int> duration = greedyActivitySelector(activities);
return duration.size();
}

int sortStart(vector<activity> &activities)
{
sort(activities.begin(), activities.end(), cmpStart);
vector<int> start = greedyActivitySelector(activities);
return start.size();
}

int main()
{
}
80 changes: 80 additions & 0 deletions ADA/Dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <vector>
#include <queue>
#include <limits>

using namespace std;

const long long INF = 10e9;

vector<vector<pair<int, int>>> graph;
vector<int> dist(graph.size(),INF);
vector<int> parent(graph.size(),-1);

void dijkstra(int src) {


dist[src] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, src});

while (!pq.empty()) {
int u = pq.top().second;
pq.pop();

if (dist[u] == INF) continue;

for (auto& edge : graph[u]) {
int v = edge.first;
int weight = edge.second;

if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
parent[v] = u;
}
}
}
}

void printPath(int dest) {
if (parent[dest] == -1) {
cout << dest << " ";
return;
}

printPath(parent[dest]);
cout << dest << " ";
}

int main() {
int n, m;
cin >> n >> m;

graph.assign(n, vector<pair<int, int>>());

for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;

graph[u].push_back({v, w});
graph[v].push_back({u, w});
}

int src;
cin >> src;

dijkstra(src);

for (int i = 0; i < dist.size(); i++) {
if (i == src) continue;

cout << "Shortest distance from " << src << " to " << i << ": " << dist[i] << endl;
cout << "Path: ";
printPath(i);
cout << endl;
}

return 0;
}
57 changes: 57 additions & 0 deletions ADA/Iterative_quicksort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include <chrono>

using namespace std;
using namespace chrono;

void quicksort(vector<int>& arr) {
vector<pair<int, int>> s;
s.push_back({0, arr.size()});

while (!s.empty()) {
auto p = s.back();
s.pop_back();

int left = p.first;
int right = p.second;

if (right - left <= 1) {
continue;
}

int pivot = left + (right - left) / 2;
swap(arr[pivot], arr[right - 1]);

int store = left;
for (int i = left; i < right - 1; i++) {
if (arr[i] < arr[right - 1]) {
swap(arr[i], arr[store]);
store++;
}
}

swap(arr[right - 1], arr[store]);

s.push_back({left, store});
s.push_back({store + 1, right});
}
}

int main() {
vector<int> arr = {5, 12, 7, 1, 13, 2, 23, 11, 18};

auto start = high_resolution_clock::now();
quicksort(arr);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);

cout << "Execution time: " << duration.count() << " microseconds" << endl;

for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
103 changes: 103 additions & 0 deletions ADA/Knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Item {
int weight;
int profit;
double ratio;
};

bool compareWeight(Item a, Item b) {
return a.weight < b.weight;
}

bool compareProfit(Item a, Item b) {
return a.profit > b.profit;
}

bool compareRatio(Item a, Item b) {
return a.ratio > b.ratio;
}

double fractionalKnapsack(vector<Item>& items, int capacity) {
sort(items.begin(), items.end(), compareWeight);

double totalProfit = 0.0;
for (auto& item : items) {
if (item.weight <= capacity) {
capacity -= item.weight;
totalProfit += item.profit;
} else {
totalProfit += item.profit * (double)capacity / item.weight;
capacity = 0;
break;
}
}

return totalProfit;
}

double fractionalKnapsackProfit(vector<Item>& items, int capacity) {
sort(items.begin(), items.end(), compareProfit);

vector<Item> items2;
for (auto& item : items) {
Item newItem = item;
newItem.weight = 1;
newItem.profit = (double)item.profit / item.weight;
items2.push_back(newItem);
}

return fractionalKnapsack(items2, capacity);
}

double fractionalKnapsackRatio(vector<Item>& items, int capacity) {
sort(items.begin(), items.end(), compareRatio);

double totalProfit = 0.0;
for (auto& item : items) {
if (item.weight <= capacity) {
capacity -= item.weight;
totalProfit += item.profit;
} else {
totalProfit += item.profit * (double)capacity / item.weight;
capacity = 0;
break;
}
}

return totalProfit;
}

int main() {
int n, capacity;
cin >> n >> capacity;

vector<Item> items(n);
for (int i = 0; i < n; i++) {
cin >> items[i].weight >> items[i].profit;
items[i].ratio = (double)items[i].profit / items[i].weight;
}

double totalProfit1 = fractionalKnapsack(items, capacity);
cout << "Sort by weight: " << totalProfit1 << endl;

double totalProfit2 = fractionalKnapsackProfit(items, capacity);
cout << "Sort by profit: " << totalProfit2 << endl;

double totalProfit3 = fractionalKnapsackRatio(items, capacity);
cout << "Sort by weight and profit: " << totalProfit3 << endl;

if (totalProfit1 >= totalProfit2 && totalProfit1 >= totalProfit3) {
cout << "Sort by weight is the most optimized." << endl;
} else if (totalProfit2 >= totalProfit1 && totalProfit2 >= totalProfit3) {
cout << "Sort by profit is the most optimized." << endl;
} else {
cout << "Sort by weight and profit is the most optimized." << endl;
}

return 0;
}
Binary file added ADA/Knapsack.exe
Binary file not shown.
Loading

0 comments on commit 68777c8

Please sign in to comment.