Graphs Flashcards

1
Q

Give an overview of how DFS is done for graphs

A

You pass DFS a start vertex and possibly an end vertex.
The start vertex is where the search will start and the end vertex if provided is where the search will stop

You have two running data structures: a list of visited vertices and a stack. The stack keeps track of

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

When does it make more sense to use an adjacency matrix?

A

Because space complexity of adjacency matrix is O(|v|^2), it makes more sense to use an adjacency matrix when we have a lot of edges between vertices to keep track of. Adjacency matrix is good when the graph is dense i.e. when the number of edges is close to the number of vertices^2. Or when v^2 is so small that conserving space wouldn’t matter.

A good way to think about this is…does the adjacency matrix have a lot of 0’s in it? This means that the graph is sparse (few edges) between vertices. A graph becomes dense when every vertex is connected to every other vertex once. Think about facebook…adjacency matrix wouldn’t make sense because not every person is going to be friends with the billion other people in the planet. So there’s going to be a lot of empty edges.

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

What is the space complexity of an adjacency list

A

O(|V| + |E|) (cardinality of vertices + cardinality of edges)

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

Graphs are made up of _____ and _____

A

vertices and edges

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

Whats the approach for determining if a directed graph has a cycle?

A

You have three sets, white, gray, and black.

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

What is more space efficient? Adjacency list or adjacency matrix. In what situation is it more space efficient?

A

Adjacency list when the graph is sparse i.e. it has relatively few edges

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

What is a strongly connected directed graph?

A

all vertices are reachable from all other vertices.

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

What is the drawback of using an adjacency matrix? What’s its advantage?

A

You use a lot more space to record edges. The benefit of using this though is that it improves time complexities for common operations like search, insert, and remove.

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

What does it mean for an undirected graph to be connected?

A

Connected undirected graph means that all vertices can be accessed from all other vertices

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

What does it mean for an edge to be weighted?

A

Edges can be weighted or unweighted. Weighted edges represent something about that edge. For example, it might represent the distance between vertices.

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

What is the time complexity of finding adjacent nodes in an adjacency matrix

A

If nodes have keys 1:1 with the indices in the adjacency matrix, if we wanted to find adjacent nodes of node 0, then searching for adjacent nodes would be O(v) because we have to search the second embedded list entirely

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

Whats the data structure used for BFS? (hint: this is the same as BFS in a binary tree)

A

queue

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

Time complexity for adding an edge in adjacency matrix vs. adjacency list?

A

matrix: O(1) because we can directly access the row and column in the list of lists without having to increase the size of the list
adjacency list: Could be amortized O(1) if its not required that the list is sorted. If it has to be sorted, then worst case of adding an edge is O(n)

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

What does it mean for a graph to be sparse and dense?

A
sparse = few edges
dense = many edges
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the space complexity of an adjacency matrix?

A

O(|V|^2) (cardinality of vertices squared)

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

Time complexity of searching if two nodes are connected in an adjacency matrix

A

O(1) because we just need to access self.ajd_matrix[row][column]

17
Q

What are directed edges?

A

Directed edges represent a one or two way connection from one vertex to the other

18
Q

When are vertices adjacent?

A

Vertices are adjacent when theres an edge connecting them

19
Q

What does it mean for a matrix to be symmetric?

A

This often occurs in undirected graphs. An edge between two vertices is reflected in the matrix in two positions for each vertex.

20
Q

What’s the data structure used for DFS? Why is a stack used?

A

Stack. A stack is used because in DFS we want to visit

21
Q

What is the time complexity of searching if node A and B are connected in an adjacency list?

A

O(v) if the list is unsorted. This is because in worst case, a given vertex can be connected to all other vertices.
It would be O(log v) if the adjacency list was sorted bc then we can do binary search

22
Q

The dijkstra algorithm from the quiz uses what data structure?

A

A priority queue. Start with the source vertex in the priority queue as a(0). Add the popped value to the reached column with its weight.

23
Q

How are edges represented in set form?

A

An edge is represented by vertex pairs e.g. E = {{v_1, v_2}, ….}

24
Q

Time complexity for finding all nodes connected to some vertex A?

A

O(v) because at worst case the node is connected to all other edges including itself

25
Q

Describe the flow of dijkstra’s algorithm

A

Start at source vertex and label it 0. Label the vertices connected to source by their distance.
Pick the vertex with the least distance. Update its connected vertices with distances.
Always picking the vertex with the least distance

26
Q

What are undirected edges?

A

Edges with no direction. It just represents a connection