C Graph Applications

Intermediate
8 min read
Updated: Nov 2026
2 examples

What Graphs Model

Graphs model road maps (routing), networks (connectivity), social networks (friend suggestions), dependencies (build order), and state spaces (puzzles, games). Many applications reduce to traversals: reachability is a DFS or BFS from the source; friend suggestions look at neighbors of neighbors.

Remember
/* reachable(a, b): DFS from a, then check visited[b]
   suggestions: friends of friends who are not friends yet */

Cheat Sheet

Keep these facts handy while you work with Graph Applications in C.

TopicDetail
RoutingShortest path
SocialNeighbors of neighbors
Build orderTopological sort
ConnectivityDFS / BFS

C Examples

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

Example 1 — Reachability

Is there a route from 0 to another vertex?

C
#include <stdio.h>
#define V 6

static int adj[V][V], visited[V];

static void dfs(int u) {
    visited[u] = 1;
    for (int v = 0; v < V; v++)
        if (adj[u][v] && !visited[v]) dfs(v);
}

int main(void) {
    int edges[][2] = {{0, 1}, {1, 2}, {3, 4}};
    for (int i = 0; i < 3; i++) {
        adj[edges[i][0]][edges[i][1]] = 1;
        adj[edges[i][1]][edges[i][0]] = 1;
    }

    dfs(0);
    printf("0 can reach 2: %s\n", visited[2] ? "yes" : "no");
    printf("0 can reach 4: %s\n", visited[4] ? "yes" : "no");
    return 0;
}

How It Works

  1. DFS from 0 visits 0, 1, and 2.
  2. Vertices 3 and 4 form a separate island.
  3. Visited flags answer reachability.

Example 2 — Friend suggestions

Suggest friends of friends who are not already friends.

C
#include <stdio.h>
#define V 5

int main(void) {
    int adj[V][V] = {{0}};
    int edges[][2] = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {2, 4}};
    for (int i = 0; i < 5; i++) {
        adj[edges[i][0]][edges[i][1]] = 1;
        adj[edges[i][1]][edges[i][0]] = 1;
    }

    int suggest[V] = {0};
    for (int f = 0; f < V; f++) {
        if (!adj[0][f]) continue;
        for (int g = 0; g < V; g++)
            if (adj[f][g] && g != 0 && !adj[0][g]) suggest[g] = 1;
    }

    printf("suggestions for 0:");
    for (int v = 0; v < V; v++) if (suggest[v]) printf(" %d", v);
    printf("\n");
    return 0;
}

How It Works

  1. Vertex 0's friends are 1 and 2.
  2. Their friends are 3 (via both) and 4 (via 2), plus 0 itself which is skipped.
  3. The suggestion flags prevent duplicates.

Edge Cases & Pitfalls

Check these before relying on Graph Applications in real programs.

model

Wrong model

Pick vertices and edges that match the question before choosing an algorithm.

scale

Scaling

Matrices do not scale to huge sparse graphs such as social networks. Use lists.

Key Takeaways

  • Model first, then choose an algorithm.
  • Reachability = traversal.
  • Neighbors of neighbors for recommendations.

One line: Graphs model road maps (routing), networks (connectivity), social networks (friend suggestions), dependencies (build order), and state spaces (puzzles, games).

Frequently Asked Questions

Maps and routing, network design, social networks, dependency resolution, scheduling, and game or puzzle state search.
Run DFS or BFS from one and see whether the other is visited.
Continue with "C Algorithm Basics" next. Learn what algorithms are, how to describe them, and how to measure time and space with Big O in C. The C Graphs hub lists every topic in order.

Next: Algorithm Basics

Learn what algorithms are, how to describe them, and how to measure time and space with Big O in C.

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.

11 people found this page helpful