public static T[] PatienceSort(T[] input, int start, int length) { CheckArguments(input, start, length); List <Stack <T> > stacks = new List <Stack <T> >(); for (int i = start; i < start + length; i++) { Stack <T> stack = new Stack <T>(); stack.Push(input[i]); int index = stacks.BinarySearch(stack); if (index < 0) { index = ~index; } if (index != stacks.Count) { stacks[index].Push(input[i]); } else { stacks.Add(stack); } } DMinHeap <T, Stack <T> > heap = new DMinHeap <T, Stack <T> >(3); foreach (Stack <T> s in stacks) { heap.Add(s, s.Peek()); } for (int i = start; i < start + length; i++) { Stack <T> stack = heap.Extract(); input[i] = stack.Pop(); if (stack.Count != 0) { heap.Add(stack, stack.Peek()); } } return(input); }
private void DijkstraInit(uint from, Dictionary<uint, int> dist, DMinHeap<int, uint> fringe) { foreach (uint v in this.Vertices.Keys) { if (v == from) dist[v] = 0; else dist[v] = int.MaxValue; fringe.Add(v, dist[v]); } }
/// <summary> /// Returns <code>true</code> if there is a path between two given nodes in the given, weighted graph; <code>false</code> otherwise. /// </summary> /// <param name="graph">The graph to find the path in.</param> /// <param name="start">The start of the path.</param> /// <param name="end">The end of the path.</param> /// <param name="path">A list containing the edges of the path that was found, in order. Empty if there is no path.</param> /// <returns><code>true</code> if there is a path; <code>false</code> otherwise.</returns> public static bool ShortestPath(IGraph <IWeightedGraphEdge> graph, IGraphNode <IWeightedGraphEdge> start, IGraphNode <IWeightedGraphEdge> end, out List <IWeightedGraphEdge> path) { path = new List <IWeightedGraphEdge>(); DMinHeap <DijkstraNode> open = new DMinHeap <DijkstraNode>(); open.Add(new DijkstraNode(start, null, null)); HashSet <uint> closed = new HashSet <uint>(); closed.Add(start.ID); DijkstraNode node; while (open.Count > 0) { node = open.Extract(); foreach (IWeightedGraphEdge e in node.Node.Neighbours) { if (closed.Contains(e.To)) { continue; } else if (e.To == end.ID) { path.Add(e); while (node.Parent != null) { path.Add(node.Edge); node = node.Parent; } path.Reverse(); return(true); } open.Add(new DijkstraNode(graph.Nodes[e.To], e, node)); } } return(false); }
private void DijkstraInit(uint from, Dictionary <uint, int> dist, DMinHeap <int, uint> fringe) { foreach (uint v in this.Vertices.Keys) { if (v == from) { dist[v] = 0; } else { dist[v] = int.MaxValue; } fringe.Add(v, dist[v]); } }