Weighted Graphs

We’ve already seen that graphs can come in a number of different flavors. Another useful type of graph, known as a weighted graph, adds additional information to the edges of the graph.

Here’s a weighted graph that represents a basic map of several major cities in the United States:

images/graphs/weighted_graph_1.png

In this graph, each edge is accompanied by a number that represents the distance in miles between the cities the edge connects—for example, 714 miles between Chicago and New York City.

It’s also possible to have weighted graphs that are also directional. In the following example, we can see that although a flight from Dallas to Toronto is $138, a flight ...

Get A Common-Sense Guide to Data Structures and Algorithms in JavaScript, Volume 1 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.