文章目录
基础知识
上三角矩阵
1.1 图的存储结构
二维矩阵
1.1. 邻接表
- 表示方式1
/*
* 邻接表(Adjacency_list) 的另一种表示方式
* vector<vector<int>> adj(SIZE); vector自动调节自己大小
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class Solution{
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites)
{
/* Example Input: 3, [[1,0],[0,1],[4,5]]
* 那么, numCourses=4, dual_course_num=3
*/
int dual_course_num = prerequisites.size();
if(course_num == 0)
return true; /* 如果没有课程当然可以学完 */
//建立入度表, 并且初始化为0
vector<int> in_degrees(numCourses, 0);
vector<vector<int>> adjList(numCourses)//建立邻接表, 表中元素个数为所有课程总数
for(auto node : prerequisites)
{
in_degrees[node.first] += 1; //入度+1
adjList[node.second].emplace_back(node.first);
}
queue<int> q; //存储入度为0的node
// 遍寻adjList, 寻找入度为0的node
for(int i=0; i<numCourses; i++)
{
if(in_degrees[i] == 0)
q.push(i);
}
int cnt = 0; //cnt : 输出的node数
while(! q.empty())
{
int getTop = q.front();
q.pop();
++cnt;
/* 二维数组adjList[][], 遍历adjList的每一行
* 比如遍历adjList[i]这一行, 相当于邻接表
*/
for(auto i : adjList[getTop])
{
in_degrees[i] -= 1;
if(in_degrees[i] == 0)
q.push(i);
}
}
return (cnt == numCourses);
} // close method
};
int main()
{
return 0;
}
1.2. 十字链表
1.3. 邻接多重表
1.4. 边集数组
2 最短路径
2.1 Dijkstra
2.1.1 普通解法(优先队列)
2.1.2 斐波那契堆优化(松弛操作的插入节点操作O(1) )
遍历
深度优先遍历
非递归遍历
参考文献
后被访问的顶点,其邻接点先被访问。后来先服务, 可以借助于栈实现。 递归本身就是使用栈实现的,因此使用递归方法更方便。
递归遍历
关键路径
事件的最早开始时间
图中, V5选最大的
小明妈妈一边炒菜,一边熬粥,炒菜需要 20 分钟,熬粥需要 30 分钟,最早什么时间开饭?肯定是最长的时间啊
事件的最迟发生时间
求这个 , 从后往前推, 事件的最迟发生时间初始化为 事件的最早发生时间
事件 Vi 的最迟发生时间不能影响其所有后继的最迟发生时间。 Vi 的最迟发生时间不能大于其后继 Vk 的最迟发生时间减去活动<Vi, Vk>的持续时间。
活动的最早开始时间
活动的最迟发生时间
pdf大话数据结构 334