Course contentsShow
← C
Lesson 1039 of 1,21914. Data Structures in CFree lesson

Vertices and Edges definition

Vertices and Edges definition

What you'll learn: How to identify the two fundamental components that form the structure of a graph.

The Map Analogy

Imagine you are looking at a map of a small delivery route. There are specific locations—like a warehouse, a coffee shop, and a residential house—and there are the streets that connect them.

In the world of computer science, we call this map a Graph. A graph is simply a collection of points and the lines between them. Instead of "locations" and "streets," we use two specific technical terms: Vertices and Edges.

1. Vertices (The Points)

A Vertex (plural: Vertices) is a single data point in your graph. If you were building a social media network, each vertex would be a unique user profile. If you were building a GPS app, each vertex would be a specific city or intersection.

In C, we usually represent a vertex using a struct to hold its data (like a name or ID number) and then group all vertices together in an array or a linked list.

2. Edges (The Connections)

An Edge is the link between two vertices. It represents a relationship. If User A is "friends" with User B, an edge exists between them. If there is a road between City A and City B, an edge exists.

Edges are what turn a random pile of data points into a connected structure. Without edges, you just have isolated dots; with edges, you have a web of information you can navigate.

Seeing it in C

While there are many ways to build a graph, here is how we conceptually define these building blocks using structures:

#include <stdio.h>

// The Vertex: A single point in the graph
struct Vertex {
    int id;
    char label[50];
};

// The Edge: A connection between two specific vertices
struct Edge {
    int start_vertex_id;
    int end_vertex_id;
    int weight; // Optional: e.g., the distance of the road
};

int main() {
    // Example: A simple graph with 2 vertices and 1 edge
    struct Vertex v1 = {1, "Warehouse"};
    struct Vertex v2 = {2, "Coffee Shop"};
    
    // This edge connects Vertex 1 to Vertex 2
    struct Edge road = {1, 2, 5}; // 5 miles long

    printf("Connection from %s to %s covers %d miles.\n", 
            v1.label, v2.label, road.weight);

    return 0;
}

Direction and Meaning

Edges can be undirected (like a two-way street where both people are friends) or directed (like a one-way street or a "follower" relationship on Twitter). Regardless of the type, every graph you encounter from here on out will be built entirely from these two simple ingredients.

Key takeaway

A Graph is a collection of Vertices (the data points) connected by Edges (the relationships between them).