public double calc(int amount, string fromUnit, string toUnit) { var from = (Units) Enum.Parse(typeof (Units), fromUnit.Replace("in", "inch")); var to = (Units) Enum.Parse(typeof (Units), toUnit.Replace("in", "inch")); var dgraf = new Graf<Units>(); var ftVertex = new Vertex<Units>(Units.ft); var inVertex = new Vertex<Units>(Units.inch); var ydVertex = new Vertex<Units>(Units.yd); var miVertex = new Vertex<Units>(Units.mi); ftVertex.AddDirectedEdge(12, inVertex); inVertex.AddDirectedEdge(1D / 12, ftVertex); ydVertex.AddDirectedEdge(3, ftVertex); ftVertex.AddDirectedEdge(1D / 3, ydVertex); miVertex.AddDirectedEdge(1760, ydVertex); ydVertex.AddDirectedEdge(1D / 1760, miVertex); dgraf.AddVertex(ftVertex); dgraf.AddVertex(inVertex); dgraf.AddVertex(ydVertex); dgraf.AddVertex(miVertex); var source = dgraf.Vertexes.FirstOrDefault(vertex => vertex.Key == from); var target = dgraf.Vertexes.FirstOrDefault(vertex => vertex.Key == to); var vertexPath = dgraf.GetBreadthPath(source, target).ToList(); var edgePath = new System.Collections.Generic.Stack<Edge<Units>>(); if (target.Color != Vertex<Units>.VertexColor.Black) return 0; var currentVertex = target; while (currentVertex != null && currentVertex != source && currentVertex.Parent != null) { var currentEdge = currentVertex.Parent.Edges.FirstOrDefault(edge => edge.Target == currentVertex); edgePath.Push(currentEdge); currentVertex = currentVertex.Parent; } if (source == target) return amount; if (!edgePath.Any()) return 0; double res = amount; double weigth = 1; foreach (var pathEdge in edgePath) weigth *= pathEdge.Weigth; return res * weigth; }