Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a job for <a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm" rel="nofollow">Djikstra's Algorithm</a>. Once you build a representation of your graph it should be easy enough to produce the lowest cost traversal ... since in your case it seems that all paths have an equal cost (1).</p> <p>You can <a href="http://www.codeproject.com/KB/recipes/Shortest_Path_Problem.aspx" rel="nofollow">look here on CodeProject</a> for a reasonably decent implementation of Djikstra in C#.</p> <blockquote> <p>could you present me with a pseudo code of your version of the graph representation for this case?</p> </blockquote> <p>There are multiple ways to represent a graph in such a problem. If the number of vertices in your graph are likely to be small - it would be sufficient to use an <a href="http://en.wikipedia.org/wiki/Adjacency_matrix" rel="nofollow">adjacency matrix</a> for the representation of edges. In which case, you could just use a .NET <a href="http://msdn.microsoft.com/en-us/library/2yd9wwz4%28VS.71%29.aspx" rel="nofollow">multidimensional array</a>. The trick here is that you need to convert vertices labelled with characters to ordinal values. One approach would be to write a wrapper class that maps character codes to indices into the adjacency matrix:</p> <pre><code>class AdjacencyMatrix { // representation of the adjacency matrix (AM) private readonly int[,] m_Matrix; // mapping of character codes to indices in the AM private readonly Dictionary&lt;char,int&gt; m_Mapping; public AdjacencyMatrix( string edgeVector ) { // using LINQ we can identify and order the distinct characters char[] distinctChars = edgeVector.Distinct().OrderBy(x =&gt; x); m_Mapping = distinctChars.Select((c,i)=&gt;new { Vertex = c, Index = i }) .ToDictionary(x=&gt;x.Vertex, x=&gt;x.Index); // build the adjacency cost matrix here... // TODO: I leave this up to the reader to complete ... :-D } // retrieves an entry from within the adjacency matrix given two characters public int this[char v1, char v2] { get { return m_Matrix[m_Mapping[v1], m_Mapping[v2]]; private set { m_Matrix[m_Mapping[v1], m_Mapping[v2]] = value; } } } </code></pre>
    singulars
    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.
 

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