public override void Execute(Instance instance) { if ( !(instance is SMInstance)) { this.result = new SMResult("Type mismatch"); return; } SMInstance smi = (SMInstance)instance; int n = smi.Size; int[] mMatch = new int[n]; int[] mProposals = new int[n]; int[] wMatch = new int[n]; Stack<int> unmatched = new Stack<int>(n); for (int i = 0; i < n; i++) { mMatch[i] = -1; unmatched.Push(i); mProposals[i] = 0; wMatch[i] = -1; } while (unmatched.Count > 0) { int m = unmatched.Pop(); int w = smi.MPrefMatrix[m][mProposals[m]]; mProposals[m]++; if (wMatch[w] == -1) { mMatch[m] = w; wMatch[w] = m; } else { int m1 = wMatch[w]; if (smi.WRankMatrix[w][m] < smi.WRankMatrix[w][m1]) { mMatch[m] = w; wMatch[w] = m; mMatch[m1] = -1; unmatched.Push(m1); } else { unmatched.Push(m); } } } this.result = new SMResult(mMatch, wMatch, smi); }
public static IEnumerable<Vertex> DepthFirstGraphTraversal( this IGraph graph, Vertex source) { Stack<Vertex> toVisit = new Stack<Vertex>(); toVisit.Push(source); while (toVisit.Count > 0) { Vertex v = toVisit.Pop(); yield return v; foreach (Vertex w in graph.Neighbors(v)) { toVisit.Push(w); } } }
public static IEnumerable<Vertex> PreOrderTreeTraversal( this IGraph graph, Vertex source) { Stack<Vertex> toVisit = new Stack<Vertex>(); HashSet<Vertex> visited = new HashSet<Vertex>(); toVisit.Push(source); visited.Add(source); while (toVisit.Count > 0) { Vertex v = toVisit.Pop(); yield return v; foreach (Vertex w in graph.Neighbors(v)) { if (!visited.Contains(w)) { toVisit.Push(w); visited.Add(w); } } } }
public void PrintInOrderNonRecursive() { if (root == null) return; Console.WriteLine("In-order traversal (non recursive):"); Stack<Node> st = new Stack<Node>(); // add the whole left-most path to stack Node p = root; while (p != null) { st.Push(p); p = p.left; } while (st.Count > 0) { Node top = st.Pop(); Console.Write("{0}, ", top.val); if (top.right != null) { // add another left-most path p = top.right; while (p != null) { st.Push(p); p = p.left; } } } Console.WriteLine(); }