2. Algorithmic Primitives for Graphs Flashcards

1
Q

What is a directed graph?

A

A graph where all the edges are directed from one vertex to another.

Sometimes called a digraph or a directed network.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an undirected graph?

A

A graph where all the edges are bidirectional.

Sometimes called an undirected network.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Matrix

A

A rectangular array of numbers, symbols, or expressions, arranged in rows and columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tree

A

Connected undirected graph with no cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Connectivity

A

u and w are connected iff there is a path between them

A graph is connected iff all pairs of vertices are connected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Cycle

A

Sequence of k vertices connected by edges.

The first k-1 edges are distinct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Rooted tree

A

Pick a node to be the root of the tree and let the rest of the tree hang under “gravity”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Distance

A

Length of the shortest path between u and v

INFINITY represents the distance between two paths which are not connected

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Breadth First Search (BFS)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

BFS Pseudocode (version 1)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Adjacency Matrices

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Adjacency Matrices

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Two flaws of adjacency matrices

A
  1. They are huge objects. They have a size of n^2.
  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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Adjacency list

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When is an adjacency matrix better than an adjacency list?

A

Adjacency matrix (AM) tell us if a pair of nodes contain an edge in constant time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 3 self-proving statements about graphs?

A
  1. G is connected
  2. G does not contain a cycle
  3. G has n-1 edges

Any two of these statements implies the third.

17
Q

Graph connectivity pseudocode

A

R will consist of nodes to which s has a path
Initially R = {s}
While there is an edge (u,v) where u w/in R and v !w/in R
Add v to R
Endwhile

18
Q
Connectivity Algo (2.3)
The set R produces at the end of the algorithm consists of precisely the nodes to which s has a path.
A

Assuming that the set R produces after k steps of the loop contain only nodes reachable from R, then the node added in the (k+1)th step must also be reachable from s.

Consider a node w !w/in R, and suppose by way of contradiction that there is an s-w path in G. Since s w/in R, but w !w/in R, there must be a first node v on P that does not belong to R; and this node v is not equal to s.

Thus, there is a node u immediately preceding v on P, so (u,v) is an edge. Since v is the first node on P that does not belong to R, we must have u w/in R.

It follows that (u,v) is an edge where u w/in R and v !w/in R; this contradicts the stopping rule for the algorithm.

19
Q

Breadth-First Search pseudocode (version 2)

A

BFS(s):

Mark s as "visited"
Initialize R = {s}
Define layer L0 = {s}
While Li is not empty:
     for each node u w/in Li:
          consider each edge (u,v) incident to v
          If v is not marked "visited" then:
               mark v "visited"
               add v to the set R and to layer Li+1
          endif
      endfor
endwhile
20
Q
BFS algo (2.4)
Let T be a breadth-first search tree, let x and y be nodes in T belonging to layers Li and Lj respectively, and let (x,y) be an edge of G that is not an edge of T. Then i and j differ by at most 1.
A

Suppose by way of contradiction that i and j differed by more than 1.
Suppose i

21
Q

Depth-First Search (DFS)

A

Starting at a specified node, a neighboring node is checked to see if it is the sought-after node. If not, the algo checks an unvisited neighbor of that node. This process continues until a dead-end is reached. The algo then moves back to the most recently checked node and checks any of its unvisited neighbors.

This continues until there are no unchecked nodes or until the search query is found.

22
Q
DFS algo (2.5)
For a given recursive call DFS(u), all nodes that are marked "visited" between the invocation and end of this recursive call are descendants of u in T
A

Observed property - proof not needed.

23
Q
DFS algo (2.6)
Let T be a depth-first search tree, let x and y be nodes in T, and let (x,y) be an edge of G that is not an edge of T. Then one of x or y is an ancestor of the other.
A

Suppose that (x,y) is an edge of G that is not an edge of T, and suppose without loss of generality that x is reached first by the DFS algorithm.

When the edge (x,y) is examined during the execution, it is not added to T because y is marked “visited.” Since y was not marked “visited” when DFS was first invoked, it is a node that was discovered between the invocation and end of the recursive call DFS(x).

It follows from (2.5) (all nodes marked visited between invocation and end are descendants of u) that y is a descendant of x.

24
Q

Why is the runtime of BFS O(m+n)?

A
A node is visited (constant time) and each edge incident to the vertex (from the adjacency list), that node is added to the queue as well.
This continues for each node.
Each edge (m) is checked twice and each node (n) is checked once.

2m + n = O(m+n)

25
Q

Why is the runtime of DFS O(m+n)?

A

IDK