Graphs--Data Structure
来源:互联网 发布:电魂网络客服电话 编辑:程序博客网 时间:2024/06/08 07:37
Definition
An (undirected) Graph is a collection V of vertices, and a collection E ofedgeseach of which connects a pair of vertices.
Vertices: A,B,C,D
Edges: (A,B), (A,C), (A,D), (C,D)
Edge List
List of all edges:
Edges: (A,B), (A,C), (A,D), (C,D)
Adjacency Matrix
Matrix. Entries 1 if there is an edge, 0 ifthere is not.
A B C D
A 0 1 1 1
B 1 0 0 0
C 1 0 0 1
D 1 0 1 0
Adjacency List
For each vertex, a list of adjacent vertices.
A adjacent to B,C,D
B adjacent to A
C adjacent to A, D
D adjacent to A, C
Op.
Is Edge? List Edge List Nbrs.
Adj. Matrix
Edge List
Adj. List
Θ(1) Θ(|V |2) Θ(|V |)
Θ(|E |) Θ(|E |) Θ(|E |)
Θ(deg) Θ(|E |) Θ(deg)
2. Dense Graphs
In dense graphs, |E | ≈ |V |2.
A large fraction of pairs of vertices are connected by edges.
Sparse Graphs
In sparse graphs, |E | ≈ |V |.
Each vertex has only a few edges.
Pseudocode
Component(s)
DiscoveredNodes ← {s}
while there is an edge e leavingDiscoveredNodes that has not beenexplored:
add vertex at other end of e to
DiscoveredNodes
return DiscoveredNodes
Depth First Ordering
Explore(v)
visited(v) ← true
for (v,w)∈E:
if not visited(w):
Explore(w )
DFS(G)
for all v ∈ V : mark v unvisited
for v ∈ V :
if not visited(v):
Explore(v )
Previsit and Postvisit Functions
Explore(v)
visited(v) ← true
previsit(v)
for (v,w)∈E:
if not visited(w):
explore(w )
postvisit(v)
Initialize clock to 1.
previsit(v)
pre(v) ← clock
clock ← clock + 1
postvisit(v)
post(v) ← clock
clock ← clock + 1
Find sink.
Put at end of order.
Remove from graph.
Repeat.
LinearOrder(G)
while G non-empty:
Follow a path until cannot extend
Find sink v
Put v at end of order
Remove v from G
O(|V|) paths.
Each takes O(|V|) time.
Runtime O(|V |2).
TopologicalSort(G)
DFS(G )
sort vertices by reverse post-order
5. connectivity:
Explore(v )
visited(v) ← true
CCnum(v) ← cc
for (v,w)∈E:
if not visited(w):
Explore(w )
DFS(G )
for all v ∈ V mark v unvisited
cc ← 1
for v ∈ V :
if not visited(v):
Explore(v )
cc ← cc + 1
Correctness
Each new explore finds new connected component.
Eventually nd every vertex.
Runtime still O (|V | + |E |).
6.Strongly Connected Components
De nition
Two vertices v,w in a directed graph are connectedif you can reach v from w and canreach w from v.
Summary
Can partition vertices into strongly connected components.
Metagraph describes how strongly connected components connect to each other.
Metagraph always a DAG.
EasySCC(G)
for each vertex v:
run explore(v) to determine vertices reachable from v
for each vertex v:
find the u reachable from v that can also reach v
these are the SCCs
Runtime O (|V |2+ |V ||E |). Want faster.
SCCs(G)
run DFS(GR)
let v have largest post number
run Explore(v)
vertices found are first SCC
Remove from G and repeat
SCCs(G)
Run DFS(GR)
for v ∈ V in reverse postorder:
if not visited(v):
Explore(v )
mark visited vertices as new SCC
Essentially DFS on GRand then on G.
Runtime O(|V|+|E|).
- Graphs--Data Structure
- Data Structure
- Data structure
- Data Structure
- Data structure
- Data structure
- data structure
- Data Structure
- Data structure
- Data Structure
- Data Structure
- Data Structure
- Data Structure
- Data structure alignmentData structure alignment
- Finish Data Structure Examination
- 【OPENGL】DEVMODE data structure
- Critical Data Structure -- sk_buff
- Data Structure数据结构
- ApplicationContext hw = new ClassPathXmlApplicationContext("ApplicationContext.xml");
- UE4实现一个简单的炸弹爆炸与镜头振动效果
- 按之字形顺序打印二叉树, 把二叉树打印成多行
- 老版本Ubuntu使用apt-get不能安装软件问题
- 创建软连接
- Graphs--Data Structure
- C# 反射
- Ubuntu安装Chrome
- spring中bean的作用域single与prototype区别
- 教你如何在微信浏览器唤醒APP
- Linux I2C framework(1)_概述
- 嵌入式应用之-任务进程框架
- webpack加载css
- 快速排序