# What Is Weight in Graph Data Structure?

//

Larry Thompson

Graph data structure is a widely used data structure in computer science. It consists of a set of vertices or nodes connected by edges.

Each edge represents a relationship or connection between two nodes. Weighted graphs are a type of graph where each edge is associated with a weight or value.

## What is Weight?

In the context of graph data structure, weight refers to the value assigned to an edge. It represents the cost or distance between two nodes connected by that particular edge. The weight can be any numerical value, such as integers or floating-point numbers, depending on the application.

## Why Use Weighted Graphs?

Weighted graphs are useful in various applications where the relationships between nodes have different values or costs associated with them. Some common examples include:

• Network Routing: In computer networks, weighted graphs can represent the connections between routers or network devices. The weights on the edges can represent metrics such as latency, bandwidth, or cost.
• Pathfinding Algorithms: Algorithms like Dijkstra’s algorithm and A* search algorithm use weighted graphs to find the shortest path between two nodes. The weights on the edges determine which path is more optimal.
• Scheduling and Planning: Weighted graphs can be used to model scheduling problems where tasks have different durations or costs associated with them.

## Representation of Weighted Graphs

A common way to represent weighted graphs is through an adjacency matrix or an adjacency list. In both representations, each edge includes not only information about its connected nodes but also its weight.

An adjacency matrix is a square matrix where each row and column represents a node in the graph. The value at the intersection of the row and column represents the weight of the edge connecting those nodes. If there is no edge between two nodes, the value is typically set to infinity or a special “null” value.