/// <summary> /// /// </summary> /// <param name="vertex"></param> /// <returns></returns> private GraphVertexInfo GetGraphVertexInfo(GraphVertex vertex) { foreach (var item in infos) { if (item.Vertex.Equals(vertex)) { return(item); } } return(null); }
/// <summary> /// /// </summary> /// <param name="startVertex"></param> /// <param name="finishVertex"></param> /// <returns></returns> public string FindShortestPath(GraphVertex startVertex, GraphVertex finishVertex) { InitInfo(); var first = GetGraphVertexInfo(startVertex); first.EdgesWeightSum = 0; var current = FindUnvisitedVertexWithSum(); while (current != null) { SetSumToNextVertex(current); current = FindUnvisitedVertexWithSum(); } return(GetPath(startVertex, finishVertex)); }
/// <summary> /// /// </summary> /// <param name="startVertex"></param> /// <param name="endVertex"></param> /// <returns></returns> private string GetPath(GraphVertex startVertex, GraphVertex endVertex) { try { var path = endVertex.ToString(); double sum = 0; while (startVertex != endVertex) { double weight = 0; string nameOfPrev = endVertex.Name; endVertex = GetGraphVertexInfo(endVertex).PreviousVertex; for (int i = 0; i < endVertex.Edges.Count; ++i) { if (nameOfPrev == endVertex.Edges[i].ConnectedVertex.Name) { weight = endVertex.Edges[i].EdgeWeight; sum += weight; break; } } path = $"{endVertex.ToString()} ({weight}) -> {path}"; } return(path + $"\n\nВес общего пути: {sum}"); } catch (Exception ex) { NotFoundPathExeption exeption = new NotFoundPathExeption { StartVertex = startVertex.Name, EndVertex = endVertex.Name }; throw exeption; } }
/// <summary> /// /// </summary> /// <param name="vertex"></param> /// <param name="weight"></param> public void AddEdge(GraphVertex vertex, double weight) { AddEdge(new GraphEdge(vertex, weight)); }