Graphs
Undirected Graphs
Notation: where
- = nodes (or vertices)
- = edges (or arcs) between pairs of nodes
Captures the pairwise relationship between objects Graph size parameters: ,
Example: communcation, circuit, mechanical, financial, transportation...
Graph Representation
Adjacency matrix is a n-by-n matrix with if is an edge.
- two representations of each edge
- space proportional to .
- Checking if is an edge takes time.
- identifying all edges takes time.
Adjacency Lists is a node-indexed array of lists.
- two representations of each edge.
- space is .
- Check if is an edge takes (degree = neighbors)
- Identifying all edges takes time.
Paths, Connectivity, Cycles, and Trees
Definitions for paths and connectivity
- A path in an undirected graph is a sequence of nodes with the property that each consecutive pair 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 and , there is a path between and .
Definitions for cycles:
- A cycle is a path in which and
- A cycle is simple if all nodes are distinct (except for and ).
Definition for trees
- An undirected graph is a tree if it is (1) connected and (2) does not contain a cycle.
- Theorem: Let be an undirected graph on nodes. Any two of the following statements imply the third: is connected, does not contain a cycle, has edges.
Rooted trees: given a tree , choose a root node and orient each edge away from . This models a hierarchical structure.
Graph Connectivity and Traversal
Connectivity
s-t connectivity problem: given two nodes and , is there a path between and ?
s-t shortest path problem: given two nodes and , what is the length of a shortest path between and ?
Applications: friendster, maze traversal, kevin bacon number, fewest hops in a communcation network
Breadth-first Search
BFS intuition is to explore outward from in all possibel directions, adding nodes one "layer" at a time.
The algorithm:
- all neighbors of
- all nodes that do not belong to or , and that have an edge to a node in .
- all nodes that do not belong to an earlier layer, and that have an edge to a node in .
Theorem: For each , consists of all nodes at distance exactly from . There is a path from to iff appears in some layer.
Property: let be a BFS tree of , and let be an edge of . Then, the levels of and differ by at most 1.
The above implementation of BFS runs in time if the graph is given by its adjacency representation. Proof:
- Easy to prove time:
- at most lists
- each node occurs on at most one list; for loop runs times
- when we consider node , there are incident edges , and we spend processing each edge.
- Actually runs in time:
- when we consider node , there are incident edges
- total time processing edges is . Each edge is counted exactly twice in sum, once in degree(u) and once in degree (v).
Use cases:
- Connected component: find all nodes reachable from .
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.
Depth First Search
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 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 is bipartite, it cannot contain an odd-length cycle. Proof: it is not possibel to 2-color the odd-length cycle.
Lemma: Let be a connected graph, and let be the layers produced by BFS starting at node . Exactly one of the following holds:
- (i) no edge of joins two nodes of the same layer, and is bipartite
- (ii) no edge of joins two nodes of the same layer, and 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 is an edge with in same level .
- let = lowest common ancestor
- let be level containing .
- consider cycle that takes edge from to , then path from to , then path from exists.
- the length is (x,y) (path from y to z) (path from z to x) which is odd.
Collorary: a graph is bipartite iff it contains no odd-length cycle.
Connectivity in Directed Graphs
- edge leaves node and enters node .
- 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 , find all nodes reachable from .
- directed s -> t shortest path problem: given two nodes and , what is the length of the shortest path from to .
- graph search: bfs extends naturally to direted graphs
- web crawler: start from web page . Frind all the web pages linked from , either directly or indirectly.
Nodes and are mutually reachable if there is both a path from to and also a path from to .
Strong connectivity: a graph is strongly connected if every pair of nodes is mutually reachable.
Lemma: let by any node. is strongly connected iff every node is reachable from , and is rechable from every node. The proof is as follows:
- => follows from definition
- <= path from u to v: concatenate path with path. Path from to : concatenate path with path (or if paths overlap)/
Theorem: can determine is strongly connected in time. Proof is as follows:
- pick any node in
- run BFS from in
- run BFS from in (reverse orientation of every edge of ).
- 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 time.
DAGs and Topological Ordering
A DAG is a directed graph that contains no directed cycles
A topological order of a directed graph is an ordering of its nodes as so that for every edge we have .
Precedence constraints: edge means task must occur before . Applications:
- application prerequuisite graph: course must be taken before
- compilation: module must be compiled before
- pipeline of computing jobs: output of job needed to determine input of job
If has a topolgical order, then is a DAG. Proof by contradiction:
- suppose that has a topological order and that also has a directed cycle
- Let be the lowest-indexed node in , and let be the node just before ; thus is an edge
- By our choice of , we have that
- On the other hand, since is an edge and is a topogical order, we must have , a contradiction.
If is a DAG, then has a node with no entering edges. Proof:
- Suppose that has a DAG and every node has at least one entering edge. Let's see what happens.
- Pick any node , and begin following edges backwards from . Since has at least one entering edge we can walk backward to .
- Then, since has at least one entering edge , we can walk backward to
- Repeat until we visit a node, say , twice.
- Let denote the sequence of nodes encountered between successive visits to . is a cycle.
Lemma: if is a DAG, then has a topological ordering. Proof by induction on :
- Base case: true if .
- Given DAG on nodes, find a node with no entering edges.
- is a DAG, since deleting cannot create cycles.
- By induction hypothesis, has a topological ordering.
- Place first in topological ordering; then append nodes of in topological order. This is valid since 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 time. Proof:
- maintain the following information: = remaining number of incoming edges, = set of remaining nodes with no incoming edges.
- initialization: via single scan through graph.
- Update: delete : remove from , decrement hits , this is per edge. 3
Linked from Algorithms