This post is completed by 2 users

  • 1
Add to List
Beginner

270. Weighted Graph Implementation – JAVA

We have already discussed about Graph basics. We recommend reading this before you continue to read this article.

What is Weighted Graph?

A Graph is called weighted graph when it has weighted edges which means there are some cost associated with each edge in graph.

Example:

Implementation:

  1. Each edge of a graph has an associated numerical value, called a weight.
  2. Usually, the edge weights are nonnegative integers.
  3. Weighted graphs may be either directed or undirected.
  4. The weight of an edge is often referred to as the "cost" of the edge.
  5. Will create an Edge class to put weight on each edge

Output:

vertex-0 is connected to 2 with weight 3
vertex-0 is connected to 1 with weight 4
vertex-1 is connected to 2 with weight 5
vertex-1 is connected to 3 with weight 2
vertex-2 is connected to 3 with weight 7
vertex-3 is connected to 4 with weight 2
vertex-4 is connected to 5 with weight 6
vertex-4 is connected to 1 with weight 4
vertex-4 is connected to 0 with weight 4

Reference: here