VOOZH about

URL: https://www.geeksforgeeks.org/c/implementation-of-graph-in-c/

⇱ Implementation of Graph in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementation of Graph in C

Last Updated : 12 Dec, 2025

Graphs are versatile data structures used to model real-world problems like networks and maps, and in C, they are commonly implemented using adjacency matrices or lists with pointers and structures.

  • This allows efficient traversal, insertion, and deletion using standard graph algorithms.
  • A graph is defined as a set of nodes that connects two or more nodes with edges often referred to as arcs.
  • The use of graphs is common in modelling different systems in the world, like the social structures, transport structures as well the computer structures.

Components of Graph

  • Vertices (Nodes): These are the entities in the graph to check the facts which are shown in the social medial.
  • Edges (Links): These are the edges which are a way of joining the vertices.

Types of Graphs

  • Directed Graph: Edges have direction, which means they will refer towards only one of the two connected nodes.
  • Undirected Graph: In this graph, the edges have no direction.
  • Weighted Graph: The edges have weights (or costs) which is needed to move on that edge.
  • Unweighted Graph: Edges do not have weights or all edges share the same weight.
👁 carim

Representation of Graphs in C

There are various forms of graphs, but the more frequently used are:

[Approach 1] Adjacency Matrix Graph Representation

An adjacency matrix is a two-dimensional matrix used with graphs. It is more efficient that the adjacency list representation when the graph is dense and we need quick access frequently.

  • In C, we can create a 2D array of size V * V where V is the number of vertices.
  • Initially, set all the elements of the matrix to 0.
  • For each edge in the graph, if the graph is unweighted, set the corresponding element in the matrix to 1, else set the corresponding element in the matrix to the weight of the edge.
  • In case of undirected graph, matrix will be symmetric such as matrix[i][j] = matrix[j][i].
👁 adjMatrix

Output
Adjacency Matrix Representation
0 1 1 0 
1 0 1 0 
1 1 0 1 
0 0 1 0 

[Approach 2] Adjacency List Graph Representation

The adjacency list is a concept where we use array of linked list in order to represent vertices of a graph. Each element denotes a vertex and the linked list present at the same index contains all the vertices that are directly connected to the vertex represented by the given element.

  • Create an array of linked lists (or arrays of arrays) of size V, where V is the number of vertices.
  • For each edge in the graph, add the destination vertex to the list of the source vertex.
  • If the graph is undirected, add the source vertex to the list of the destination vertex as well.
  • If the graph is weighted, modify each element in the list to store a pair of values: the destination vertex and the weight of the edge.
👁 Direct-Graph-to-Adjacency-list

Output
Adjacency list representation:
0: 4 1
1: 4 3 2 0
2: 3 1
3: 4 2 1
4: 3 1 0

Frequently Asked Questions on Graphs in C

Q1. What is the difference between directed and undirected graphs?

Directed graphs have arrows on their edges and directed edge are also referred to as arcs, while undirected graphs do not have arrows or directions on the edges at all.

Q2. How do you represent a weighted graph?

Adjacency list is the simplest way of representing the graph where each node can accommodate weight. In an adjacency matrix or some other concepts, it is possible to create the weight from the value in the matrix.

Comment
Article Tags: