M5: Graphs Flashcards

(27 cards)

1
Q

What does G = (V, E) mean?

A

G means graph. V means vertices. E means edges.

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

Define directed graphs.

A

A graph where direct matters. (i.e., you can go from A to B, but B may not be directed to A.)

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

In the context of directed graphs, what do out-degrees mean?

A

Out degrees refers to the number of edges exiting that node.

(In an adjacency matrix, this would be the number of nodes listed in a ROW)

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

In the context of directed graphs, what do in-degrees mean?

A

In degrees refers to the number of edges entering that node.

(In an adjacency matrix, this would be the number of nodes listed in a COLUMNS)

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

Define a weighted graph

A

This means the edges have weights. For example, a graph where the edges have associated numbers (i.e., lengths).

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

How much space does a weighted graph’s adjacency matrix and list take?

A

Adjacency Matrix: O(V²) space
Adjacency List: O(V + E) space

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

Define BFS

A

Breadth-first search. For each node, explore all its neighbors. This is used to find the shortest path in an unweighted graph.

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

What time does a BFS graph take?

A

O(V + E)

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

Write pseudocode for BFS.

A

BFS(G, s):
for v in G:
color[v] = WHITE, dist[v] = ∞, parent[v] = NIL
color[s] = GRAY, dist[s] = 0, Q = {s}

  while Q not empty:
       u = dequeue(Q)
       for each v in Adj[u]:
           if color[v] == WHITE:
              color[v] = GRAY
              dist[v] = dist[u] + 1
              parent[v] = u
              enqueue(Q, v)
        color[u] = BLACK
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define DFS

A

Depth first search. For a node, explore all the way down before exploring the other routes. Used for edge classification, cycle detection, and topological sort.

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

Write pseudocode for DFS

A

DFS(G):
for v in G:
color[v] = WHITE, parent[v] = NIL
time = 0
for v in G:
if color[v] == WHITE:
DFS-Visit(v)
DFS-Visit(u):
color[u] = GRAY, d[u] = ++time
for v in Adj[u]:
if color[v] == WHITE:
parent[v] = u
DFS-Visit(v)
color[u] = BLACK, f[u] = ++time

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

What time does a DFS graph take?

A

O(V + E)

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

In a directed graph, define a tree edge type.

A

A tree edge indicates it was discovered by DFS for the first time.

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

In a directed graph, define a back edge type.

A

A back edge is an edge that connects back to an ancestor.

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

In a directed graph, define a forward edge type.

A

A back edge is an edge that connects to a descendent.

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

In a directed graph, define a cross edge type.

A

A cross edge type is an edge that connects two separate branches. An edge that connects two nodes that are neither ancestors nor decedents of each other.

17
Q

Define Topological Sort

A

This is when tasks (nodes) are ordered with dependencies. This is only possible for DAGs (Directed Acyclic Graphs).

Input: V = 6, edges = [[2, 3], [3, 1], [4, 0], [4, 1], [5, 0], [5, 2]]
Output: 5 4 2 3 1 0]

The first vertex in topological sorting is always a vertex with an in-degree of 0 (a vertex with no incoming edges). A topological sorting of the following graph is “5 4 2 3 1 0”. There can be more than one topological sorting for a graph. Another topological sorting of the following graph is “4 5 2 3 1 0”.

18
Q

Minimum Spanning Tree (MST)

A

The minimum spanning tree has all the properties of a spanning tree with an added constraint of having the minimum possible weights among all possible spanning trees. Like a spanning tree, there can also be many possible MSTs for a graph.

The number of vertices (V) in the graph and the spanning tree is the same.
There is a fixed number of edges in the spanning tree which is equal to one less than the total number of vertices ( E = V-1 ).
The spanning tree should not be disconnected, as in there should only be a single source of component, not more than that.
The spanning tree should be acyclic, which means there would not be any cycle in the tree.
The total cost (or weight) of the spanning tree is defined as the sum of the edge weights of all the edges of the spanning tree.
There can be many possible spanning trees for a graph.

19
Q

Define Prim’s algorithm

A

This is greedy algorithm that creates a MST from a starting vertex. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way. At every step, it considers all the edges that connect the two sets and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST.
Typically uses a min-heap on key values.

20
Q

What time does Prim’s take?

21
Q

Write pseudocode for Prim’s algorithm.

A

Prim(G, w, r):
for v in G: key[v] = ∞, parent[v] = NIL
key[r] = 0
Q = all vertices
while Q not empty:
u = ExtractMin(Q)
for v in Adj[u]:
if v in Q and w(u,v) < key[v]:
parent[v] = u
key[v] = w(u,v)

22
Q

Define Kruskal’s algorithm.

A

In this greedy algorithm, we sort all edges of the given graph in increasing order. Then it keeps on adding new edges and nodes in the MST if the newly added edge does not form a cycle. It picks the minimum weighted edge at first and the maximum weighted edge at last.

Steps:
1.) Sort all the edges in increasing order of their weight.
2.) Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If the cycle is not formed, include this edge. Else, discard it.
3.) Repeat step 2 until there are (V-1) edges in the spanning tree.

23
Q

What time does Kruskal’s algorithm take?

A

O(ElogE + ElogV)
Thus: O(E * logE) or O(E*logV)

24
Q

Write pseudocode for Kruskal’s algorithm.

A

for v in G: MakeSet(v)
sort edges by weight
for (u,v) in edges:
if FindSet(u) ≠ FindSet(v):
T = T ∪ {(u,v)}
Union(u,v)

25
Define Dijkstra' algorithm.
The goal of this algorithm is to find the shortest distance from a given source node to all other nodes in the graph. As the source node is the starting point, its distance is initialized to zero. From there, we iteratively pick the unprocessed node with the minimum distance from the source, this is where a min-heap (priority queue) or a set is typically used for efficiency. This algorithm requires non-negative weights.
26
What is the time complexity of Dijkstra's?
O(E*logV) OR O((V+E)*logV)
27
Define/Write pseudo code for the relaxation method used in Dijkstra's and Bellmon-Ford algorithms.
Relax(u, v, w): if dist[v] > dist[u] + w(u, v): dist[v] = dist[u] + w(u, v) prev[v] = u