Drawing Weighted Directed Graph from It`s Adjacency Matrix

维普资讯 http://www.cqvip.com
Jo
urnal
o
fShanghai
Uni
ver
si
t
y (Engl
i
sh Edit
i
on),2005,9(
5):407—41
0 Ar
t
i
cl
e
I
D:1
007—
6417(
2OO5)
O5—
0407—
04 Dr
awi
ng We
i
ght
ed Di
re
ct
e
d Gr
aph f
r
om I
t’
s
Adj
acency
Mat
ri
x MAO Guo—
yong(毛 国勇), ZHANG Wu(张武 )
School
f Comput
o
r e
Engi
nee
ri
ng and Sci
ence,Sh
a ngh
ai
Uni
ve
rs
it
y,Shangh
ai
20007
2,P.
R.Chi
na Abst
rac
t
Thi
s
paper
pr
opo
ses
an al
gor
i
t
hm f
or
buUdmg wei
ght
ed di
rect
ed gr
aph, de
f
m es
t
he
w ei
ght
ed di
rect
ed rel
at
i
on
sh
ip m at
r
i
x of
t
he gra
ph,a
n d desc
ibe
r
s lgor
a
it
hm i
mpl
em ent
at
i
on usi
ng t
hi
s
ma
tr
i
x.Based on t
hi
s lgor
a
it
m
h
,a
n ef
f
ect
i
ve way or
f
bui
l
di
ng n d a
dr
aw i
ng
wei
ght
ed di
rect
ed g
ra
p hs
i
s
pr
esent
ed,f
or
mi
ng a f
ounda
ion t
or
f
v
i
s
ual
i
mpl
ement
at
ion of
t
he a
lgor
it
m h
n i
t
he gr
aph t
heor
y.
Key words
wei
ght
ed di
rect
ed gr
aph,adj
acency mat
ri
x,r
el
a
ion
t
sh
ip mat
r
i
x.
m at
ix r
show n i
n Fi
g.1 i
s
stud
i ed f
ir
s t. F
i g.2 shows a 1 Int
roduct
i
on Wi
th rapi
d devel
opm ent
of com put
er technol
ogy,r
e—
i rect
d
ed g
ra
p h cor
respond
i ng to t
he rel
ati
on
sh
i p m atr
ix.
search on the graph t
heor
y has pr
ovi
ded a great
deal
of advanced resul
t
s.How ever,one can not
f
ind m any r
e—
s
e
r che
a
s
on v
isua
l
bu
i
l
d of
gr
a
phs
ba
sed on adj
ace
nc
y m at
ri
x or rel
ati
onshi
p m atri
x of t
he graph , a
lt
hough such r
esea
r ch i
s usef
ul
i
n t
he t
eachi
ng of gr
a ph theor
y and ot
her pract
i
ca
l appl
i
cat
i
ons.Take wei
ghted di
rect—
ed graph a
s an exampl
e, onl
y af
ter bui
l
di
ng a graph si
u
ng adj
acency or
r
el
a
t
i
on
sh
ip mat
r
i
x,c
an one
isual
v
—
Fi
g.1 Relati
on
sh
i p nmtri
x l
y n d ef
a
fect
ivel
y i
m pl
em ent
al
gor
ithm s such a
s shor
t—
est
path, connected com ponent
s , and m i
i m um span—
n
ni
ng
t
r
ee.As
bui
l
ing d
g
r
aph si
u
ng
adj
acency
mat
r
i
x or
r
el
at
i
onsh
i p mat
ri
x i
s d
iff
erent f
rom graph drawi
ng,
popu
lar d
r aw
i ng sof
t
wa
r e i
s not
desi
rabl
e.Researchers have devel
oped dynam i
c gr
a ph ed
it
or speci
f
i
cal
l
y u
s ed i
n d
r aw
i ng graph. I
t
on
l y faci
l
i
t
ates
d
r aw i
ng of g
r aph com pa
r ed w i
t
h ot
her d
r aw i
ng sof
t
wa
r e. So f
r no ef
a
-
f
ect
i
ve al
gor
it
m s have been devel
h
oped for
bu
il
i ng d
gr
aph si
u
ng
a ̄ac
ency ma
t
r
i
x or
r
el
a
t
i
onsh
ip mat
r
i
x.
Fi
g.
2 Di
rect
ed g
ra
p h 2 M odel
and Approach One can observe f
rom the m at
ix and the core-
r
Usuall
y d
i rect
ed gr
aph i
s r
epr
esented by a rel
ati
on—
sh
i p m at
ri
x. Each rel
at
i
on
sh
i p m at
ri
x corresponds t
o a i rect
d
ed graph. I
n or
der to bu
il
d a graph ba
s ed on a rel
at
ion
s h
i p m at
ri
x , the charact
er
isti
c of r
el
at
i
on
s h
i p spond
i ng gr
aph t
hat t
he sum of each col
umn i
n h e m a—
t
tr
ix i
s
0, that i
s, posi
t
i
ve num bers equa
l to negat
i
ve nu
m
ber
s i
n each col
m n.A u
posi
t
i
ve nu
m
ber repr
esent
s n edge that
a
i
nci
dent
s f
rom th
i s ver
t ex ,a
n d a negat
i
ve Recei
ved Feb.23.2004:
Revi
sed Aug. 1
8,200
4 nu
m ber represent
s an edge that i
nci
dent
s to th
i s ver-
Pr
oj
ect
suppor
t
ed by Sci
ence Foundat
ion of
Sha
ng
hai
Muni
ci
pa
l tex.Connect
i
ng the two vert
i
ces
cor espond
ing to neg—
Conmi
ssi
on of
Educat
i
on (Gr
a
nt
No.03A203)
ati
ve a
n d ver
ti
ces posi
t
i
ve nu
m ber
s , i.e.,con ecti
ng MAO Guo—
yong. Ph.D .Ca
n di
dat
e. E—
m ar
l :g
ynl
ao @ m ai
l.shu.
edu.
cn;
ANG W u, Ph. D . , Prof., E—
m ai
l:zhang @ s
t
af
f.
shu.edu.cn Vl
corespondi
ng to a posi
ti
ve nu
m ber and core—
spond
i ng t
o a negati
ve nu
m ber i
n t
he 5t
h col
m n i
u
n 维普资讯 http://www.cqvip.com
408 Journa
/o
fShanghai
Uni
ver
sit
y Fi
g.1,we get
e5
connect
ing Vl
and .Connect
ing ver
ti
ces i
n each col
umn one by one, one can get 7 edges i
n t
0ta1.equal to t
he number
of col
m ns i
u
n t
he m at
ri
x.
The edge di
rect
ion can al
so be det
ermi
ned by the above approach.The d
irect
ed edge i
s obt
ai
ned si
mpl
y by usi
ng the ver
tex cor
respond
ing t
o a posi
ive nu
t
m ber as a source vert
ex a
n d a ver
tex cor espond
i ng t
o a negat
ive nu
m
Fig.4 W DRM bera
s a dest
inat
ion ver
tex .The arrow i—
d
rect
ion i
s det
er
mi
ned by t
he edge di
rect
ion.From this irect
d
i
on,t
he l
ength of
arrow a
n d the heml
i
ne,a
n ar—
row ca
n be drawn a
s shown i
n Fi
g.2. The arr
ow shoul
d be sl
i
m a
s t
here may be m ore tha
n one edges that
i
nci
dent
to a cert
ai
n vert
ex.Fi
g.3 i
l
l
st
u
rat
e s t
h e Mgonthm of arrow , supposi
ng t
hat
M l
a
n d M 2
are WDRM can be ach
i eved i
f we change t
he val
ue 1
i
n t
he rel
at
i
on
sh
i p mat
ix r
i
nt
o t
he rea
l wei
ght.The nega—
ti
ve va
lue i
n a rel
at
ion
sh
i p m
a t
ri
x rem ai
s t
n
he sam e becau
s e i
t i
s onl
y u
s ed f
or
det
er
m i
ni
ng d
ir
ect
ion.
There m ay exi
st a l
oop bet
w een w o vert
t
i
ces i
n pract
i—
ca
l appl
i
cat
ion
s .Theref
ore, an a
r c shoul
d be u
s ed i
n—
sou
r ce dest
i
nat
i
on and ver
ti
ces of
a d
ir
ect
e d edge,r
e—
st
ead of a l
i
ne t
o con ect
these t
w o vert
i
ces.W e st
il
l
spect
i
vel
y.
use l
i
ne to con ect
t
w o vert
i
ces i
f no l
oop ex
i st
s be—
w een t
them ,as shown i
n Fi
g.
5.
Fi
g.
3 Ar
r
ow al
gor
i
t
hm Fi
g.
3
s
hows
t
hat
d
ir
ect
i
on,l
eng
th a
n d wi
dt
h of
t
he
arrow a
r e M l
M 2, IM 2, and 2 × IM 3, respect
ivel
y.
Coord
i nat
es of M l
a
n d M 2
det
er
mi
ne the angl
e be—
Fi
g.
5 W ei
ght
ed di
r
ec
t
ed gr
aph t
ween M l
M2
n d a
t
he
hor
i
zont
al
l
i
ne (
we
cal
l
t
hi
s
angl
e
s
l
oppy”).Coor
inat
d
es
of
I
a
re
de
t
er
m i
ned by
t
is
h
r om t
F
he propert
y 0f W DRM .each edge i
n Fi
g.
4 i
s a
ngl
e
a
n d t
he
l
eng
th of
ar
r
ow .Coor
inat
d
es
of
M3
a
re
s soci
a
at
e d w i
th t
w o ver
ti
ces.Thu
s ,t
her
e a
r e onl
y w o t
“
det
er
mined by t
he coord
i nat
e s of I, t
h e angl
e of M3
I
K , wh
ich e
qual
s
90一
s
l
oppy, a
n d t
he
l
eng
th of
IM 3.Coord
i nat
e s of M 4
ca
n be obtained i
n a si
mi
l
r a
non-
zero el
em ent
s i
n each col
m n .I
u
f si
u
ng l
a
n d —l
to represent
respecti
vel
y a posi
t
i
ve wei
ght and a nega—
t
i
ve wei
ght
i
n the m
a tr
i
x, a loop ex
i st
s bet
ween t
w o vert
i
ces cor espond
i ng t
o t
w o non—
zer
o el
ement
s when w a_
y.
Gi
ven a wei
ght
e d d
i rect
e d graph G Def
t ni
t
i
on i thout
w
paral
l
el
e
dges,G =(V,E), V= {vl,v2,
…
,
=
v },E ={
el,e2,… ,e
},P X
q
ma
t
r
i
x M (G)
(m ),a
n d f
w,
1
fl
0re.a
n a
r c shou
l d be u
s ed to con ect t
hese t
w o vert
i
CeS .
Det
er
mi
nat
ion of t
he a
r c’s di
rect
i
on i
s the sam e a
s of
a l
i
ne.
I
f i
s the sou
r ce ver
tex of ej
i
n G ,
w i
s
t
h
e
we
i
g
h
t
of
,w>0,
1
【
一l
,Ⅱ
i
s
t
h
e
d
e
s
t
i
n
a
t
i
o
n
v
e
r
t
e
x
o
f
0,
el
em ent
s of w o col
t
um ns a
r e exact
l
y t
he sam e.Ther
e.
I
f
Vi
d0es
n’
t
i
nci
dent
on .
e st
il
l
u
s e t
he ver
tex cor espond
i ng to a posi
ti
ve el
em ent i
n t
he m at
r
i
x a
s a source ver
t
ex,
n d a negat
a
ive el
em ent
s a a
dest
inat
ion vert
ex.The di
—
rect
i
on of
ar
row i
s cl
ockw i
se.An r c i
a
s determ i
ned by at l
ea
s t 3 vert
i
ces, so we m u
s t
f
ind at l
ea
s t one add
it
—
i
ona
l vert
ex. Accord
in
g to the arrow al
gor
it
hm , we Then M (G)i
s
r
ef
er
red t
o s a
t
he wei
ght
ed di
r
ect
ed r
e—
l
at
i
on
s hi
p mat
r
i
x(
WDRM)of
G,a
s s
how
n i
n Fi
g.
4.
ext
end t
he
l
eng
t h of
ar
r
ow a
n d I
M 4
a
ppr
opr
ia
el
t
y,
维普资讯 http://www.cqvip.com
Vo1.
9 No.
5
Oct.
2005 MAO G Y,et
a1.: Dra
wi
ng
Wei
ght
ed Di
rec
t
ed Gr
aph f
r
om I
t’s
Adj
acenc
y Mat
r
i
x 409 dr
aw a new arrow of t
he sam e si
ze i
n M 1, t
hen a vert
ex.t
he cent
er of el
—
der t
o get
coord
inates
of each cl
ockw i
se arc f
rom M l
to M 2
i
s det
ermi
ned by M 1,
l
i
pse i
s f
ixed,and ll
a
ver
ti
ces
a
r e draw n on ci
rcumfer—
M 6, M 4
and M 2
as show n i
n Fi
g.6.W e ca
n l
abel
t
he ence of
t
h e el
l
i
pse.The angl
e of
ci
rcumf
erence i
s d
i—
w ei
ght of a m at
ri
x besi
de t
h e di
rect
ed edge or d
ir
ect
ed i ded equal
V
l
y am ong a
ll
ver
ti
ces.Suppose the rat
io be—
t
w een h e l
t
ong axi
s a a
n d t
he shor
t axi
s b i
s
R ,f
rom £
urc .
he
t
equa
t
i
on x /
(Rb)
+
/6
=l
a
n d ig.
F
8,one
ca
n get
t
h e l
engt
h of
OP wh
i ch con ect
s t
h e el
l
i
pse cent
er n d t
a
h e vert
ex d
ivi
i ng t
d
h e angl
e of
c ̄cum f
erence equal
l
y:
0P[
si
n z
r
at
i
o
CO S
e ,
∈[
0,
2
)'
e wher
e
OP[0]equa
ls
b,and i
r
anges
f
rom 0
t
o t
he
Fi
g.6 Arc algori
thm 3 Com puter Impl
em entati
on num ber ofver
texes m i
nu
s 1.The coord
i nat
es of P ca
n be
der
ived by he t
va
lues
of
OP[i]a
nd
t
he
a
ngl
e臼:
P[i].
x=0.
x+OP[i]*
s
i
n(i*臼)
/(
180
/
 ̄)),
Al
m ost al
l
sof
tw a
r e packages associ
at
ed w i
h t
t
h e P[i].
Y=O.
Y一0P[i]*
COS(i*臼)
/(1
80
/
 ̄))
gr
a
ph t
heor
y
use
ad
jacency
mat
r
i
x a
s i
n
put.So i
t
i
s
ne
ces
s
ar
y t
o se
u
ad
jacenc
y mat
r
i
x f
or
dat
a i
nput,con—
vert
i
t
t
o a wei
ght
ed d
ir
ect
ed rel
ati
onshi
p m atri
x, and h e code segm ent u
T
s ed for
com puti
ng t
he coord
i—
nates of
ver
ti
ces i
s l
i
sted a
s f
ol
l
ows.
t
hen u
s e t
h e al
gor
it
hms a
s descr
ibed above t
o draw a graph .
.
The
a
d
j
acency mat
r
i
x cor
r
es
pond
ing
t
o a we
i
ght
ed 。 i rect
d
ed m at
ri
x i
n F
i g.
4 i
s gi
ven i
n F
i g.7.
Fig.8 Comput
at
ion of the l
ength of OP and h e coordi
t
nates of P on ell
i
pse Program 1 angl
e =2 pi/number
o
fve
rt
e
x:/
/The ci
r
cum f
er
ence of
el
—
l
i
pse i
s di
ded equa
ll
y by verti
ces f
or(i=0;i<numbe
ro
fve
rt
e
x;{ ++ )
Fi
g.
7 Ad
j
ace
ncy mat
r
i
x cor
r
espondi
ng t
o WDRM i
n Fi
g.
4
I
t
i
s
ea
s y t
o conver
t
a
n ad
jacency mat
r
i
x i
nt
o 1
center.
=420: ce
n te
r .Y = 260;//the coordinates 0f the el
li
pse’s center W DRM .One thi
ng should be rem em bered i
s that
a val
—
ue — l i
s used t
o determ i
ne t
he d
irect
i
on of edge i
n WDRM .wh
il
e
t
he act
ua
l wei
ght
i
n n a
ad
jacency mat
r
i
x 印 [i]=r
at
i
o*b/sq ̄((
si
n (angl
e)
si
n(angl
e)
+r
at
i
o COS (angl
e)*rat
i
o
COS (angl
e ))
P [i].x =ce?
t
t
r[
e
0].x +i
nt(印 [i]*si
n (i
m ay i
ndeed be — 1. Ther
ef
ore t
i s w ei
h
ght cannot
be put i
nto W DRM d
i rect
l
y i
n order to avoi
d am bi
gui
t
y .I
f
angl
e));
/
/X coor
di
na
t
es
of
each ver
t
ex P [i].Y =ce
nt
r 0].
e
一i
nt(印 [i]*cos(i* her
t
e
a
re
negat
i
ve
wei
ght
s
i
n t
he
ad
ja
cency mat
r
i
x,
one can si
m pl
y f
ind t
h e nf
ini
m um wei
ght
f
irst, a
n d cha
n ge a
ll
wei
ghts i
n t
h e m at
ri
x i
nt
o posi
ti
ve val
ues by add
i ng a posi
t
i
ve val
ue to t
h e ori
gi
na
l w ei
ght
s. Not
e agai
n t
hat
t
h e ch nged va
l ues i
n t
h e WDRM i
s onl
y s eful
u
i
n det
er
m i
m ng t
he d
irect
ion of edge.The or
igi
—
nal
wei
ght
s m u
st
be u
s ed when i
m pl
em ent
ing a
l go—
ri
t
hm s i
n t
h e gr
aph t
heory or
l
abel
i
ng wei
ght
s.
Thi
s sof
tw a
r e i
s i
m pl
em ent
ed u
si
ng VC + + 6.I
n or—
angl
e ));
/
/ Y coor
di
nat
es
of
each ve
 ̄ex I
I
f
M .i
s t
he source ver
tex and M 。 t
he desti
nat
ion vert
ex t
hen t
h e code segm ent
.u
s ed for com puti
ng the coor
ina
d
t
e
s
of
t
he
ot
her
t
wo ver
t
exe
s(s
ay M 3,M4)of
h e arr
t
ow i
s
a
s f
ol
l
ow s.
Program 2 s p =a
t
an2((M l
Y —M 2.Y ),(M 1.x — M 2.x));
/
/
cor
n—
维普资讯 http://www.cqvip.com
4l
O Journal
o
fShanghai
Uni
ver
si
t
y put
e t
he sl
ope of
edge
f
rom M l
t
o M 2;
cos
Y = co
s ( sl
opy );
Usi
ng t
he a
l gor
i
t
hms and t
he above programs,we s
i
n Y = si
n ( s泖 );
ca
n get
gr
aphs
ba
s ed on t
he
adi
acenc
y ma
t
r
i
x a
s M3.x=M2.
x +i
nt(Le
ngt
ho
farr
ow * C
OS
Y)
一
M l
a
s t
he desti
nat
i
on.
show
n i
n F
i g.
5.
nt(Lengt
i
ho
farrow /rat
e * si
n Y );
3.Y=M2.Y + i
nt(Le
ngt
ho
farr
ow si
n Y)
4 Summ ary +i
nt(Le
ngt
ho
farro
w /rat
e * COS
Y ));
M4.x=M2.
x +i
nt(Lengt
ho
farr
ow * co
s Y)
+i
nt
(Lengaofarr
ow /rat
e si
n Y );
4.
Y=M2.Y — i
nt(Lengt
ho
farr
ow /r
at
e * COS
Y)
一
nt(Le
i
ngt
hof
arr
ow * s
n Y ).
i
I
n t
hi
s
c
ode
s
egment,l
e
ngt
ho
farr
o
w r
e
pr
es
ent
s t
he
l
e
ng
t
h of
ar
r
ow, a
nd l
e
ngt
ho
farr
o
w/
rat
e
r
e
pr
es
ent
s hal
f
widt
h of ar
row , as IM 4
or IM 3
shown i
n Fi
g.
3.
An arr
ow ca
n be construct
ed by connect
i
ng M 2, M 3,
n d a
M 4.Di
f
f
er
ent
l
eng
th a
n d i dt
w
h l
ead t
o
ar
r
ows
of
I
he proposed m ethod i
s superi
or t
o ot
her
draw
i ng s
of
t
wa
re s a
i
t
ca
n bui
l
d gr
aph
s di
rec
t
l
y f
rom a
n adj
a—
cency m at
ri
x,m aki
ng i
t possi
bl
e t
o visua
ll
y i
mpl
em ent
som e a
l gor
ithm s i
n t
he graph t
heory such a
s m
i ni
m um spanni
ng t
ree, short
est pat
h, a
n d con ect
e d com po—
nent
s .Therefore,t
hi
s
sof
tw a
r e i
s pr
act
i
ca
ll
y useful
i
n i sua
v
li
zed t
e ach
i ng of the graph t
heor
y a
n d i
n t
he i
m—
pl
em ent
at
i
on of
t
h e a
l gor
it
hm s i
n the g
r aph t
heor
y,
wh
i ch i
s t
he f
oundat
i
on of m any engi
neer
ing appl
i
ca—
t
i
ORS.
vari
ous shapes.
The
val
ue
of
l
engt
ho
farr
o
w ca
n var
y bet
we
en 1
/
4 t
o 1
/
3 of
t
he l
eng
th of
M l,
M 2.The
coor
di
nat
e
s
of
M6
i
s
Ref
efences l
l
J
Dong Li
—mi
n.Desi
gn and real
i
zat
i
on of
dynami
c obj
ect
-
cal
cul
ated usi
ng t
he fol
l
owi
ng code segm ent
to draw or
i
ent
e
d gr
aph edi
t
or [
J]. Comput
er Engi
nee
ri
ng and an ar
c
.det
er
mi
ned by M l,M 6,M 4,and M 2(see
Fi
g.
Appl
i
cat
i
ons,2001,37(
3):l12— 11
5 (i
n Chi
nese).
6).
1
2 J
Hua
n g Ji
ng—
wei.Bi
nar
y t
ree drawi
ng al
gor
it
hm based on genet
i
c a
lgor
it
m lJ
h
J. Jour
na
l o
f So
tware,2000,11
f
Program 3 (8):l
11
2一 l
117 (i
n Chi
nese).
M6.x:M1.x + i
nt(一Le
ngt
ho
farrow * COS Y一 (Le
ngt
ho
farr
ow /r
at
e
si
n Y));
6.Y:M1.Y + i
nt(一Le
ngt
ho
farro
w * si
n Y+(Le
ngt
ho—
farr
ow /r
at
e
COS Y)).
The coord
i nat
es
of M 4
ca
n be ca
l cu
l at
e d by usi
ng a ne
w va
lue
of
l
engt
ho
farr
o
w i
n Pr
ogr
am 2.Now we
ge
t
coord
i nates of
4 vert
ices, w hich ca
n be used to det
er—
mine an a
r c f
rom M 、t
0 M .
The ar
row at t
he endpoi
nt
of a
r c ca
n be draw n by execut
i
ng P
r ogr
am 2 usi
ng M 4
a
s t
h e source vert
ex n d a
1
3 J
Mao Guo—
yong. Al
gor
it
hms a
n d i
t
s i
mpl
ement
at
i
on i
n buddi
ng wei
ght
ed di
r
ec
t
ed gr
aph 【
J].Jour
na
l f o
Uni
ve
r-
s o
f Sh
a ngh
ai
for Sci
ence
and Tec
hnol
ogy,2003,25 (3):263—266 (i
n Chi
nese).
[4]
Wang Shu—
he.Gr
aph Theor
y n d a
lgor
A
it
m
h
[
M].Uni
ver
—
si
毋 of
Sci
ence
a
n d Technol
ogy of
Chi
na Pr
ess, Hef
ei
,
1
990 (i
n Chi
nese).
1
5] Davi
d J K,S
c ot
W ,Geor
ge S.P
r ogr
an ̄ni
ng Vi
sua
l
C++ lM J.Fi
f
t
h ed
it
i
on,Bei
j
ng i
Hope r es
P
s,Be
i
j
i
ng,1999(i
n Chi
nese).
(Edi
t
or YA0 Yue
-
yuan)