public void IndexedMinPQDecreaseKeyTest() { // arrange var pq = new IndexMinPQ <string>(array); // act + assert initial Assert.False(pq.IsEmpty); Assert.Equal(10, pq.Size); Assert.Equal(3, pq.Index); Assert.Equal(3, pq.MinIndex); Assert.Equal("best", pq.Min); Assert.Equal("best", pq.TopKey); // act + assert ex try { pq.decreaseKey(int.MaxValue - 1, null); } catch (Exception ex) { Assert.IsType <ArgumentOutOfRangeException>(ex); } try { pq.decreaseKey(10, null); } catch (Exception ex) { Assert.IsType <ArgumentOutOfRangeException>(ex); } // act + assert decrease try { pq.decreaseKey(5, "zzz"); } catch (Exception ex) { Assert.IsType <ArgumentException>(ex); Assert.Equal("Calling decreaseKey() with given argument would not strictly decrease the key", ex.Message); } pq.decreaseKey(5, "aaa"); Assert.Equal("aaa", pq.Min); Assert.Equal("aaa", pq.TopKey); Assert.Equal(5, pq.Index); Assert.Equal(5, pq.MinIndex); }
private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; foreach (Edge e in G.Adj(v)) { int w = e.other(v); if (marked[w]) { continue; // v-w is obsolete edge } if (e.Weight() < distTo[w]) { distTo[w] = e.Weight(); edgeTo[w] = e; if (pq.contains(w)) { pq.decreaseKey(w, distTo[w]); } else { pq.insert(w, distTo[w]); } } } }
// relax edge e and update pq if changed private void relax(Edge e, int v) { int w = e.other(v); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert(w, distTo[w]); } }
// relax edge e and update pq if changed private void relax(DirectedEdge e) { int v = e.from(), w = e.to(); if (distTo[w] > distTo[v] + e.weight()) { distTo[w] = distTo[v] + e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert(w, distTo[w]); } }
// scan vertex v private void scan(EdgeWeightedGraph G, int v) { marked[v] = true; for (Edge e : G.adj(v)) { int w = e.other(v); if (marked[w]) continue; // v-w is obsolete edge if (e.weight() < distTo[w]) { distTo[w] = e.weight(); edgeTo[w] = e; if (pq.contains(w)) pq.decreaseKey(w, distTo[w]); else pq.insert(w, distTo[w]); } } }