【算法notes】Elementary Graph Algorithms 1
来源:互联网 发布:文登政府网通知公告栏 编辑:程序博客网 时间:2024/06/05 22:56
图算法的核心是:searching the graph。因为可以借此获得graph structure的信息。
一,如何表示一个图?
G=(V,E)
1. adjacency-list(用的多,有向无向都可以用):an array,Adj, of |V| lists, each list contains all adjacent vertices of Vi;
也表示了图的edges,无向图总数等于2|E|, 有向图总数=|E|。
advantage: 空间省,O(V+E),sparse的图
disadvantage: 判断edge (u,v)是否存在,只能去Adj[u]里面查找v.
2. adjacency-matrix(特殊要求下使用): A
对无向图来说,A=A^T.如果需要,可以只存对角线的上一半。
advantage: 可以快速知道edge(u,v)是否存在。对unweighted图来说,one bit per entry挺省的。
disadvantage: 空间多,O(V^2),用在dense的图。
二,BFS(宽搜)和生成BF-tree
BFS是很多其他算法的archetype,对每个v找到的是s到v的最短路径长度,时间复杂度O(V+E),底层数据结构用的Queue(FIFO)。
java实现的source codes在github
shortest-path distance S(u,v): number of edges between u and v is the smallest.
parent[v]保存了v的parent的信息,可以recursive的画出从v到s的path.
三,DFS和一些证明
DFS是从source vertex s出发,处理所有从s可以reach的顶点后,从未被reach的顶点中选择一个作为新的source vertex,重复以上过程,直到所有的顶点都被访问过为止。
所以DFS所生成的可能不仅是一棵树,而是DF-forest,而且算法保证了组成forest的DF-trees are disjoint。
DFS常被用作其他算法的sub-routine。实现的底层,多用stack或者recursive,时间复杂度O(V+E)。
Parenthesis theorem: [u.d, u.f]和[v.d, v.f]只有3种关系,disjoint, 一个完全包含另一个*2
对edges的分类(无向图里没有3,4):
通过(u,v)时,v的颜色来判断,白的是1, 灰的2, 黑的3或者4。
- Tree edges: 组成DF-forest的edges,发现white vertex时候沿着的边
- Back edges: 回到DF-tree的ancestors的edge。有向图撒,不能有这种edge才是acyclic (经常被叫做dag)。
- Forward edges:
- cross edges:
- 【算法notes】Elementary Graph Algorithms 1
- chapter 22 Elementary Graph Algorithms
- CLRS Chap 22 Elementary Graph Algorithms
- 2011-02-25 CLRS Chapter22 Elementary Graph Algorithms 图的几个基础算法
- Algorithms Notes (1)
- Algorithms - Week 2-1 Elementary Sorts
- Algorithms - Week 4-1 Elementary Symbol Tables
- Graph Algorithms
- Princeton Algorithms: Part 1 [week 2:Elementary Sorts]
- Internal Sorting Algorithms Part 1/2: Elementary Sorts
- 算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph
- Algorithms--Heap Notes
- Algorithms--QuickSort Notes
- Algorithms--CountingSort Notes
- CLRS(Introduction to Algorithms 2nd) reading notes 1
- Python Algorithms Learning Notes(1)--Asymptotic Notations
- Machine Learning Algorithms Study Notes
- Force-Directed Graph Drawing Algorithms
- 链表的逆序,排序等
- ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别
- ZOJ 3811 —— Untrusted Patrol(2014牡丹江网络赛C题)
- 窗口句柄的获取MFC
- java整理笔记之多线程
- 【算法notes】Elementary Graph Algorithms 1
- sqlite
- Ubuntu 14.04 LTS正式版 安装教训
- 花生壳http版
- 龙组总结
- uva 11796 Dog Distance(计算几何, 基础)
- 随机数范围扩展方法总结
- 软件开发评估-——投入知多少?
- 编辑距离(Minimum Edit Distance)