
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Weighted Graph Representation in Data Structure
As we know that the graphs can be classified into different variations. They can be directed or undirected, and they can be weighted or unweighted. Here we will see how to represent weighted graph in memory. Consider the following graph −
Adjacency matrix representation
To store weighted graph using adjacency matrix form, we call the matrix as cost matrix. Here each cell at position M[i, j] is holding the weight from edge i to j. If the edge is not present, then it will be infinity. For same node, it will be 0.
0 | ∞ | 6 | 3 | ∞ |
3 | 0 | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | 2 | ∞ |
∞ | 1 | 1 | 0 | ∞ |
∞ | 4 | ∞ | 2 | 0 |
Adjacency List representation
In the adjacency list, each element in the list will have two values. The first one is the destination node, and the second one is the weight between these two nodes. The representation is like below.
- Related Questions & Answers
- ADT-array Representation in Data Structure
- Graph Data Structure in Javascript
- Array of Arrays Representation in Data Structure
- Searching a Graph in Data Structure
- Java Program to Implement the graph data structure
- Representation of Relations using Graph
- Rectangle Data in Data Structure
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Binary Tree Representation in Data Structures
- Halfedge data structure
- Deaps in Data Structure
- Quadtrees in Data Structure
- Weighted Job Scheduling
- Arrays Data Structure in Javascript
- Stack Data Structure in Javascript
Advertisements