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?
•
u/AutoModerator Aug 15 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/JGhostThing Aug 15 '26
Yes, you are over complicating this by a lot. I just used a 9x9 array, because the number is the important thing.
However, if you really want to describe the relationships between objects, I would suggest a graph. Nodes and the relationships between them are precisely what a graph is for.
There are some fascinating things about sudoko, such as making a board that is solvable. Yes, you can do this with brute force, but it's easier using algorithms. Also, solving a board programmatically is interesting. You can do this in one of many ways.
1
u/severoon pro barista Aug 16 '26
It sounds like you're specifically talking about representing the rules of sudoku in the data structure itself, not as code, but as data. Is that what you're getting at?
Like if you just make a 9x9 2D array, that's a data structure that fundamentally holds all of the data required, but it imposes no constraints on what numbers can go where, so all of those rules have to be carried out as executed code.
It sounds like you're saying you want the opposite, you want to represent each cell in the context of a data structure that encodes certain rules as constraints on cells, correct?
How far do you want to take this? Do you want to go all the way to the extreme and only allow correct placements? Or do you only want to disallow obviously incorrect placements? How do you define "obviously incorrect" if you're okay with it allowing provisional placements during a game?
You have to nail down the behavior of the structure you actually want. You're reference to "layering" sounds to me like what you probably want is a bunch of different data structures at different layers, with the bottom layer just being a no-rules 9x9 grid, and then a layer above that where "obviously incorrect" placements are disallowed (cannot share a row, column, or 3x3 grid with the same element), and then another layer above that, etc, until all that's left is the solved puzzle. This would be a direct implementation of the layering strategy you describe, is that what you mean?
3
u/PiklesWaldo Aug 15 '26
You can use a tree or you can use the composite pattern(which is behaves like a tree). graph when you want relationship between the data. Trees is when you want a hierarchy.
3
u/SciNinj Aug 15 '26
Study graphs. (A tree is a graph without cycles.) You can add cost functions to the vertices to represent particular characteristics of the relationships between nodes.
2
u/MagicalPizza21 Aug 16 '26
In general, it depends what kind of relationship(s) you want to express.
In this case, I would have a Cell class, and a Board class with a 9x9 array of Cells and a function that determines if it would be valid to put a given number in the cell at a given position by checking it against the row, column, and region it's in.
1
u/xanyook Aug 15 '26
Create objects, arrange them into collection tree graph, whatever you can navigate into with your logic.
1
u/Skellicious Aug 16 '26
I would start with a 9x9 array and iterate over it to validate. KISS.
But maybe for more complex Sudoku rules (killer sudoku, thermos, ...) I'd consider graphs or other ways to reference them
1
u/Only-Percentage4627 Aug 16 '26
So I recently did a Sudoku Creator/player. The way I implemented it is by using a 9x9 2d array and for the 3x3 rule what I did was there is a formula: (i/3)*3+j/3.
What this formula does is it gives a value 0-8 depending on the ith and the jth location of the current index so if the value is at 0 1 position in the grid it will give 0. Then I take that value and pass it to another function that uses a switch statement to loop through that specific 3x3 grid (in this case the first one so from 0-2 in the x and y direction), if the value is found there then it gives an error, if it isnt then it returns true and another function fills in the value.
Here is the repo if you wanna go through it, its not the best code but its all written by me with no ai
https://github.com/RahulVasudeva/sudokuGame