Graph Terminology Vertices Edges Directed Undirected Weighted in DSA Rust - Time & Space Complexity
Graphs are made of points and connections. We want to understand how the size of a graph affects how long it takes to look at its parts.
How does the number of points and connections change the work needed to explore the graph?
Analyze the time complexity of the following code snippet.
struct Graph {
vertices: usize,
edges: Vec<(usize, usize, Option)>, // (from, to, weight)
}
impl Graph {
fn count_edges(&self) -> usize {
let mut count: usize = 0;
for _ in &self.edges {
count += 1;
}
count
}
}
This code stores a graph with points and connections, some with weights. It counts how many connections exist.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the edge list to count connections.
- How many times: The loop executes once per edge.
Explain the growth pattern intuitively.
| Input Size (number of edges) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of connections. More edges mean more work.
Time Complexity: O(E)
This means the time to check or process edges grows in a straight line with the number of edges.
[X] Wrong: "The time depends only on the number of points (vertices)."
[OK] Correct: The connections (edges) can be many even if points are few, so time depends on edges too.
Understanding how vertices and edges affect time helps you explain graph problems clearly and choose the right approach.
"What if the graph was directed instead of undirected? How would the time complexity for counting edges change?"
Practice
vertex?Solution
Step 1: Understand the definition of vertex
A vertex is a fundamental unit in a graph representing a point or node.Step 2: Differentiate vertex from edges and graph types
Edges connect vertices; graphs can be weighted or unweighted, directed or undirected, but vertices are the points themselves.Final Answer:
A point or node in the graph where edges meet -> Option AQuick Check:
Vertex = point/node [OK]
- Confusing vertices with edges
- Thinking vertex means weighted edge
- Mixing vertex with graph type
Solution
Step 1: Recall Rust tuple syntax
Tuples in Rust are declared with parentheses and comma-separated values, e.g., (1, 2).Step 2: Check other options for correctness
Square brackets [] are for arrays, curly braces {} are for blocks or sets (not in Rust), angle brackets <> are not used for tuples.Final Answer:
let edge = (1, 2); -> Option BQuick Check:
Tuple syntax uses parentheses ( ) [OK]
- Using square brackets for tuples
- Using curly braces instead of parentheses
- Confusing angle brackets with tuple syntax
struct Edge {
from: usize,
to: usize,
weight: i32,
}
fn main() {
let e = Edge { from: 3, to: 5, weight: 10 };
println!("{} -> {} with weight {}", e.from, e.to, e.weight);
}Solution
Step 1: Understand struct initialization
The struct Edge is initialized with from=3, to=5, weight=10 correctly.Step 2: Analyze the println! macro output
The print statement outputs the from, to, and weight fields in order: "3 -> 5 with weight 10".Final Answer:
3 -> 5 with weight 10 -> Option CQuick Check:
Prints from-to-weight as given [OK]
- Swapping from and to values
- Assuming default weight 0
- Thinking missing semicolon causes error here
struct Edge {
vertex1: usize,
vertex2: usize,
weight: i32,
}
fn main() {
let e = Edge { vertex1: 1, vertex2: 2 };
println!("Edge between {} and {} with weight {}", e.vertex1, e.vertex2, e.weight);
}Solution
Step 1: Check struct initialization
The Edge struct requires vertex1, vertex2, and weight fields. Weight is missing in initialization.Step 2: Understand Rust's strict initialization rules
Rust requires all fields to be initialized; missing weight causes compile-time error.Final Answer:
Missing weight field initialization causes a compile error -> Option AQuick Check:
All struct fields must be initialized [OK]
- Assuming missing fields default to zero
- Thinking vertex order matters for undirected edges
- Believing println! syntax is wrong here
Solution
Step 1: Understand adjacency list for weighted graphs
Weighted graphs need to store neighbors and weights; a Vec of Vec of tuples (neighbor, weight) fits well.Step 2: Evaluate other options
HashMap without weights loses weight info; flat Vec of tuples lacks adjacency structure; Vec of usize ignores weights.Final Answer:
Vec> where each inner Vec holds (neighbor_vertex, weight) -> Option DQuick Check:
Adjacency list with (neighbor, weight) tuples is best [OK]
- Ignoring weights in adjacency list
- Using flat edge list without adjacency
- Using HashMap without weight info
Ready to prove it?
Pass the test and this pattern moves unknown
