Graphs Flashcards

1
Q

What is a graph?

A

A set of vertices and edges. Each edge connects a pair of verticies

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

What is a path in a graph

A

A sequences of vertices connected by edges with no repeated edges

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

What is a simple path in a graph?

A

A path (which is a sequence of vertices connected by edges, with no repeated edges) with no repeated vertices.

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

What is a cycle in a graph?

A

A path (which is a sequence of vertices connected by edges, where no edge is repeated) with at least one edge. The path’s first and last vertices are the same.

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

What is a simple cycle?

A

A cycle (which is a path with at least one edge whose first and last vertices are the same, and where a path is a sequence of vertices connected by edges with no repeated edges) with no repeated vertices. Note that the first and last vertices will be the same.

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

What is the length of a cycle or path in a graph?

A

It is the number of edges in a path (vertices connected by edges with no repeated vertices) or a cycle (path with at least one edge whose first and last vertices are the same).

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

What is a connected graph?

A

A graph that contains a path (vertices with no rotated edges) between all vertices.

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

Describe a tree in terms of a graph

A

An acyclic connected graph (or a graph with no cycles and where every vertex has a path to every other vertex)

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

What is a spanning tree of a connected graph?

A

A connected graph is a graph that contains a path from every vertex, to every other vertex. The spanning tree is a subgraph that contains all the vertices of the connected graph. The subgraph is a tree (acyclic and connected graph)

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

Describe how a depth first search algorithm visits the vertices of a graph

A

When visiting a vertex

  1. Mark it as visited
  2. Visit (recursively) all the vertices that are adjacent to it and that have not yet been marked
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the time complexity of DFS when marking all nodes?

A

The time complexity is proportional to the sum of all vertices degrees.

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

Write a simple DFS algorithm that searches each vertex.

A

private void dfs(int source) {

marked[source] = true;
count++;
for(int w : adj.get(source))
if (!marked[w]) dfs(w)

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

Name two problems DFS can solve when looking at graphs

A

Connectivity: are two different vertices connected? How many connected components does a graph have?

Single-source paths: find a path from source vertex to target vertex

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

Write a simple DFS algorithm that finds paths from a source to all vertices

A

Utilize an array called edgeTo[], where edgeTo[w] = v indicates that v-w edge accesses w first in the path. The array is a parent-link representation of the tree rooted at the source. Said another way v is the parent of w.

Private void dfs(int s) {
marked[v] = true;
for (int w : adj.get(s)) {
if (!marked[w]) {
edgeTo[w] = s;
dfs(w);
}
}
}

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

What is the time complexity of a depth first search algorithm providing a path from a source vertex to a target vertex?

A

The time complexity is equal to the length of the path.

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

Name one problem breadth first search solves

A

This algorithm solves the single-source shortest paths problem. This is equivalent to finding the shortest path (path with fewest edges) from source to some vertex.

17
Q

How does breadth first search visit each vertex?

A

This algorithm visits each graph vertex by:
Remove the next vertex from the queue
Put onto the queue all unmarked vertices that are adjacent to the vertex and mark them as seen.

18
Q

Write a breadth first search algorithm that visits all nodes and records a path from a source to all vertices

A

Utilize the edge to array to record each vertices parent vertex. Each vertex in the graph is visited first from their parent vertex.

private void bfs(int s) {
Queue<Integer> queue = new Queue<Integer>();
marked[s] = true;
while(!queue.isEmpty()) {
int v = queue.dequeue();
for(int w : adj.get(v)) {
if (!marked[w]) {
edgeTo[w] = v;
marked[w] = true;
queue.enqueue(w);
}
}
}</Integer></Integer>

}

19
Q

Write a breadth first search algorithm that visits all nodes and records a path from a source to all vertices

A

Utilize the edge to array to record each vertices parent vertex. Each vertex in the graph is visited first from their parent vertex.

private void bfs(int s) {
Queue<Integer> queue = new Queue<Integer>();
marked[s] = true;
while(!queue.isEmpty()) {
int v = queue.dequeue();
for(int w : adj.get(v)) {
if (!marked[w]) {
edgeTo[w] = v;
marked[w] = true;
queue.enqueue(w);
}
}
}</Integer></Integer>

}

20
Q

Which graph algorithm finds the shortest path from source vertex s to all vertices?

A

The breadth first search algorithm finds these paths within a graph

21
Q

What is the worst case time complexity of breadth first search?

A

The worst case time complexity of this graph algorithm is Edges+Vertices

22
Q

What is the worst case time complexity of breadth first search?

A

The worst case time complexity of this graph algorithm is Edges+Vertices

23
Q

How can you modify the depth first search algorithm to determine if two vertices are connected?

A

Modify the depth first search algorithm by assigning a count or id value to every source vertex passed to the DFS method. The count value is a class instance variable. The DFS method is called for every vertex in the graph that has not been marked (visited) yet. Every time a DFS is called, increase the instance count value. This will ensure that every vertex that is reached by the DFS algorithm will be assigned to the same id value. Therefore, all vertices within the same subgraph will have the same id.