开发者

Represent Graph as a 2d Matrix

开发者 https://www.devze.com 2023-01-11 07:02 出处:网络
Lets say I have a graph of x nodes. I want to first represent it and then with help of a Algorithm Y, I would assign value to each of the node. Then I want to refresh the graph to display the value ca

Lets say I have a graph of x nodes. I want to first represent it and then with help of a Algorithm Y, I would assign value to each of the node. Then I want to refresh the graph to display the value calculated.

Steps 1) Represent the graph as 2d Matrix. Perform processing over the 2d Matrix and then use the result to generate a new graph. [Better than doing processing by iterating the grap开发者_运维知识库h]

Problems:

1) I need to create a 2d Array with indexes as node names i.e string. I am not sure what's the best way to do this?

2) What's the best graph api which can A) produce good looking results 2) user friendly 3) allows vertex names as strings

I hope I made myself clear. Any input would be of immense help.

Thanks Sunil


A Java array can't use strings as indexes, only non-negative integers. I think what you want is a java.util.Map. To get a two-dimensional structure, you probably want to create an array of Maps, or a Map that contains arrays, or something like that.


1) I need to create a 2d Array with indexes as node names i.e string. I am not sure what's the best way to do this?

Instead of using node names as indexes, you can arbitrarily label each node with an integer and use this as the index into the array. This is generally how adjancy matrices are implemented.

For example, if you have a graph of nodes A, B, C, D, E, and F, choose an arbitrary labeling like

0 -> A
1 -> B
2 -> C
3 -> D
4 -> E
5 -> F

You can store this in a Map<Integer, String> (or the reverse) if need be.

Then whenever there is an edge between two vertices, add a 1 (or the weight of the edge, if it is a weighted graph) to the matrix at that location.

0

精彩评论

暂无评论...
验证码 换一张
取 消