Upload
xavier-caldwell
View
18
Download
0
Embed Size (px)
DESCRIPTION
a. c. b. c. a. d. e. f. c. f. b. e. c. a. e. d. f. c. e. f. Adjacent lists. Graph Algorithms. Data structure for graph algorithms : Adjacent list, Adjacent matrix. Adjacent matrix. Adjacent list: Each vertex u has an adjacent list Adj[ u ] - PowerPoint PPT Presentation
Citation preview
Graph Algorithms
a
d e
c b
f
)},(),,(),,(),,(),,(),,(),,(),,{(
),,,,,(
),(
feedfbceecadcbcaE
fedcbaV
EVG
0 0 0 0 0 0 f
1 0 0 1 0 0 e
0 1 0 0 0 1 d
0 1 0 0 0 0 c
1 0 0 1 0 0 b
0 0 0 1 0 0 a
f e d c b a abcdef
cc fea ef c
Adjacent lists
Adjacent matrix
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Adjacent list: Each vertex u has an adjacent list Adj[u]
(1) For a connected graph G, if G is directed graph the total size of all the adjacent lists is |E|, and if G is undirected graph then the total size of all the adjacent lists is 2|E|. Generally, the total size of adjacent lists is O(V+E).
(2) For a weighted graph G, weight w(u,v) of edge (u,v) is kept in Adj[u] with vertex v.
Adjacent matrix : Each vertex is given a number from 1,2,…,|V|.
(1) For a undirected graph, its adjacent matrix is symmetric.
(2) For a weighted graph, weight w(u,v) is kept in its adjacent matrix at row i and column j.
1 2
5 4
33 4
3
2
2
1
2
2
4
5
5
4
5
1
12
3
45
1 2 3 4 5
1 0 1 0 0 1
2 1 0 1 1 1
3 0 1 0 1 0
4 0 1 1 0 1
5 1 1 0 1 0
1 2
4 5
3
6
2
5
6
2
4
4
5
12
3
45
66
1 2 3 4 5 6
1 0 1 0 1 0 0
2 0 0 0 0 1 0
3 0 0 0 0 1 1
4 0 1 0 0 0 0
5 0 0 0 1 0 0
6 0 0 0 0 0 1Comparison between adjacent list and adjacent matrix
(1) If |E| is much smaller than then adjacent list is better (using less memory).
(2) It costs time using adjacent lists to find if v is adjacent to u.
|| 2V
Given G = (V,E) and vertex s, search all the vertices that s can arrive.
Breadth-first search (BFS): Searching the vertices whose distance from s is k ealier than visiting those whose distance from s is k+1.
sQ
0
8 0
8 8 8 888
ur s t
v w x y
(1)01
8 1 8 888
wQ
r1 1
ur s t
v w x y
(2)
01
8 1 2 882
ur s t
v w x y1 2 2r
Qt x(3)
01
2 1 2 882
ur s t
v w x y2 2 2
tQ
x v(4)
r01
2 1 2 8
32us t
v w x y2 2 3
xQv u(5)
2 3 3
01
2 1 2 3
32ur s t
v w x y
vQ
u y(6)
01
2 1 2 3
32ur s t
v w x y
Qu y
3 3(7)
ts u01
2 1 2 3
32r
v w x y
Qy
3(8)
01
2 1 2 3
32ur s t
v w x y
Q : (8)
{s}:Q 8
NIL:[s] 7
0:d[s] 6
GRAY:color[s] 5
NIL:[u] 4
:d[u] 3
white: color[u] do 2
{s}-V[G]u vertex eachefor 1
),(
sGBFS
green:color[u] 18
DEQUEUE(Q) 17
v)ENQUEUE(Q, 16
u:[v] 15
1d[u]:d[v] 14
GRAY:color[v]n the 13
whitecolor[v] if do 12
Adj[u]each vfor 11
head[Q]:u do 10
Q while9
Analysis of the algorithm
(1) Each vertex is put into queue Q at most once. Therefore, the number of operation for the queue is O(|V|).
(2) Each adjacent list is at most scanned once. Therefore, the total running time for scanning adjacent lists is O(|E|).
(3) The running time for initiation is O(|V|).
Therefore, the total running time of the algorithm is O(|V|+|E|).
Find the path from s to v in BSF
v
vs
vsGPATHPTINT
print 6
[v]) s, PATH(G,-PRINT else 5
" to frompath no is there"print n the 4
NIL[v] if else 3
sprint then2
s vif 1
),,(
Depth-first search : Search deeper in the graph whenever possible.
(1) Each vertex has two timestamps: d[v] is the first timestamp when v is first discovered, and f[v] is the second timestamps when the search finishes examining v’s adjacent list. (2) It generates a number of depth-first search trees.
u v w1/
x y z(a)
u v w1/ 2/
x y z(b)
u v w1/ 2/
3/ x y z
(c)
u v w1/ 2/
4/ 3/ x y z
(d)u v w
1/ 2/
4/ 3/ x y z
(e)
B
u v w1/ 2/
4/5 3/ x y z
(f)
B
u v w1/ 2/
4/5 3/6 x y z
(g)
B
u v w1/ 2/7
4/5 3/6 x y z
(h)
B
u v w1/8 2/7
4/5 3/6 x y z
(i)
BF
u v w1/8 2/7
4/5 3/6 x y z
(j)
BF
(l)
u v w1/8 2/7 9/
4/5 3/6 x y z
BFC
(k)
u v w1/8 2/7 9/
4/5 3/6 x y z
BF
(m)
u v w1/8 2/7 9/
4/5 3/6 10/ x y z
BFC
(n)
u v w1/8 2/7 9/
4/5 3/6 10/ x y z
BFC
B
(p)
u v w1/8 2/7 9/12
4/5 3/6 10/11 x y z
BFC
B
(o)
u v w1/8 2/7 9/
4/5 3/6 10/11 x y z
BFC
B
VISIT(u)-DFShen t 7
WHITEcolor[u] if do 6
V[G]ueach for 5
0 : time4
NIL :[u] 3
WHITE:color[u] do 2
V[G]ueach for 1
)(
GDFS
1time:time:f[u] 8
*/* finished. isit u;Blacken ** / GREEN:color[u] 7
VISIT[v]-DFS 6
u:[v] n the 5
WHITEcolor[v] if do 4
*/* v).(u, edge Explore **/ Adj[u]veach for 3
1time:time:d[u] 2
/* discoveredbeen just hasu verteshite * /GRAY:color[u] 1
)(
uVISITDFS
Running time
(1) The running time except DFS-VIST is O(|V|).
(2) Each vertex is called by DFS-VISIT only once, because only white vertices will be called by DES-VISIT and when they are called their color is changed to gray immediately.
(3) The loop in DFS-VISIT is executed only |Adj[v]| times.
Therefore, the total running time of the algorithm is O(|V|+|E|).
Vv
EOvAdj |)(||][|