Discover how simple terms like vertices and edges unlock powerful ways to solve complex connection puzzles!
Why Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C++?
Imagine you want to map out all the roads connecting cities on a map by hand. You try to list every city and every road, but it quickly becomes confusing to track which cities connect and how.
Manually keeping track of connections between many cities is slow and error-prone. You might forget a road or mix up directions, making it hard to answer questions like "Can I get from city A to city B?" or "Which roads are one-way?"
Graphs use clear terms like vertices (cities) and edges (roads) to organize connections. Directed edges show one-way roads, undirected edges show two-way roads, and weighted edges add distances or costs. This structure helps computers quickly understand and work with complex connections.
string graph[100][100]; // manually fill connections // hard to track direction and weights
struct Edge { int to; int weight; };
vector<Edge> graph[100]; // clear, easy to manage directions and weights;Graphs let us model and solve real-world connection problems like navigation, social networks, and resource flows efficiently.
GPS apps use graphs to represent cities (vertices) and roads (edges), including one-way streets (directed edges) and distances (weights), to find the fastest route.
Vertices represent points or objects.
Edges represent connections, which can be directed or undirected.
Weights add extra info like distance or cost to edges.
Practice
Solution
Step 1: Understand graph components
A graph consists of points called vertices and connections called edges.Step 2: Identify the term for points
The points or nodes where edges meet are called vertices.Final Answer:
Vertices -> Option BQuick Check:
Points in graph = Vertices [OK]
- Confusing edges with vertices
- Thinking weights are points
- Mixing paths with vertices
Solution
Step 1: Understand adjacency list declaration
An adjacency list for a graph with 5 vertices can be declared as a vector of vectors and resized to 5.Step 2: Check options for correct syntax
vector graph; graph.resize(5); // Undirected graph uses vector graph; graph.resize(5); which is correct for undirected graph adjacency list.Final Answer:
vector graph; graph.resize(5); // Undirected graph -> Option AQuick Check:
Correct adjacency list syntax = vector graph; graph.resize(5); // Undirected graph [OK]
- Using fixed size array without resize
- Confusing weighted graph syntax
- Using vector<int> graph(5) which is vector of ints, not list
vector> graph[3];
graph[1].push_back({2, 10});
graph[2].push_back({1, 5});
for(auto &edge : graph[1]) {
if(edge.first == 2) {
cout << edge.second << endl;
}
}Solution
Step 1: Understand graph construction
graph[1] has an edge to vertex 2 with weight 10; graph[2] has edge to 1 with weight 5.Step 2: Trace the loop output
The loop checks edges from vertex 1; finds edge to 2 and prints weight 10.Final Answer:
10 -> Option CQuick Check:
Weight from 1 to 2 = 10 [OK]
- Confusing edge direction
- Printing weight from wrong vertex
- Assuming undirected graph
vector> graph[3];
graph[0].push_back({1, 4});
graph[1].push_back(0, 4);Solution
Step 1: Check push_back usage
push_back expects a single argument; graph[1].push_back(0, 4) is invalid syntax.Step 2: Identify correct syntax
It should be graph[1].push_back({0, 4}); to add a pair.Final Answer:
Incorrect syntax in push_back for second edge -> Option DQuick Check:
push_back needs one argument (pair) [OK]
- Passing multiple arguments to push_back
- Not using braces for pair
- Confusing vector size with error
vector> graph[4];
graph[0].push_back({1, 3});
graph[1].push_back({0, 3});
graph[1].push_back({2, 5});
graph[2].push_back({1, 5});
graph[2].push_back({3, 7});
graph[3].push_back({2, 7});How would you add an edge between vertex 0 and vertex 3 with weight 2 correctly?
Solution
Step 1: Understand undirected edge addition
In undirected graphs, edges must be added in both directions to represent connection.Step 2: Add edge in both adjacency lists
Add {3, 2} to graph[0] and {0, 2} to graph[3] to represent the edge with weight 2.Final Answer:
graph[0].push_back({3, 2}); graph[3].push_back({0, 2}); -> Option AQuick Check:
Undirected edge = add both directions [OK]
- Adding edge only one way
- Wrong syntax for push_back
- Trying to push triple instead of pair
Ready to prove it?
Pass the test and this pattern moves unknown
