0
0
DSA Cprogramming~30 mins

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C - Build from Scratch

Choose your learning style9 modes available
Graph Terminology: Vertices, Edges, Directed, Undirected, Weighted
📖 Scenario: Imagine you are organizing a small network of cities connected by roads. Each city is a point (called a vertex), and each road connecting two cities is a line (called an edge). Some roads allow travel in both directions (undirected), while others only allow one-way travel (directed). Also, roads have distances (weights) showing how far apart the cities are.
🎯 Goal: You will create a simple graph representation using arrays to list cities (vertices) and roads (edges). You will mark if roads are one-way or two-way and include distances. This will help you understand basic graph terms like vertices, edges, directed, undirected, and weighted.
📋 What You'll Learn
Create an array of city names called vertices with 4 exact cities.
Create an array of edges called edges where each edge has a start city index, end city index, and distance.
Add a variable is_directed to indicate if the roads are one-way (1) or two-way (0).
Write a loop to print all edges with city names, direction, and distance.
💡 Why This Matters
🌍 Real World
Graphs are used to model networks like roads, social connections, and computer networks.
💼 Career
Understanding graph basics is important for software roles involving data structures, algorithms, and network programming.
Progress0 / 4 steps
1
Create the list of cities (vertices)
Create a char* array called vertices with these exact city names: "A", "B", "C", "D".
DSA C
Hint

Think of vertices as the list of city names.

2
Create the list of roads (edges)
Create a 2D integer array called edges with these exact rows: {0, 1, 5}, {1, 2, 3}, {2, 3, 4}, {3, 0, 2}. Each row means start city index, end city index, and distance.
DSA C
Hint

Each edge connects two cities by their indexes and has a distance.

3
Add direction information
Create an integer variable called is_directed and set it to 1 to mean roads are one-way (directed).
DSA C
Hint

Use 1 for directed roads and 0 for undirected.

4
Print all roads with details
Write a for loop using i from 0 to 3 to print each edge in this format: "A -> B : 5" if is_directed is 1, or "A -- B : 5" if is_directed is 0. Use printf and the vertices and edges arrays.
DSA C
Hint

Use printf inside the loop to show each road with arrow or dash based on direction.