Be the first user to complete this post

  • 1
Add to List
Beginner

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:

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

Code:


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



Also Read: