Graphs Flashcards

(38 cards)

1
Q

What are the four “types” of graphs?

A

Undirected, Directed, unweighted, weighted

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

What is the difference in the direction type?

A

Undirected: Can move either way along an edge
Directed: Can move one way along an edge

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

What is the difference in the weight type?

A

Unweighted: There is no “cost” to travel an edge
Weighted: Some edges “cost” more than others to travel

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

How can graphs be represented?

A

An adjacency list or adjacency matrix

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

What a two examples of graph exploration algorithms?

A

Breadth First Search and Depth First Search

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

In an adjacency list, how many nodes are there compared to the amount of edges?

A

There are twice as many nodes as edges

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

What are the pros and cons of an adjacency list?

A

Easy to read, memory is O(|v| + 2|E|), but doesn’t guarantee a deterministic solution

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

What are the pros and cons of an adjacency matrix?

A

Easy to implement, guarantees deterministic solution, but memory is O(|v|^2) and it is difficult to read

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

What are the basic steps of the BFS algorithm?

A
  1. Chose a start vertex and enque
  2. 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
  3. Repeat until Q is empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the time complexity of BFS, for AL AND AM?

A

AL: O(|E| + |v|)
AM: O(|v|^2)

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

What are the basic steps of the DFS algorithm?

A
  1. Choose a start vertex and push onto stack
  2. 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
  3. Repeat until S is empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the time complexity of DFS, for AL AND AM?

A

AL: O(|E| + |v|)
AM: O(|v|^2)

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

For this problem, should you apply BFS or DFS?

Checking if G is a tree

A

Both, BFS and DFS

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

For this problem, should you apply BFS or DFS?

Checking if G is bipartite

A

Both, BFS and DFS

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

For this problem, should you apply BFS or DFS?

Checking if G is connected

A

Both, BFS and DFS

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

For this problem, should you apply BFS or DFS?

Checking if G is biconnected

17
Q

For this problem, should you apply BFS or DFS?

Finding the vertex with a given property that is closest to the source

A

Both, BFS and DFS

18
Q

For this problem, should you apply BFS or DFS?

Check if G does or doesn’t have a cycle

A

Both, BFS and DFS

19
Q

For this problem, should you apply BFS or DFS?

Finding all leaves in G

A

Neither, BFS and DFS are overkill
Look at AL, and if v is connected to only one other node, then v is a leaf

20
Q

What is a DAG?

A

An unweighted directed graph that does not have a cycle

21
Q

What is topological sorting in a DAG?

A

A linear ordering of a directed graph so that for every edge v -> w, v comes before w

22
Q

What is the indegree of a vertex?

A

The amount of edges going in to that vertex

23
Q

What is a basic topological sorting algorithm?

A
  1. Compute the indegree of every vertex
  2. Place all 0 degree vertices in Q
  3. Dequeue vertex and check AL for any adjacencies of v
  4. If adjacency found, reduce w’s indegree by one
  5. Repeat steps 2 - 4 until all indegrees are 0
24
Q

What are the time and space complexities of a basic topological sorting algorithm?

A

TIME:
Computing indegree: O(|v| + |E|)
Enqueues: O(|v|)
Dequeues: O(|v| + |E|)

SPACE:
Queue: O(|v|)

TOTAL: O(|v| + |E|)

25
What is Single Source Shortest Path?
Given a source vertex, find the shortest path to all reachable vertices
26
What kind of graph can Single Source Shortest Path be used on?
Weighted Directed Graph AND Weighted Undirected Graph
27
What is the colloquial name for the most common Single Source Shortest Path algorithm?
Dijkstra's Algorithm
28
What bookkeeping is necessary for Dijkstra's algorithm?
The parent of each vertex, the shortest distance of from each vertex to the source, and if a vertex has been visited
29
How is the adjacency list of a WDG formatted?
vertex | (w, weight), (w2, weight), ...
30
What is the starting set up for Dijkstra's algorithm?
Source vertex: distance = 0, parent = null, add to MinHeap Other vertices: distance = infinity, parent = null
31
What is the basic loop of Dijkstra's algorithm?
1. Remove the vertex with the smallest distance from the MinHeap 2. Set as visited, add unvisitd neighbors to MinHeap 3. If neighbor's current distance is greater than the distance to the current vertex, then update distance and parent 4. Repeat until all vertices are visited
32
How much memory is used to store a WDG's adjacency list?
O(3|E| + |v|)
33
When using a MinHeap, what is the time complexity of Dijkstra's algorithm?
Step1: 3|v| = O(|v|) Step2: O(|v|log(|v|)) Step3: |v|deleteMin + |E|reduce =|v|log(|v|) + |E|log|v|) =(|v| + |E|) log(|v|) Dominant term is Step 3: O((|v| + |E|) log(|v|)
34
What is a Minimum Spanning Tree?
The set of edges in G that span each vertex once while also minimizing the sum of the weights
35
What are the two approaches for finding an MST?
Kruskal's and Prim's
36
What is the BEST approach for finding an MST?
Prim's, it is easier to implement and it has a smaller time complexity than Kruskal's
37
Explain Kruskal's Algorithm
1. Start with each vertex as its own tree 2. Consider all edges in increasing order 3. Incorporate an edge if it unites vertices in different trees 4. Repeat until all edges are in the MST
38
Explain Prim's Algorithm
1. Start with every vertex having a parent of null and a distance of infinity 2. Choose a random starting vertex and set its distance to 0 3. Add the edge with the minimum weight that connects an unvisited vertex to a visited one 4. Repeat until all edges are in the MST