|
Did you even look at the results of the search? The article there will tell you how to achieve what you are hoping to do.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I set myself "coding challenges:" this is one of them.
The questions: how to "stop" the recursion without using an external boolean flag. Context: while I know how to write hierarchy-enumeration code using a Stack, for TreeViews and such, the "path-finding" problem, here, is running down the walls of my brain like warm honey .. the code works, but it don't feel right. A bigger issue may be if the strategy I used to try and optimize the search ... via keeping track of "visited" values ... is accurate, robust, etc.
Using a generic Dictionary<T1, List<T1>> RDictT1T2; named 'test5 ... with these sample values:
var test5 = new Dictionary<string. List<string>>>();
test5.Add("a", new List<string>{"c", "d", "b" });
test5.Add("b", new List<string> { "c","f","g", "e" });
test5.Add("e", new List<string> { "g", "h", "k" });
test5.Add("k", new List<string> { "b", "l",}); I use the following code to get a possible "path" between one string value and another:
private List<T1> Result, Visited;
private T1 Start, Target;
private bool found = false;
public List<T1> FindPath(T1 start, T1 target)
{
Result = new List<T1>();
Visited = new List<T1>();
Start = start;
if (RDictT1T2[Start].Count == 0) return null;
Target = target;
Visited.Add(Start);
Result.Add(Start);
if (RDictT1T2.ContainsKey(Start) && RDictT1T2[Start].Contains(Target))
{
Result.Add(Target);
return Result;
}
found = false;
return FindRecurse(Start);
}
private List<T1> FindRecurse(T1 rstart)
{
foreach (var t1 in RDictT1T2[rstart])
{
if (Visited.Contains(t1))
{
continue;
}
else
{
if(
!RDictT1T2.ContainsKey(t1)
||
RDictT1T2[t1].Count == 0
)
{
Visited.Add(t1);
continue;
}
}
Result.Add(t1);
Visited.Add(t1);
if (RDictT1T2[t1].Contains(Target))
{
Result.Add(Target);
found = true;
}
else
{
FindRecurse(t1);
}
}
return found ? Result : null;
} These tests produce the expected results:
var r1 = test5.FindPath("a", "d");
var r2 = test5.FindPath("a", "f");
var r3 = test5.FindPath("a", "k");
var r4 = test5.FindPath("k", "x");
var r5 = test5.FindPath("k", "g");
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
I would have taken it one step further ... and built the actual "object graph"; which would be easier to traverse than probing a dictionary.
I use producedural code to build object graphs that can then be accessed "functionally" at run time.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Gerry Schmitz wrote: I use producedural code to build object graphs that can then be accessed "functionally" at run time. I do too; it would be great to see an example of your code.
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
Message Closed
modified 5-Jan-18 2:47am.
|
|
|
|
|
Message Closed
modified 15-Jan-18 13:17pm.
|
|
|
|
|
Sorry ... I thought it was a tree.
And sorry for boring you.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Something like this seems to work:
public static IReadOnlyCollection<T> FindPath<T>(this IReadOnlyDictionary<T, IReadOnlyCollection<T>> nodes, T start, T target, IEqualityComparer<T> comparer = null)
{
if (nodes == null) throw new ArgumentNullException(nameof(nodes));
if (comparer == null) comparer = EqualityComparer<T>.Default;
var prefix = new List<T> { start };
return FindPathCore(start, prefix);
IReadOnlyCollection<T> FindPathCore(T current, IReadOnlyCollection<T> pathToCurrent)
{
if (comparer.Equals(current, target))
{
return pathToCurrent;
}
if (nodes.TryGetValue(current, out var connections))
{
foreach (T node in connections.Except(pathToCurrent))
{
var path = new List<T>(pathToCurrent);
path.Add(node);
var result = FindPathCore(node, path);
if (result != null) return result;
}
}
return null;
}
} Test:
var test5 = new Dictionary<string, IReadOnlyCollection<string>>
{
["a"] = new List<string> { "c", "d", "b" },
["b"] = new List<string> { "c", "f", "g", "e" },
["e"] = new List<string> { "g", "h", "k" },
["k"] = new List<string> { "b", "l" },
};
test5.FindPath("a", "d").Dump();
test5.FindPath("a", "f").Dump();
test5.FindPath("a", "k").Dump();
test5.FindPath("k", "x").Dump();
test5.FindPath("k", "g").Dump();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
@RichardDeeming
Thanks, Richard; I don't know how I missed seeing this post until now, but I look forward to studying your code !
cheers, Bill
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
I'm developing an app on multiple PC's. Sometimes at the office, sometimes at home.
I keep having to go into the App.Config and commenting/uncommenting connection strings to point to the PC that I'm on that day.
Is there a better way to do this?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
The SQL instance names are different on each PC, so I have different connection strings
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
That's what I use it for: different connections strings for my Desktop and the WookieTab. Change in one place on each machine (when necessary, I did it when I changed the PC name for the desktop and found out how many different little apps I had accessing SQL the hard way) and it changes in all apps on that machine.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If it's SQL server on your home machine, you can set up an alias so that when it sees the instance name used at work, it actually points to your local instance. You can therefore use the same connection string on both machines.
You set this up using SQL configuration manager
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
Environement.MachineName tells me "who" is running ... and I can build my connections strings (db; web server; web services) accordingly.
If it's one of my "development machines", the app will run in "developer mode" (when activated).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
hi, i'm rezki from Indonesia, i was a student at a university in Indonesia..
i use emgu cv to make a SURF program to image retrieval, and i have a list code like this :
Index fln = new Index(supermatrix, 4);
but then, it gives me an error like this :
Argument 2 : cannot convert from 'int' to 'Emgu.CV,Flann.IIndexparams
can you help me please..
i'm so stressfull to solving this error :'(
thank you so much.. and i'm sorry to take your time
|
|
|
|
|
The Index constructor does not take a simple integer values for the second parameter. See Index Constructor[^] for details.
|
|
|
|
|
|
It's telling you that you can't pass an integer in; instead, you have to pass in something that implements IIndexParams. The default implementations from Emgu are AutotunedIndexParams, CompositeIndexParams, HierarchicalClusteringIndexParams, KdTreeIndexParams, KMeansIndexParams, LinearIndexParams, LshIndexParams and SearchParams. You just need to choose the appropriate one (also, you need to learn how to read a build error; this was all clearly laid out).
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: also, you need to learn how to read a build error; this was all clearly laid out
Yes, but I remember in this galaxy long, long ago, when I was still a padawan, I also had trouble reading those d*mn build errors.
With time, one learns to understand the force and read build errors.
|
|
|
|
|
Hi, thank you so much for your solution 😄..
I will do that sir,thank you..
|
|
|
|
|
how 3tier application works.......??
|
|
|
|
|
|
You forgot the "presentation layer" (PAL?).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
yeah..!! Plz explain tat also..
|
|
|
|
|