Graphs

Created

Undirected Graphs

Notation: G=(V,E)G = (V, E) where

  • VV = nodes (or vertices)
  • EE = edges (or arcs) between pairs of nodes

Captures the pairwise relationship between objects Graph size parameters: n=∣V∣n = |V|, m=∣E∣m = |E|

Example: communcation, circuit, mechanical, financial, transportation...

Graph Representation

Adjacency matrix is a n-by-n matrix with Auv=1A_{uv} = 1 if (u,v)(u,v) is an edge.

  • two representations of each edge
  • space proportional to n2n^2.
  • Checking if (u,v)(u,v) is an edge takes Θ(1)\Theta(1) time.
  • identifying all edges takes Θ(n2)\Theta(n^2) time.

Adjacency Lists is a node-indexed array of lists.

  • two representations of each edge.
  • space is Θ(M=n)\Theta(M=n).
  • Check if (u,v)(u,v) is an edge takes O(degree(u))O(degree(u)) (degree = neighbors)
  • Identifying all edges takes Θ(m+n)\Theta(m+n) time.

Paths, Connectivity, Cycles, and Trees

Definitions for paths and connectivity

  • A path in an undirected graph G=(V,E)G=(V,E) is a sequence of nodes v1,v2,…,vkv_1,v_2,\dots,v_k with the property that each consecutive pair vi−1,viv_{i-1}, v_i is joined by a different edge in E.
  • A path is simple if all the nodes are distinct
  • An undirected graph is connected if for every pair of nodes uu and vv, there is a path between uu and vv.

Definitions for cycles:

  • A cycle is a path v1,v2,…,vkv_1,v_2,\dots,v_k in which v1=vkv_1=v_k and k≥2k\geq2
  • A cycle is simple if all nodes are distinct (except for v1v_1 and vkv_k).

Definition for trees

  • An undirected graph is a tree if it is (1) connected and (2) does not contain a cycle.
  • Theorem: Let GG be an undirected graph on nn nodes. Any two of the following statements imply the third: GG is connected, GG does not contain a cycle, GG has n−1n-1 edges.

Rooted trees: given a tree TT, choose a root node rr and orient each edge away from rr. This models a hierarchical structure.

Graph Connectivity and Traversal

Connectivity

s-t connectivity problem: given two nodes ss and tt, is there a path between ss and tt?

s-t shortest path problem: given two nodes ss and tt, what is the length of a shortest path between ss and tt?

Applications: friendster, maze traversal, kevin bacon number, fewest hops in a communcation network

BFS intuition is to explore outward from ss in all possibel directions, adding nodes one "layer" at a time.

The algorithm:

  • L0=sL_0={s}
  • L1=L_1 = all neighbors of L0L_0
  • L2=L_2 = all nodes that do not belong to L0L_0 or L1L_1, and that have an edge to a node in L1L_1.
  • Li+1=L_{i+1}= all nodes that do not belong to an earlier layer, and that have an edge to a node in LiL_i.

Theorem: For each ii, LiL_i consists of all nodes at distance exactly ii from ss. There is a path from ss to tt iff tt appears in some layer.

Property: let TT be a BFS tree of G=(V,E)G=(V,E), and let (x,y)(x,y) be an edge of GG. Then, the levels of xx and yy differ by at most 1.

The above implementation of BFS runs in O(m+n)O(m+n) time if the graph is given by its adjacency representation. Proof:

  • Easy to prove O(n2)O(n^2) time:
    • at most nn lists L[i]L[i]
    • each node occurs on at most one list; for loop runs ≤n\leq n times
    • when we consider node uu, there are ≤n\leq n incident edges (u,v)(u,v), and we spend O(1)O(1) processing each edge.
  • Actually runs in O(m+n)O(m+n) time:
    • when we consider node uu, there are degree(u)degree(u) incident edges (u,v)(u,v)
    • total time processing edges is ∑u∈Vdegree(u)=2m\sum_{u \in V} degree(u) = 2m. Each edge (u,v)(u,v) is counted exactly twice in sum, once in degree(u) and once in degree (v).

Use cases:

  • Connected component: find all nodes reachable from ss.
R will consist of nodes to which s has a path
Initially, R = {s}
While there is an edge (u,v) where u in R and v not in R,
	Add v to R
Endwhile

Theorem: Upon termination, R is the connected component containing s.

  • Flood fill: given a lime green pixel in an image, change color of entire blob of nieghboring lime pixels to blue. Node=pixel, edge=two neighboring lime pixels, blob=connected component of lime pixels.

BFS is used to explore in order of distance from s DFS is to explore in a different way.

DFS(u):
	Mark u as "Explored" and add u to R
	For each edge (u,v) incident to u
		If v is not marked "Explored" then
			Recursively invoke DFS(v)
		Endif
	Endfor

Bartiteness

An undirected graph G=(V,E)G=(V,E) is bipartite if the node can be colored blue or white such that every edge has one white and one blue end (if it is possible)

Applications:

  • stable matching where med-school residents are one color and hospitals are white
  • scheduling: machine are blue and jobs are white.

Many graph probelms become easier if the underlying graph is bipartite (matching). Or, tractable if the underlying graph is bipartite (independent set).

Lemma: if a graph GG is bipartite, it cannot contain an odd-length cycle. Proof: it is not possibel to 2-color the odd-length cycle.

Lemma: Let GG be a connected graph, and let L0,…,LkL_0,\dots, L_k be the layers produced by BFS starting at node ss. Exactly one of the following holds:

  • (i) no edge of GG joins two nodes of the same layer, and GG is bipartite
  • (ii) no edge of GG joins two nodes of the same layer, and GG contains an odd-length cycle (and hence is not bipartite).

Proof of (i):

  • suppose no edge joins two nodes in the same layer.
  • by BFS property, each edge joins two nodes in adjacent levels
  • bipartition: white = nodes on odd levels, blue = nodes on even levels.

Proof of (ii):

  • suppose (x,y)(x,y) is an edge with x,yx,y in same level LjL_j.
  • let z=lca(x,y)z = lca(x,y) = lowest common ancestor
  • let LiL_i be level containing zz.
  • consider cycle that takes edge from xx to yy, then path from yy to zz, then path from ztoxz to x exists.
  • the length is 11 (x,y) +(j−i)+ (j-i) (path from y to z) +(j−i)+ (j-i) (path from z to x) which is odd.

Collorary: a graph GG is bipartite iff it contains no odd-length cycle.

Connectivity in Directed Graphs

G=(V,E)G=(V,E)

  • edge (u,v)(u,v) leaves node uu and enters node vv.
  • e.g., web graph: hyperlink points from one page to another. the orientation of edges is crucial. modern web search engines exploit hyperlink strucutre to rank web pages by importance.

Examples

  • Web graph: node = webpage, edge=hyperlink from one page to another (orientation is crucial), modern search engine exploit hyperlink structure to rank web pages by importance.
  • Road network: node-intersection; edge = one-way street
  • Political pblogosphere graph: node = political blog; edge = link
  • Ecological food web: node = species, edge= from prey to predator

In a graph search

  • directed reachability: give a node ss, find all nodes reachable from ss.
  • directed s -> t shortest path problem: given two nodes ss and tt, what is the length of the shortest path from ss to tt.
  • graph search: bfs extends naturally to direted graphs
  • web crawler: start from web page ss. Frind all the web pages linked from ss, either directly or indirectly.

Nodes uu and vv are mutually reachable if there is both a path from uu to vv and also a path from vv to uu.

Strong connectivity: a graph is strongly connected if every pair of nodes is mutually reachable.

Lemma: let ss by any node. GG is strongly connected iff every node is reachable from ss, and ss is rechable from every node. The proof is as follows:

  • => follows from definition
  • <= path from u to v: concatenate u⇝su\leadsto s path with s⇝vs\leadsto v path. Path from vv to uu: concatenate v⇝sv\leadsto s path with s⇝us \leadsto u path (or if paths overlap)/

Theorem: can determine GG is strongly connected in O(m+n)O(m+n) time. Proof is as follows:

  • pick any node in ss
  • run BFS from ss in GG
  • run BFS from ss in GreverseG^{reverse} (reverse orientation of every edge of GG).
  • return true iff all nodes reached in both BFS executions
  • correctness follows immediately from previous lemma.

A strong component is a maximal subset of mutually reachable nodes.

Theorem: you can find all strong components in O(m+n)O(m+n) time.

DAGs and Topological Ordering

A DAG is a directed graph that contains no directed cycles

A topological order of a directed graph G=(V,E)G=(V,E) is an ordering of its nodes as v1,v2,⋯ ,vnv_1, v_2, \cdots, v_n so that for every edge (vi,vj)(v_i,v_j) we have i<ji < j.

Precedence constraints: edge (vi,vj)(v_i, v_j) means task viv_i must occur before vjv_j. Applications:

  • application prerequuisite graph: course viv_i must be taken before vjv_j
  • compilation: module viv_i must be compiled before vjv_j
  • pipeline of computing jobs: output of job vjv_j needed to determine input of job vjv_j

If GG has a topolgical order, then GG is a DAG. Proof by contradiction:

  • suppose that GG has a topological order v1,v2,⋯ ,vnv_1, v_2, \cdots, v_n and that GG also has a directed cycle CC
  • Let viv_i be the lowest-indexed node in CC, and let vjv_j be the node just before viv_i; thus (vi,vj)(v_i, v_j) is an edge
  • By our choice of ii, we have that i<ji<j
  • On the other hand, since (vi,vj)(v_i, v_j) is an edge and v1,v2,⋯ ,vnv_1, v_2, \cdots, v_n is a topogical order, we must have j<ij < i, a contradiction.

If GG is a DAG, then GG has a node with no entering edges. Proof:

  • Suppose that GG has a DAG and every node has at least one entering edge. Let's see what happens.
  • Pick any node vv, and begin following edges backwards from vv. Since vv has at least one entering edge (u,v)(u,v) we can walk backward to uu.
  • Then, since uu has at least one entering edge (x,u)(x,u), we can walk backward to xx
  • Repeat until we visit a node, say ww, twice.
  • Let CC denote the sequence of nodes encountered between successive visits to ww. CC is a cycle.

Lemma: if GG is a DAG, then GG has a topological ordering. Proof by induction on nn:

  • Base case: true if n=1n=1.
  • Given DAG on n>1n>1 nodes, find a node vv with no entering edges.
  • G−vG - {v} is a DAG, since deleting vv cannot create cycles.
  • By induction hypothesis, G−vG-{v} has a topological ordering.
  • Place vv first in topological ordering; then append nodes of G−vG-{v} in topological order. This is valid since vv has no entering edges, hence it is a topological ordering.
to compute a topogical ordering of G:
find a node v with no incoming edge and order it first 
Delete v from G
Recursively compute a topologial ordering of G - {v} and append this order after v. 

Theorem: algorithm finds a topological order in O(m+n)O(m+n) time. Proof:

  • maintain the following information: count(w)count(w) = remaining number of incoming edges, SS = set of remaining nodes with no incoming edges.
  • initialization: O(m+n)O(m+n) via single scan through graph.
  • Update: delete vv: remove vv from SS, decrement count(w)count(w) hits 00, this is O(1)O(1) per edge. 3