算法之绪论

绪论

对之后算法需要学习的内容,需要的基础知识,进行大致的讲解和概述

算法

 所谓算法,即特定计算模型下,旨在解决特定问题的指令序列
 输入 待处理的信息(问题)
 输出  经处理的信息(答案)
 正确性:的确可以解决指定的问题
 确定性:任意算法都可以描述为一个由基本操作完成的序列
 可行性:每一基本操作都可以实现,且在常熟时间内完成
 有穷性:对于认可输入,经有穷次基本操作,都可以得到输出结果

好算法

 正确,健壮,可读,效率
 
 效率:速度尽可能快,存储空间竟可能少

算法分析

 两个主要任务=正确性(不变性×单调性) + 复杂度
 

复杂度分析的主要方法

 迭代:级数求和
 递归:递归跟踪+递归方程

级数:

  1. 算数技术:与末项平方同阶
  2. T(n) = 1+2+3+…+n=n(n+1)/2 = O(n^2)
  3. 幂方级数:比幂次高出一级
  4. T(n) = 1^2+2^2+3^2+…+n^2=n(n+1)/2 = O(n^3)
  5. 几何级数:与末项同阶
  6. T(n) = a^0+a^1+a^2+…a^n = O(a^n)
  7. 收敛级数
  8. 可能未收敛,长度有限
  9. T(n) = 1+1/2+1/3+…+1/n = O(logn) //调和级数 log1+log2+log3+…logn = O(nlogn) //对数级数

增长速度:

迭代与递归

减而治之:

为求解一个大问题,可以将其划分为两个子问题:其一平凡,另一缩减 分别求解子问题。由子问题的解,得到原问题的解

递归跟踪:

检查每个递归实例,累计所需时间,其总和即算法执行时间
直观形象,仅适用于简明的递归模式

递推方程:

从递推的角度看,求解sum(A,n),需递归求解规模为n-1的su
m(A,n-1)再累加上A[n-1]
递归基:sum(A,0)
递推方程

T(n) = T(n-1) + O(1)
T(0) = O(1)

分而治之:

为求解一个大规模的问题,可以将其划分若干(通常两个)子问题分别求解子问题,由子问题的解,得到原问题的解
  

银行家算法是一种避免死锁的算法,它通过动态地分配资源来避免进程死锁。下面是一个简单的银行家算法的C语言实现: ```c #include <stdio.h> #include <stdbool.h> #define P 5 // 进程数 #define R 3 // 资源数 int available[R] = {3, 3, 2}; // 可用资源 int max[P][R] = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3}}; // 最大需求矩阵 int allocation[P][R] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2}}; // 已分配矩阵 int need[P][R]; // 需求矩阵 bool finish[P] = {false, false, false, false, false}; // 进程是否完成 void calculate_need() { for (int i = 0; i < P; i++) { for (int j = 0; j < R; j++) { need[i][j] = max[i][j] - allocation[i][j]; } } } bool is_safe() { int work[R]; for (int i = 0; i < R; i++) { work[i] = available[i]; } bool finish_all = false; while (!finish_all) { bool can_allocate = false; for (int i = 0; i < P; i++) { if (!finish[i]) { bool can_finish = true; for (int j = 0; j < R; j++) { if (need[i][j] > work[j]) { can_finish = false; break; } } if (can_finish) { can_allocate = true; finish[i] = true; for (int j = 0; j < R; j++) { work[j] += allocation[i][j]; } } } } if (!can_allocate) { for (int i = 0; i < P; i++) { if (!finish[i]) { return false; } } finish_all = true; } } return true; } void request_resources(int process_id, int request[]) { for (int i = 0; i < R; i++) { if (request[i] > need[process_id][i] || request[i] > available[i]) { printf("Error: Requested resources exceed the need or available resources.\n"); return; } } for (int i = 0; i < R; i++) { available[i] -= request[i]; allocation[process_id][i] += request[i]; need[process_id][i] -= request[i]; } if (is_safe()) { printf("Request approved.\n"); } else { printf("Request denied. Rollback.\n"); for (int i = 0; i < R; i++) { available[i] += request[i]; allocation[process_id][i] -= request[i]; need[process_id][i] += request[i]; } } } int main() { calculate_need(); int request[P][R] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}; // 请求资源矩阵 int process_id; printf("Enter process id (0-4): "); scanf("%d", &process_id); printf("Enter request (e.g. 1 0 2): "); for (int i = 0; i < R; i++) { scanf("%d", &request[process_id][i]); } request_resources(process_id, request[process_id]); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值