The difference between weighted and unweighted graphs lies in the values associated with their edges.
In an unweighted graph, all edges have the same value or weight, which is typically 1. This means that there is no distinction between different edges, and the shortest path between two vertices is the path with the fewest number of edges. For example, consider a graph representing a road network between cities, where each city is a vertex and each edge represents a road connecting two cities. In an unweighted graph, each road has the same weight, so the shortest path between two cities is the path with the fewest number of roads.
In a weighted graph, each edge has a value or weight associated with it, which can represent a variety of quantities such as distance, cost, or time. This means that some edges may be more important than others, and the shortest path between two vertices is not necessarily the path with the fewest number of edges. For example, consider a graph representing a flight network between cities, where each city is a vertex and each edge represents a flight between two cities. In a weighted graph, each flight has a different duration or cost, so the shortest path between two cities may involve taking a longer flight with a lower cost or a shorter flight with a higher cost.
Weighted graphs are useful for modeling real-world scenarios where the edges have varying degrees of importance or cost. They can be used in various applications such as route planning, resource allocation, and network optimization.