📖 Scenario: Imagine you are creating a simple map of cities connected by roads. Each city is a point (vertex), and each road is a connection (edge) between cities. Some roads have directions (one-way), and some have distances (weights).
🎯 Goal: You will build a graph using C++ that shows cities as vertices and roads as edges. You will mark if roads are one-way (directed) or two-way (undirected), and add distances (weights) to roads.
📋 What You'll Learn
Create a list of city names as vertices
Create a list of edges connecting cities with direction and weight
Use a struct to represent edges with source, destination, and weight
Print the graph showing each edge with its direction and weight
💡 Why This Matters
🌍 Real World
Graphs are used to model maps, social networks, and many connections in real life.
💼 Career
Understanding graph basics is essential for software roles involving networking, route planning, and data relationships.
Progress0 / 4 steps
1
Create the list of cities (vertices)
Create a std::vector<std::string> called cities with these exact city names: "A", "B", "C", "D"
DSA C++
Hint
Use std::vector<std::string> and initialize it with the city names inside curly braces.
2
Define the edge structure and create edges
Define a struct Edge with int source, int destination, and int weight. Then create a std::vector<Edge> called edges with these exact edges: from city 0 to 1 with weight 5, from city 1 to 2 with weight 3, from city 2 to 3 with weight 4, and from city 3 to 0 with weight 2
DSA C++
Hint
Define the struct before main(). Then create the vector of edges with the exact values inside curly braces.
3
Add a boolean to mark directed edges and update edges
Add a bool directed field to the Edge struct. Update the edges vector so that the edge from city 0 to 1 is directed (true), the edge from city 1 to 2 is undirected (false), the edge from city 2 to 3 is directed (true), and the edge from city 3 to 0 is undirected (false)
DSA C++
Hint
Add bool directed to the struct and update each edge initializer with true or false as specified.
4
Print the graph edges with direction and weight
Use a for loop with variable edge to iterate over edges. For each edge, print the source city name, an arrow "->" if directed is true, or a double arrow "<->" if directed is false, then the destination city name, and the weight in parentheses. Use std::cout to print each edge on its own line.
DSA C++
Hint
Use a range-based for loop and std::cout. Check edge.directed to decide which arrow to print.
Practice
(1/5)
1. In a graph, what do we call the points or nodes where edges meet?
easy
A. Edges
B. Vertices
C. Weights
D. Paths
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 B
Quick Check:
Points in graph = Vertices [OK]
Hint: Vertices are the dots, edges are the lines connecting them [OK]
Common Mistakes:
Confusing edges with vertices
Thinking weights are points
Mixing paths with vertices
2. Which of the following is the correct way to declare an undirected graph using adjacency list in C++?
easy
A. vector graph; graph.resize(5); // Undirected graph
B. vector> graph[5]; // Weighted graph
C. int graph[5][5]; // Undirected graph
D. vector<int> graph(5); // Undirected graph
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 A