Graphs Flashcards
(38 cards)
What are the four “types” of graphs?
Undirected, Directed, unweighted, weighted
What is the difference in the direction type?
Undirected: Can move either way along an edge
Directed: Can move one way along an edge
What is the difference in the weight type?
Unweighted: There is no “cost” to travel an edge
Weighted: Some edges “cost” more than others to travel
How can graphs be represented?
An adjacency list or adjacency matrix
What a two examples of graph exploration algorithms?
Breadth First Search and Depth First Search
In an adjacency list, how many nodes are there compared to the amount of edges?
There are twice as many nodes as edges
What are the pros and cons of an adjacency list?
Easy to read, memory is O(|v| + 2|E|), but doesn’t guarantee a deterministic solution
What are the pros and cons of an adjacency matrix?
Easy to implement, guarantees deterministic solution, but memory is O(|v|^2) and it is difficult to read
What are the basic steps of the BFS algorithm?
- Chose a start vertex and enque
- While Q is not empty:
A. Dequeue first value
B. For every vertex w in v’s adjacency list, check if found
C. If not then adjust bookkeeping and enque - Repeat until Q is empty
What is the time complexity of BFS, for AL AND AM?
AL: O(|E| + |v|)
AM: O(|v|^2)
What are the basic steps of the DFS algorithm?
- Choose a start vertex and push onto stack
- While S is not empty
A. Pop top of stack
B. For the FIRST value w in v’s adjaceny list, check if found
C. If not then adjust bookkeeping and push - Repeat until S is empty
What is the time complexity of DFS, for AL AND AM?
AL: O(|E| + |v|)
AM: O(|v|^2)
For this problem, should you apply BFS or DFS?
Checking if G is a tree
Both, BFS and DFS
For this problem, should you apply BFS or DFS?
Checking if G is bipartite
Both, BFS and DFS
For this problem, should you apply BFS or DFS?
Checking if G is connected
Both, BFS and DFS
For this problem, should you apply BFS or DFS?
Checking if G is biconnected
Only DFS
For this problem, should you apply BFS or DFS?
Finding the vertex with a given property that is closest to the source
Both, BFS and DFS
For this problem, should you apply BFS or DFS?
Check if G does or doesn’t have a cycle
Both, BFS and DFS
For this problem, should you apply BFS or DFS?
Finding all leaves in G
Neither, BFS and DFS are overkill
Look at AL, and if v is connected to only one other node, then v is a leaf
What is a DAG?
An unweighted directed graph that does not have a cycle
What is topological sorting in a DAG?
A linear ordering of a directed graph so that for every edge v -> w, v comes before w
What is the indegree of a vertex?
The amount of edges going in to that vertex
What is a basic topological sorting algorithm?
- Compute the indegree of every vertex
- Place all 0 degree vertices in Q
- Dequeue vertex and check AL for any adjacencies of v
- If adjacency found, reduce w’s indegree by one
- Repeat steps 2 - 4 until all indegrees are 0
What are the time and space complexities of a basic topological sorting algorithm?
TIME:
Computing indegree: O(|v| + |E|)
Enqueues: O(|v|)
Dequeues: O(|v| + |E|)
SPACE:
Queue: O(|v|)
TOTAL: O(|v| + |E|)