Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a...

Preview:

Citation preview

Graphs-I CS2110,Spring2017Leonhard Euler. 1736 7 Bridges of Konigsberg

Thesearen’tthegraphswe’reinterestedin

Thisis

V.J.WedeenandL.L.Wald,MarAnosCenterforBiomedicalImagingatMGH

Andsoisthis

ThiscarriesInternettrafficacrosstheoceans

Anoldersocialgraph

Anoldersocialgraph

VoltaireandBenjaminFranklin

AficAonalsocialgraph

Atransportgraph:NYsubwaysystem

Anothertransportgraph

Acircuitgraph(flip-flop)

Acircuitgraph(Intel4004)

Thisisnotagraph,thisisacat

Thisisagraph(icalmodel)thathaslearnedtorecognizecats

Someabstractgraphs

K5

K3,3

ApplicaAonsofGraphs•  CommunicaAonnetworks•  Socialnetworks•  RouAngandshortestpathproblems•  CommoditydistribuAon(networkflow)•  Trafficcontrol•  ResourceallocaAon•  Numericallinearalgebra(sparsematrices)•  Geometricmodeling(meshes,topology,…)•  Imageprocessing(e.g.graphcuts)•  ComputeranimaAon(e.g.moAongraphs)•  Systemsbiology•  DigitalhumaniAes(e.g.RepublicofLe[ers)•  …

Directedgraphs

•  Adirectedgraph(digraph)isapair(V, E)where–  Visa(finite)set–  Eisasetoforderedpairs(u, v)whereu,v ∈ V

•  O]enrequireu ≠ v (i.e.noself-loops)

•  AnelementofViscalledavertexornode•  AnelementofEiscalledanedgeorarc

•  |V|=sizeofV,o]endenotedbyn •  |E|=sizeofE,o]endenotedbym

A

B C

DE

V = {A, B, C, D, E} E = {(A,C), (B,A), (B,C),

(C,D), (D,C)} |V| = 5 |E| = 5

UndirectedGraphs

•  Anundirectedgraphisjustlikeadirectedgraph!– …exceptthatEisnowasetofunordered

pairs{u, v}whereu,v ∈ V

•  EveryundirectedgraphcanbeeasilyconvertedtoanequivalentdirectedgraphviaasimpletransformaAon:–  ReplaceeveryundirectededgewithtwodirectededgesinoppositedirecAons

•  …butnotviceversa

A

B C

DE

V = {A, B, C, D, E} E = {{A,C}, {B,A},

{B,C}, {C,D}} |V| = 5 |E| = 4

Graphterminology

•  VerAcesuandvarecalled–  thesourceandsinkofthedirectededge(u, v),respecAvely

–  theendpointsof(u, v) or {u, v}•  TwoverAcesareadjacentiftheyare

connectedbyanedge•  Theoutdegreeofavertexuinadirected

graphisthenumberofedgesforwhichuisthesource

•  Theindegreeofavertexvinadirectedgraphisthenumberofedgesforwhichvisthesink

•  Thedegreeofavertexuinanundirectedgraphisthenumberofedgesofwhichuisanendpoint

A

B C

DE

A

B C

DE

Moregraphterminology

•  Apathisasequencev0,v1,v2,...,vpofverAcessuchthatfor0 ≤ i < p,–  (vi, vi+1)∈Eifthegraphisdirected –  {vi, vi+1}∈Eifthegraphisundirected

•  Thelengthofapathisitsnumberofedges•  Apathissimpleifitdoesn’trepeatanyverAces•  Acycleisapathv0, v1, v2, ..., vp suchthatv0 = vp

•  AcycleissimpleifitdoesnotrepeatanyverAcesexceptthefirstandlast

•  Agraphisacyclicifithasnocycles•  AdirectedacyclicgraphiscalledaDAG

A

B C

DE

A

B C

DE

DAG

NotaDAG

PathA,C,D

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

A

B

C

D

E

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

B

C

D

E

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

C

D

E

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

D

E

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

E

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

F

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

YES!

Topologicalsort

•  WejustcomputedatopologicalsortoftheDAG– ThisisanumberingoftheverAcessuchthatalledgesgofromlower-tohigher-numberedverAces

– Usefulinjobschedulingwithprecedenceconstraints

1

2

3

4

5

6

Graphcoloring

•  AcoloringofanundirectedgraphisanassignmentofacolortoeachnodesuchthatnotwoadjacentverAcesgetthesamecolor

•  Howmanycolorsareneededtocolorthisgraph?

A

B

C

D

E

F

Graphcoloring

•  AcoloringofanundirectedgraphisanassignmentofacolortoeachnodesuchthatnotwoadjacentverAcesgetthesamecolor

•  Howmanycolorsareneededtocolorthisgraph?

A

B

C

D

E

F

AnapplicaAonofcoloring

•  VerAcesaretasks•  Edge(u, v)ispresentiftasksuandveachrequireaccessto

thesamesharedresource,andthuscannotexecutesimultaneously

•  Colorsare9meslotstoschedulethetasks•  Minimumnumberofcolorsneededtocolorthegraph=

minimumnumberofAmeslotsrequired

A

B

C

D

EF

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

A

B

C

D

E

F

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

– Yes!

A

B

C

D

E

F

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

– Yes!

A

B

C

D

E

F

DetecAngPlanarity

Kuratowski'sTheorem:

•  AgraphisplanarifandonlyifitdoesnotcontainacopyofK5orK3,3(possiblywithothernodesalongtheedgesshown)

K5 K3,3

DetecAngPlanarityIn the early 1970’s, Cornell Prof John Hopcroft spent a sabbatical at Stanford and worked with PhD student Bob Tarjan. They developed the first linear-time algorithm for testing whether a graph was planar. They later received the ACM Turing Award for their work on algorithms.

Tarjan was hired at one point in the 1970’s into our department, but the Ithaca weather was too depressing for him and he left for Princeton.

Coloring a graph: How many colors are needed to color the countries so that no two adjacent countries have the same color? Question asked as early as 1852.

1879. Kemp publishes a theorem that only 4 colors are needed!

1880. Julius Peterson finds a flaw in the Kemp’s proof!

Four-ColorTheorem:Everyplanargraphis4-colorable[Appel&Haken,1976]Theproofrestedoncheckingthat1,936specialgraphshadacertainproperty.

Theyusedacomputertocheckthatthose1,936graphshadthatproperty!

BasicallythefirstAmeacomputerwasneededtochecksomething.Causedalotofcontroversy.

Grieslookedattheircomputerprogram,arecursiveprogramwri[enintheassemblylanguageoftheIBM7090computer,andfoundanerror,whichwassafe(itsaidsomethingdidn’thavethepropertywhenitdid)andcouldbefixed.Othersdidthesame.

Sincethen,therehavebeenimprovements.AndaformalproofhasevenbeendoneintheCoqproofsystem

Another4-coloredplanargraph

h[p://www.cs.cmu.edu/~bryant/boolean/maps.html

BiparAtegraphs

•  AdirectedorundirectedgraphisbiparAteiftheverAcescanbeparAAonedintotwosetssuchthatnoedgeconnectstwoverAcesinthesameset

•  Thefollowingareequivalent–  GisbiparAte–  Gis2-colorable–  Ghasnocyclesofoddlength

1

2

3

A

B

C

D

Someabstractgraphs

K5

K3,3

=

Travelingsalesperson

Findapathofminimumdistancethatvisitseverycity

Amsterdam

Rome

Boston

Atlanta

London Paris

Copenhagen

Munich

Ithaca

New York

Washington

1202 1380

1214

1322

1356

1002

512 216

441

189 160

1556 1323

419

210

224 132

660 505

1078

RepresentaAonsofgraphs

2 3

2 4

3

1

2

3

4

AdjacencyList AdjacencyMatrix

1 2

3 4

12341234

0101001000000110

AdjacencymatrixoradjacencyList?–  n=numberofverAces–  m=numberofedges–  d(u)=degreeofu=no.ofedgesleavingu

•  AdjacencyMatrix–  UsesspaceO(n2) –  EnumeratealledgesinAmeO(n2)–  Answer“Isthereanedgefromutov?”inO(1)Ame–  Be[erfordensegraphs(lotsofedges)

12341234

0101001000000110

–  n=numberofverAces–  e=numberofedges–  d(u)=degreeofu=no.edgesleavingu

•  AdjacencyList–  UsesspaceO(e + n)–  EnumeratealledgesinAmeO(e + n)–  Answer“Isthereanedgefromutov?”inO(d(u))Ame–  Be[erforsparsegraphs(feweredges)

2 3

2 4

3

1

2

3

4

Adjacencymatrixoradjacencylist?

Graphalgorithms

•  Search–  Depth-firstsearch–  Breadth-firstsearch

•  Shortestpaths–  Dijkstra'salgorithm

•  Minimumspanningtrees–  Jarnik/Prim/Dijkstraalgorithm–  Kruskal'salgorithm

Recommended