Graphs SFO 337 1843 802 ORD LAX 1233 DFW
Graphs A graph is a pair (V, E), where V is a set of odes, called vertices E is a collectio of pairs of vertices, called edges Vertices ad edges are positios ad store elemets Example: A vertex represets a airport ad stores the three-letter airport code A edge represets a flight route betwee two airports ad stores the mileage of the route HNL 2555 SFO LAX 2014 Goodrich, Tamassia, Goldwasser 337 1843 1233 Graphs 802 ORD DFW 849 1120 LGA PVD MIA 2
Edge Types Directed edge ordered pair of vertices (u,v) first vertex u is the origi secod vertex v is the destiatio e.g., a flight Directed graph all the edges are directed e.g., route etwork Udirected edge uordered pair of vertices (u,v) e.g., a flight route Udirected graph all the edges are udirected e.g., flight etwork SFO LAX Soeyik Goodrich UA 120 AA 49 SW 45 ORD UA 877 DL 335 DFW AA 411 AA 523 AA 1387 Garg Goldwasser Tamassia Tollis NW 35 JFK AA 903 MIA BOS DL 247 Vitter Preparata Chiag Graphs 3
Applicatios Electroic circuits Prited circuit board Itegrated circuit Trasportatio etworks Highway etwork Flight etwork Computer etworks Local area etwork Iteret Web Databases Etity-relatioship diagram cslab1a cslab1b math.brow.edu cs.brow.edu brow.edu west.et att.et cox.et Joh Paul David 2014 Goodrich, Tamassia, Goldwasser Graphs 4
Termiology Ed vertices (or edpoits) of a edge U ad V are the edpoits of a Edges icidet o a vertex a a, d, ad b are icidet o V outgoig edges U icomig edges Adjacet vertices c U ad V are adjacet Degree of a vertex w X has degree 5 i-degree out-degree Parallel edges h ad i are parallel edges Self-loop j is a self-loop V d W f b e X Y g h i Z j Graphs 5
Questio What is the sum of degrees of all vertices? Graphs 6
Termiology (2) Path seuece of alteratig vertices ad edges begis with a vertex eds with a vertex each edge is preceded ad followed by its edpoits Simple path path such that all its vertices ad edges are distict Examples P 1 =(V,b,X,h,Z) is a simple path P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is ot simple SFO U a c V d P 2 W b e f ORD P 1 X Y g h JFK BOS Z LAX DFW MIA 2014 Goodrich, Tamassia, Goldwasser Graphs 7
Termiology (3) Cycle circular seuece of alteratig vertices ad edges each edge is preceded ad followed by its edpoits Simple cycle cycle such that all its vertices ad edges are distict Examples C 1 =(V,b,X,g,Y,f,W,c,U,a, ) is a simple cycle C 2 =(U,c,W,e,X,g,Y,f,W,d,V,a, ) is a cycle that is ot simple U a c V d C 2 W f e b X Y C 1 g h Z 2014 Goodrich, Tamassia, Goldwasser Graphs 8
Termiology (4) BOS Reachable V is reachable from U if there is a path from U to V. Udirected graph reachability is symmetric Coected a graph is coected if, for ay two vertices, there is a path betwee them some path subgraph subset of vertices ad edges formig a graph SFO LAX DFW ORD JFK MIA coected Graphs ot coected 9
Termiology (5) Coected Compoet: maximal coected subgraph, e.g., the graph below has 3 coected compoets. Graphs 10
Termiology (6) Tree coected graph without cycles free tree Forest collectio of trees tree tree forest tree tree Graphs 11
Coectivity Let = # of vertices m = # of edges = 5 m = (5 4)/2 = 10 Complete graph all pairs of vertices are adjacet There (-1)/2 pairs of vertices ad so m=(-1)/2 if a graph is ot complete, m<(-1)/2 Graphs 12
Coectivity (2) = #vertices m= #edges For a tree m=-1 = 5 m = 4 Prove! Tree coected graph without cycles Graphs 13
Coectivity (3) = #vertices m= #edges For a tree m=-1 If m<-1, G is ot coected = 5 m = 3 Prove Graphs 14
Spaig Tree A spaig tree of G is a subgraph which is a tree cotais all vertices of G G spaig tree of G Failure of ay edge discoects system Graphs 15
Euler ad the Koeigsberg Bridges Gilliga s Isle? C Pregal River A D B Ca oe walk across each bridge exactly oce ad retur at the startig poit? I 1736, Euler proved that this is ot possible. Graphs 16
Graph Model (with parallel edges) Euleria Tour- path that traverses every edge exactly oce ad returs to the first vertex Euler s Theorem A graph has a Euleria tour if ad oly if all vertices have eve degree C A D Graphs B 17
Vertices ad Edges A graph is a collectio of vertices ad edges. We model the abstractio as a combiatio of three data types: Vertex, Edge, ad Graph. A Vertex is a lightweight object that stores a arbitrary elemet provided by the user (e.g., a airport code) We assume it supports a method, elemet(), to retrieve the stored elemet. A Edge stores a associated object (e.g., a flight umber, travel distace, cost), retrieved with the elemet( ) method. 2014 Goodrich, Tamassia, Goldwasser Graphs 18
Edge List Structure Vertex object elemet referece to positio i vertex seuece Edge object elemet origi vertex object destiatio vertex object referece to positio i edge seuece Vertex seuece seuece of vertex objects Edge seuece seuece of edge objects 2014 Goodrich, Tamassia, Goldwasser Graphs 19
Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 20
Adjacecy List Structure Icidece seuece for each vertex seuece of refereces to edge objects of icidet edges Augmeted edge objects refereces to associated positios i icidece seueces of ed vertices 2014 Goodrich, Tamassia, Goldwasser Graphs 21
Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 22
Adjacecy Matrix Structure Edge list structure Augmeted vertex objects Iteger key (idex) associated with vertex 2D-array adjacecy array Referece to edge object for adjacet vertices Null for o oadjacet vertices The old fashioed versio just has 0 for o edge ad 1 for edge 2014 Goodrich, Tamassia, Goldwasser Graphs 23
Performace vertices, m edges o parallel edges o self-loops Edge List Adjacecy List Adjacecy Matrix Space + m + m 2 icidetedges(v) m deg(v) areadjacet (v, w) m mi(deg(v), deg(w)) 1 isertvertex(o) 1 1 2 isertedge(v, w, o) 1 1 1 removevertex(v) m deg(v) 2 removeedge(e) 1 1 1 2014 Goodrich, Tamassia, Goldwasser Graphs 24
Graph Search Algorithms Systematic search of every edge ad vertex of the graph Graph G = (V, E) is either directed or udirected Applicatios Compilers Networks Graphics Gamig 25
Breadth-First Search L 0 A L 1 B C D L 2 E F
Breadth-First Search Breadth-first search (BFS) is a geeral techiue for traversig a graph A BFS traversal of a graph G Visits all the vertices ad edges of G Determies whether G is coected Computes the coected compoets of G Computes a spaig forest of G BFS o a graph with vertices ad m edges takes O( + m ) time BFS ca be further exteded to solve other graph problems Fid ad report a path with the miimum umber of edges betwee two give vertices 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 27
BFS Algorithm The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm BFS(G) Iput graph G Output labelig of the edges ad partitio of the vertices of G for all u G.vertices() setlabel(u, UNEXPLORED) for all e G.edges() setlabel(e, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED BFS(G, v) Algorithm BFS(G, s) L 0 ew empty seuece L 0.addLast(s) setlabel(s, VISITED) i 0 while L i.isempty() L i +1 ew empty seuece for all v L i.elemets() for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) setlabel(w, VISITED) L i +1.addLast(w) else setlabel(e, CROSS) i i +1 2014 Goodrich, Tamassia, Goldwasser 28 Breadth First Search
Example A uexplored vertex L 0 A A visited vertex uexplored edge L 1 B C D discovery edge cross edge E F L 0 A L 0 A L 1 B C D L 1 B C D E F E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 29
Example (cot.) L 0 A L 0 A L 1 B C D L 1 B C D E F L 2 E F L 0 A L 0 A L 1 B C D L 1 B C D L 2 E F L 2 E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 30
Example (cot.) L 0 A L 0 A L 1 B C D L 1 B C D L 2 E F L 2 E F L 0 A L 1 B C D L 2 E F 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 31
BFS - Example 0 A B C D E F G H I J K L M N O P Breadth First Search 32
BFS - Example 0 1 A B C D E F G H I J K L M N O P Breadth First Search 33
BFS - Example 0 1 2 A B C D E F G H I J K L M N O P Breadth First Search 34
BFS - Example 0 1 2 3 A B C D E F G H I J K L M N O P Breadth First Search 35
BFS - Example 0 1 2 3 A B C D E F G H 4 I J K L M N O P Breadth First Search 36
BFS - Example 0 1 2 3 A B C D E F G H 4 I J K L M N O P 5 Breadth First Search 37
Aalysis Settig/gettig a vertex/edge label takes O(1) time Each vertex is labeled twice oce as UNEXPLORED oce as VISITED Each edge is labeled twice oce as UNEXPLORED oce as DISCOVERY or CROSS Each vertex is iserted oce ito a seuece L i Method icidetedges is called oce for each vertex BFS rus i O( + m) time provided the graph is represeted by the adjacecy list structure Recall that Σ v deg(v) = 2m 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 38
Properties Notatio G s : coected compoet of s G s is a breadth first tree V s cosists of the vertices reachable from s, ad for all v i V s there is a uiue simple path from s to v i G that is also a shortest path from s to v i G The edges i G s are called tree edges For every vertex v reachable from s, the path i the breadth first tree from s to v, correspods to a shortest path i G L 1 B L 0 L 2 B A E A E C C F F D D Breadth First Search 39
Properties (2) Notatio G s : coected compoet of s Property 1 BFS(G, s) visits all the vertices ad edges of G s Property 2 The discovery edges labeled by BFS(G, s) form a spaig tree T s of G s Property 3 B L 0 A E A C F D For each vertex v i L i The path of T s from s to v has i edges Every path from s to v i G s has at least i edges L 1 L 2 B E C F D 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 40
Applicatios We ca specialize the BFS traversal of a graph G to solve the followig problems i O( + m) time Compute the coected compoets of G Compute a spaig forest of G Give two vertices of G, fid a path i G betwee them with the miimum umber of edges, or report that o such path exists Check if a coected graph G is bipartite. Breadth First Search 41
Applicatios Computig Coected Compoets a b c d f g e h k j l i Breadth First Search 42
BFS Algorithm The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm BFS(G) Iput graph G Output labelig of the edges ad partitio of the vertices of G for all u G.vertices() setlabel(u, UNEXPLORED) for all e G.edges() setlabel(e, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED BFS(G, v) Algorithm BFS(G, s) L 0 ew empty seuece L 0.addLast(s) setlabel(s, VISITED) i 0 while L i.isempty() L i +1 ew empty seuece for all v L i.elemets() for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) setlabel(w, VISITED) L i +1.addLast(w) else setlabel(e, CROSS) i i +1 2014 Goodrich, Tamassia, Goldwasser Breadth First Search 43
Applicatios Computig Spaig Forest a b c d f g e h k j l i Breadth First Search 44
Applicatios Shortest Path betwee two odes I a BFS startig from a vertex v, the level umber of vertex u is the legth of the shortest path from v to u. Proof show there is a path from u to v. a path of smaller legth will jump a level w violatig the BFS. Breadth First Search 45
Applicatios Check if a graph is bipartite A udirected graph G = (V, E) is bipartite if the odes ca be colored red or blue such that every edge has oe red ad oe blue ed. Breadth First Search 46
Testig Bipartiteess Give a graph G, is it bipartite? Before attemptig to desig a algorithm, we eed to uderstad structure of bipartite graphs. v 2 v 3 v 2 v 1 v 4 v 6 v 5 v 4 v 3 v 5 v 6 v 7 v 1 v 7 a bipartite graph G aother drawig of G Breadth First Search 47
Prelimiary Lemma. If G has a odd cycle, the it caot be bipartite. Proof: Not possible to 2-color the odd cycle, let aloe G. bipartite (2-colorable) ot bipartite (ot 2-colorable) Breadth First Search 48
Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. L 1 L 1 L 2 L 2 L 3 L 3 Breadth First Search 49
Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. case 2: A edge of G jois two odes of the same layer, ad G cotais a odd-legth cycle (ad hece is ot bipartite). L 1 L 2 L 3 Breadth First Search 50
Bipartiteess ad BFS Lemma. Let G be a coected graph, ad let L 0,, L k be the layers produced by BFS startig at ode s. Exactly oe of the followig holds. case 1: No edge of G jois two odes of the same layer, ad G is bipartite. case 2: A edge of G jois two odes of the same layer, ad G cotais a odd-legth cycle (ad hece is ot bipartite). Proof case 2: Suppose (x,y) is a edge with x, y i the same level L j. Let z = LCA(x, y) lowest commo acestor Let L i be the level cotaiig z Cosider the cycle that takes edge from x to y, the the path from y to x, the z to x The legth of this path is 1+(j-i)+(j-i), which is odd Breadth First Search 51
Questio If G has oly eve legth cycles, the G is bipartite. Breadth First Search 52
Depth-First Search A B D E C
Depth-First Search Depth-first search (DFS) is a geeral techiue for traversig a graph A DFS traversal of a graph G Visits all the vertices ad edges of G Determies whether G is coected Computes the coected compoets of G Computes a spaig forest of G DFS o a graph with vertices ad m edges takes O( + m ) time DFS ca be further exteded to solve other graph problems Fid ad report a path betwee two give vertices Fid a cycle i the graph Depth-first search is to graphs what Euler tour is to biary trees 2014 Goodrich, Tamassia, Goldwasser Depth First Search 54
Example A uexplored vertex A A visited vertex uexplored edge B D E discovery edge back edge C A A B D E B D E C C 2014 Goodrich, Tamassia, Goldwasser Depth First Search 55
Example (cot.) A A B D E B D E C C A A B D E B D E C C 2014 Goodrich, Tamassia, Goldwasser Depth First Search 56
DFS ad Maze Traversal The DFS algorithm is similar to a classic strategy for explorig a maze We mark each itersectio, corer ad dead ed (vertex) visited We mark each corridor (edge ) traversed We keep track of the path back to the etrace (start vertex) by meas of a rope (recursio stack) 2014 Goodrich, Tamassia, Goldwasser Depth First Search 57
DFS for a Etire Graph The algorithm uses a mechaism for settig ad gettig labels of vertices ad edges Algorithm DFS(G) Iput graph G Output labelig of the edges of G as discovery edges ad back edges for all u G.vertices() setlabel(u, UNEXPLORED) for all e G.edges() setlabel(e, UNEXPLORED) for all v G.vertices() if getlabel(v) = UNEXPLORED DFS(G, v) Algorithm DFS(G, v) Iput graph G ad a start vertex v of G Output labelig of the edges of G i the coected compoet of v as discovery edges ad back edges setlabel(v, VISITED) for all e G.icidetEdges(v) if getlabel(e) = UNEXPLORED w opposite(v,e) if getlabel(w) = UNEXPLORED setlabel(e, DISCOVERY) DFS(G, w) else setlabel(e, BACK) 2014 Goodrich, Tamassia, Goldwasser Depth First Search 58
Example A B C D E F G H I J K L M N O P Depth First Search 59
Example A B C D E F G H I J K L M N O P Depth First Search 60
Example A B C D E F G H I J K L M N O P Depth First Search 61
Example A B C D E F G H I J K L M N O P Depth First Search 62
Example A B C D E F G H I J K L M N O P Depth First Search 63
Example A B C D E F G H I J K L M N O P Depth First Search 64
Aalysis of DFS Settig/gettig a vertex/edge label takes O(1) time Each vertex is labeled twice oce as UNEXPLORED oce as VISITED Each edge is labeled twice oce as UNEXPLORED oce as DISCOVERY or BACK Method icidetedges is called oce for each vertex DFS rus i O( + m) time provided the graph is represeted by the adjacecy list structure Recall that Σ v deg(v) = 2m 2014 Goodrich, Tamassia, Goldwasser Depth First Search 65
Properties of DFS Property 1 DFS(G, v) visits all the vertices ad edges i the coected compoet of v Property 2 The discovery edges labeled by DFS(G, v) form a spaig tree of the coected compoet of v B A C D E 2014 Goodrich, Tamassia, Goldwasser Depth First Search 66