Python Matplotlib绘制贪心算法和动态规划算法的运行时间曲线图
Python Matplotlib绘制贪心算法和动态规划算法的运行时间曲线图
本示例展示如何使用Python的Matplotlib库绘制贪心算法和动态规划算法的运行时间曲线图。
import matplotlib.pyplot as plt
# 假设你已经获得了n_values, greedy_times, dp_times数据
# n_values: 活动数量列表
# greedy_times: 贪心算法对应活动数量的运行时间列表
# dp_times: 动态规划算法对应活动数量的运行时间列表
# 绘制曲线图
plt.plot(n_values, greedy_times, label='Greedy')
plt.plot(n_values, dp_times, label='Dynamic Programming')
# 设置坐标轴标签
plt.xlabel('Number of activities')
plt.ylabel('Execution time')
# 显示图例
plt.legend()
# 显示图形
plt.show()
代码说明:
- 使用
plt.plot(n_values, greedy_times, label='Greedy')绘制贪心算法的运行时间曲线,n_values为横坐标,greedy_times为纵坐标。 - 使用
plt.plot(n_values, dp_times, label='Dynamic Programming')绘制动态规划算法的运行时间曲线。 - 使用
plt.xlabel('Number of activities')设置横坐标标签。 - 使用
plt.ylabel('Execution time')设置纵坐标标签。 - 使用
plt.legend()显示图例。 - 使用
plt.show()显示图形。
通过观察曲线图,可以直观地比较贪心算法和动态规划算法在不同活动数量下的运行时间差异,从而评估两种算法的性能。
原文地址: https://www.cveoy.top/t/topic/fwA4 著作权归作者所有。请勿转载和采集!