You are on page 1of 3

GraphTheory

ThisdescriptionisadaptedfromRobertSedgewicksAlgorithms,(AddisonWesley,1983).This
book, a reference for virtually all genres of computer algorithms, contains many example
programsinPascal.Moreover,itislucidandwellwritten.Werecommendthisbookhighly.
Manyproblemsarenaturallyformulatedintermsofpointsandconnectionsbetweenthem.For
example,anelectriccircuithasgatesconnectedbywires,anairlinemaphascitiesconnectedby
routes,andaprogramflowcharthasboxesconnectedbyarrows. Agraphisamathematical
objectwhichmodelssuchsituations.
Agraphisacollectionofverticesandedges.Anedgeisaconnectionbetweentwovertices(or
nodes).Onecandrawagraphbymarkingpointsfortheverticesanddrawinglinesconnecting
themfortheedges,butitmustbeborneinmindthatthegraphisdefinedindependentlyofthe
representation.Forexample,thefollowingtwodrawingsrepresentthesamegraph:(page374)
I

K
M

D
D
E

E
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}.
A path from vertex x to y inagraph is alist ofvertices, inwhichsuccessive vertices are
connectedbyedgesinthegraph.Forexample,BAFEGispathfromBtoGinthegraphabove.
Asimplepathisapathwithnovertexrepeated.Forexample,BAFEGACisnotasimplepath.
Agraphis connected ifthereisapathfromeveryvertextoeveryothervertexinthegraph.
Intuitively,iftheverticeswerephysicalobjectsandtheedgeswerestringsconnectingthem,a
connectedgraphwouldstayinonepieceifpickedupbyanyvertex. Agraphwhichisnot
connected is made up of connected components. For example, the graph above has three
connectedcomponents:{I,H},{J,K,L,M}and{A,B,C,D,E,F,G}.
Acycleisapath,whichissimpleexceptthatthefirstandlastvertexarethesame(apathfroma
pointbacktoitself).Forexample,thepathAFEGAisacycleinourexample.Verticesmustbe
listedintheorderthattheyaretraveledtomakethepath;anyoftheverticesmaybelistedfirst.
Thus,FEGAFandGAFEGaredifferentwaystoidentifythesamecycle.Forclarity,welistthe
start/endvertextwice:onceatthestartofthecycleandonceattheend.Agraphwithnocycles
iscalledatree.Thereisonlyonepathbetweenanytwonodesinatree.AtreeonNvertices
contains exactly N1edges. A spanningtree ofagraphisasubgraphthatcontains allthe
verticesandformsatree.Agroupofdisconnectedtreesiscalledaforest.

1of3

GraphTheory

Directedgraphsaregraphswhichhaveadirectionassociatedwitheachedge.Anedgexyina
directedgraphcanbeusedinapaththatgoesfromxtoybutnotnecessarilyfromytox.For
example,adirectedgraphsimilartoourexamplegraphisdrawnbelow.(page422)
H

A
C
B
D

G
E

ThereisonlyonedirectedpathfromDtoF.NotethattherearetwoedgesbetweenHandI,one
eachdirection,whichessentiallymakesanundirectededge.Anundirectedgraphcanbethought
ofasadirectedgraphwithalledgesoccurringinpairsinthisway.Adag(directedacyclic
graph)isadirectedgraphwithnocycles.
WelldenotethenumberofverticesinagivengraphbyV,thenumberofedgesbyE.Notethat
EcanrangeanywherefromVtoV2(or1/2V(V1)inanundirectedgraph).Graphswillalledges
presentarecalledcompletegraphs;graphswithrelativelyfewedgespresent(saylessthanV
log(V))arecalledsparse;graphswithrelativelyfewedgesmissingarecalleddense.
Itisfrequentlyconvenienttorepresentagraphbyamatrix,asshowninthesecondsample
problembelow.IfweconsidervertexAas1,Bas2,etc.,thenaoneinMatrowiandcolumn
jindicatesthatthereisapathfromvertexitoj.IfweraiseMtothepthpower,theresulting
matrixindicateswhichpathsoflengthpexistinthegraph.Infact,thequantityMp(i,j)isthe
numberofpaths.
References
Ore,Oystein.GraphsandTheirUses,MAANewMathematicLibrary#10(1963).
Sedgewick,Robert.Algorithms.AddisonWesley(1983).

2of3

GraphTheory
SampleProblems
Findthenumberofdifferentcycles
containedinthedirectedgraphwith
vertices
{A,B,C,D,E}
andedges
{AB,BA,BC,CD,DC,DB,DE}.

Thegraphisasfollows:
A

Byinspection,thecyclesare:{A,B},{B,C,D}and
{C,D}.Thus,thereare3cyclesinthegraph.
Inthefollowingdirectedgraph,findthe Byinspection,theonlypathoflength2is
totalnumberofdifferentpathsfrom
AAC.
vertexAtovertexCoflength2or4.
Thepathsoflength4are:AAAAC,
AACACandACAAC.
A

Alternatively,letmatrixMrepresentthegraph.
Recallthatthenumberofpathsfromvertexito
vertexjoflengthpequalsMp(i,j).Thevaluesof
M,M2andM4are:
101201503
011,111,413
100101302

Thereis1pathoflength2(M2(1,3))and3pathsof
length4(M4(1,3)).

3of3