Graphs
Captures pairwise relationship between objects
Definition (Graph)
Graph where
- = nodes (or vertices)
- = edges (or arcs) between pairs of nodes
As a shorthand, ,
A graph is simple if there is at most one edge between two vertices
Basic Definitions
Definition (Path)
A path is an undirected graph is a sequence of nodes with the property that each consecutive pair is jointed by a different edge in .
A path is simple if all nodes are distinct.
Definition (Connected)
An undirected graph is connected if for every pair of nodes and there is a bath between and
Definition (Cycle)
A cycle is a path in which and
A cycle is simple if all nodes are distinct (except for and ).
Definition (Tree)
An undirected graph is a tree if it is connected and does not contain a cycle.
Given a tree, you can choose a root node and orient each edge away from to model a hierarchical structure
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
Implementations
| Edge List | Adjacency Matrix | Adjacency List | |
|---|---|---|---|
| Connectedness | |||
| Adjacency | |||
| Space |
- Edge List
- Good for particular algorithms
- Adjacency Matrix
- Good for dense graphs
- Adjacency List
- Good for sparse graphs
Traversals
Time complexity:
Breadth-First Search
Intuition: Explore outward from is all possible directions, adding nodes one “layer” at a time
Implementation
Take an arbitrary start vertex, mark it as visited, and place it in a queue
while the queue is not empty
take a vertex, u out of the queue
visit u
for all vertices, v, adjacent to u
if v has not been visited
mark v as visited
insert vertex v into the queue
s-t Connectivity Problem
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 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.
Proof: Follows from the definition of a path
Base Case: has a path of length 1 from since is neighbors of Inductive Hypothesis: For any vertex , there is a path from to of length Inductive Step: For any vertex , there is an edge to a vertex in , so there is a path from to of length .
BFS Tree
Definition (Breadth First Search Tree)
A tree formed from a graph by adding only vertices and edges traversed during a BFS
Claim
Let be a BFS tree of and let . Then the levels of and differ by at most 1.
Depth-First Search
Intuition: Explores child before sibling until reaching dead and and backtracking until a new path can be traversed
Implementation: Same as breadth first but with a stack instead of a queue
Connected Component
Definition (Connected Component)
All nodes reachable from
Algorithm:
R will constist 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 RTheorem
Upon termination, is the connected component containing .
- BFS = explore order of distance from
- DFS = explore in a different way
Bipartite Graphs
Definition (Bipartite graphs)
An undirected graph is bipartite if the nodes can be colored blue or white such that every edge has one white and one blue end
Applications
Lemma
If a graph is bipartite, it cannot contain an odd-length cycle.
Proof: Not possible to 2-color an 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:
- No edge of joins two nodes of the same layer, and is bipartite.
- An edge of joint two nodes of the same layer, and contains an odd-length cycle (and hence is not bipartite).
Proof:
(1)
Suppose no edge joins two nodes in the same layer.
By BFS property, each edge joints two nodes in adjacent levels.
Bipartition: white nodes on odd levels, blue nodes on even level.
(2) Suppose is an edge with in the same level Let lowest common ancestor Let be the level containing Consider cycle that takes edge from to , then path from to , then path from to . Its length is which is odd
Corollary
A graph is bipartite iff it contains no odd-length cycle
Proof: The contrapositive of case 2 of the previous lemma
TODO
Directed Graphs
Graphs where an individual edge only goes in one direction
Definition (Strong Component)
A strong component is a maximal subset of mutually reachable nodes
Weighted graphs assigns lengths to edges (an unweighted graph is essentially a weighted graph with every edge having a weight of 1)
Shorted path in a weighted directed graph: Dijkstra’s Algorithm
Types
- Directed (Digraph) vs Undirected
- Weighted vs Unweighted
- Connected vs Unconnected
- Cyclic vs Acyclic
- Dense vs Sparse
- The density of a graph is the ratio of to
- We can assume that is
- for a dense graph (density ~ 1)
- for a dense sparse (density ~ 0)
- Directed Graphs:
- Because every node can be connected to vertices
- Undirected Graphs:
Algorithms
- S-T Path Problem
- Checking if there is a path between two vertices
- Solutions
- Do a traversal and start at the first vertex
- If during the traversal you encounter the other node, there is a path between the two
- Shortest Path
- Spanning Tree
- Cycle Detection
- Topological Sort