Click here to Skip to main content
15,895,554 members
Home / Discussions / C#
   

C#

 
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 
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 
Something like this seems to work:
C#
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:
C#
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(); // a d
test5.FindPath("a", "f").Dump(); // a b f
test5.FindPath("a", "k").Dump(); // a b e k
test5.FindPath("k", "x").Dump(); // null
test5.FindPath("k", "g").Dump(); // k b g




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


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 
GeneralRe: SURF and flann to image retrieval in emgu CV Pin
V.4-Jan-18 18:51
professionalV.4-Jan-18 18:51 
GeneralRe: SURF and flann to image retrieval in emgu CV Pin
Member 136054437-Jan-18 22:36
Member 136054437-Jan-18 22:36 
QuestionBAL,DAL Pin
Member 136064294-Jan-18 0:38
Member 136064294-Jan-18 0:38 
AnswerRe: BAL,DAL Pin
OriginalGriff4-Jan-18 1:00
mveOriginalGriff4-Jan-18 1:00 
AnswerRe: BAL,DAL Pin
Gerry Schmitz4-Jan-18 10:42
mveGerry Schmitz4-Jan-18 10:42 
GeneralRe: BAL,DAL Pin
Member 136064294-Jan-18 19:20
Member 136064294-Jan-18 19: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.