educative.io

How to store edge value?

A reader asked us the following question:

Hi, I had a question, If we don’t assume the edges to be of unit 1, then do we store it’s value in the DLL as below?

public class Node { 
   public T nextVertex; 
   public int edgeValue; 
   public Node nextNode; 
   public Node prevNode; 
}

Hi,

This is Fatimah Abdullah from Educative. I noticed your feedback here and I’m glad you reached out to us.

In response to your question, I believe what you are mentioning is called a weighted edge. The DLL Node implementation that you have given is indeed the correct way to handle weighted graphs. The int value will represent the weight or the value of each edge, while the other fields remain the same.

Best Regards,
Fatimah Abdullah | Developer Advocate
Educative Inc.

2 Likes

Hey @FatimahAbdullah

For traversing a weighted graph - when visiting a node that has already been visited, can we still assume that the initial visitation is the shortest path? Or do we need to re-visit the node and update the path with a new length?