Graph A graph G is a set V(G) of vertices (nodes) and a set E(G) of edges which are pairs of vertices. abcd e i fgh jkl V = { a, b, c, d, e, f, g, h, i, j, k, l } E = { (a, b), (a, e), (b, e), (b, f), (c, j), (c, g), (c, h), (d, h), (e, j), (g, k), (g, l), (g, h), (i, j) }
A Directed Graph In a directed graph (digraph), edges are ordered pairs. TW 45 UA 120 AA 49 AA 411 AA 523 AA 1387 DL 335 UA 877 AA 903 DL 247 NW 35 From Goodrich and Tamassia (1998) SFO ORD BOS JFK LAX DFW MIA
Applications of Graphs Graphs describe relationships The Internet Streets / Highways (Roadmaps) Molecules Social Networks Geometric Surfaces (CAD) Circuits Parts in an Assembly … John Yoko Ringo George Paul Linda Flow Charts
More General Graphs A multipgraph allows multiple edges between two vertices. a bdf c A pseudograph is a multigraph that allows self-loops (edges from a vertex to itself)
Edges & Degrees u w v e e 1 2 endpoint incident on u and v degree(u) = 2 deg(w) = 1 b a d e c destination source in-degree(b) = 3 out-degree(b) = 4 u and v are adjacent
Handshaking Theorem Let G = (V, E) be an undirected simple graph. ∑ deg(v) = 2 | E | v Vv V If G is directed: ∑∑ v Vv Vv Vv V indeg(v) = outdeg(v) = | E | | E | ≤ | V | · (| V | – 1) / 2 K has ( ) = 6 edges | E | ≤ | V | · (| V | – 1)
Path A path of length k is a sequence v, v, …, v of vertices such that (v, v ) for i = 0, 1, …, k – 1 is an edge of G. 0 1 k i i+1 g a e j n b f k dc o h l p m q Non-simple path: a, b, e, f, g, b, g, l Simple path: a, e, k, p, l, q m, h, d, c, g (no repeated vertices) b, c, d not a path
Cycle a e j n b f k dc o g h l p m q A cycle is a path that starts and ends at the same vertex. A simple cycle has no repeated vertices. k, j, n, k, p, o,k is not simple.
Subgraph a e j n b f k d c o g h l p m q A subgraph H of G is a graph; its edges and vertices are subsets of those of G. V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b, e), (b, g), (e, f), (d, h), (l, p), (l, q)}
Connectivity G is connected if there is a path between every pair of vertices. a b d f e c If G is not connected, the maximal connected subgraphs are the connected components of G. efg a c b d C C C 1 3 2
Strong & Weak Connectivity A directed graph is strongly connected if every two vertices are reachable from each other. a f e c d b It is weakly connected if the underlying undirected graph is connected. f e a d c b
Property of Connectivity Let G = (V, E) be an undirected graph. If G is connected, then | E | ≥ | V | – 1. If G is a tree, then | E | = | V | – 1. If G is a forest, then | E | ≤ | V | – 1.
Adjacency Lists If G is directed, the total length of all the adjacency lists is | E | Adj If G is undirected, the total length is 2 | E |. Space requirement: (|V| + |E|). Preferable representation.
Adjacency Matrix ij A = (a ) a = ij 1 if (i, j) E(G) 0 otherwise Space: (|V| ). 2 Preferred when the graph is small or dense.
Operation Time Operation Adjacency List Adjacency Matrix Scan incident edges (deg(v)) (|V|) Test adjacency of u and v (min(deg(u), deg(v)) (1) Scan outgoing edges (outdeg(v)) (|V|) Scan incoming edges (indeg(v)) (|V|) Lecture notes modified over Dr. Fernandez-Baca’s Space (|V|+|E|) (|V| ) 2