Professional Documents
Culture Documents
J, K, L, M), and The Set of Edges Between These Vertices (AG, AB, AC, LM, JM, JL, JK, ED, FD, HI, FE, AF, GE)
J, K, L, M), and The Set of Edges Between These Vertices (AG, AB, AC, LM, JM, JL, JK, ED, FD, HI, FE, AF, GE)
ThisdescriptionisadaptedfromRobertSedgewicksAlgorithms,(AddisonWesley,1983).Thisbook,a
referenceforvirtuallyallgenresofcomputeralgorithms,containsmanyexampleprogramsinPascal.
Moreover,itislucidandwellwritten.Werecommendthisbookhighly.
Manyproblemsarenaturallyformulatedintermsofpointsandconnectionsbetweenthem.Forexample,an
electriccircuithasgatesconnectedbywires,anairlinemaphascitiesconnectedbyroutes,andaprogram
flowcharthasboxesconnectedbyarrows.Agraphisamathematicalobjectwhichmodelssuchsituations.
Agraphisacollectionofverticesandedges.Anedgeisaconnectionbetweentwovertices(ornodes).One
candrawagraphbymarkingpointsfortheverticesanddrawinglinesconnectingthemfortheedges,butit
mustbeborneinmindthatthegraphisdefinedindependentlyoftherepresentation.Forexample,thefollowing
twodrawingsrepresentthesamegraph:(page374)
I G
J
A
A
H I
B
C
B C
K
M L
J K
H G
D
D
E
E F L M
F
Theprecisewaytorepresentthisgraphistosaythatitconsistsofthesetofvertices{A,B,C,D,E,F,G,H,I,
J,K,L,M},andthesetofedgesbetweenthesevertices{AG,AB,AC,LM,JM,JL,JK,ED,FD,HI,FE,AF,
GE}.
Apathfromvertexxtoyinagraphisalistofvertices,inwhichsuccessiveverticesareconnectedbyedgesin
thegraph.Forexample,BAFEGispathfromBtoGinthegraphabove.Asimplepathisapathwithnovertex
repeated.Forexample,BAFEGACisnotasimplepath.
Agraphisconnectedifthereisapathfromeveryvertextoeveryothervertexinthegraph.Intuitively,ifthe
verticeswerephysicalobjectsandtheedgeswerestringsconnectingthem,aconnectedgraphwouldstayinone
pieceifpickedupbyanyvertex.Agraphwhichisnotconnectedismadeupofconnectedcomponents.For
example,thegraphabovehasthreeconnectedcomponents:{I,H},{J,K,L,M}and{A,B,C,D,E,F,G}.
Acycleisapath,whichissimpleexceptthatthefirstandlastvertexarethesame(apathfromapointbackto
itself).Forexample,thepathAFEGAisacycleinourexample.Verticesmustbelistedintheorderthatthey
aretraveledtomakethepath;anyoftheverticesmaybelistedfirst.Thus,FEGAFandGAFEGaredifferent
waystoidentifythesamecycle.Forclarity,welistthestart/endvertextwice:onceatthestartofthecycleand
onceattheend.Agraphwithnocyclesiscalledatree.Thereisonlyonepathbetweenanytwonodesina
tree.AtreeonNverticescontainsexactlyN1edges.Aspanningtreeofagraphisasubgraphthatcontainsall
theverticesandformsatree.Agroupofdisconnectedtreesiscalledaforest.
Directedgraphsaregraphswhichhaveadirectionassociatedwitheachedge.Anedgexyinadirectedgraph
canbeusedinapaththatgoesfromxtoybutnotnecessarilyfromytox.Forexample,adirectedgraphsimilar
toourexamplegraphisdrawnbelow.(page422)
H I
A
B G J K
E
D
L M
F
ThereisonlyonedirectedpathfromDtoF.NotethattherearetwoedgesbetweenHandI,oneeachdirection,
whichessentiallymakesanundirectededge.Anundirectedgraphcanbethoughtofasadirectedgraphwithall
edgesoccurringinpairsinthisway.Adag(directedacyclicgraph)isadirectedgraphwithnocycles.
WelldenotethenumberofverticesinagivengraphbyV,thenumberofedgesbyE.NotethatEcanrange
anywherefromVtoV2(or1/2V(V1)inanundirectedgraph).Graphswillalledgespresentarecalled
completegraphs;graphswithrelativelyfewedgespresent(saylessthanVlog(V))arecalledsparse;graphs
withrelativelyfewedgesmissingarecalleddense.
Itisfrequentlyconvenienttorepresentagraphbyamatrix,asshowninthesecondsampleproblembelow.If
weconsidervertexAas1,Bas2,etc.,thenaoneinMatrowiandcolumnjindicatesthatthereisapath
fromvertexitoj.IfweraiseMtothepthpower,theresultingmatrixindicateswhichpathsoflengthpexistin
thegraph.Infact,thequantityMp(i,j)isthenumberofpaths.
References
Ore,Oystein.GraphsandTheirUses,MAANewMathematicLibrary#10(1963).
Sedgewick,Robert.Algorithms.AddisonWesley(1983).
SampleProblems
Findthenumberofdifferentcyclescontainedinthe Thegraphisasfollows:
directedgraphwithvertices A B C
{A,B,C,D,E}
andedges
{AB,BA,BC,CD,DC,DB,DE}.
E D
Byinspection,thecyclesare:{A,B},{B,C,D}and
{C,D}.Thus,thereare3cyclesinthegraph.
Inthefollowingdirectedgraph,findthetotalnumber Byinspection,theonlypathoflength2isAAC.
ofdifferentpathsfromvertexAtovertexCoflength Thepathsoflength4are:AAAAC,
2or4. AACACandACAAC.
Alternatively,letmatrixMrepresentthegraph.Recall
A B thatthenumberofpathsfromvertexitovertexjof
lengthpequalsMp(i,j).ThevaluesofM,M2andM4
are:
101201503
C
011,111,413
100101302
Thereis1pathoflength2(M2(1,3))and3pathsof
length4(M4(1,3)).