Bird
Raised Fist0
DSA Cprogramming

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C

Choose your learning style10 modes available
Mental Model
A graph is a way to connect points (vertices) with lines (edges) that can have directions and weights.
Analogy: Imagine a city map where intersections are points (vertices) and roads are lines (edges). Some roads go one way (directed), some both ways (undirected), and some roads are longer or shorter (weighted).
Vertices: A, B, C, D
Edges:
A -> B (directed)
B ↔ C (undirected)
C -> D (directed, weight 5)

  A -> B
      ↘
       C ↔ D

Weights shown on edges where applicable.
Dry Run Walkthrough
Input: Graph with vertices A, B, C, D; edges: A->B (directed), B↔C (undirected), C->D (directed, weight 5)
Goal: Understand how vertices and edges connect, and how direction and weight affect the graph
Step 1: Identify vertices as points A, B, C, D
Vertices: [A] [B] [C] [D]
Why: Vertices are the main points or nodes in the graph
Step 2: Add directed edge from A to B
A -> B
Vertices: [A] [B] [C] [D]
Why: Directed edge means connection goes only from A to B
Step 3: Add undirected edge between B and C
A -> B ↔ C
Vertices: [A] [B] [C] [D]
Why: Undirected edge means connection goes both ways between B and C
Step 4: Add directed weighted edge from C to D with weight 5
A -> B ↔ C -> D (weight 5)
Vertices: [A] [B] [C] [D]
Why: Weighted edge shows cost or distance; direction shows flow from C to D
Result:
A -> B ↔ C -> D (weight 5)
Vertices: [A] [B] [C] [D]
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Define a structure for an edge
typedef struct Edge {
    char from;
    char to;
    int directed; // 1 if directed, 0 if undirected
    int weight;   // 0 if no weight
    struct Edge* next;
} Edge;

// Define a structure for a graph
typedef struct Graph {
    char vertices[10];
    int vertex_count;
    Edge* edges;
} Graph;

// Function to add an edge to the graph
void add_edge(Graph* g, char from, char to, int directed, int weight) {
    Edge* new_edge = (Edge*)malloc(sizeof(Edge));
    new_edge->from = from;
    new_edge->to = to;
    new_edge->directed = directed;
    new_edge->weight = weight;
    new_edge->next = g->edges;
    g->edges = new_edge;
}

// Function to print the graph
void print_graph(Graph* g) {
    printf("Vertices: ");
    for (int i = 0; i < g->vertex_count; i++) {
        printf("[%c] ", g->vertices[i]);
    }
    printf("\nEdges:\n");
    Edge* current = g->edges;
    while (current != NULL) {
        if (current->directed) {
            if (current->weight > 0) {
                printf("%c -> %c (weight %d)\n", current->from, current->to, current->weight);
            } else {
                printf("%c -> %c\n", current->from, current->to);
            }
        } else {
            if (current->weight > 0) {
                printf("%c <-> %c (weight %d)\n", current->from, current->to, current->weight);
            } else {
                printf("%c <-> %c\n", current->from, current->to);
            }
        }
        current = current->next;
    }
}

int main() {
    Graph g;
    g.vertex_count = 4;
    g.vertices[0] = 'A';
    g.vertices[1] = 'B';
    g.vertices[2] = 'C';
    g.vertices[3] = 'D';
    g.edges = NULL;

    add_edge(&g, 'A', 'B', 1, 0); // directed edge A->B
    add_edge(&g, 'B', 'C', 0, 0); // undirected edge B<->C
    add_edge(&g, 'C', 'D', 1, 5); // directed weighted edge C->D weight 5

    print_graph(&g);
    return 0;
}
Edge* new_edge = (Edge*)malloc(sizeof(Edge));
allocate memory for new edge
new_edge->from = from; new_edge->to = to; new_edge->directed = directed; new_edge->weight = weight;
set edge properties: start, end, direction, weight
new_edge->next = g->edges; g->edges = new_edge;
insert new edge at the front of edge list
while (current != NULL) { ... current = current->next; }
traverse all edges to print them
OutputSuccess
Vertices: [A] [B] [C] [D] Edges: C -> D (weight 5) B <-> C A -> B
Complexity Analysis
Time: O(E) because printing edges requires visiting each edge once
Space: O(V + E) because we store all vertices and edges separately
vs Alternative: Compared to adjacency matrix (O(V^2) space), adjacency list with edges is more space efficient for sparse graphs
Edge Cases
Empty graph with no vertices or edges
Prints no vertices and no edges without error
DSA C
g.vertex_count = 0;
Graph with only one vertex and no edges
Prints single vertex and no edges
DSA C
g.vertex_count = 1;
Edges with zero weight treated as unweighted
Edges print without weight label
DSA C
if (current->weight > 0)
When to Use This Pattern
When a problem involves connecting points with lines that may have directions or costs, recognize it as a graph problem and use graph terminology to describe vertices and edges.
Common Mistakes
Mistake: Confusing directed edges with undirected edges and printing arrows incorrectly
Fix: Check the directed flag before printing arrow direction
Mistake: Ignoring weights and printing all edges the same
Fix: Include weight check and print weight only if greater than zero
Summary
Graphs connect points called vertices with lines called edges that can be directed or undirected and may have weights.
Use graph terminology to describe and solve problems involving networks, maps, or relationships.
The key insight is that edges can have direction and cost, changing how connections work.

Practice

(1/5)
1. Which of the following best describes a vertex in a graph?
easy
A. A point or node representing an entity in the graph
B. A connection between two nodes
C. A path that starts and ends at the same node
D. A graph with no edges

Solution

  1. Step 1: Understand the definition of vertex

    A vertex is a fundamental unit in a graph representing an entity or point.
  2. Step 2: Differentiate vertex from edges and other terms

    Edges connect vertices; paths and graph types are different concepts.
  3. Final Answer:

    A point or node representing an entity in the graph -> Option A
  4. Quick Check:

    Vertex = node/entity [OK]
Hint: Vertices are the points or nodes in a graph [OK]
Common Mistakes:
  • Confusing vertices with edges
  • Thinking vertices are paths
  • Mixing vertex with graph types
2. Which of the following is the correct way to represent a weighted edge from vertex u to vertex v with weight 5 in C struct?
easy
A. struct Edge { int u, v; }; struct Weight { int w; };
B. struct Edge { int u; int v; }; int weight = 5;
C. struct Edge { int u; int v; int weight; };
D. struct Edge { int u; int v; };

Solution

  1. Step 1: Identify the correct struct fields for weighted edge

    A weighted edge needs source vertex, destination vertex, and weight as fields.
  2. Step 2: Check struct syntax correctness

    struct Edge { int u; int v; int weight; }; correctly declares all three fields in one struct.
  3. Final Answer:

    struct Edge { int u; int v; int weight; }; -> Option C
  4. Quick Check:

    Weighted edge struct = u, v, weight [OK]
Hint: Weighted edges need u, v, and weight fields in one struct [OK]
Common Mistakes:
  • Separating weight outside the edge struct
  • Incorrect field order causing confusion
  • Missing weight field
3. What is the output of the following code snippet that prints edges of a directed weighted graph?
typedef struct { int u; int v; int weight; } Edge;
Edge edges[] = { {0, 1, 10}, {1, 2, 20}, {2, 0, 30} };
int n = 3;
for (int i = 0; i < n; i++) {
    printf("%d -> %d (weight %d)\n", edges[i].u, edges[i].v, edges[i].weight);
}
medium
A. 0 -> 1 (weight 10) 1 -> 2 (weight 30) 2 -> 0 (weight 20)
B. 0 -> 1 (weight 10) 1 -> 2 (weight 20) 2 -> 0 (weight 30)
C. 0 -> 1 (weight 30) 1 -> 2 (weight 20) 2 -> 0 (weight 10)
D. 1 -> 0 (weight 10) 2 -> 1 (weight 20) 0 -> 2 (weight 30)

Solution

  1. Step 1: Read the edges array values

    Edges are: (0,1,10), (1,2,20), (2,0,30).
  2. Step 2: Trace the loop printing each edge

    Loop prints each edge in order with format "u -> v (weight w)".
  3. Final Answer:

    0 -> 1 (weight 10) 1 -> 2 (weight 20) 2 -> 0 (weight 30) -> Option B
  4. Quick Check:

    Print edges as stored = 0 -> 1 (weight 10) 1 -> 2 (weight 20) 2 -> 0 (weight 30) [OK]
Hint: Print edges in array order with their weights [OK]
Common Mistakes:
  • Reversing u and v in print
  • Mixing up weights between edges
  • Printing edges in wrong order
4. Identify the error in this code snippet that tries to create an undirected graph edge list:
typedef struct { int u; int v; } Edge;
Edge edges[2];
edges[0].u = 0; edges[0].v = 1;
edges[1].u = 1; edges[1].v = 0;
// Print edges
for (int i = 0; i <= 2; i++) {
    printf("Edge from %d to %d\n", edges[i].u, edges[i].v);
}
medium
A. Edge direction is incorrect for undirected graph
B. Edges should have weight field
C. Edges array size should be 1, not 2
D. Loop condition should be i < 2, not i <= 2

Solution

  1. Step 1: Check array size and loop bounds

    Edges array has size 2, valid indices are 0 and 1.
  2. Step 2: Identify loop error

    Loop runs from i=0 to i=2 inclusive, which accesses edges[2] (out of bounds).
  3. Final Answer:

    Loop condition should be i < 2, not i <= 2 -> Option D
  4. Quick Check:

    Array size 2 means max index 1 [OK]
Hint: Loop index must be less than array size [OK]
Common Mistakes:
  • Using <= instead of < in loop
  • Assuming edges need weight for undirected
  • Confusing edge direction in undirected graphs
5. You have a weighted undirected graph with vertices 0, 1, 2 and edges:
(0,1,4), (1,2,6), (0,2,5)
How many edges will be stored if you represent this graph using an edge list storing each edge only once?
medium
A. 3
B. 6
C. 2
D. 1

Solution

  1. Step 1: Understand undirected edge storage

    In undirected graphs, each edge connects two vertices without direction, so store only one entry per edge.
  2. Step 2: Count unique edges given

    Edges are (0,1), (1,2), (0,2) -- total 3 unique edges.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Undirected edges stored once = 3 [OK]
Hint: Undirected edges stored once, count unique pairs [OK]
Common Mistakes:
  • Counting each edge twice
  • Confusing directed with undirected storage
  • Ignoring weights when counting edges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard