Hi Guys
I have this algorithm problem to share with you: :)
Imagine you have two dimensional array like this: -
$my_array = array(
-
"b" => array("t", "p"),
-
"t" => array("p", "b"),
-
"d" => array("b", "t"),
-
"p" => array("b", "d", "n"),
-
"n" => array("d", "b")
-
);
-
I need algorithm/function that can find all the circles within this two dimensional array. Simplest circle from this example above would be
b - t - b
Where do I see circle? Well its simple really "b" element of $my_array contains "t" and "t" element of $my_array contains "b" so we are back at the starting point.
Lets see another bit more complex circle:
b - p - d - b
"b" contains "p"
"p" contains "d"
"d" contains "b"
$my_array is dynamically created and the only rule is that its elements (arrays) can not contain its index. What that means is that this element "b" => array("t", "p", "b") can not happen cos "b" element can not contain "b".
I hope that I managed to explain problem well and appreciate any input. If something is unclear please ask I'll try to explain.
Thanx
.......... And after a while and some help from jkmyoung http://bytes.com/topic/algorithms/an...ree-flattening here is recursive function that does the business: -
foreach($a as $key => $arr){
-
getClosedList($key, $a);
-
}
-
-
function getClosedList($element, $mix, $main_key = NULL, $main_array = NULL, $array = array()){
-
if((end($array) == $main_key)&&($main_key != NULL)){
-
var_dump($array);
-
return;
-
}
-
if(!$main_key){
-
$main_key = $element;
-
$main_array = $mix[$main_key];
-
}
-
unset($mix[$element]);
-
foreach($mix as $firma_key => $array_duga){
-
$a = $array;
-
$a[] = $firma_key;
-
if((in_array($firma_key, $main_array))&&(in_array($element, $array_duga))){
-
$a[] = $main_key;
-
getClosedList($firma_key, $mix, $main_key, $main_array, $a);
-
}elseif(in_array($element, $array_duga)){
-
getClosedList($firma_key, $mix, $main_key, $main_array, $a);
-
}
-
}
-
}
-
15 1939
Is this for a game? I think in_array() will be very useful.
But you will hve to hand loop the outer array
@code green
Thanks for reply
Yes in_array() will be useful I am sure, but my problem is not in using manual :) Its more of an algorithm issue. Could you elaborate on your "hand loop the outer array" approach. I would be very grateful.
Thanx Again
Could you elaborate on your "hand loop the outer array" approach.
I'll have go - foreach($my_array as $key=>$arrayin)
-
{
-
foreach($arrayin as $keyin=>$valuein)
-
{
-
if(in_array($keyin,$my_array)
-
{
-
//Do something, not sure what. My head hurts
-
}
-
}
-
Might have that the wrong way around but you get the idea
@code green
hahahaha
"//Do something, not sure what. My head hurts" is exactly where I loose it :)
Thanx :)))
Hmm I think that is wrong - foreach($my_array as $key=>$arrayin)
-
{
-
foreach($arrayin as $valuein)
-
{
-
if(in_array($valuein,$my_array)
-
{
-
echo 'Yehey I've found a circle; }
-
}
-
Hm I just tested it for the example above and it didn't say "'Yehey Ive found a circle'" at all.
Its more complex than it looks at first glance. I think it has to be some sort of recursive function but its very confusing.
I doubt a recursive loop is needed. I've still got it wrong.
Replace the in_array() with array_key_exists() or whatever it is.
We are trying to match the 'keys' in $my_array, not the values which are arrays again.
Could even use - foreach($my_array as $key=>$arrayin)
-
{
-
foreach($arrayin as $valuein)
-
{
-
if(isset($my_array[$valuein])
-
{
-
echo 'Yehey I've found a circle; }
-
}
-
Hmm
I can see how this can work for simple circles like "b - t - b" but you can not find circles like "b - p - d - b" using this approach. Maybe I'm missing something in your approach but if you would echo actual circle instead of 'Yehey I've found a circle' you would have only current outer and current inner array to do so and circles may be made from unlimited number of arrays
or am I missing something????
I see your point. There is no limit to the size of the chain.
So we don't know how deep to nest the loops.
In fact every single permutation has to be mapped.
Can we make use of database tables somehow?
If necessary we can use database no probs.
I was considering to find all permutations and than just pick those that complete the circle. However if you have just 10 elements in array you have 10! permutations and its very likely that number much higher than 1000 so imagine 1000! that would take forever to compute.
OK, this is an idea.
Just concentrating on the 2-deep "b - t - b" run through all the arrays building another set of arrays that define what each array can possibly map to.
ie $b array via 't' and 'p' can map to p,b,d,n so becomes $b(p,b,d,n).
And then loop through the new map arrays and create more arrays that map the maps to the maps. $bp array for ex
The thing is, whatever the solution there has to be a cut-off point somewhere or you could search going round in circles(pun) forever.
A maximum chain length needs to be calculated based on what you start with.
I still think solution is somewhere with recursive functions however much I dont like them. Thanks for trying.
Maybe this one should be moved to Algorithms too?!
.......... And after a while and some help from jkmyoung http://bytes.com/topic/algorithms/an...ree-flattening here is recursive function that does the business: -
foreach($a as $key => $arr){
-
getClosedList($key, $a);
-
}
-
-
function getClosedList($element, $mix, $main_key = NULL, $main_array = NULL, $array = array()){
-
if((end($array) == $main_key)&&($main_key != NULL)){
-
var_dump($array);
-
return;
-
}
-
if(!$main_key){
-
$main_key = $element;
-
$main_array = $mix[$main_key];
-
}
-
unset($mix[$element]);
-
foreach($mix as $firma_key => $array_duga){
-
$a = $array;
-
$a[] = $firma_key;
-
if((in_array($firma_key, $main_array))&&(in_array($element, $array_duga))){
-
$a[] = $main_key;
-
getClosedList($firma_key, $mix, $main_key, $main_array, $a);
-
}elseif(in_array($element, $array_duga)){
-
getClosedList($firma_key, $mix, $main_key, $main_array, $a);
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ranjit |
last post by:
Hi,
I would like to know how to place circles in the
connecting points of the lines, and very close to the
circles I would like to place the values like a,b,c...etc
automatically for each...
|
by: dgk |
last post by:
A friend wants to have two circles containing letters. One would be
stationary and the other would rotate around the first one. I know how
to get the graphic form thingie from the Form Paint event...
|
by: Peter Olcott |
last post by:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
>...
|
by: kmercer46 |
last post by:
have a class that contains an array and i would like to access the
individual array elements in a form. I understand that VB.net does not
allow the array to be public and i think i need to pass it...
|
by: Gilgamesh |
last post by:
Hi,
I have some very limited knowledge of web development but I have an
imagemap in which I want to be able to display a small yellow circle at
certain pre-define locations. When the user moves...
|
by: Douglas Douglas |
last post by:
Hi everybody.
I have a paper form that I scan into an image. My user fills some circles in
this paper form using black ink. Every form has ten rows with five circles each
and the user fills only...
|
by: jason.m.ho |
last post by:
Hello,
I am writing a no-plugin networked Sketchpad program (DHTML,
Comet/Ajax). Because I am trying to avoid using Flash or Java Applet, I
rendered my strokes at first with the wz_jsgraphics...
|
by: funke |
last post by:
An archery target consists of a central circle of yellow surrounded by concentric rings of red, blue,balck,white.Each ring has the same width which is the same aas the radius of the yellow...
|
by: meyboat |
last post by:
hello there, i just started writing some simple programs in c plus plus and now on another one , which is suppose to draw concentric circles. having some difficulties and wondering if u could help me...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |