Home » Data Structure

Representation of a Graph in Data Structure

Graph representation: In this article, we are going to see how to represent graphs in data structure?
Submitted by Souvik Saha, on March 17, 2019

What you will learn?

In this article we are going to study how graph is being represented?

Following is an undirected graph,

Graph image 3

We can represent the same graph by two different methods:

  1. Adjacency Matrix
  2. Adjacency List

1) Adjacency Matrix

A graph can represent matrix elements. Initially, all the elements of a matrix are zero. If there is an edge between two vertices (example vertex A and B) then we mark '1' to the element at the position MAB and MBA for undirected graph and for a directed graph, we mark '1' to the element at the position MAB.

Example:

Graph image 4

2) Adjacency List

A graph can also be represented by a list. We need an array of the list and for each time if there is an edge exist between two vertices then we push one vertex to another vertex list and vice versa. For the previous graph, we get an Adjacency List like this:

Graph image 5






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.