Click here to Skip to main content
15,908,173 members
Home / Discussions / C#
   

C#

 
PraiseRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
J Kaur11-Jan-18 10:52
J Kaur11-Jan-18 10:52 
AnswerRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
Vincent Maverick Durano5-Jan-18 1:59
professionalVincent Maverick Durano5-Jan-18 1:59 
PraiseRe: PetaPoco (T4 Template) in .Net Core 2.0 for MSql : - SerializationException in generating Database.cs Pin
J Kaur11-Jan-18 10:51
J Kaur11-Jan-18 10:51 
QuestionAdding Items in a Class Pin
Member 136074754-Jan-18 12:04
Member 136074754-Jan-18 12:04 
AnswerRe: Adding Items in a Class Pin
Mycroft Holmes4-Jan-18 12:41
professionalMycroft Holmes4-Jan-18 12:41 
AnswerRe: Adding Items in a Class Pin
BillWoodruff4-Jan-18 21:27
professionalBillWoodruff4-Jan-18 21:27 
AnswerRe: Adding Items in a Class Pin
OriginalGriff4-Jan-18 21:39
mveOriginalGriff4-Jan-18 21:39 
Questionlistview to combobox - HELP Pin
Member 136074164-Jan-18 10:31
Member 136074164-Jan-18 10:31 
QuestionRe: listview to combobox - HELP Pin
Gerry Schmitz4-Jan-18 10:40
mveGerry Schmitz4-Jan-18 10:40 
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 
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 

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.