Posted on by Kalkicode
Code Graph

Adjacency list representation of graph

A graph is a non-linear data structure that is used to represent networks, connections, and relationships between various entities. Graphs consist of nodes (also known as vertices) and edges (also known as links or arcs), where nodes represent entities and edges represent connections or relationships between entities. Graphs are used in a wide range of applications such as social networks, transportation networks, computer networks, and many more.

There are two types of graphs: directed graphs and undirected graphs. In a directed graph, edges have a direction associated with them, while in an undirected graph, edges do not have any direction associated with them.

Adjacency List Representation:

The adjacency list is one of the most commonly used data structures to represent a graph. An adjacency list is an array of linked lists, where each element of the array represents a node in the graph, and the linked list associated with the element contains the nodes that are adjacent to the node represented by that element.

In other words, each element of the array stores a list of nodes that are directly connected to the node represented by that element. The adjacency list representation is more space-efficient than the adjacency matrix representation for sparse graphs.

This article is based on Adjacency list.

Directed Graph Adjacency list

Here given code implementation process.

Un-Directed Graph Adjacency list

Adjacency list of undirected graph

Note that in both example first use an array which are contain actual node values. Array is useful to get any node quickly in existing array. When graph nodes are not predefined or you are remove existing graph node then array are not suitable here.

Because after create array, In most of programming language are not allowing to resize the array size such as add or delete existing node. In this case you'll can use linked list to storing the value of actual graph node.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment