* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: A graph problem (not necessarily math-heavy)  (Read 1731 times)

0 Members and 1 Guest are viewing this topic.

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
A graph problem (not necessarily math-heavy)
« on: November 09, 2016, 11:01:40 am »
Well, I have a graph problem. Yeah, that's the title.

Basically, imagine nodes in a plane (well, they'll be in space but let's say plane to keep things simple), without any edge for now, because the problem is exactly to find out where to put the edges. And where do we want edges? We want edges between neighbour nodes, meaning that a node will have an edge with all the closest nodes. What does that mean "closest"? There is a difference between "the 5 closest" and "the 20 closest", so yeah, my definition doesn't mean s***, that's exactly my problem :P

So I'll give an example. Here are 3 nodes in line with the wanted edges. There is no edge between 1 and 3 because 2 is closer.
Code: [Select]
­1-2-3

Well, that was one stupid example. But basically, imagine more nodes, not necessarily in line. Well, imagine you're a point, moving in the plane. There is obviously a node that you're closest to, that we will call X. And a node Y would be called a "closest node to X" if your point can move from X to Y while having its closest node being always either X or Y.
In the above example, that's why 1 isn't a "closest node to 3", because whatever you do, if you try to move from 3 to 1, at some point the closest node to you will be 2.

So I have to code this, but I can't code something I didn't even define by words and rules, so does anyone have any idea how to get these rules down on paper?



Offline kotu

  • -< \'V/,,,
  • Super User
  • Join Date: Aug 2016
  • Location: don't panic
  • Posts: 1160
  • Post Rating Ratio: +7/-11
  • woah
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @romanov-psyco
    • futurerave.uk
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #1 on: November 09, 2016, 11:04:47 am »
i have no idea how to approah this

i skim-read your post because it looked difficult to understand, i doubt i could understand it if i had read it (without you explaining in person) and tbh i think you need someone trained in graph theory lol

*edit*
is it possible to make a picture of the 1-2-3 example?
« Last Edit: November 09, 2016, 11:07:14 am by kotu »
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #2 on: November 09, 2016, 11:07:06 am »
I am not sure of it really. On the contrary, someone trained in graph theory or algorithms would start thinking about Dijkstra and stuff and where to apply it and then would get a biased definition with a forced Dijkstra in it even though that may be unnecessary or even false. Or they would skim-read too and answer "that's just the minimum spanning tree" even though nope, it's not, I'm sure I can get cycles in the stuff I want (just imagine 3 points at an equal distance, you want the three edges, not just 2).
« Last Edit: November 09, 2016, 11:10:30 am by Hayleia »

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2293
  • Post Rating Ratio: +9/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #3 on: November 09, 2016, 11:12:06 am »
thats some dificult to understand post you made there O.o

But for finding the next neighbors just use aa FOR loop on all other node and calculate the distance A(Xa|Ya) <--->B(Xb|Yb) by doing squareroot(Xa-Xb)^2+(Ya-Yb)^2)
but for putting it down on paper in a formula...
I dont know if thats even possible... xD

I hope that's what you ment. But it seems too easy to be the answer to your question... <_<
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #4 on: November 09, 2016, 11:15:38 am »
thats some dificult to understand post you made there O.o

But for finding the next neighbors just use aa FOR loop on all other node and calculate the distance A(Xa|Ya) <--->B(Xb|Yb) by doing squareroot(Xa-Xb)^2+(Ya-Yb)^2)
but for putting it down on paper in a formula...
I dont know if thats even possible... xD

I hope that's what you ment. But it seems too easy to be the answer to your question... <_<
Nope, because that would give me the X closest nodes, not the "closest nodes" I'm looking for. They are most probably contained in some "X closest nodes" set, but which one? Especially annoying to know since X depends on the node (see the stupid example where 1 has X=1 closest nodes and 2 has X=2 closest nodes).
« Last Edit: November 09, 2016, 11:20:11 am by Hayleia »

Offline p2

  • CodeWalrus Staff
  • Super User
  • Moderator
  • CodeWalrus Supporter
  • *
  • Join Date: Aug 2016
  • Location: Germany
  • Posts: 2293
  • Post Rating Ratio: +9/-0
  • *Wubba lubba dub dub*
    • KaliPhobos
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #5 on: November 09, 2016, 11:45:57 am »
okeyyy, then to get the 5 nearest ones:

create "SMALLEST" var set to >9000
FOR loop on all nodes
  IF distance < "SMALLEST" var
  Then distance --> SMALLEST
  save node handle somewhere
EndFOR

that'd e the right code to get the smallest one.
if you need multiple results:

add an array for the node handles instead of just one single var
add if-condition in for-loop: to skip a node if it'S handle is already in the handle-array
use a for-loop to execute the previous for-loop as many times you want, eah time saving the resulting node handle in the Nth position of the array.
what you get is the node's handles, sorted by their distances to your current node. :)

is this even helpful? Or am I just repeating stuff you already know but didnt use as it was a too crappy way of doing things? ^^
  • Calculators owned: ti-83+, ti-84+, ti-84+, ti-84+se, ti-84+se(te), ti-nsphire, ti-nsphire CX-CAS, ti-voyage, ti-voyage, Who reads this list anyways...?
Anyway war sucks. Just bring us your food instead of missiles  :P ~ DJ Omnimaga (11.10.2016 20:21:48)
if you cant get a jframe set up, draw stuff to it, and receive input, i can only imagine how horrible your game code is _._   ~ c4ooo (14.11.2016 22:44:07)
If they pull a Harambe on me tell my family I love them ~ u/Pwntear37d (AssangeWatch /r/)
make Walrii great again ~ DJ Omnimaga (28.11.2016 23:01:31)
God invented the pc, satan the smartphone I guess ~ p4nix (16.02.2017 22:51:49)

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #6 on: November 09, 2016, 01:28:47 pm »
It is not helpful, but not because it's a crappy way to do things (you should read my code, you'd see I don't care about crappy stuff :P), just because it is not what I want.

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #7 on: November 09, 2016, 01:53:19 pm »
Kind of an update so I double post.

First, I have images, as asked by @kotu, see below.

Second, I have an almost mathematical definition of what I'm looking for.
The nodes X and Y are neighbours if there exists a path between X and Y (straight line or not) such as for all points p on that line, the closest node to p is either X or Y.

Here is the line example. You can see that there is a path between 1 and 2 such as you're always closest to 1 or 2 (on the yellow part of the path you're closest to 1, and on the red one you're closest to 2). same for 2 and 3, and there is no requirement to be a straight line. But for 1 and 3 nope, you can't find a path without a point p that will have 2 as its closest node (note, I put colors roughly where I think stuff is closest, but don't start measuring, these are just examples made in 5 minutes).


And here is a triangle example. You can see that 1 and 3 become neighbours because if we go far enough towards the bottom, we can avoid getting close to 2. And 1 and 2 are still neighbours, same for 2 and 3 but you know that.


Offline kotu

  • -< \'V/,,,
  • Super User
  • Join Date: Aug 2016
  • Location: don't panic
  • Posts: 1160
  • Post Rating Ratio: +7/-11
  • woah
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @romanov-psyco
    • futurerave.uk
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #8 on: November 09, 2016, 02:17:33 pm »
just seems like a load of calls to sqrt() * to determine 2D distances

* (if you're using C orC++)

yeah ok you can't test a path in its purest sense, as you will have to break the path down into points (pixels for example)

that would require a  formulaic solution and more processing time. the pixels based approach would probably suffice, i guess
« Last Edit: November 09, 2016, 02:20:13 pm by kotu »
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Offline Snektron

  • Lvl 69 Russian Snake
  • Super User
  • Join Date: Dec 2014
  • Location: Netherlands
  • Posts: 3165
  • Post Rating Ratio: +32/-0
  • SSSssssss.....
    • RobinDeWalvis
    • Kzyrox
    • RobinDeWalvis
    • quantuminfinity
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #9 on: November 09, 2016, 02:18:50 pm »
Have you tried Delaughney triangularization? Or Isosurfaces? Or voronoi-like algoritms? Im not sure, but maybe either of those might help you.
On a side note, bute force drawing it in a shader might be possible.

Also what kind of results are you supposed to get? because there are either one, infinite or zero possibilities i think (which makes me think of matrices).
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #10 on: November 09, 2016, 02:45:23 pm »
yeah ok you can't test a path in its purest sense, as you will have to break the path down into points (pixels for example)

that would require a  formulaic solution and more processing time. the pixels based approach would probably suffice, i guess
Nope, can't do that. Far too slow. I mean, cutting a path into discrete subpaths is possible, but combined to the fact there are quite a big number of paths to test, this is not doable.

Have you tried Delaughney triangularization? Or Isosurfaces? Or voronoi-like algoritms? Im not sure, but maybe either of those might help you.
On a side note, bute force drawing it in a shader might be possible.
No idea what isosurfaces are doing here :P
edit yeah I see what, probably has to do with the border between the "influence zones" of two nodes.
But the other two seem very related indeed. I'll check these out.

Also what kind of results are you supposed to get? because there are either one, infinite or zero possibilities i think (which makes me think of matrices).
Well I'd like to get the one result when there is one result :P
« Last Edit: November 09, 2016, 02:47:45 pm by Hayleia »

Offline kotu

  • -< \'V/,,,
  • Super User
  • Join Date: Aug 2016
  • Location: don't panic
  • Posts: 1160
  • Post Rating Ratio: +7/-11
  • woah
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @romanov-psyco
    • futurerave.uk
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #11 on: November 09, 2016, 03:33:14 pm »
Are you trying to generate 2D meshes from general shape definitions? Because I played about with something like that about 12 years ago..... just finding the file. It might not help you though... will post some pics in a bit

*update*


the program generates flat 2D 'panels' based on points that you input...... however I'm not sure if the app fulfils yours criteria as I'm not sure if it would do this...


or this......


in a given situation. It does however allow for holes in a mesh...


There is a bug in it though... see this,,


You can have the source if it helps you

Sorry if this wasn't relevant to you lol
« Last Edit: November 09, 2016, 03:41:12 pm by kotu »
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Offline Snektron

  • Lvl 69 Russian Snake
  • Super User
  • Join Date: Dec 2014
  • Location: Netherlands
  • Posts: 3165
  • Post Rating Ratio: +32/-0
  • SSSssssss.....
    • RobinDeWalvis
    • Kzyrox
    • RobinDeWalvis
    • quantuminfinity
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #12 on: November 09, 2016, 05:20:05 pm »
Should the path be a smooth function? Because then you might be able to do something with parametric or parabolic equations.
The problem is the closest path will always (with 3 nodes) be either a straight line, or 2 straight lines with a sharp turn. Which isn't very mathematicy.
The only solution i can think of right now requires an awful lot of vector math (calculate {p | d(A, p) = d(B, p) = d(C, p) ^ A != B ^ A != C ^ B !+ C} and calculate a path through that. That also requires a special case for when the sortest path is "straight").
« Last Edit: November 09, 2016, 05:22:15 pm by Snektron »
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Offline kotu

  • -< \'V/,,,
  • Super User
  • Join Date: Aug 2016
  • Location: don't panic
  • Posts: 1160
  • Post Rating Ratio: +7/-11
  • woah
    • dj_kotu
    • @UCAy2ZF4hbc08iStnTn6jgxQ
    • rowanmedhurst
    • @romanov-psyco
    • futurerave.uk
  • Gender: Male
Re: A graph problem (not necessarily math-heavy)
« Reply #13 on: November 09, 2016, 05:52:40 pm »
if you are looking at ways of making efficient networks between many nodes (for example to design an underground rail system) you might want to look at slime molds. these single-cell organisms get quite large (20cm across easily) and they form inter-cell paths for food to travel down. found to create very efficient networks, equivalent to expert human designers (for example of rail system)



apparently they use pulses to coordinate the maze mapping (i think)
  • Calculators owned: TI 84+CE-T
  • Consoles, mobile devices and vintage computers owned: Sega Master System, Sony PlayStation 3
SUBSCRIBE TO THE FUTURERAVE.UK MAILING LIST
http://futurerave.uk

Offline Hayleia

  • Full User
  • Join Date: Nov 2014
  • Location:
  • Posts: 324
  • Post Rating Ratio: +12/-0
Re: A graph problem (not necessarily math-heavy)
« Reply #14 on: November 10, 2016, 09:55:58 am »
Are you trying to generate 2D meshes from general shape definitions? Because I played about with something like that about 12 years ago..... just finding the file. It might not help you though... will post some pics in a bit

*snip*

You can have the source if it helps you

Sorry if this wasn't relevant to you lol
I don't know if this would work, depending on the algorithm you use to generate meshes (not mentioning the bug).
See this example:

Both meshes are valid meshes for the 4 given points, but the right one according to what I want is the one on the right.

Should the path be a smooth function? Because then you might be able to do something with parametric or parabolic equations.
The problem is the closest path will always (with 3 nodes) be either a straight line, or 2 straight lines with a sharp turn. Which isn't very mathematicy.
The only solution i can think of right now requires an awful lot of vector math (calculate {p | d(A, p) = d(B, p) = d(C, p) ^ A != B ^ A != C ^ B !+ C} and calculate a path through that. That also requires a special case for when the sortest path is "straight").
I don't care about the path :P
The input is nodes, the output is nodes' neighbours, and you can use whatever you want in between, not paths, paths, whatever paths, smooth or not.

But I think I'll give up on this complicated subject, I'll just use the X closest ones (where I'd have to find a working X) instead of trying something smart...

 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games