46
Graphs - I CS 2110, Spring 2017 Leonhard Euler. 1736 7 Bridges of Konigsberg

Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 2: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Thesearen’tthegraphswe’reinterestedin

Page 3: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Thisis

V.J.WedeenandL.L.Wald,MarAnosCenterforBiomedicalImagingatMGH

Page 4: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Andsoisthis

Page 5: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

ThiscarriesInternettrafficacrosstheoceans

Page 6: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Anoldersocialgraph

Page 7: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Anoldersocialgraph

VoltaireandBenjaminFranklin

Page 8: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

AficAonalsocialgraph

Page 9: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Atransportgraph:NYsubwaysystem

Page 10: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Anothertransportgraph

Page 11: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Acircuitgraph(flip-flop)

Page 12: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Acircuitgraph(Intel4004)

Page 13: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Thisisnotagraph,thisisacat

Page 14: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Thisisagraph(icalmodel)thathaslearnedtorecognizecats

Page 15: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Someabstractgraphs

K5

K3,3

Page 16: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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)•  …

Page 17: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 18: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 19: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 20: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 21: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

A

B

C

D

E

F

Page 22: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

B

C

D

E

F

Page 23: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

C

D

E

F

Page 24: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

D

E

F

Page 25: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

E

F

Page 26: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

F

Page 27: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

IsthisaDAG?

•  IntuiAon:–  Ifit’saDAG,theremustbeavertexwithindegreezero

•  Thisidealeadstoanalgorithm– AdigraphisaDAGifandonlyifwecaniteraAvelydeleteindegree-0verAcesunAlthegraphdisappears

YES!

Page 28: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Topologicalsort

•  WejustcomputedatopologicalsortoftheDAG– ThisisanumberingoftheverAcessuchthatalledgesgofromlower-tohigher-numberedverAces

– Usefulinjobschedulingwithprecedenceconstraints

1

2

3

4

5

6

Page 29: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Graphcoloring

•  AcoloringofanundirectedgraphisanassignmentofacolortoeachnodesuchthatnotwoadjacentverAcesgetthesamecolor

•  Howmanycolorsareneededtocolorthisgraph?

A

B

C

D

E

F

Page 30: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Graphcoloring

•  AcoloringofanundirectedgraphisanassignmentofacolortoeachnodesuchthatnotwoadjacentverAcesgetthesamecolor

•  Howmanycolorsareneededtocolorthisgraph?

A

B

C

D

E

F

Page 31: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

AnapplicaAonofcoloring

•  VerAcesaretasks•  Edge(u, v)ispresentiftasksuandveachrequireaccessto

thesamesharedresource,andthuscannotexecutesimultaneously

•  Colorsare9meslotstoschedulethetasks•  Minimumnumberofcolorsneededtocolorthegraph=

minimumnumberofAmeslotsrequired

A

B

C

D

EF

Page 32: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

A

B

C

D

E

F

Page 33: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

– Yes!

A

B

C

D

E

F

Page 34: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Planarity

•  Agraphisplanarifitcanbedrawnintheplanewithoutanyedgescrossing

•  Isthisgraphplanar?

– Yes!

A

B

C

D

E

F

Page 35: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

DetecAngPlanarity

Kuratowski'sTheorem:

•  AgraphisplanarifandonlyifitdoesnotcontainacopyofK5orK3,3(possiblywithothernodesalongtheedgesshown)

K5 K3,3

Page 36: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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.

Page 37: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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!

Page 38: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 39: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Another4-coloredplanargraph

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

Page 40: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

BiparAtegraphs

•  AdirectedorundirectedgraphisbiparAteiftheverAcescanbeparAAonedintotwosetssuchthatnoedgeconnectstwoverAcesinthesameset

•  Thefollowingareequivalent–  GisbiparAte–  Gis2-colorable–  Ghasnocyclesofoddlength

1

2

3

A

B

C

D

Page 41: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Someabstractgraphs

K5

K3,3

=

Page 42: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 43: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

RepresentaAonsofgraphs

2 3

2 4

3

1

2

3

4

AdjacencyList AdjacencyMatrix

1 2

3 4

12341234

0101001000000110

Page 44: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

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

Page 45: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

–  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?

Page 46: Graphs - I · Directed graphs • A directed graph (digraph) is a pair (V, E) where – V is a (finite) set – E is a set of ordered pairs (u, v) where u,v ∈ V • Oen require

Graphalgorithms

•  Search–  Depth-firstsearch–  Breadth-firstsearch

•  Shortestpaths–  Dijkstra'salgorithm

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