Graphs Flashcards
Give an overview of how DFS is done for graphs
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
When does it make more sense to use an adjacency matrix?
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.
What is the space complexity of an adjacency list
O(|V| + |E|) (cardinality of vertices + cardinality of edges)
Graphs are made up of _____ and _____
vertices and edges
Whats the approach for determining if a directed graph has a cycle?
You have three sets, white, gray, and black.
What is more space efficient? Adjacency list or adjacency matrix. In what situation is it more space efficient?
Adjacency list when the graph is sparse i.e. it has relatively few edges
What is a strongly connected directed graph?
all vertices are reachable from all other vertices.
What is the drawback of using an adjacency matrix? What’s its advantage?
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.
What does it mean for an undirected graph to be connected?
Connected undirected graph means that all vertices can be accessed from all other vertices
What does it mean for an edge to be weighted?
Edges can be weighted or unweighted. Weighted edges represent something about that edge. For example, it might represent the distance between vertices.
What is the time complexity of finding adjacent nodes in an adjacency matrix
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
Whats the data structure used for BFS? (hint: this is the same as BFS in a binary tree)
queue
Time complexity for adding an edge in adjacency matrix vs. adjacency list?
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)
What does it mean for a graph to be sparse and dense?
sparse = few edges dense = many edges
What is the space complexity of an adjacency matrix?
O(|V|^2) (cardinality of vertices squared)