C Directed and Undirected Graphs

Intermediate
6 min read
Updated: Nov 2026
1 example

One-Way or Two-Way Edges

In an undirected graph an edge u-v can be followed in both directions, so the adjacency matrix is symmetric. In a directed graph (digraph) an edge u -> v goes one way only; vertices have separate in-degree and out-degree. Roads with one-way streets and web links are directed; friendships are usually undirected.

Remember
undirected: m[u][v] = m[v][u] = 1;
directed  : m[u][v] = 1;

Cheat Sheet

Keep these facts handy while you work with Directed and Undirected Graphs in C.

TopicDetail
UndirectedSymmetric matrix
DirectedOne entry per edge
Out-degreeRow sum
In-degreeColumn sum

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Weighted Graphs.

Example 1 — Directed versus undirected edges

The same edge 0 to 1 stored both ways.

C
#include <stdio.h>

int main(void) {
    int dir[3][3] = {{0}}, und[3][3] = {{0}};

    dir[0][1] = 1;
    dir[1][2] = 1;
    und[0][1] = und[1][0] = 1;

    printf("directed: 0->1=%d, 1->0=%d\n", dir[0][1], dir[1][0]);
    printf("undirected: 0-1=%d, 1-0=%d\n", und[0][1], und[1][0]);

    int out1 = 0, in1 = 0;
    for (int v = 0; v < 3; v++) { out1 += dir[1][v]; in1 += dir[v][1]; }
    printf("vertex 1: out-degree=%d in-degree=%d\n", out1, in1);
    return 0;
}

How It Works

  1. The directed edge exists only from 0 to 1.
  2. The undirected edge sets both matrix cells.
  3. Vertex 1 has one edge leaving (to 2) and one arriving (from 0).

Edge Cases & Pitfalls

Check these before relying on Directed and Undirected Graphs in real programs.

sym

Forgetting the mirror

Setting only m[u][v] for an undirected graph makes traversals miss edges.

dag

Cycles in digraphs

Directed cycles matter for ordering problems such as topological sort.

Key Takeaways

  • Undirected: symmetric.
  • Directed: one cell.
  • In-degree and out-degree.

One line: In an undirected graph an edge u-v can be followed in both directions, so the adjacency matrix is symmetric.

Frequently Asked Questions

Edges in a directed graph have a direction (u to v only); edges in an undirected graph connect two vertices both ways.
Set both m[u][v] and m[v][u] for each edge, producing a symmetric matrix.
Continue with "C Weighted Graphs" next. Store edge weights and compute path costs. The C Graphs hub lists every topic in order.

Next: Weighted Graphs

Store edge weights and compute path costs.

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