# What Is a Weighted Graph in Data Structure?

//

Larry Thompson

A weighted graph is a fundamental concept in data structures and graph theory. In this article, we will explore what a weighted graph is and how it is used in various applications.

What is a Weighted Graph?

In simple terms, a weighted graph is a type of graph where each edge has an associated weight or cost. Unlike an unweighted graph, where each edge has equal weight or cost, a weighted graph assigns different weights to different edges. These weights can represent various quantities such as distance, time, cost, or any other relevant measure.

Representation of Weighted Graph

There are several ways to represent a weighted graph in data structures. One common approach is to use an adjacency matrix or adjacency list.

An adjacency matrix is a two-dimensional array where the rows and columns represent the vertices of the graph. The value at each position (i,j) in the matrix represents the weight of the edge between vertex i and vertex j. If there is no edge between two vertices, the value can be set to infinity or any other suitable value.

An adjacency list, on the other hand, uses a collection of linked lists or arrays to represent the edges connected to each vertex. Each element in the list/array contains information about the adjacent vertex and its associated weight.

Applications of Weighted Graphs

Weighted graphs find numerous applications in various domains such as:

### 1. Shortest Path Algorithms:

Weighted graphs are extensively used in finding shortest paths between two vertices. Dijkstra’s algorithm and Bellman-Ford algorithm are popular algorithms that use weights on edges to determine the shortest path based on some criteria (e.g., minimum distance).

### 2. Network Optimization:

Weighted graphs are utilized for optimizing network design and routing problems. For example, in a communication network, the weights on edges can represent the bandwidth or latency, and algorithms can be developed to find the optimal route with minimum delay or maximum bandwidth.

### 3. Minimum Spanning Tree:

Weighted graphs are used to find the minimum spanning tree (MST) of a connected graph.

The MST is a subgraph that contains all vertices of the original graph with minimum total edge weight. Kruskal’s and Prim’s algorithms are commonly employed to find MSTs.

Conclusion

In conclusion, a weighted graph is a powerful data structure that assigns different weights to edges, enabling us to represent various quantities in real-world scenarios. Its applications range from finding shortest paths to optimizing network designs and constructing minimum spanning trees. By understanding weighted graphs, we can develop efficient algorithms and solve complex problems in different domains.

So, next time you encounter a problem involving relationships with varying costs or measures, consider utilizing weighted graphs as an effective tool for analysis and optimization.