You are on page 1of 15

Graphs

Contents
Introduction
Terminology
Somespecialkindsofgraphs
TestYourself#1
UsesforGraphs
RepresentingGraphs
TestYourself#2
GraphOperations
DepthFirstSearch
TestYourself#3
UsesforDepthFirstSearch
TestYourself#4
BreadthFirstSearch
Dijkstra'sAlgorithm
Summary
Introduction
Graphsareageneralizationoftrees.Liketrees,graphshavenodesandedges.(Thenodesare
sometimescalledverticesandtheedgesaresometimescalledarcs.)However,graphsaremoregeneral
thantrees:inagraph,anodecanhaveanynumberofincomingedges(inatree,therootnodecannot
haveanyincomingedgesandtheothernodescanonlyhaveoneincomingedge).Everytreeisagraph,
butnoteverygraphisatree.
Therearetwokindsofgraphs,directedandundirected:
Notethatinadirectedgraph,theedgesarearrows(aredirectedfromonenodetoanother)whileinthe
undirectedgraphtheedgesareplainlines(theyhavenodirection).Inadirectedgraph,youcanonlygo
fromnodetonodefollowingthedirectionofthearrows,whileinanundirectedgraph,youcangoeither
wayalonganedge.Thismeansthatinadirectedgraphitispossibletoreacha"deadend"(togettoa
nodefromwhichyoucannotleave).
Terminology
Herearetwoexamplegraphs(onedirectedandoneundirected)andtheterminologytodescribethem.
Inthedirectedgraph,thereisanedgefromnode2tonode1therefore:
Thetwonodesareadjacent(theyareneighbors).
Node2isapredecessorofnode1.
Node1isasuccessorofnode2.
Thesourceoftheedgeisnode2andthetargetoftheedgeisnode1.
Intheundirectedgraph,thereisanedgebetweennode1andnode3therefore:
Nodes1and3areadjacent(theyareneighbors).
Nowconsiderthefollowing(directed)graph:
Inthisgraph,thereisapathfromnode2tonode5:215.Thereisapathfromnode1tonode2:
1342.Thereisalsoapathfromnode1backtoitself:13421.Thefirsttwopathsare
acyclicpaths:nonodeisrepeatedthelastpathisacyclicpathbecausenode1occurstwice.
Notethatthelayoutofthegraphisarbitrarytheimportantthingiswhichnodesareconnectedto
whichothernodes.So,forexample,thefollowinggraphisthesameastheonegivenabove,it'sjust
beendrawndifferently:
Alsonotethatanedgecanconnectanodetoitselfforexample:
Somespecialkindsofgraphs
Adirectedgraphthathasnocyclicpaths(thatcontainsnocycles)iscalledaDAG(aDirected
AcyclicGraph).
Anundirectedgraphthathasanedgebetweeneverypairofnodesiscalledacompletegraph.
Here'sanexample:
Adirectedgraphcanalsobeacompletegraphinthatcase,theremustbeanedgefromevery
nodetoeveryothernode.
Agraphthathasvaluesassociatedwithitsedgesiscalledaweightedgraph.Thegraphcanbe
eitherdirectedorundirected.Theweightscanrepresentthingslike:
Thecostoftraversingtheedge.
Thelengthoftheedge.
Thetimeneededtotraversetheedge.
Here'sanexampleofaweighted,directedgraph:
Anundirectedgraphisconnectedifthereisapathfromeverynodetoeveryothernode.For
example:
connected notconnected
Adirectedgraphisstronglyconnectedifthereisapathfromeverynodetoeveryothernode.A
directedgraphisweaklyconnectedif,treatingalledgesasbeingundirected,thereisapathfrom
everynodetoeveryothernode.Forexample:
stronglyconnected weaklyconnectedbut
notstronglyconnected
neitherweaklynor
stronglyconnected
TESTYOURSELF#1
Foreachofthefollowinggraphs,saywhetheritis:
connected,stronglyconnected,weaklyconnected,ornotconnected
completeornotcomplete
Ifthegraphisadirectedgraph,alsosaywhetheritiscyclicoracyclic.
solution
UsesforGraphs
Ingeneral,thenodesofagraphrepresentobjectsandtheedgesrepresentrelationships.Herearesome
examples:
Flightsbetweencities.Thenodesrepresentthecitiesandthereisanedgejkiffthereisaflight
fromcityjtocityk.Thisgraphcouldbeaweightedgraph,usingtheweightstorepresentthe
distance,theflighttime,orthecost.
Interdependenttaskstobedone.Thenodesrepresentthetasksandthereisanedgejkifftaskj
mustbecompletedbeforetaskk.Forexample,wecanuseagraphtorepresentwhatmustbe
donetofinishaCSmajor,withthenodesrepresentingthecoursestobetaken,andtheedges
representingprerequisites.Here'sapartialexample:
Flowcharts(alsoknownascontrolflowgraphs).Thenodesrepresentthestatementsand
conditionsinaprogramandtheedgesrepresenttheflowofcontrol.
Statetransitiondiagrams.Thenodesrepresentstatesandtheedgesrepresentlegalmovesfrom
statetostate.Forexample,wecoulduseagraphtorepresentlegalmovesinagameoftictactoe.
Here'sasmallpartofthatgraph:
Thereasongraphsaregoodrepresentationsincaseslikethosedescribedaboveisthattherearemany
standardgraphalgorithms(operationsongraphs)thatcanbeusedtoanswerusefulquestionslike:
WhatisthecheapestwaytoflyfromMadisontoSaskatoon?
WhichCSclassesmustItakebeforeIcantakeCS640?
Isitpossibleforvariablektobeusedbeforebeinginitialized?
CanIwinagameoftictactoestartingfromthecurrentposition?
RepresentingGraphs
Inatree,allnodescanbereachedfromtherootnode,soatreecanberepresentedusingtwoclasses:a
Treenodeclass(usedtorepresenteachindividualnode)andaTreeclassthatcontainsapointertothe
rootnode.Somegraphshaveasimilarproperty,i.e.,thereisaspecial"root"nodefromwhichallother
nodesarereachable(controlflowgraphsoftenhavethisproperty).Inthatcase,agraphcanalsobe
representedusingaGraphnodeclassfortheindividualnodesandaGraphclassthatcontainsapointer
totherootnode.However,ifthereisnorootnode,thentheGraphclassneedstousesomeotherdata
structuretokeeptrackofthenodesinthegraph.Therearemanypossibilities:anarray,aList,oraSetof
Graphnodescouldbeused.
TheGraphnodeswillcontainwhateverdataisstoredinanode(e.g.,thenameofacity,thenameofa
CSclass,thestatementrepresentedbyacontrolflowgraphnode).Thenodeswillalsocontainpointers
totheirsuccessors(stored,e.g.,inanarray,aList,oraSet).
Here'sonereasonablepairof(incomplete)classdefinitionsfordirectedgraphs,usingArrayListsto
storethenodesinthegraphandthesuccessorsofeachnode:
class Graphnode<T> {
// *** fields ***
private T data;
private List<Graphnode<T>> successors = new ArrayList<Graphnode<T>>();

// *** methods ***
...
}

public class Graph {
// *** fields ***
private List<Graphnode<T>> nodes = new ArrayList<Graphnode<T>>;

// *** methods ***
...
}
TESTYOURSELF#2
Supposewehaveaweightedgraph(oneinwhicheachedgehasanassociatedvalue).Howcouldthe
classdefinitionsgivenabovebeextendedtostoretheedgeweights?
solution
GraphOperations
Asdiscussedabove,graphsareoftenagoodrepresentationforproblemsinvolvingobjectsandtheir
relationshipsbecausetherearestandardgraphoperationsthatcanbeusedtoanswerusefulquestions
aboutthoserelationships.Herewediscusstwosuchoperations:depthfirstsearchandbreadthfirst
searchandsomeoftheirapplications.
Bothdepthfirstandbreadthfirstsearchare"orderly"waystotraversethenodesandedgesofagraph
thatarereachablefromsomestartingnode.Themaindifferencebetweendepthfirstandbreadthfirst
searchistheorderinwhichnodesarevisited.Ofcourse,sinceingeneralnotallnodesarereachable
fromallothernodes,thechoiceofthestartingnodedetermineswhichnodesandedgeswillbetraversed
(eitherbydepthfirstorbreadthfirstsearch).
DepthfirstSearch
Depthfirstsearchcanbeusedtoanswermanyquestionsaboutagraph:
Isitconnected?
Isthereapathfromnodejtonodek?
Doesitcontainacycle?
Whatnodesarereachablefromnodej?
Canthenodesbeorderedsothatforeverynodej,jcomesbeforeallofitssuccessorsinthe
ordering?
Thebasicideaofadepthfirstsearchistostartatsomenoden,andthentofollowanedgeoutofn,then
anotheredgeout,etc.,gettingasfarawayfromnaspossiblebeforevisitinganymoreofn'ssuccessors.
Topreventinfiniteloopsingraphswithcycles,wemustkeeptrackofwhichnodeshavebeenvisited.
Hereisthebasicalgorithmforadepthfirstsearchfromnoden,startingwithallnodesmarked
"unvisited":
1. markn"visited"
2. recursivelydoadepthfirstsearchfromeachofn'sunvisitedsuccessors
Informationaboutwhichnodeshavebeenvisitedcanbekeptinthenodesthemselves(e.g.,usinga
booleanfield)or,ifthenodesarenumberedfrom1toN,the"visited"informationcanbestoredinan
auxiliaryarrayofbooleansofsizeN.Belowiscodefordepthfirstsearch,assumingthatvisited
informationisinanodefieldnamedvisited,thateachnode'ssuccessorsareinaListnamed
successors,andthattheGraphnodeclassprovidestheusualget/setmethodstoaccessitsfields.Note
thatthisbasicdepthfirstsearchdoesn'tactuallydoanythingexceptmarknodesashavingbeenvisited.
We'llseeinthenextsectionhowtousevariationsonthiscodetodousefulthings.
public static void dfs (Graphnode<T> n) {
n.setVisited(true);
for (Graphnode<T> m : n.getSuccessors()) {
if (! m.getVisited()) {
dfs(m);
}
}
}
Here'sapicturethatillustratesthedfsmethod.Inthisexample,nodenumbersareusedtodenotethe
nodesthemselves(i.e.,thecalldfs(0)reallymeansthatthedfsmethodiscalledwithapointertothe
nodelabeled0).Twodifferentcolorsareusedtoindicatethenodecurrentlybeingvisitedandthe
previouslyvisitednode.
Notethatintheexampleillustratedabove,theorderinwhichthenodesarevisitedis:0,2,3,1,4.
Anotherpossibleorder(ifnode4werethefirstsuccessorofnode0)is:0,4,2,3,1.
Toanalyzethetimerequiredfordepthfirstsearch,notethatonecallismadetodfsforeachnodethatis
reachablefromthestartnode.Eachcalllooksatallsuccessorsofthecurrentnode,sothetimeisO(#
reachablenodes+total#ofoutgoingedgesfromthosenodes).Intheworstcase,thisisallnodesandall
edges,sotheworstcasetimeisO(N+E),whereNisthenumberofnodesinthegraphandEisthe
numberofedgesinthegraph.
TESTYOURSELF#3
Assumethatyoustartwithallnodes"unvisited"andyoudoadepthfirstsearch.Writea(Graph)
methodthatsetsallnodesbackto"unvisited".
solution
UsesforDepthFirstSearch
Recallthatatthebeginningofthissectionwesaidthatdepthfirstsearchcanbeusedtoanswers
questionsaboutagraphsuchas:
1. Isitconnected?
2. Isthereapathfromnodejtonodek?
3. Doesitcontainacycle?
4. Whatnodesarereachablefromnodej?
5. Canthenodesbeorderedsothatforeverynodej,jcomesbeforeallofitssuccessorsinthe
ordering?
Questions2,3and5arediscussedtheothersareleftasexercises.
PathDetection
Thefirstquestionwewillconsideris:isthereapathfromnodejtonodek?Thisquestionmightbe
useful,forexample:
whenthegraphrepresentsairlineroutesandwewanttoask"CanIflyfromMadisontoLondon
(maybew/someconnections)?",or
whenthegraphrepresentCScourseprerequisitesandwewanttoask"IsCS367a(transitive)
prerequisiteforCS640?"
Toanswerthequestion,dothefollowing:
step1:markallnodes"notvisited"
step2:dfs(j)
step3:thereisapathfromjtokiffkismarked"visited"
CycleDetection
Therearetwovariationsthatmightbeinteresting:
1. Doesagraphcontainacycle?
2. Isthereacyclicpathstartingfromnodej?
Considertheexamplegivenabovetoillustratedepthfirstsearch.Thereisacycleinthatgraphstarting
fromnode0.Istheresomethingthathappensduringthedepthfirstsearchthatindicatesthepresenceof
thatcycle?Notethatduringdfs(1),0isasuccessorof1butisalreadyvisited.Butthatisn'tquite
enoughtosaythatthere'sacycle,becauseduringdfs(3),node4isasuccessorof3thathasalready
beenvisited,butthereisnocyclestartingfromnode4.
What'sthedifference?Theansweristhatwhennode0isconsideredasasuccessorofnode1,thecall
dfs(0)isstill"active"(i.e.,itsactivationrecordisstillonthecallstack)however,whennode4is
consideredasasuccessorofnode3,thecalldfs(4)hasalreadyfinished.Howcanwetellthe
difference?Theansweristokeeptrackofwhenanodeis"inprogress"(aswellaswhetherithasbeen
visitedornot).Wecandothisbyusingamarkfieldwiththreepossiblevalues:
1. UNVISITED
2. IN_PROGRESS
3. DONE
insteadofthebooleanvisitedfieldwe'vebeenusing.Initially,allnodesaremarkedUNVISITED.
Whenthedfsmethodisfirstcalledfornoden,itismarkedIN_PROGRESS.Onceallofitssuccessors
havebeenprocessed,itismarkedDONE.Thereisacyclicpathreachablefromnodeniffsomenode's
successorisfoundtobemarkedIN_PROGRESSduringdfs(n).
Here'sthecodeforcycledetection:
public boolean hasCycle(Graphnode<T> n) {
n.setMark(IN_PROGRESS);
for (Graphnode<T> m : n.getSuccessors()) {
if (m.getMark() == IN_PROGRESS) {
return true;
}
if (m.getMark() != DONE) {
if (hasCycle(m)) {
return true;
}
}
}
n.setMark(DONE);
return false;
}
Notethatifwewanttoknowwhetheragraphcontainsacycleanywhere(notjustonethatisreachable
fromnoden)wemighthavetocallhasCycleatthe"toplevel"morethanonce.Here'sapseudocode
versionofamethodoftheGraphclassthatreturnstrueiffthereisacyclesomewhereinthegraph:
public boolean graphHasCycle() {
mark all nodes unvisited
for each node k in the graph {
if (node k is marked unvisited) {
if (hasCycle(k)) {
return true;
}
}
}
return false;
}
TopologicalNumbering
Thinkagainaboutthegraphthatrepresentscourseprerequisites.Aslongastherearenocyclesinthe
graphthereisatleastoneorderinwhichtotakecourses,suchthatallprereqsaresatisfied,i.e.,sothat
foreverycourse,allprerequisitesaretakenbeforethecourseitselfistaken.(Notethatisisreasonableto
assumethattherearenocyclesinagraphthatrepresentscourseprerequisitesbecauseacyclewould
meanthatacoursewasaprerequisiteforitself!)
Topologicalnumberingcanbeusedtofindtheorderinwhichtotaketheclasses(sothatallprereqsare
satisfiedfirst).Thegoalistoassignnumberstonodessothatforeveryedgejk,thenumberassigned
tojislessthanthenumberassignedtok.Atopologicalnumberingoftheprerequisitesgraphwouldtell
youonelegalorderinwhichtotaketheCScourses.Forexample:
Tofindatopologicalnumbering,weuseavariationofdepthfirstsearch.Theintuitionisasfollows:as
longastherearenocyclesinthegraph,theremustbeatleastonenodewithnooutgoingedges:
Thelastnumber(N)canbegiventoanysuchnode(310,577,or640inourexample).
Onceallofanode'ssuccessorshavenumbers,thenodeitselfcangetthenextsmallestnumber.
These2situationscorrespondtothepointinmethodhasCyclewherenodenismarkedDONE(whenit
hasnomoreunvisitedsuccessors).Wejustneedtokeeptrackofthecurrentnumber.Belowisamethod
that,givenanodenandanumbernum,assignstopologicalnumberstoallunvisitednodesreachable
fromn,startingwithnumandworkingdown.Notethatbeforecallingthismethodforthefirsttime,all
nodesshouldbemarkedUNVISITEDandthattheinitialcallshouldpassN(thenumberofnodesinthe
graph)asthe2ndparameter.
public int topNum(Graphnode<T> n, int num) throws CycleException {
n.setMark(IN_PROGRESS);
for (Graphnode<T> m : n.getSuccessors()) {
if (m.getMark() == IN_PROGRESS) {
// no topological ordering for a cyclic graph!
throw new CycleException();
}
if (m.getMark() != DONE) {
num = topNum(m, num);
}
}

// here when n has no more successors
n.setMark(DONE);
n.setNumber(num);
return num - 1;
}
Aswasthecaseforcycledetection,wemightneedseveral"toplevel"callstonumberallnodesina
graph.
TESTYOURSELF#4
Question1:Givetwodifferenttopologicalnumberingsforthefollowinggraph.
Question2:ThetopNummethodgivenaboveonlyassignsnumberstothenodesreachablefromnode
n.WritepseudocodeformethodnumberGraph,similartothecodegivenformethodgraphHasCycle
above,thatassignstopologicalnumberstoallnodesinagraph.
Question3:WriteaGraphmethodisConnectedthatreturnstrueifandonlyifthegraphis
connected.Assumethateverynodehasalistofitspredecessorsaswellasalistofitssuccessors.
solution
BreadthfirstSearch
Breadthfirstsearchprovidesanother"orderly"waytovisit(partof)agraph.Thebasicideaistovisit
allnodesatthesamedistancefromthestartnodebeforevisitingfartherawaynodes.Likedepthfirst
search,breadthfirstsearchcanbeusedtofindallnodesreachablefromthestartnode.Itcanalsobe
usedtofindtheshortestpathbetweentwonodesinanunweightedgraph.
Breadthfirstsearchusesaqueueratherthanrecursion(whichactuallyusesastack)thequeueholds
"nodestobevisited".Ifthegraphisatree,breadthfirstsearchgivesyoualevelordertraversal.Here's
thepseudocode:
public void bfs(Graphnode<T> n) {
Queue<Graphnode> queue = new Queue<Graphnode>();

n.setVisited(true);
queue.enqueue( n );
while (!queue.isEmpty())) {
Graphnode<T> current = queue.dequeue();
for (Graphnode<T> k : current.getSuccessors()) {
if (! k.getVisited()){
k.setVisited(true);
queue.enqueue(k);
} // end if k not visited
} // end for every successor k
} // end while queue not empty
}
Here'sthesameexamplegraphweusedfordepthfirstsearchandanillustrationofbreadthfirstsearch
startingwithnode0:
Theorderinwhichnodesare"visited"asaresultofbfs(0)is:
Aswithdepthfirstsearch,allnodesmarkedVISITEDarereachablefromthestartnode,butnodesare
visitedinadifferentorderthantheywouldbeusingdepthfirstsearch.
Wecanuseavariationofbfstofindtheshortestdistance(thelengthoftheshortestpath)toeach
reachablenode:
addadistancefieldtoeachnode
whenbfsiscalledwithnoden,setn'sdistancetozero
whenanodekisabouttobeenqueued,setk'sdistancetothedistanceofthecurrentnode(the
onethatwasjustdequeued)+1
Thistechniqueonlyworksinunweightedgraphs(i.e.,ingraphsinwhichalledgesareassumedtohave
length1).Aninterestingproblemishowtofindshortestpathsinaweightedgraphi.e.,givena"start"
noden,tofind,foreachothernodem,thepathfromntomforwhichthesumoftheweightsonthe
edgesisminimal(assumingthatnoedgehasanegativeweight).Forexample,inthefollowinggraph,
nodesrepresentcities,edgesrepresenthighways,andtheweightsontheedgesrepresentdistances(the
lengthofthehighwaybetweenthetwocities).Breadthfirstsearchcanonlytellyouwhichroutefrom
MadisontoGreenBaygoesthroughthefewestothercitiesitcannottellyouwhichrouteistheshortest.
Dijkstra'salgorithm
Acleveralgorithmthatcanbeusedtosolvethisproblem(tofindshortestpathsinaweightedgraph
withnonnegativeedgeweights)hasbeendefinedbyEdsgarDijkstra(andsoiscalled"Dijkstra's
algorithm").TheworstcaserunningtimeofthealgorithmisO(ElogN),whereEisthenumberof
edgesandNisthenumberofnodes.Thedetailsofthealgorithmarecoveredinlecture(andmaybe
expandeduponhereinthisreadingcheckbacklaterformoredetails).
Summary
Agraphisasetofnodesandasetofedges.
Therearetwokindsofgraphs:directedandundirected.
Highleveloperationsinclude:
depthfirstsearch,whichcanbedoneontheentiregraph(e.g.,tofindcyclesorto
produceatopologicalordering)oronpartofagraph(e.g.,todeterminewhichnodesare
reachablefromagivennode)
breadthfirstsearch,whichcanalsobeusedtodeterminereachabilityandcanbeusedto
findshortestpathsinunweightedgraphs
Dijkstra'salgorithm,whichfindsshortestpathsinweightedgraphs.

You might also like