Reference facts and proof sketches for graphs, kept terse. The BFS pattern gets a fuller treatment in Breadth First Search Pattern.
Undirected Graphs
- The number of odd degree vertices is even
- If a graph is acyclic, there is a vertex of degree
Trees
For any graph , if two of the following are true, then all three are and is a tree
- is connected
- is acyclic
Breadth-First Search
- visits iff there is a path from
- Edges into then-undiscovered vertices define the BFS-tree of
- Level of the BFS-tree contains all vertices with shortest path from the root
- All non-tree edges join vertices on the same, or adjacent levels of the tree
- If contains edges between vertices in the same layer, it is not bipartite, nor a tree
Bipartite Graphs
- is bipartite iff you can partition into and such that all edges are between and , i.e. no edges between vertices in the same set
- is bipartite iff has no odd cycles
Depth First Search
- visits iff there is a path from (so you can find C.C.s)
- DFS Spanning Tree formed by edges into then-undiscovered vertices
- DFS tree not minimum depth, nor do its levels reflect the min distance
- Non-tree edges never join vertices on the same or adjacent level. Always join a vertex with one of its ancestors or descendants
- All vertices visited during are a descendant of in the DFS tree
- For every edge , either is an ancestor of or is an ancestor of
Directed Acyclic Graphs
- Source: vertex with no incoming edges
- Sink: vertex with no outgoing edges
- Every DAG has a source and a sink
- Every DAG has a topological order, and every graph with a topological order is a DAG
Topological Order
An ordering of nodes so that for every edge , .
- To find, initialize map of in-degrees for each vertex, and a queue of vertices with in-degree 0.
- Then, while the queue isn’t empty, remove a vertex, adding it to the ordering, and decrement its neighbors in-degree.
- If any of them become 0, add them to the queue.
Cuts
- A cut of is a bipartition of into for some
- if exactly one of
- If is connected, then there is at least one edge in every cut
- Every cycle crosses a cut an even number of times
- Cut property: Let be any cut, and let be the min cost edge with exactly one endpoint in . Then every MST contains .
- Cycle property: Let be any cycle, and be the max cost edge belonging to . Then no MST contains .
Minimum Spanning Tree
- Algorithm:
- Sort edges by increasing weight, initialize an empty tree , and add each vertex to its own set.
- Then, for each edge , if and are currently in different sets, add to and merge the sets containing and .
- Proof:
- Show that it is a tree
- Initially start with sets, and only add an edge if you are connecting two of them. Therefore, we end with sets to add an edge between each original set
- Only add edges between disconnected components, so it must be acyclic, since each additional edge connecting and (two disconnected components) is the only edge between them.
- Must be an MST
- Considered edges in increasing order of cost. Taking the first edge where the optimal and Kruskal’s differ, we can exchange them for an equal or better solution.
- Show that it is a tree
Disjoint Sets
- Implementation:
- Maintain a tree of pointers, where every vertex is labeled with the longest path ending at that vertex. To check set membership of and , traverse to root and check if . This is
- To merge two sets, point root with smaller label to root with larger label, adjusting labels of the new root if necessary. This is .
- Properties:
- If the label of a root is , there are at least elements in the set.