树的DFS&BFS
prim算法
prim 调整的是小根堆
图的DFS和BFS
BFS
DFS
广度优先遍历:检测到没有遍历 先放入队列中 ,做访问标记
深度优先遍历: 检测到没有遍历,直接递归调用直接遍历那个点
拓扑序列
一个ID数组:存放顶点入度
一个t数组:存放拓扑序列
一个队列q:存放入度为0的顶点
template<class T> int Graph<T>::InDgree(int v)const //函数:计算入度 { int InDegree = 0; for (int i = 0; i < SizeV(); i++) { typename list<EdgeNode>::const_iterator first = HL[i].begin(), last = HL[i].end(); for (; first != last; ++first) { if ((*first).dest == v) ++InDegree; } } return InDegree; } //私有函数 template<class T> bool Graph<T>::TopSort(int* t, int ne)const //拓扑排序 { int pos, j,i, count = 0;; int* ID = new int[ne]; queue<int> q; for (i = 0; i < ne; i++) { ID[i] = InDgree(i); //给ID数组赋予每个点的入度 if (ID[i] == 0) q.push(i); } typename list<EdgeNode>::const_iterator first, last; int id = 0; while (!q.empty()) { pos = q.front(); q.pop(); t[id++] = pos; count++; first = HL[pos].begin(), last = HL[pos].end(); for (; first != last; ++first) { j = first->dest; ID[j]--; if (ID[j] == 0) q.push(j); } } delete[]ID; return count == ne; } //公有函数 template<class T> bool Graph<T>::TopSort(list<T>& l)const //拓扑排序 { int *top = new int[SizeV()+1]; if(TopSort(top, SizeV())) { for (int i = 0; i < SizeV(); i++) { l.push_back(VA[top[i]]); } delete[]top; return true; } delete[]top; return false; }