2011-02-25 CLRS Chapter22 Elementary Graph Algorithms 图的几个基础算法
来源:互联网 发布:dota2外围菠菜软件 编辑:程序博客网 时间:2024/05/20 11:46
Representations of graphs:
There are two standard ways to represent a graph G = (V, E): as a collection of adjacency lists(O(V+E)) or as an adjacency matrix(O(V^2)).
The adjacency-list representation is usually preferred, because it provides a compact way to represent sparse graphs-those for which |E| is much less than |V|2.
An adjacency-matrix representation may be preferred, however, when the graph is dense-|E| is close to |V|2-or when we need to be able to tell quickly if there is an edge connecting two given vertices.
the simplicity of an adjacency matrix may make it preferable when graphs are reasonably small. Moreover, if the graph is unweighted, there is an additional advantage in storage for the adjacency-matrix representation. Rather than using one word of computer memory for each matrix entry, the adjacency matrix uses only one bit per entry.
Breadth-first search:
BFS(G, s)
1 for each vertex u ∈ V [G] - {s}
2 do color[u] ← WHITE
3 d[u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q ← Ø
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE(Q)
12 for each v ∈ Adj[u]
13 do if color[v] = WHITE
14 then color[v] ← GRAY
15 d[v] ← d[u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color[u] ← BLACK
Required running time: O(V+E).
Printing shortest path:
PRINT-PATH(G, s, v)
1 if v = s
2 then print s
3 else if π[v] = NIL
4 then print "no path from" s "to" v "exists"
5 else PRINT-PATH(G, s, π[v])
6 print v
This procedure runs in time linear
Depth-first search:
DFS(G)1 for each vertex u ∈ V [G]2 do color[u] ← WHITE3 π[u] ← NIL4 time ← 05 for each vertex u ∈ V [G]6 do if color[u] = WHITE7 then DFS-VISIT(u)
DFS-VISIT(u)1 color[u] ← GRAY ▹White vertex u has just been discovered.2 time ← time +13 d[u] time4 for each v ∈ Adj[u] ▹Explore edge(u, v).5 do if color[v] = WHITE6 then π[v] ← u7 DFS-VISIT(v)8 color[u] BLACK ▹ Blacken u; it is finished.9 f [u] ▹ time ← time +1
Topological sort:
TOPOLOGICAL-SORT(G)1 call DFS(G) to compute finishing times f[v] for each vertex v2 as each vertex is finished, insert it onto the front of a linked list3 return the linked list of verticesStrongly connected components:
STRONGLY-CONNECTED-COMPONENTS (G)1 call DFS (G) to compute finishing times f[u] for each vertex u2 compute GT3 call DFS (GT), but in the main loop of DFS, consider the vertices in order of decreasing f[u] (as computed in line 1)4 output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component
- 2011-02-25 CLRS Chapter22 Elementary Graph Algorithms 图的几个基础算法
- CLRS Chap 22 Elementary Graph Algorithms
- 【算法notes】Elementary Graph Algorithms 1
- chapter 22 Elementary Graph Algorithms
- 2011-03-03 CLRS Chapter 5 Randomized Algorithms 随机化的算法
- 算法导论Introduction to Algorithms(CLRS)
- Graph Algorithms
- CLRS 1.2作为一种技术的算法
- CLRS 4.2矩阵乘法的Strasson算法
- CLRS 8.1排序算法的下界
- CLRS 16.2贪心算法的原理
- 几个基础的排序算法
- 算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph
- Algorithms - Week 2-1 Elementary Sorts
- Algorithms - Week 4-1 Elementary Symbol Tables
- 【算法总结】graph 图的问题
- 算法(algorithms)基础之:插入排序
- 算法(algorithms)基础之:选择排序
- C# 透明窗体制作方法比较(1)
- 学习笔记-----------------------银行调度系统
- 【原创】arm是如何运行裸机程序的?
- 今天开始来写的博客了
- 项目管理中的人力资源
- 2011-02-25 CLRS Chapter22 Elementary Graph Algorithms 图的几个基础算法
- 2011-02-26 CLRS Chapter23 Minimum Spanning Trees 最小生成树
- 从小事看老板
- STL vector
- collections - Blackbery JDE ArrayList? - Stack Overflow
- Java下UDP协议的代码讲解
- 2011-02-27 CLRS Chapter24 Single-Source Shortest Paths 单源点最短路径
- .Net ArrayList Into LabView
- Error: VBA initialization failed