Click here to Skip to main content
15,881,588 members
Home / Discussions / C#
   

C#

 
AnswerRe: listview to combobox - HELP Pin
Member 136074165-Jan-18 12:32
Member 136074165-Jan-18 12:32 
GeneralRe: listview to combobox - HELP Pin
Gerry Schmitz6-Jan-18 6:28
mveGerry Schmitz6-Jan-18 6:28 
GeneralRe: listview to combobox - HELP Pin
Member 136074167-Jan-18 3:59
Member 136074167-Jan-18 3:59 
GeneralRe: listview to combobox - HELP Pin
Gerry Schmitz7-Jan-18 7:10
mveGerry Schmitz7-Jan-18 7:10 
AnswerRe: listview to combobox - HELP Pin
Mycroft Holmes4-Jan-18 12:44
professionalMycroft Holmes4-Jan-18 12:44 
GeneralRe: listview to combobox - HELP Pin
Member 136074165-Jan-18 12:26
Member 136074165-Jan-18 12:26 
GeneralRe: listview to combobox - HELP Pin
Mycroft Holmes6-Jan-18 10:27
professionalMycroft Holmes6-Jan-18 10:27 
Questionimprove a recursive search method ? Pin
BillWoodruff4-Jan-18 5:01
professionalBillWoodruff4-Jan-18 5:01 
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 Smile | :) .. 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);

    // check for direct linkage
    if (RDictT1T2.ContainsKey(Start) && RDictT1T2[Start].Contains(Target))
    {
        Result.Add(Target);
        return Result;
    }

    // recursion time
    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"); // a d

var r2 = test5.FindPath("a", "f"); // a b f

var r3 = test5.FindPath("a", "k"); // a b e k

var r4 = test5.FindPath("k", "x"); // null

var r5 = test5.FindPath("k", "g"); // k b 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)

AnswerRe: improve a recursive search method ? Pin
Gerry Schmitz4-Jan-18 10:02
mveGerry Schmitz4-Jan-18 10:02 
GeneralRe: improve a recursive search method ? Pin
BillWoodruff4-Jan-18 12:24
professionalBillWoodruff4-Jan-18 12:24 
GeneralMessage Closed Pin
4-Jan-18 13:05
mveGerry Schmitz4-Jan-18 13:05 
GeneralMessage Closed Pin
4-Jan-18 20:10
professionalBillWoodruff4-Jan-18 20:10 
GeneralRe: improve a recursive search method ? Pin
Gerry Schmitz4-Jan-18 20:27
mveGerry Schmitz4-Jan-18 20:27 
AnswerRe: improve a recursive search method ? Pin
Richard Deeming8-Jan-18 5:55
mveRichard Deeming8-Jan-18 5:55 
GeneralRe: improve a recursive search method ? Pin
BillWoodruff15-Jan-18 6:01
professionalBillWoodruff15-Jan-18 6:01 
QuestionManaging Connection Strings From App.Config Pin
Kevin Marois4-Jan-18 4:18
professionalKevin Marois4-Jan-18 4:18 
AnswerRe: Managing Connection Strings From App.Config Pin
OriginalGriff4-Jan-18 4:40
mveOriginalGriff4-Jan-18 4:40 
GeneralRe: Managing Connection Strings From App.Config Pin
Kevin Marois4-Jan-18 4:44
professionalKevin Marois4-Jan-18 4:44 
GeneralRe: Managing Connection Strings From App.Config Pin
OriginalGriff4-Jan-18 4:59
mveOriginalGriff4-Jan-18 4:59 
GeneralRe: Managing Connection Strings From App.Config Pin
Chris Quinn4-Jan-18 20:58
Chris Quinn4-Jan-18 20:58 
AnswerRe: Managing Connection Strings From App.Config Pin
Gerry Schmitz4-Jan-18 10:09
mveGerry Schmitz4-Jan-18 10:09 
QuestionSURF and flann to image retrieval in emgu CV Pin
Member 136054434-Jan-18 3:05
Member 136054434-Jan-18 3:05 
AnswerRe: SURF and flann to image retrieval in emgu CV Pin
Richard MacCutchan4-Jan-18 3:37
mveRichard MacCutchan4-Jan-18 3:37 
GeneralRe: SURF and flann to image retrieval in emgu CV Pin
Member 136054437-Jan-18 22:37
Member 136054437-Jan-18 22:37 
AnswerRe: SURF and flann to image retrieval in emgu CV Pin
Pete O'Hanlon4-Jan-18 5:20
mvePete O'Hanlon4-Jan-18 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.