(W4) Greedy Algorithms Flashcards
(42 cards)
What is a graph?
Encoding pairwise relationships among a set of objects
A graph consists of vertices and edges.
Define vertex or node in the context of a graph.
Object in the graph
Vertices are the fundamental units or points in a graph.
What is an edge in a graph?
Line that connects two vertices
Edges represent the relationships between vertices.
What is the formal notation for a graph?
graph G = (V, E)
Where V is a set of vertices and E is a set of edges.
How is an edge represented in formal notation?
edge e = (u, v)
Where u and v are two vertices.
What characterizes undirected graphs?
(u, v) = (v, u)
There is no sense of direction in undirected graphs.
What is a directed graph?
(u, v) is from u to v
The direction matters in directed graphs.
What are weighted edges?
Edges that have an associated weight w
The weight can represent cost, distance, or any other metric.
Define a simple graph.
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.
What is a connected graph?
Connected graphs have all vertices part of a single connected component.
Any node can find a path to any other node
What defines a connected component in a graph?
Subgraph in which each pair of nodes is connected via a path
Each connected component contains nodes that can reach one another.
Describe an adjacency matrix for unweighted graphs.
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.
What is the space complexity of an adjacency matrix?
O(V²)
This is regardless of the number of edges.
How do you check if an edge exists in an adjacency matrix?
O(1)
Checking for the presence of an edge is constant time.
Describe an adjacency list for unweighted graphs.
Create an array of size V, storing adjacent vertices in V[i]
This representation is efficient for sparse graphs.
What is the space complexity of an adjacency list?
O(V + E)
This accounts for both vertices and edges.
What is the time complexity for checking if a particular edge exists in an adjacency list?
O(log V)
If the list is sorted
What is the time complexity for retrieving all adjacent vertices on a given vertex in an adjacency list?
O(X)
Where X is the number of adjacent vertices.
What is the main characteristic of Breadth-First Search (BFS)?
Traverses the graph uniformly from the source vertex.
Note - it doesn’t revisit vertex
In BFS, how are vertices visited in relation to their distance from the source?
All vertices that are k edges away from the source vertex are visited before vertices k+1 edges away.
What is the BFS algorithm
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
What is the time complexity of the BFS algorithm?
O(V + E) if using adjancey list
O(V^2) if using adjancey matrix
What is the space complexity of the BFS algorithm?
O(V + E)
What is the main characteristic of Depth-First Search (DFS)?
Traverses the graph as deeply as possible, then backtracks.
Note - it doesn’t revisit vertex