Professional Documents
Culture Documents
DS Using C Material12
DS Using C Material12
DS Using C Material12
·--.......
<rI
~Cf3Fs:)
..
- CDF0 .
. ·, . '\_. ~\r,,
( .: .) ·,. .~ d l )
,- I . l ..
I l .
1..
we__ , i \·
. _1
OtV---
'nn .
t ( ,f.
f I .
CL I
ii~
~~ .¼
£.Xl'f .
, ! a
.r • • J \
- 'tW-5, . ·~ a_ ('
...
_ .
,J
:£;k
,..
_mrxkd.- .c~d.-~
• ..,. ,-
U, , - Cb· . ., < - .
I -- - 11•
I
. .'01'awd: ,&;f::
' -
---' 2 ~--2>-; ~ -
' ;
; .. . -. I •
,. I .. f
•• .J
' - - -
7
.
I •
'
\
A, ••
\
':'-•.
-' I
.') J
---
. ·, '•
f
-
\
'
...' .
'-\\ •, ..:>j . \
I
. .' --- .LL .\
:, .J - ,,/ r. .
i ; ·);~) ·-
1'
>
'
' .
f\ 'J
...!,
)J~.J
v·
LI
)};:
1 ', .
•
...
J •
.
I
-...
("• 'J j _,.
\) I
( \- - '
,JC.
j
E~mPle: Consider the graph shown in Figure 7.21. Suppose we_.w ant to find and process I
all the nodes reachable from the node 1 (including node ~). One way to doth is is to use a
depth first search of G starting at node 1. The steps ~re given below.
1 4
Node Adjacent List
I 2,. 3, 4
5 2 3
3
4 3, 5 /
2 5 3
Figure 7.21
...) p0 p and process node l, and change its status to the processed state and push onto
ttl • hb
stack all_the netg ours of l those are in the ready state.
stack: 2, 3, 4 Ready state (5)
Waiting state (2, 3, 4)
Processed state ( l)
iv) Pop and .process node 4, and change its status,to the processed state and push onto
stack ~ll the neighbours of 4 those are in the ready state.
stack: 2, 3, S ·_ Ready state ()
Waiting state (2, 3, 5)
. Processed state ( 1, 4)
v) Pop and process·node 5, and change its status to the processed-state·and push onto
stack all the neighbours of 5 those are in the ready state. ·
stack: 2, 3 Ready state ()
Waiting state (2, 3)
(-
vi) Pop and process node 3, and change its status to the processed state and push onto
stack all the neighbours of lthose are iri the ready state.
stack: 2 Ready state ()
Waiting state {2)
Proce·ssed state ( 1, 4, 5, 3)
vii) Pop and process node 2, and change its status to the processed state ~nd push onto
stack all the neighbours of 3 those are in the ready state. .
stack: ._ Ready state ()
, . Waiting state ()
Processed state ( l, 4, 5, 3, 2)
Th e stack
· 1s . now empty, so the depth first search, ..of G starting at node 1 is now complete.
The nodes are processed in the ord er l, 4, 5' 3' 2, ·
'- 1 \ r J J ' • I i .•
,
1·' I -
\ ,
;
)
- ' .
r./1-
• I
- · I.
,•
1
7.4.2 _Modified Wa.rshall's Shortest-Path Algorithm
· Let G be a directed graph with m nodes, v0, vi' ..., vm-1' Suppose G_is_weigh_ted; that is
. suppose each edge e in G is assigned a nonnegative number w( e) called the we·ight or length
of the edge e. Then G may be maintained in memory by its weight ma~rix W-: [w;); defined
as follows: . , · ·- ·
w . _. { w(e) _ if there is an edge from vi to vi
lj . 0 - if there is no edge from V . to V
. I )
The path matrix P tells us whether or not there are path bet~een the nodes._Now we want
to find a matrix Q which will tell us the lengths ofthe shortest paths between the nodes or,
more exactly, a matrix Q = [q) where
q . = length of a shortest path from v . to v .
I) I )
More exactly,
Qk[i][j] = min (Qk_,[i][j], Qk _,[i][ k] + Qk _ifk][j]), .· ,
. . · w·
The initial matrix Q0 is the same as the weight matr~x W ex~ept that eac~ 0 1~- _1~ re
placed
by 00 ( or a very, very large number). The final matnx Qm w1 II be _the desired mat~ix Q.
Graphs* 7.17
~nsider the weighted graph G in Figure 7.17 Then the weight matrix W of G
tisJJlP s· \/ i. v·,.v::. ,{ 41, . .
·s3S follow . Vi 7 5 0 0 .
I W =•f. - 7 0 0 2
1:; 0 3 OO
,,~ 4 0 1 0
1 1. the modified Warshall's algorithm, we obtain the following matrices Q0, Q1, Q2, _Q3
Y~Q. To the right ·of each matrix·Qk, we show the matrix of paths which correspond
APPdQ4-
an I gths in the matnx Qk.
wthe~ . .
2
5
3
. V2
Figure 7.17 . V3
7 5 00 00 VIVI v1v2
7 00 00 2 . v2v1 v2v3
Qo = 00 3 00 00 v~v 2
4 00 1 00 v4v1 v4v3
7 5 00 00 VIVI v1v2
7 12 00 2 v2v1 v2v1~2 v2v4
QI= 00 3 00 . 00 v3v2
4 (9) 1 00 v4v1 l\V 1V2 v4v3
•
,.
v,v, v1v2 v,v::?4
7 5 00 7 v2v1v2 v2v.i
7 12 ' ex:, 2 V:/1 v3v2v4
Q3:::: 10 5 v3v2v, v3v2 v4v3v2v4
3 00
v4v3v2 V4V3
4 @) I 6 v4v,
7.18 * Data Structures Using C_
v1v2 v1v2v4v3 v1v2v4
VIVI
5 8 7
7 v2v1 vzv4v1v2 v2v4v3 v2v4
6 6 3 2 , v3vzv4v3
v3vzv4v1 v3v2 vJv2v4
-Q4 = 9 3 6 5
v4v1 v4v3v2 v4v3 v4vJv2v4
6
© 4 1
We indicate how the circled entries are obtained_:
Q [4] _[2] = min(Q [4]~2], Q0[4][1_] + QJ1][2]) = rnin(oo, 4+ 5) = 9
1 0
Algorithm 7.2
A weighted graph G with rn nodes is_maintained in, memory by its weight matrix W. This
algorithm finds a matrix Q such that Q[i][j] i; the length or-a .shortest path from node v to
,_ - - I
node v . INFINITY is a very large number, arid min -is the minimum value function.
1
1. Repeat Steps 2 an~ 3 for i,j = 0, 1-, -. ·: ., m -1 [Initializes Q] _
2. •If W[i]l/]=0 then Set Q[i]I/] == INFINITY
3.- Else Set Q[i]U] =W[i]U]
[End of Step I loop.]
4. Repeat Steps 5 to 7 fork= 0, I, ... , m - I [Updates Q]
5. Repeat Steps 6 and 7 for i = 0, I, ... , n1 -1
6. Repeat Step 7 for j = 0, I, -... , n1 - I
7. - Set Q[i]I/] = Min(Q[i]UJ, Q[,l[k] + Q[k]U]).
[End of Step 6 loop.]
[End of Step 5 loop.]
[End ofStep 4 loop.]
8. Exit
Algorithrri 7.2 can also be used for a graph G without weights by simply assigning the
weight w(e) = -1 to each edge e in G.