Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have not worked on this kind of problems earlier. So just tried out a simple solution. Check this out.</p> <pre><code>public class PathFinder { private static Dictionary&lt;string, Path&gt; pathsDictionary = new Dictionary&lt;string, Path&gt;(); private static List&lt;Path&gt; newPaths = new List&lt;Path&gt;(); public static Dictionary&lt;string, Path&gt; GetBestPaths(List&lt;Edge&gt; edgesInTree) { foreach (var e in edgesInTree) { SetNewPathsToAdd(e); UpdatePaths(); } return pathsDictionary; } private static void SetNewPathsToAdd(Edge currentEdge) { newPaths.Clear(); newPaths.Add(new Path(new List&lt;Edge&gt; { currentEdge })); if (!pathsDictionary.ContainsKey(currentEdge.PathKey())) { var pathKeys = pathsDictionary.Keys.Where(c =&gt; c.Split(",".ToCharArray())[1] == currentEdge.StartPoint.ToString()).ToList(); pathKeys.ForEach(key =&gt; { var newPath = new Path(pathsDictionary[key].ConnectedEdges); newPath.ConnectedEdges.Add(currentEdge); newPaths.Add(newPath); }); pathKeys = pathsDictionary.Keys.Where(c =&gt; c.Split(",".ToCharArray())[0] == currentEdge.EndPoint.ToString()).ToList(); pathKeys.ForEach(key =&gt; { var newPath = new Path(pathsDictionary[key].ConnectedEdges); newPath.ConnectedEdges.Insert(0, currentEdge); newPaths.Add(newPath); }); } } private static void UpdatePaths() { Path oldPath = null; foreach (Path newPath in newPaths) { if (!pathsDictionary.ContainsKey(newPath.PathKey())) pathsDictionary.Add(newPath.PathKey(), newPath); else { oldPath = pathsDictionary[newPath.PathKey()]; if (newPath.PathWeights &lt; oldPath.PathWeights) pathsDictionary[newPath.PathKey()] = newPath; } } } } public static class Extensions { public static bool IsNullOrEmpty(this IEnumerable&lt;object&gt; collection) { return collection == null || collection.Count() &gt; 0; } public static string PathKey(this ILine line) { return string.Format("{0},{1}", line.StartPoint, line.EndPoint); } } public interface ILine { int StartPoint { get; } int EndPoint { get; } } public class Edge :ILine { public int StartPoint { get; set; } public int EndPoint { get; set; } public Edge(int startPoint, int endPoint) { this.EndPoint = endPoint; this.StartPoint = startPoint; } } public class Path :ILine { private List&lt;Edge&gt; connectedEdges = new List&lt;Edge&gt;(); public Path(List&lt;Edge&gt; edges) { this.connectedEdges = edges; } public int StartPoint { get { return this.IsValid ? this.connectedEdges.First().StartPoint : 0; } } public int EndPoint { get { return this.IsValid ? this.connectedEdges.Last().EndPoint : 0; } } public bool IsValid { get { return this.EdgeCount &gt; 0; } } public int EdgeCount { get { return this.connectedEdges.Count; } } // For now as no weights logics are defined public int PathWeights { get { return this.EdgeCount; } } public List&lt;Edge&gt; ConnectedEdges { get { return this.connectedEdges; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload