主题:关于图的,大家讨论讨论做一做
shuimiu
[专家分:0] 发布于 2008-12-08 19:37:00
分别用邻接矩阵和邻接表存储图,对邻接矩阵存储的图,计算每个结点的度,并输出每个顶点的度。
分别对图进行深度优先遍历和广度优先遍历。
回复列表 (共1个回复)
沙发
ruicosta27 [专家分:0] 发布于 2008-12-19 15:02:00
For adjecency matrix representation, we can get the every vertice's degree by counting the number of "1" for each row. This is because the definition:
We use a 2D array A[1..n,1..n] to represent G=(V,E):
A[i,j] = 1 if (vi,vj) belongs to E; otherwise A[i,j] = 0;
To run breath first search algorithm, we first define its datastructure:
Adj[u]: the Adj list for u.
color[u]: it can be one of the following:
white: u has not been visited yet.
grey: u has been vistited, but some neighbors of u have not been visited yet.
black: u and all neighbors of u have been visited.
pi[u]: the parent of u in the BFS tree.
d[u]: the distance from u to the starting vertex s.
BFS(G,s)
Q is null
for each u in V-{s} do
pi[u]=NIL; d[u]=infinite; color[u]=white
d[s]=0;color[s]=grey;pi[s]=NIL
Enqueue(Q,s)
while Q is not null do
u is Dequeue(Q)
for each v in Adj[u] do
if color[v]=white
then color[v]=grey;d[v]=d[v]+1;pi[v]=u;Enqueue(Q,v)
color[u]=black
DFS(G)
for each vertex u in V do
color[u]=white; pi[u]=NIL
time=0
for each vertex u in V do
if color[u]=white then DFS-Visit(u)
DFS-Visit(u)
color[u]=grey; time=time+1;d[u]=time
for each vertex v in Adj[u] do
if color[v]=white
then pi[v]=u; DFS-Visit(v)
color[u]=black
f[u]=time=time+1
我来回复