Graphs SO 337 LX 843 743 233 OR 802 W Hiroaki Kobayashi Outline Graphs efinition pplications Terminology Properties T ata structures for graphs dge list structure djacency list structure djacency matrix structure Hiroaki Kobayashi 2
Graph graph is a pair (V, ), where V is a set of nodes, called vertices is a collection of pairs of vertices, called edges Vertices and edges are positions and store elements xample: vertex represents an airport and stores the three-letter airport code n edge represents a flight route between two airports and stores the mileage of the route PV OR SO LG HNL 2555 337 LX 843 743 233 802 W 387 849 20 42 099 MI Hiroaki Kobayashi 3 dge Types irected edge ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the destination e.g., a flight Undirected edge unordered pair of vertices (u,v) e.g., a flight route irected graph all the edges are directed e.g., route network Undirected graph all the edges are undirected e.g., flight network OR OR flight 206 849 miles PV PV Hiroaki Kobayashi 4
pplications lectronic circuits Printed circuit board Integrated circuit Transportation networks Highway network light network omputer networks Local area network Internet Web atabases ntity-relationship diagram cslaba Paul cs.brown.edu att.net cslabb cox.net brown.edu avid math.brown.edu John qwest.net Hiroaki Kobayashi 5 Terminology nd vertices (or endpoints) of an edge U and V are the endpoints of a dges incident on a vertex a, d, and b are incident on V djacent vertices U and V are adjacent egree of a vertex X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop U a c V d W f b e X Y g h i Z j Hiroaki Kobayashi 6
Terminology (cont.) Path sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct xamples P =(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 not simple U a c V d P 2 W f b e P X g Y h Z Hiroaki Kobayashi 7 Terminology (cont.) ycle circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices and edges are distinct xamples =(V,b,X,g,Y,f,W,c,U,a, ) is a simple cycle 2 =(U,c,W,e,X,g,Y,f,W,d,V,a, ) is a cycle that is not simple U a c V d 2 W f b X e Y g h Z Hiroaki Kobayashi 8
Properties Property Σ v deg(v) = 2m Proof: each edge is counted twice Property 2 In an undirected graph with no self-loops and no multiple edges m n (n )/2 Proof: each vertex has degree at most (n ) simple graph with n vertices has O(n 2 ) edges Notation n m deg(v) number of vertices number of edges degree of vertex v xample n = 4 m = 6 deg(v) = 3 Hiroaki Kobayashi 9 What is the bound for a directed graph? Main Methods of the Graph T Vertices and edges are positions store elements ccessor methods avertex() incidentdges(v) endvertices(e) isirected(e) origin(e) destination(e) opposite(v, e) aredjacent(v, w) Update methods insertvertex(o) insertdge(v, w, o) insertirecteddge(v, w, o) removevertex(v) removedge(e) Generic methods numvertices() numdges() vertices() edges() Hiroaki Kobayashi 0
dge List Structure Vertex object element reference to position in vertex sequence dge object element origin vertex object destination vertex object reference to position in edge sequence Vertex sequence sequence of vertex objects dge sequence sequence of edge objects v a a u b Hiroaki Kobayashi c u v w z w d b c d z djacency List Structure dge list structure Incidence sequence for each vertex sequence of references to edge objects of incident edges ugmented edge objects references to associated positions in incidence sequences of end vertices a v b u w u v w a b Hiroaki Kobayashi 2
djacency Matrix Structure dge list structure ugmented vertex objects Integer key (index) associated with vertex 2-array adjacency array Reference to edge object for adjacent vertices Null for non nonadjacent vertices u a 0 u v 2 w a 0 2 0 v b 2 w b Hiroaki Kobayashi 3 symptotic Performance n vertices, m edges no parallel edges no self-loops ounds are big-oh Space incidentdges(v) aredjacent (v, w) insertvertex(o) insertdge(v, w, o) removevertex(v) removedge(e) dge List n + m m m m djacency List n + m deg(v) min(deg(v), deg(w)) deg(v) djacency Matrix n 2 n 2 n 2 Hiroaki Kobayashi 4 n
epth-irst Search Hiroaki Kobayashi 5 Outline efinitions Subgraph onnectivity Spanning trees and forests epth-first search lgorithm xample Properties nalysis pplications of S Path finding ycle finding Hiroaki Kobayashi 6
Subgraphs subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph Hiroaki Kobayashi 7 onnectivity graph is connected if there is a path between every pair of vertices connected component of a graph G is a maximal connected subgraph of G onnected graph Non connected graph with two connected components Hiroaki Kobayashi 8
Trees and orests (free) tree is an undirected graph T such that T is connected T has no cycles This definition of tree is different from the one of a rooted tree forest is an undirected graph without cycles The connected components of a forest are trees Tree orest Hiroaki Kobayashi 9 Spanning Trees and orests spanning tree of a connected graph is a spanning subgraph that is a tree spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree Hiroaki Kobayashi 20
epth-irst Search epth-first search (S) is a general technique for traversing a graph S traversal of a graph G Visits all the vertices and edges of G etermines whether G is connected omputes the connected components of G omputes a spanning forest of G S on a graph with n vertices and m edges takes O(n + m ) time S can be further extended to solve other graph problems ind and report a path between two given vertices ind a cycle in the graph Hiroaki Kobayashi 2 S lgorithm The algorithm uses a mechanism for setting and getting labels of vertices and edges lgorithm S(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G.vertices() setlabel(u, UNXPLOR) for all e G.edges() setlabel(e, UNXPLOR) for all v G.vertices() if getlabel(v) = UNXPLOR S(G, v) lgorithm S(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setlabel(v, VISIT) for all e G.incidentdges(v) if getlabel(e) = UNXPLOR w opposite(v,e) if getlabel(w) = UNXPLOR setlabel(e, ISOVRY) S(G, w) else setlabel(e, K) Hiroaki Kobayashi 22
xample unexplored vertex visited vertex unexplored edge discovery edge back edge Hiroaki Kobayashi 23 xample (cont.) Hiroaki Kobayashi 24
S and Maze Traversal The S algorithm is similar to a classic strategy for exploring a maze We mark each intersection, corner and dead end (vertex) visited We mark each corridor (edge ) traversed We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack) Hiroaki Kobayashi 25 Properties of S Property S(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by S(G, v) form a spanning tree of the connected component of v Hiroaki Kobayashi 26
nalysis of S Setting/getting a vertex/edge label takes O() time ach vertex is labeled twice once as UNXPLOR once as VISIT ach edge is labeled twice once as UNXPLOR once as ISOVRY or K Method incidentdges is called once for each vertex S runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that Σ v deg(v) = 2m Hiroaki Kobayashi 27 Path inding We can specialize the S algorithm to find a path between two given vertices u and z using the template method pattern We call S(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex and the current vertex s soon as destination vertex z is encountered, we return the path as the contents of the stack lgorithm paths(g, v, z) setlabel(v, VISIT) S.push(v) if v = z return S.elements() for all e G.incidentdges(v) if getlabel(e) = UNXPLOR w opposite(v,e) if getlabel(w) = UNXPLOR setlabel(e, ISOVRY) S.push(e) paths(g, w, z) S.pop(e) else setlabel(e, K) S.pop(v) Hiroaki Kobayashi 28
ycle inding We can specialize the S algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex s soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w lgorithm cycles(g, v) setlabel(v, VISIT) S.push(v) for all e G.incidentdges(v) if getlabel(e) = UNXPLOR w opposite(v,e) S.push(e) if getlabel(w) = UNXPLOR setlabel(e, ISOVRY) paths(g, w) S.pop(e) else T new empty stack repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v) Hiroaki Kobayashi 29 readth-irst Search L L 2 Hiroaki Kobayashi 30
Outline readth-first search lgorithm xample Properties nalysis pplications Hiroaki Kobayashi 3 readth-irst Search readth-first search (S) is a general technique for traversing a graph S traversal of a graph G Visits all the vertices and edges of G etermines whether G is connected omputes the connected components of G omputes a spanning forest of G S on a graph with n vertices and m edges takes O(n + m ) time S can be further extended to solve other graph problems ind and report a path with the minimum number of edges between two given vertices ind a simple cycle, if there is one Hiroaki Kobayashi 32
S lgorithm The algorithm uses a mechanism for setting and getting labels of vertices and edges lgorithm S(G) Input graph G Output labeling of the edges and partition of the vertices of G for all u G.vertices() setlabel(u, UNXPLOR) for all e G.edges() setlabel(e, UNXPLOR) for all v G.vertices() if getlabel(v) = UNXPLOR S(G, v) lgorithm S(G, s) new empty sequence.insertlast(s) setlabel(s, VISIT) i 0 while L i.ismpty() L i + new empty sequence for all v L i.elements() for all e G.incidentdges(v) if getlabel(e) = UNXPLOR w opposite(v,e) if getlabel(w) = UNXPLOR setlabel(e, ISOVRY) setlabel(w, VISIT) L i +.insertlast(w) else setlabel(e, ROSS) i i + Hiroaki Kobayashi 33 xample unexplored vertex visited vertex unexplored edge discovery edge cross edge L L L Hiroaki Kobayashi 34
xample (cont.) L L L 2 L L L 2 L 2 Hiroaki Kobayashi 35 xample (cont.) L L L 2 L 2 L L 2 Hiroaki Kobayashi 36
Properties Notation G s : connected component of s Property S(G, s) visits all the vertices and edges of G s Property 2 The discovery edges labeled by S(G, s) form a spanning tree T s of G s Property 3 or each vertex v in L i The path of T s from s to v has i edges very path from s to v in G s has at least i edges L L 2 Hiroaki Kobayashi 37 nalysis Setting/getting a vertex/edge label takes O() time ach vertex is labeled twice once as UNXPLOR once as VISIT ach edge is labeled twice once as UNXPLOR once as ISOVRY or ROSS ach vertex is inserted once into a sequence L i Method incidentdges is called once for each vertex S runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that Σ v deg(v) = 2m Hiroaki Kobayashi 38
pplications Using the template method pattern, we can specialize the S traversal of a graph G to solve the following problems in O(n + m) time ompute the connected components of G ompute a spanning forest of G ind a simple cycle in G, or report that G is a forest Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists Hiroaki Kobayashi 39