example.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <string>
#include <cmath>

// Structure to represent a computational task
struct ComputationalTask {
    std::string name;
    int priority;
    double complexity;

    ComputationalTask(std::string n, int p, double c) : name(n), priority(p), complexity(c) {}

    // Overload the comparison operator for priority queue
    bool operator<(const ComputationalTask& other) const {
        return priority < other.priority;
    }
};

// Priority Queue for managing computational tasks
class TaskScheduler {
private:
    std::priority_queue<ComputationalTask> tasks;

public:
    void addTask(const ComputationalTask& task) {
        tasks.push(task);
    }

    ComputationalTask getNextTask() {
        if (tasks.empty()) {
            throw std::runtime_error("No tasks available");
        }
        ComputationalTask nextTask = tasks.top();
        tasks.pop();
        return nextTask;
    }

    bool hasTasks() const {
        return !tasks.empty();
    }
};

// Recursive function to compute factorial
unsigned long long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

// Recursive function for numerical integration using Simpson's rule
double simpsonIntegration(std::function<double(double)> f, double a, double b, int depth, int maxDepth) {
    double c = (a + b) / 2;
    double h = b - a;
    double fa = f(a), fb = f(b), fc = f(c);

    double s = (h / 6) * (fa + 4 * fc + fb);

    if (depth >= maxDepth) {
        return s;
    }

    double left = simpsonIntegration(f, a, c, depth + 1, maxDepth);
    double right = simpsonIntegration(f, c, b, depth + 1, maxDepth);

    return left + right;
}

int main() {
    TaskScheduler scheduler;

    // Add some computational tasks
    scheduler.addTask(ComputationalTask("Matrix Multiplication", 3, 100.0));
    scheduler.addTask(ComputationalTask("FFT", 2, 50.0));
    scheduler.addTask(ComputationalTask("Neural Network Training", 1, 500.0));
    scheduler.addTask(ComputationalTask("Data Preprocessing", 4, 30.0));

    std::cout << "Processing tasks in priority order:\n";
    while (scheduler.hasTasks()) {
        ComputationalTask task = scheduler.getNextTask();
        std::cout << "Executing task: " << task.name 
                  << " (Priority: " << task.priority 
                  << ", Complexity: " << task.complexity << ")\n";
    }

    std::cout << "\nDemonstrating recursive factorial calculation:\n";
    int n = 5;
    std::cout << "Factorial of " << n << " is: " << factorial(n) << "\n";

    std::cout << "\nDemonstrating recursive numerical integration:\n";
    auto f = [](double x) { return std::sin(x); };  // Function to integrate
    double a = 0, b = M_PI;
    int maxDepth = 10;
    double result = simpsonIntegration(f, a, b, 0, maxDepth);
    std::cout << "Integral of sin(x) from 0 to pi: " << result << "\n";

    return 0;
}
Back to wk09_algorithms_datastructures_2