r/javahelp • u/KeineAhnungBruder001 • Aug 15 '26
Unsolved Naive question: What Data Structure to express different layers of relationships between Objects?
Hello guys,
I wanted to ask naively, what the ideal data structure would be for expressing different layers of relationships between Objects.
For example in modern Sudoku the game has a 9x9 number grid composed of nine 3x3 sub-grids (which I call region). In each row, column and region each number between one and nine can only appear once. The goal is to find missing numbers in this number grid without violating the aforementioned rule.
One way to do it, would be to make 9x9 byte-array and express the relationship between numbers through methods checking if the rule is violated or not by checking if any row, column or region doesn't contain a value between one and ten.
I also thought on using a Graph to express the relationship between numbers in rows and columns, though I don't know how to express the relationship between numbers inside of a region.
Maybe even a multiple Objects like Board, Row, Column, Region but then I have redundant data.
Another problem I encounter with aforementioned implementations is, that if you introduce more relationships f. e. in the game of killer sudoku, where there are more rules are added f. e. by also introducing cages in which the numbers must add up to a target number, you need to implement that relationship logic tediously.
Am I overcomplicating things?
View all comments
Show parent comment
1
u/KeineAhnungBruder001 Aug 16 '26 edited Aug 16 '26
This is the direction I was going for. Of course I could just implement these rules in form of methods allowing or disallowing a specific placement. Though I would like a solution where it is inheritely not possible to insert a value into the data structure if it doesn't abide the set rules. Sudoku is more serving as an example but the post isn't really about solving sudoku, it is more about finding an ideal data structure which is able to represent multiple layers relationship between it's data in itself without including redundant data.
This can be expanded upon to f. e. cities. A house can be part of a city. But it can also be part of a street. And another house can be outside of the city but still be part of the same street. The rules themselves can then be set on the underlying groups. F. e. one city may allow only allow 3 houses painted green, the other city only allow houses to be build up to 10 floors.
The goal would be to have a cell (the data itself) being part of multiple other groups like city or street. The rules of the cell is all of the rules of the groups it is in. And also having the ability to have rules which are dependent on other data in the group itself. Like the example that you can only have three green houses in a city.
If you would do that with merely methods in a single file you will inevitably have a hard time navigating through your rules. If you would do that with Objects like "Street" or "City" you will run into the problem of having redundant data.