public void Search(int startVertexIndex) { DeclareArrays(); Weights[startVertexIndex] = 0; List <DijkstraVertex> dijkstraVertices = new List <DijkstraVertex> { new DijkstraVertex(startVertexIndex, 0) }; while (dijkstraVertices.Count != 0) { dijkstraVertices = dijkstraVertices.OrderBy(x => x.Weight).ToList(); int currentVertexIndex = dijkstraVertices[0].Index; dijkstraVertices.RemoveAt(0); for (int i = 0; i < Graph.AdjacencyList.Vertices[currentVertexIndex].Count; i++) { int neighborIndex = Graph.AdjacencyList.Vertices[currentVertexIndex][i]; if (Visited[neighborIndex]) { continue; } int edgeToNeighbor = Graph.AdjacencyList.Edges[currentVertexIndex][i]; double weightToNeighbor = Graph.EdgesWeights[edgeToNeighbor]; if (Weights[neighborIndex] > Weights[currentVertexIndex] + weightToNeighbor) { Weights[neighborIndex] = Weights[currentVertexIndex] + weightToNeighbor; PreviousArray[neighborIndex] = currentVertexIndex; DijkstraVertex newDijkstraVertex = new DijkstraVertex(neighborIndex, Weights[neighborIndex]); if (!dijkstraVertices.Contains(newDijkstraVertex)) { dijkstraVertices.Add(newDijkstraVertex); } else { DijkstraVertex oldDijkstraVertex = dijkstraVertices.First(p => p.Index == neighborIndex); oldDijkstraVertex.Weight = Weights[neighborIndex]; } } } Visited[currentVertexIndex] = true; VisitedVertices.Add(Graph.Vertices[currentVertexIndex]); } }
public Path GetShortestPath(int startVertexIndex, int endVertexIndex) { if (startVertexIndex == endVertexIndex) { throw new ArgumentException("Start Vertex should be different from End Vertex"); } DeclareArrays(); Weights[startVertexIndex] = 0; List <DijkstraVertex> dijkstraVertices = new List <DijkstraVertex> { new DijkstraVertex(startVertexIndex, 0) }; bool foundEnd = false; while (dijkstraVertices.Count != 0 && !foundEnd) { dijkstraVertices = dijkstraVertices.OrderBy(x => x.Weight).ToList(); int currentVertexIndex = dijkstraVertices[0].Index; dijkstraVertices.RemoveAt(0); for (int i = 0; i < Graph.AdjacencyList.Vertices[currentVertexIndex].Count; i++) { int neighborIndex = Graph.AdjacencyList.Vertices[currentVertexIndex][i]; if (Visited[neighborIndex]) { continue; } int edgeToNeighbor = Graph.AdjacencyList.Edges[currentVertexIndex][i]; double weightToNeighbor = Graph.EdgesWeights[edgeToNeighbor]; if (Weights[neighborIndex] > Weights[currentVertexIndex] + weightToNeighbor) { Weights[neighborIndex] = Weights[currentVertexIndex] + weightToNeighbor; PreviousArray[neighborIndex] = currentVertexIndex; PreviousEdgeArray[neighborIndex] = edgeToNeighbor; DijkstraVertex newDijkstraVertex = new DijkstraVertex(neighborIndex, Weights[neighborIndex]); if (!dijkstraVertices.Contains(newDijkstraVertex)) { dijkstraVertices.Add(newDijkstraVertex); } else { DijkstraVertex oldDijkstraVertex = dijkstraVertices.First(p => p.Index == neighborIndex); oldDijkstraVertex.Weight = Weights[neighborIndex]; } } } Visited[currentVertexIndex] = true; VisitedVertices.Add(Graph.Vertices[currentVertexIndex]); if (currentVertexIndex == endVertexIndex) { foundEnd = true; } } if (foundEnd) { Path shortestPath = new Path(startVertexIndex, endVertexIndex, Graph, PreviousArray, PreviousEdgeArray); return(shortestPath); } throw new ArgumentException("Couldn't find a correct path between those vertices"); }