public void Search2(IDirectedGraph <TVertex, TEdge> graph, IEnumerable <TVertex> roots) { Initialize(graph); Stack <SearchFrame> searchStack = new Stack <SearchFrame>(); if (roots == null || roots.Count() == 0) { roots = graph.Vertices.Where(v => graph.InEdges(v).Count() == 0); } foreach (var root in roots) { vertexColors[root] = VertexColor.Gray; OnDiscoverVertex(root); searchStack.Push(new SearchFrame(root, graph.OutEdges(root).GetEnumerator())); while (searchStack.Count > 0) { var currentFrame = searchStack.Pop(); var u = currentFrame.Vertex; var edges = currentFrame.Edges; while (edges.MoveNext()) { TEdge e = edges.Current; OnExamineEdge(e); TVertex v = e.Target; if (vertexColors[v] == VertexColor.White) { searchStack.Push(new SearchFrame(u, edges)); u = v; vertexColors[u] = VertexColor.Gray; OnDiscoverVertex(u); edges = graph.OutEdges(u).GetEnumerator(); } } if (edges != null) { edges.Dispose(); } vertexColors[u] = VertexColor.Black; OnFinishVertex(u); } } }
/// <summary> /// Dijsktra algoritam za trazenje najmanjeg rastojanja i odgovarajucih /// putanja. Graf mora imati sve pozitivne tezine grana ali moze da bude neusmeren /// i/ili da sadrzi cikluse (petlje). /// </summary> /// <param name="start"></param> public void Dijkstra_shortest_path(IDirectedGraph <TVertex, TEdge> graph, TVertex start) { //Inicijalizujemo kolekcije Initialize(graph, start); minQueue.Clear(); minQueue.Insert(start); while (minQueue.Count > 0) { TVertex u = minQueue.Remove(); if (vertexColors[u] == VertexColor.Black) { continue; } vertexColors[u] = VertexColor.Black; OnExamineVertex(u); foreach (TEdge edge in graph.OutEdges(u)) { if (RelaxEdge(edge)) { vertexColors[edge.Target] = VertexColor.Gray; minQueue.Insert(edge.Target); OnEdgeRelaxed(edge); } } } }
private void Visit( IDirectedGraph <TVertex, TEdge> graph, TVertex vertex, SearchState <TVertex, TEdge> state, Queue <TVertex> output) { if (state.HasPermanentMark(vertex)) { return; } if (state.HasTemporaryMark(vertex)) { throw new CyclicGraphsNotSupportedException(); } state.SetTemporaryMark(vertex); foreach (var connectedVertex in graph.OutEdges(vertex).Select(e => e.To)) { this.Visit(graph, connectedVertex, state, output); } state.RemoveTemporaryMark(vertex); state.SetPermanentMark(vertex); output.Enqueue(vertex); }
public TraversalResult <TVertex> Traverse(IDirectedGraph <TVertex, TEdge> graph, TVertex sourceVertex, TVertex targetVertex) { var comparer = graph.VertexComparer; var current = sourceVertex; var distance = new Dictionary <TVertex, double>(comparer) { { sourceVertex, 0D } }; // Tracks the vertex that gave the shortest hop to another vertex (key: vertex, value: previous vertex) var previousBestHop = new Dictionary <TVertex, TVertex>(comparer); // The remaining vertices that haven't been considered as part of the traversal yet var remainingVertices = graph.AllVertices.Select(v => v.Vertex).ToHashSet(); while (remainingVertices.Count > 0) { var next = GetNextVertex(distance, remainingVertices); if (next == null) { return(TraversalResult <TVertex> .Failure); } var(nextVertex, nextVertexDistance) = next.Value; if (comparer.Equals(nextVertex, targetVertex)) { return(TraversalResult <TVertex> .Complete(BuildResultPath(comparer, sourceVertex, targetVertex, previousBestHop))); } remainingVertices.Remove(nextVertex); var neighborEdges = graph.OutEdges(nextVertex) .Where(v => remainingVertices.Contains(v.To)); foreach (var neighborEdge in neighborEdges) { var alt = nextVertexDistance + neighborEdge.Weight; if (!distance.TryGetValue(neighborEdge.To, out var neighborDistance) || alt < neighborDistance) { distance[neighborEdge.To] = alt; previousBestHop[neighborEdge.To] = nextVertex; } } } // Shouldn't get here as we should either run out of edges to traverse, or we should reach the target return(TraversalResult <TVertex> .Failure); }
public void Search(IDirectedGraph <TVertex, TEdge> graph, IEnumerable <TVertex> roots) { Initialize(graph); Stack <TVertex> stack = new Stack <TVertex>(); if (roots == null || roots.Count() == 0) { roots = graph.Vertices.Where(v => graph.InEdges(v).Count() == 0); } foreach (var root in roots) { stack.Push(root); while (stack.Count > 0) { TVertex current = stack.Peek(); vertexColors[current] = VertexColor.Gray; OnDiscoverVertex(current); foreach (TEdge e in graph.OutEdges(current)) { OnExamineEdge(e); TVertex v = e.Target; if (vertexColors[v] == VertexColor.White) { stack.Push(v); } } //Zavrsavamo sa nod-om, posto je moguce da postoji vise instanci istog noda //koje nisu beli nodovi potrebno ih je izbaciti sa steka. while (stack.Count > 0 && vertexColors[stack.Peek()] != VertexColor.White) { current = stack.Pop(); if (vertexColors[current] == VertexColor.Gray) { vertexColors[current] = VertexColor.Black; OnFinishVertex(current); } } } } }
protected void SearchInternal(TVertex u) { vertexColors[u] = VertexColor.Gray; OnDiscoverVertex(u); foreach (TEdge e in graph.OutEdges(u)) { OnExamineEdge(e); TVertex v = e.Target; if (vertexColors[v] == VertexColor.White) { SearchInternal(v); } } vertexColors[u] = VertexColor.Black; OnFinishVertex(u); }
/// <summary> /// Efikasna implementacija algoritma za trazenje najkraceg puta u slucaju kada /// je graf usmeren i bez ciklusa. Sa druge strane graf moze da sadrzi negativne /// tezine grana. /// </summary> /// <param name="graph"></param> /// <param name="start"></param> public void DAG_shortest_path(IDirectedGraph <TVertex, TEdge> graph, TVertex start) { IEnumerable <TVertex> topoList = topologicalSort.Search(graph, start); Initialize(graph, start); foreach (TVertex u in topoList) { vertexColors[u] = VertexColor.Black; OnExamineVertex(u); foreach (TEdge edge in graph.OutEdges(u)) { if (RelaxEdge(edge)) { vertexColors[edge.Target] = VertexColor.Gray; OnEdgeRelaxed(edge); } } } }
public void Search(IDirectedGraph <TVertex, TEdge> graph, IEnumerable <TVertex> roots = null) { Initialize(graph); if (roots == null || roots.Count() == 0) { roots = graph.Vertices.Where(v => graph.InEdges(v).Count() == 0); } foreach (var root in roots) { vertexColors[root] = VertexColor.Gray; OnDiscoverVertex(root); } Queue <TVertex> queue = new Queue <TVertex>(roots); while (queue.Count > 0) { TVertex u = queue.Dequeue(); OnExamineVertex(u); foreach (TEdge e in graph.OutEdges(u)) { TVertex v = e.Target; OnExamineEdge(e); if (vertexColors[v] == VertexColor.White) { vertexColors[v] = VertexColor.Gray; OnDiscoverVertex(v); queue.Enqueue(v); } } vertexColors[u] = VertexColor.Black; OnFinishVertex(u); } }