Graphs
Graphs are, basically, relations, with a little bit of extra syntax
that lets you express interesting properties of connections between
nodes. Strictly speaking, a graph is an ordered pair G = (V,E), where
V is a set of vertices, (like the set "A" we've talked about for the
last several days), and E is a set of edges between vertices (where is
a relation on A). For reasons that are not clear to me, a graph G is
often written in a way that looks like G is a function of V,E.. the
following notation is the way you define a graph G:
G(V,E) with the constraint that V is a set and E is a
subset of VxV.
Graphs are perhaps the most important data structure in
computer science. They are "simple" in the sense that they have a
very well defined structure (nodes and edges, that's it). But they
are also "expressive" in that these nodes and edges can represent an
amazingly large collection of problems that are of interest.
So, the diagrams of relations from a set to itself are graphs.
When we have a relation defined from one set to another, we can define
a "bipartite" graph as: G( V = A union B, E = A x B), where A and B
are non-overlapping, and all the edges going between a node in set A
and a node in set B.
There are a small number of important properties about graphs that
come up in a large number of problems:
- Paths: A path is a list of vertices v_0 ... v_k where every
consectutive pair of vertices is in the edges set of the graph. (how
would you write this formally?)
- A simply path is one that never uses the same node twice.
- A cycle is a path whose first and last vertices are the same.
- A "simple cycle"... you can probably guess.
- An undirected graph is a graph whose edge set E is symmetric (so
that any vertices that are connected are connected both ways).
- A connected component is a collection of nodes that are mutually reachable.
- If you consider the edge set as a relation and compute the
reflexive, symmetric and transitive closure of that set, the
equivalence classes of the resulting relation are the connected
components of the graph.
- A graph is connected if there is one connected component.
- For directed graphs, the equivalent concept is a "strongly
connected component". Two nodes are in the same "strongly connected
component" if there is a directed path in each direction between
them.
- A "tree" is a connected, acyclic graph.
- There are rooted and unrooted trees. Usually unrooted trees are
undirected graphs, and rooted trees are directed graphs, with edges
pointing away from the root.
- Nodes with just one edge are called "leaves"
- In an undirected tree, there is a unique path between every pair
of vertices. (could prove this).
- adding any edge creates a cycle.
- Removing any edge disconnects the graph (creates more than one
connected component).
- if |V| >= 2, then the number of leaves >= 2.
- |V| = |E| + 1.
- Hamiltonian and Euler Paths. Hard to tell if graph has
hamiltonian path. Easy (?) to tell if there is an euler path, pretty
easy to construct it.
- Many problems can be expressed by adding a function w(e): E -> R
that is a "weight" or "distance" of an edge, or a function c(v): C ->
R that is a "cost" or "price" of a vertex.
- k-center problem.
- shortest path problem
- travelling salesman problem