(W4) Greedy Algorithms Flashcards

(42 cards)

1
Q

What is a graph?

A

Encoding pairwise relationships among a set of objects

A graph consists of vertices and edges.

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

Define vertex or node in the context of a graph.

A

Object in the graph

Vertices are the fundamental units or points in a graph.

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

What is an edge in a graph?

A

Line that connects two vertices

Edges represent the relationships between vertices.

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

What is the formal notation for a graph?

A

graph G = (V, E)

Where V is a set of vertices and E is a set of edges.

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

How is an edge represented in formal notation?

A

edge e = (u, v)

Where u and v are two vertices.

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

What characterizes undirected graphs?

A

(u, v) = (v, u)

There is no sense of direction in undirected graphs.

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

What is a directed graph?

A

(u, v) is from u to v

The direction matters in directed graphs.

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

What are weighted edges?

A

Edges that have an associated weight w

The weight can represent cost, distance, or any other metric.

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

Define a simple graph.

A

Does not have loops and does not contain multiple edges between the same pair of nodes

Simple graphs have unique edges between pairs of vertices.

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

What is a connected graph?

A

Connected graphs have all vertices part of a single connected component.

Any node can find a path to any other node

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

What defines a connected component in a graph?

A

Subgraph in which each pair of nodes is connected via a path

Each connected component contains nodes that can reach one another.

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

Describe an adjacency matrix for unweighted graphs.

A

Create a V x V matrix M and store the weight at M[i][j] if an edge exists

It represents the presence of edges using a matrix.

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

What is the space complexity of an adjacency matrix?

A

O(V²)

This is regardless of the number of edges.

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

How do you check if an edge exists in an adjacency matrix?

A

O(1)
Checking for the presence of an edge is constant time.

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

Describe an adjacency list for unweighted graphs.

A

Create an array of size V, storing adjacent vertices in V[i]

This representation is efficient for sparse graphs.

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

What is the space complexity of an adjacency list?

A

O(V + E)

This accounts for both vertices and edges.

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

What is the time complexity for checking if a particular edge exists in an adjacency list?

A

O(log V)
If the list is sorted

18
Q

What is the time complexity for retrieving all adjacent vertices on a given vertex in an adjacency list?

A

O(X)

Where X is the number of adjacent vertices.

19
Q

What is the main characteristic of Breadth-First Search (BFS)?

A

Traverses the graph uniformly from the source vertex.

Note - it doesn’t revisit vertex

20
Q

In BFS, how are vertices visited in relation to their distance from the source?

A

All vertices that are k edges away from the source vertex are visited before vertices k+1 edges away.

21
Q

What is the BFS algorithm

A

Create a queue - insert the starting node
Create a visited array - mark the start as visisted

While the queue is not empty:
1. get the first vertex from the queue
2. relax the edges - so for each edge
- mark the end as visited, add the end vertex to the queue

22
Q

What is the time complexity of the BFS algorithm?

A

O(V + E) if using adjancey list
O(V^2) if using adjancey matrix

23
Q

What is the space complexity of the BFS algorithm?

24
Q

What is the main characteristic of Depth-First Search (DFS)?

A

Traverses the graph as deeply as possible, then backtracks.

Note - it doesn’t revisit vertex

25
What is the DFS algorithm?
Initialize a 'visited' data structure add the initial vertex loop thru each neighbour in the graph: 1. if the neighbour has not been visited -> recursive call from that neighbour
26
What is the time complexity of the DFS algorithm?
O(V + E)
27
What is the space complexity of the DFS algorithm?
O(V + E) for adjacency list O(V^2) for adjacency matirx (V iterations, indexing over V possible edges)
28
What does DAG stand for?
Directed Acyclic Graph
29
What are some practical examples of a Directed Acyclic Graph (DAG)?
* Subtasks of a project which must finish before other projects * Relationship between subjects for degrees (i.e. pre-reqs) * People genealogy - x is an ancestor of * Power sets
30
In a DAG, how is the order of vertices defined?
A < B if A -> B
31
What does it mean if some vertices in a DAG are incomparable?
Cannot know the order of B and C.
32
What is a topological order in a DAG?
Permutation of the vertices such that for every directed edge u -> v, u appears before v.
33
What is the time complexity for DFS when used for topological sort?
O(V + E)
34
What is the space complexity for DFS when used for topological sort?
Probably O(V + E), but requires storage for the vertex.
35
What can 2-coloring also be used to represent
Bipartite Graphs Note - the number of 2-coloring sets is 2 ^ connected components
36
How to find connected components in an unweighted undirected graph
Observe the fact that a DFS call from a random vertex will reach every vertex in the component so keep calling DFS on non-visited nodes, and each DFS call, you will get a different component
37
Finding cycles in undirected graphs (+ why doesn't this approach work in directed graphs)
Keep track of nodes visited. 1. perform a DFS on unvisited nodes 2. if you find a path to a node that has been visited, then you know there is a cycle (note - you'll need to keep a reference to the previous node so that you don't travel from a -> b, and then go back to a and consider it a cycle) Issue with directed graphs - when there are two paths to the same node, it will detect it as a cycle
38
Finding cycles in directed graphs
Requires a modification to the previous example Key Idea: only mark a loop when you manage to reach the starting node (for that DFS) Keep a status array -> 3x values, unvisited, active, inactive Then for each unvisited node: mark it as an active node, do dfs, and for each node, check if it's 'active'. when you finish DFS mark the node as inactive BFS is more clunky - not as easy to keep track of previous nodes
39
Finding the shortest cycle in a directed graph
Trick - use BFS (why? because you want to find the shortest distance back to the source node by edges, so BFS will spread out and find that, where DFS may not) Otherwise - you also want to make sure you start from every possible source node - to ensure you find the shortest path O(V(V+E))
40
BFS with multiple sources?
Set all sources to equal 0. BFS will then just find the shortest path to each node, and will ignore longer paths from other sources
41
Find connected components in an undirected graph that are cycles
Important trick - the degree at every node should be 2 (as otherwise it's not a full cycle) otherwise - check all non visited nodes, perform DFS, and for each confirmed cycle - increase a counter
42
how to calculate height of graph
the number of edges between start and end node