public Path GetShortestPath(int startVertexIndex, int endVertexIndex) { if (startVertexIndex == endVertexIndex) { throw new ArgumentException("Start Vertex should be different from End Vertex"); } DeclareArrays(); Queue <int> queue = new Queue <int>(); queue.Enqueue(startVertexIndex); Visited[startVertexIndex] = true; VisitedVertices.Add(Graph.Vertices[startVertexIndex]); bool foundEnd = false; while (queue.Count != 0 && !foundEnd) { int vertex = queue.Dequeue(); for (var i = 0; i < Graph.AdjacencyList.Vertices[vertex].Count; i++) { int neighbor = Graph.AdjacencyList.Vertices[vertex][i]; int neighborEdge = Graph.AdjacencyList.Edges[vertex][i]; if (Visited[neighbor]) { continue; } queue.Enqueue(neighbor); Visited[neighbor] = true; VisitedVertices.Add(Graph.Vertices[neighbor]); PreviousArray[neighbor] = vertex; PreviousEdgeArray[neighbor] = neighborEdge; if (neighbor == endVertexIndex) { foundEnd = true; break; } } } 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"); }
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]); } }
private void MainLoopForSeparatedConnectedGraph(Queue <int> queue, bool[] visited, int[] previous, int startVertex) { queue.Enqueue(startVertex); visited[startVertex] = true; VisitedVertices.Add(Graph.Vertices[startVertex]); while (queue.Count != 0) { int vertex = queue.Dequeue(); foreach (int neighbor in Graph.AdjacencyList.Vertices[vertex]) { if (visited[neighbor]) { continue; } queue.Enqueue(neighbor); visited[neighbor] = true; VisitedVertices.Add(Graph.Vertices[neighbor]); previous[neighbor] = vertex; } } }
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; double absoluteDistanceToEnd = Graph.Vertices[startVertexIndex].DistanceTo(Graph.Vertices[endVertexIndex]); List <AStarVertex> aStarVertices = new List <AStarVertex> { new AStarVertex(startVertexIndex, 0, absoluteDistanceToEnd) }; bool foundEnd = false; while (aStarVertices.Count != 0 && !foundEnd) { aStarVertices = aStarVertices.OrderBy(x => x.Weight + x.StraightLineDistanceToEnd).ToList(); int currentVertexIndex = aStarVertices[0].Index; aStarVertices.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]; absoluteDistanceToEnd = Graph.Vertices[neighborIndex].DistanceTo(Graph.Vertices[endVertexIndex]); if (Weights[neighborIndex] > Weights[currentVertexIndex] + weightToNeighbor) { Weights[neighborIndex] = Weights[currentVertexIndex] + weightToNeighbor; PreviousArray[neighborIndex] = currentVertexIndex; PreviousEdgeArray[neighborIndex] = edgeToNeighbor; AStarVertex newAStarVertex = new AStarVertex(neighborIndex, Weights[neighborIndex], absoluteDistanceToEnd); if (!aStarVertices.Contains(newAStarVertex)) { aStarVertices.Add(newAStarVertex); } else { AStarVertex oldAStarVertex = aStarVertices.First(p => p.Index == neighborIndex); oldAStarVertex.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"); }