2. Algorithmic Primitives for Graphs Flashcards
What is a directed graph?
A graph where all the edges are directed from one vertex to another.
Sometimes called a digraph or a directed network.
What is an undirected graph?
A graph where all the edges are bidirectional.
Sometimes called an undirected network.
Matrix
A rectangular array of numbers, symbols, or expressions, arranged in rows and columns.
Tree
Connected undirected graph with no cycles.
Connectivity
u and w are connected iff there is a path between them
A graph is connected iff all pairs of vertices are connected
Cycle
Sequence of k vertices connected by edges.
The first k-1 edges are distinct.
Rooted tree
Pick a node to be the root of the tree and let the rest of the tree hang under “gravity”
Distance
Length of the shortest path between u and v
INFINITY represents the distance between two paths which are not connected
Breadth First Search (BFS)
Searches a tree or graph at the root (or arbitrary node of a graph) and explores the neighbor (child) nodes first, before moving on to the next level of neighbors (children)
BFS Pseudocode (version 1)
for each node n in Graph:
n. distance = INFINITY
n. parent = NULL
create empty queue Q
root.distance = 0
Q.enqueue(root)
while Q is not empty:
current = Q.dequeue()
for each node n that is adjacent to current:
if n.distance == INFINITY:
n.distance = current.distance + 1
n.parent = current
Q.enqueue(n)
Adjacency Matrices
A two-dimensional array A, with n rows and n columns, where n = |V|. The entry A[i,j] is equal to 1 if there is an edge joining i and j, and it is equal to 0 otherwise.
Thus, row i of A corresponds to the node i, in that the sequence of 0s and 1s in row i indicate precisely the nodes to which i has edges.
Column i of A performs the same function; notice that since G is undirected, A is symmetric: A[i,j] = A[j,i]
Adjacency Matrices
A two-dimensional array A, with n rows and n columns, where n = |V|. The entry A[i,j] is equal to 1 if there is an edge joining i and j, and it is equal to 0 otherwise.
Thus, row i of A corresponds to the node i, in that the sequence of 0s and 1s in row i indicate precisely the nodes to which i has edges.
Column i of A performs the same function; notice that since G is undirected, A is symmetric: A[i,j] = A[j,i]
Two flaws of adjacency matrices
- They are huge objects. They have a size of n^2.
- Determining the number of edges for any node i takes O(n) because every element in row i has to be checked to see if it’s a 1.
Adjacency list
An n-element array V, where V[i] represents node i. V[i] simply points to a doubly linked list Li that contains an entry for each edge e=(i,j) incident to i.
If we want to enumerate all the edges incident to node i, we first locate entry V[i] and then walk through the doubly linked list of edges that it points to. If there are d incident edges, this takes time O(d), independent of n.
Interesting phenomenon, each edge is represented twice. Once as V[i] -> (i,j) and once as V[j] -> (j,i)
When is an adjacency matrix better than an adjacency list?
Adjacency matrix (AM) tell us if a pair of nodes contain an edge in constant time.