# Weighted Graph Implementation – JAVA

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

Complete 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

__________________________________________________
Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________