开发者

Java Graph's nodes representation

开发者 https://www.devze.com 2023-04-05 05:25 出处:网络
So I have to build this for my assignment, where I am given an input file that creates a graph using x and y coordinates like:

So I have to build this for my assignment, where I am given an input file that creates a graph using x and y coordinates like:

(1,3)
(4,4)
(5,10)
开发者_开发技巧

etc...(thats the input in the file)

Afterwards, I then give the user the option to do DFS/BFS where they then input the start and goal coordinates for this. I have created an adjacency matrix before where my nodes (vertices) are A, B, C etc but I have never used coordinates similar to this one as my nodes.

My question is: how do I represent these coordinates as nodes in a graph...I know an adjacency matrix won't work in this case and I need something different (due to there being no max size), but I'm completely confused about how to represent these coordinates as nodes at the moment. Can someone help me?

Additional info [from comments]:

each pair represents an edge between the two, so (1,3) has an edge/connected to (4,4) and (5,10) would be connected to the next pair given underneath (that I did not write up), we can assume the input is correct/is even in pairs


You will need to map your coordinates, each coordinate will have an integer representing it, and the integers should be continious [0,1,2,...,n-1] where n is the number of coordinates.

Do it by first iterating on the coordinates, and insert each new coordinate to the map with a new integer.
This way, you can create an adjancy matrix, because you know exactly how big your graph is [remember, you counted it, so you just found the number of nodes, which is the matrix's size!]

Now you can easily create a matrix, refer to each coordinate as an integer. An edge in the matrix is like always, if there is an edge: p1,p2, you should set matrix[map.get(p1)][map.get(p2)] = 1

0

精彩评论

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

关注公众号