Graphs are a popular data structure used in computer science and mathematics for representing a collection of objects (vertices) and their connections (edges). A weighted graph is a type of graph where each edge has a weight or a value assigned to it. In this article, we will explore the concept of weighted graph data structure , their applications, and implementation using Python programming language.

## Introduction to Weighted Graphs

In a weighted graph, each edge is assigned a numerical value or weight that represents some cost or distance associated with that edge. For example, in a map, the distance between two cities can be represented by the weight assigned to the edge connecting those cities.

Weighted graphs are used in a variety of applications, including:

• Network routing algorithms
• Shortest path algorithms
• Traffic flow analysis
• Scheduling and resource allocation
• Game AI and pathfinding

## Types of Weighted Graphs

There are two main types of weighted graphs:

1. Directed weighted graph: In this type of graph, each edge has a direction and a weight associated with it. This means that the edge represents a one-way connection between two vertices, and the weight represents the cost of traversing that edge in that direction.
2. Undirected weighted graph: In this type of graph, each edge has a weight associated with it, but there is no direction. This means that the edge represents a connection between two vertices that can be traversed in both directions, and the weight represents the cost of traversing that edge in either direction.

## Implementing Weighted Graphs in Python

In Python, we can implement a weighted graph using a dictionary and a list. The dictionary is used to store the vertices of the graph and their adjacent vertices, while the list is used to store the edges and their weights.

Let’s see an example of implementing a directed weighted graph in Python:

``````class WeightedGraph:
def __init__(self):
self.vertices = {}

self.vertices[vertex] = []

def add_edge(self, start_vertex, end_vertex, weight):
self.vertices[start_vertex].append((end_vertex, weight))
``````

In this implementation, the `WeightedGraph` class has two methods: `add_vertex` and `add_edge`. The `add_vertex` method adds a new vertex to the graph, while the `add_edge` method adds a new edge with a weight between two vertices.

To create a new weighted graph, we can do the following:

``````g = WeightedGraph()
``````

This creates a directed weighted graph with three vertices (‘A’, ‘B’, and ‘C’) and three edges with weights (2, 3, and 1) connecting them.

## Dijkstra’s Algorithm for Shortest Path

One of the most common algorithms used with weighted graphs is Dijkstra’s algorithm for finding the shortest path between two vertices. This algorithm works by starting at the source vertex and iteratively exploring the neighboring vertices to find the shortest path to each one.

Let’s see an example of implementing Dijkstra’s algorithm in Python:

``````import heapq

def dijkstra(graph, start_vertex):
distances = {vertex: float('inf') for vertex in graph.vertices}
distances[start_vertex] = 0
pq = [(0, start_vertex)]

while len(pq) > 0:
current_distance, current_vertex
``````

(current_vertex) = heapq.heappop(pq)

``````    if current_distance > distances[current_vertex]:
continue

for neighbor, weight in graph.vertices[current_vertex]:
distance = current_distance + weight

if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))

return distances
``````

## Conclusion

In summary, a weighted graph is a type of graph where each edge has a weight or value assigned to it. Weighted graphs are used in a variety of applications, including network routing algorithms, shortest path algorithms, traffic flow analysis, scheduling and resource allocation, and game AI and pathfinding. In Python, we can implement a weighted graph using a dictionary and a list, and we can use Dijkstra’s algorithm to find the shortest path between two vertices.

## FAQs

1. What is the difference between a weighted graph and an unweighted graph?
• A weighted graph is a type of graph where each edge has a weight or value assigned to it, while an unweighted graph does not have any weights or values assigned to its edges.
1. What is Dijkstra’s algorithm used for?
• Dijkstra’s algorithm is a commonly used algorithm for finding the shortest path between two vertices in a graph.
1. What is the time complexity of Dijkstra’s algorithm?
• The time complexity of Dijkstra’s algorithm is O(E log V), where E is the number of edges in the graph and V is the number of vertices.
1. Can a weighted graph be cyclic?
• Yes, a weighted graph can be cyclic, which means that there can be a path that starts and ends at the same vertex while traversing through one or more edges.
1. What are some real-world examples of weighted graphs?
• Some real-world examples of weighted graphs include maps (where the distances between cities are represented by weights), social networks (where the strength of relationships between people is represented by weights), and airline flight schedules (where the flight durations are represented by weights).