C Graphs

Intermediate
15 min read
Updated: Nov 2026
1 example
9 guides

Definition and Usage

A graph is a set of vertices connected by edges. Graphs model maps, networks, dependencies, and social connections. In C you store one with an adjacency matrix (a 2D array) or an adjacency list (an array of linked lists) and explore it with breadth-first or depth-first search. This section covers terminology, directed, undirected, and weighted graphs, both representations, building a graph, BFS, DFS, and applications.

Always track visited.

Because graphs can contain cycles, every traversal needs a visited array or it may loop forever.

Learning Path

Work through the stages in order, or jump to a topic from the index below (9 tutorials).

C Examples

A quick taste of graphs. Compile with gcc file.c -std=c11 -Wall -o out.

Example 1 — A graph as an edge list

Count each vertex's connections.

C
#include <stdio.h>

int main(void) {
    int edges[][2] = {{0, 1}, {0, 2}, {1, 2}, {2, 3}};
    int deg[4] = {0};

    for (int i = 0; i < 4; i++) {
        deg[edges[i][0]]++;
        deg[edges[i][1]]++;
    }
    for (int v = 0; v < 4; v++) printf("degree(%d)=%d\n", v, deg[v]);
    return 0;
}

How It Works

  1. Each edge adds one to the degree of both endpoints.
  2. Vertex 2 touches three edges.
  3. The four edges give a degree sum of 8.

C Graphs Topics

Every guide in this section with a one-line summary (9 tutorials).

Graph concepts

3 tutorials

Vocabulary and graph kinds.

TopicDescriptionTutorial
Graph terminologyLearn degree, path, cycle, and connectivity with code.Try it
Directed and undirected graphsStore directed edges one way and undirected edges both ways.Try it
Weighted graphsStore edge weights and compute path costs.Try it

Representation

3 tutorials

Matrices, lists, and building graphs in C.

TopicDescriptionTutorial
Adjacency matrixStore a graph in a 2D array with O(1) edge checks.Try it
Adjacency listStore only existing edges using an array of linked lists.Try it
Creating a graphWrap a dynamic adjacency matrix in a struct with add, degree, and free.Try it

Traversal and use

3 tutorials

BFS, DFS, and applications.

TopicDescriptionTutorial
Breadth-first searchExplore a graph with a queue and compute shortest hop distances.Try it
Depth-first searchExplore a graph recursively and count connected components.Try it
Graph applicationsCheck reachability and suggest friends in a social graph.Try it

Edge Cases & Pitfalls

Common mistakes when learning graphs in C.

visit

Missing visited array

Cycles make traversals run forever.

dir

Treating directed as undirected

For a directed edge store only one direction.

mem

Matrix for sparse graphs

An adjacency matrix uses O(V^2) memory even if there are few edges.

Key Takeaways

  • Vertices and edges.
  • Matrix or list.
  • BFS and DFS explore.

One line: A graph is a set of vertices connected by edges.

Frequently Asked Questions

A data structure of vertices and edges, usually stored as an adjacency matrix or an adjacency list.
Use a matrix for dense graphs or constant-time edge checks; use lists for sparse graphs to save memory and iterate neighbors quickly.
BFS explores level by level using a queue; DFS goes as deep as possible first using recursion or a stack.

Next: Graph Terminology

Learn degree, path, cycle, and connectivity with code.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful