1
I’m trying to implement a code to find the best path in a graph. I was able to develop the recursion method and now I’m trying to implement using stack.
The problem is that I get an exception when I try to access . Min(), and this does not happen with recursion.
The rest of the code is working.
If you have any other suggestions, I’d also appreciate
Here is the recursion and stack code
public KeyValuePair<int, List<GrafoNode>> AcharCaminho_recursao(GrafoNode inicio, GrafoNode fim)
{
var visitados = new List<GrafoNode>();
visitados.Add(fim);
return AcharCaminho_recursao(inicio, fim, visitados);
}
protected KeyValuePair<int, List<GrafoNode>> AcharCaminho_recursao(GrafoNode inicio, GrafoNode fim, List<GrafoNode> visitados)
{
var caminho = new List<GrafoNode>();
var caminhos = new SortedList<int, List<GrafoNode>>(); // caminhos possiveis
if (ReferenceEquals(fim, inicio)) // isso VAI ACONTECER por causa da recursão
{
caminho.Add(inicio);
return new KeyValuePair<int, List<GrafoNode>>(0, caminho);
}
foreach (var item in fim.Vizinhos)
{
if (!visitados.Contains(item)) // não pode voltar onde já foi
{
var node = (GrafoNode)item;
visitados.Add(node);
var pair = AcharCaminho_recursao(inicio, node, visitados); // RECURSÃO!!!
if (!pair.Equals(default(KeyValuePair<int, List<GrafoNode>>)))
// se o retorno da recursão não for "vazio"
{
pair.Value.Add(fim);
var dist = pair.Key + fim.Custos[node];
caminhos.Add(dist, pair.Value);
}
}
}
if (caminhos.Count > 0)
return caminhos.Min(); // seleciona o menor caminho dos selecionados
else
return default(KeyValuePair<int, List<GrafoNode>>);
}
public KeyValuePair<int, List<GrafoNode>> AcharCaminho_pilha(GrafoNode inicio, GrafoNode fim)
{
var caminhos = new SortedList<int, List<GrafoNode>>();
var visitados = new List<GrafoNode>();
var pilha = new Stack<GrafoNode>();
var custo = 0;
pilha.Push(fim);
while (pilha.Count >= 1)
{
var retiraPilha = true;
foreach (var item in pilha.Peek().Vizinhos)
{
if (!visitados.Contains(item))
{
var node = (GrafoNode)item;
visitados.Add(node);
custo += pilha.Peek().Custos[node];
pilha.Push(node);
if (item.Vizinhos.Contains(inicio))
{
pilha.Push(inicio);
custo += node.Custos[inicio];
var lista = pilha.ToList();
caminhos.Add(custo, lista);
pilha.Pop();
}
else
retiraPilha = false;
break;
}
}
if (retiraPilha)
pilha.Pop();
}
if (caminhos.Count > 0)
return caminhos.Min(); // seleciona o menor caminho dos selecionados; A EXCEÇÃO É LANÇADA AQUI
else
return default(KeyValuePair<int, List<GrafoNode>>);
}
Details of the exception:
{"At least one object must implement Icomparable."}
If necessary, I can provide more details on the implementation of the Graphoponderate class.
Grateful!
You can pass the exception details and the stack trace?
– Jéf Bueno
Put inside a
try { } catch (Exception e){ }
to catch what is generating the error.– Marco Souza
@Dotnet is the exact opposite of what one should do
– Maniero
@bigown, I don’t quite understand the opposite. Try { } catch strip ?
– Marco Souza
You mustn’t put this on. This will only hide the exception (which is what the person likes to do, because no one wants to fix anything, just to pretend that there is a mistake), the way that the error is now presented, is much better. Just tell us where it is.
– Maniero
Remember, the line that is error is identical (apparently) to the recursion method (which works)
– Tiago Dall'Oca
@bigown my intention was for him to debug and post the error that is generating.
– Marco Souza
@Dot Net, I already made an edit on the question with the exception message
– Tiago Dall'Oca
@Tiagodall'Oca In the title you pass an error (which is different from what was originally posted), then says that the exception is another. You can’t trust this information. better post the stacktrace complete, perhaps even complementing a print of error.
– Maniero
@Dotnet if you make your suggestion will swallow the error, smooth everything and make debugging difficult
– Maniero
Your Graphonode implement Icomparable
– Marco Souza
@Dotnet but I wanted the ordeal to be done through the
int
that I pass:caminhos.Add(custo, lista);
That is, the ordering would be done through theint custo
. This seems to work in recursion– Tiago Dall'Oca