What methods can represent weighted directed graphs in Java?

I can't use any external libraries, so I try to think of some ways to build my own data structure I was thinking maybe it was like this:

public class Node{
    Set<Edge> adjacent;
    int value;
}

public class Edge{
    Node target;
    int weight;
}

But I guess it might be a better way

I finally used this diagram to run the bellman Ford algorithm on it, but I obviously need a function diagram first!

Solution

The answer depends largely on the algorithm you plan to apply to the chart

There are two common ways to represent graphics - an adjacency list and an adjacency matrix In your case, the adjacency matrix is a square array of integers representing weights Your representation uses adjacency lists

Some algorithms work better on the adjacency matrix (e.g. Floyd warhall algorithm) Other algorithms work better on adjacency lists (such as Dijkstra algorithm) If the chart is sparse, the use of adjacency matrix may be prohibitive

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>