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.



  • 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

Complete Code:


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

1 Response

  1. elvin says:


    Thank you for sharing this. Is there any way to display a graph like an example one with Java?

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: