Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 564935689ab5527f955e5449ded02799 > files > 361

rust-doc-1.19.0-1.mga6.armv7hl.rpm

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="rustdoc">
    <meta name="description" content="API documentation for the Rust `binary_heap` mod in crate `collections`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, binary_heap">

    <title>collections::binary_heap - Rust</title>

    <link rel="stylesheet" type="text/css" href="../../normalize.css">
    <link rel="stylesheet" type="text/css" href="../../rustdoc.css">
    <link rel="stylesheet" type="text/css" href="../../main.css">
    

    <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico">
    
</head>
<body class="rustdoc mod">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    

    <nav class="sidebar">
        <a href='../../collections/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
        <p class='location'>Module binary_heap</p><div class="block items"><ul><li><a href="#structs">Structs</a></li></ul></div><p class='location'><a href='../index.html'>collections</a></p><script>window.sidebarCurrent = {name: 'binary_heap', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script>
    </nav>

    <nav class="sub">
        <form class="search-form js-only">
            <div class="search-container">
                <input class="search-input" name="search"
                       autocomplete="off"
                       placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
                       type="search">
            </div>
        </form>
    </nav>

    <section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Module <a href='../index.html'>collections</a>::<wbr><a class="mod" href=''>binary_heap</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'>
                   <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
                       [<span class='inner'>&#x2212;</span>]
                   </a>
               </span><a class='srclink' href='../../src/collections/binary_heap.rs.html#11-1236' title='goto source code'>[src]</a></span></h1>
<div class='docblock'><p>A priority queue implemented with a binary heap.</p>

<p>Insertion and popping the largest element have <code>O(log n)</code> time complexity.
Checking the largest element is <code>O(1)</code>. Converting a vector to a binary heap
can be done in-place, and has <code>O(n)</code> complexity. A binary heap can also be
converted to a sorted vector in-place, allowing it to be used for an <code>O(n log n)</code> in-place heapsort.</p>

<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<p>This is a larger example that implements <a href="http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm">Dijkstra&#39;s algorithm</a>
to solve the <a href="http://en.wikipedia.org/wiki/Shortest_path_problem">shortest path problem</a> on a <a href="http://en.wikipedia.org/wiki/Directed_graph">directed graph</a>.
It shows how to use <a href="struct.BinaryHeap.html"><code>BinaryHeap</code></a> with custom types.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">cmp</span>::<span class="ident">Ordering</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">collections</span>::<span class="ident">BinaryHeap</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">usize</span>;

<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Copy</span>, <span class="ident">Clone</span>, <span class="ident">Eq</span>, <span class="ident">PartialEq</span>)]</span>
<span class="kw">struct</span> <span class="ident">State</span> {
    <span class="ident">cost</span>: <span class="ident">usize</span>,
    <span class="ident">position</span>: <span class="ident">usize</span>,
}

<span class="comment">// The priority queue depends on `Ord`.</span>
<span class="comment">// Explicitly implement the trait so the queue becomes a min-heap</span>
<span class="comment">// instead of a max-heap.</span>
<span class="kw">impl</span> <span class="ident">Ord</span> <span class="kw">for</span> <span class="ident">State</span> {
    <span class="kw">fn</span> <span class="ident">cmp</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">other</span>: <span class="kw-2">&amp;</span><span class="ident">State</span>) <span class="op">-&gt;</span> <span class="ident">Ordering</span> {
        <span class="comment">// Notice that the we flip the ordering on costs.</span>
        <span class="comment">// In case of a tie we compare positions - this step is necessary</span>
        <span class="comment">// to make implementations of `PartialEq` and `Ord` consistent.</span>
        <span class="ident">other</span>.<span class="ident">cost</span>.<span class="ident">cmp</span>(<span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">cost</span>)
            .<span class="ident">then_with</span>(<span class="op">||</span> <span class="self">self</span>.<span class="ident">position</span>.<span class="ident">cmp</span>(<span class="kw-2">&amp;</span><span class="ident">other</span>.<span class="ident">position</span>))
    }
}

<span class="comment">// `PartialOrd` needs to be implemented as well.</span>
<span class="kw">impl</span> <span class="ident">PartialOrd</span> <span class="kw">for</span> <span class="ident">State</span> {
    <span class="kw">fn</span> <span class="ident">partial_cmp</span>(<span class="kw-2">&amp;</span><span class="self">self</span>, <span class="ident">other</span>: <span class="kw-2">&amp;</span><span class="ident">State</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">Ordering</span><span class="op">&gt;</span> {
        <span class="prelude-val">Some</span>(<span class="self">self</span>.<span class="ident">cmp</span>(<span class="ident">other</span>))
    }
}

<span class="comment">// Each node is represented as an `usize`, for a shorter implementation.</span>
<span class="kw">struct</span> <span class="ident">Edge</span> {
    <span class="ident">node</span>: <span class="ident">usize</span>,
    <span class="ident">cost</span>: <span class="ident">usize</span>,
}

<span class="comment">// Dijkstra&#39;s shortest path algorithm.</span>

<span class="comment">// Start at `start` and use `dist` to track the current shortest distance</span>
<span class="comment">// to each node. This implementation isn&#39;t memory-efficient as it may leave duplicate</span>
<span class="comment">// nodes in the queue. It also uses `usize::MAX` as a sentinel value,</span>
<span class="comment">// for a simpler implementation.</span>
<span class="kw">fn</span> <span class="ident">shortest_path</span>(<span class="ident">adj_list</span>: <span class="kw-2">&amp;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">Edge</span><span class="op">&gt;&gt;</span>, <span class="ident">start</span>: <span class="ident">usize</span>, <span class="ident">goal</span>: <span class="ident">usize</span>) <span class="op">-&gt;</span> <span class="prelude-ty">Option</span><span class="op">&lt;</span><span class="ident">usize</span><span class="op">&gt;</span> {
    <span class="comment">// dist[node] = current shortest distance from `start` to `node`</span>
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">dist</span>: <span class="ident">Vec</span><span class="op">&lt;</span>_<span class="op">&gt;</span> <span class="op">=</span> (<span class="number">0</span>..<span class="ident">adj_list</span>.<span class="ident">len</span>()).<span class="ident">map</span>(<span class="op">|</span>_<span class="op">|</span> <span class="ident">usize</span>::<span class="ident">MAX</span>).<span class="ident">collect</span>();

    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">heap</span> <span class="op">=</span> <span class="ident">BinaryHeap</span>::<span class="ident">new</span>();

    <span class="comment">// We&#39;re at `start`, with a zero cost</span>
    <span class="ident">dist</span>[<span class="ident">start</span>] <span class="op">=</span> <span class="number">0</span>;
    <span class="ident">heap</span>.<span class="ident">push</span>(<span class="ident">State</span> { <span class="ident">cost</span>: <span class="number">0</span>, <span class="ident">position</span>: <span class="ident">start</span> });

    <span class="comment">// Examine the frontier with lower cost nodes first (min-heap)</span>
    <span class="kw">while</span> <span class="kw">let</span> <span class="prelude-val">Some</span>(<span class="ident">State</span> { <span class="ident">cost</span>, <span class="ident">position</span> }) <span class="op">=</span> <span class="ident">heap</span>.<span class="ident">pop</span>() {
        <span class="comment">// Alternatively we could have continued to find all shortest paths</span>
        <span class="kw">if</span> <span class="ident">position</span> <span class="op">==</span> <span class="ident">goal</span> { <span class="kw">return</span> <span class="prelude-val">Some</span>(<span class="ident">cost</span>); }

        <span class="comment">// Important as we may have already found a better way</span>
        <span class="kw">if</span> <span class="ident">cost</span> <span class="op">&gt;</span> <span class="ident">dist</span>[<span class="ident">position</span>] { <span class="kw">continue</span>; }

        <span class="comment">// For each node we can reach, see if we can find a way with</span>
        <span class="comment">// a lower cost going through this node</span>
        <span class="kw">for</span> <span class="ident">edge</span> <span class="kw">in</span> <span class="kw-2">&amp;</span><span class="ident">adj_list</span>[<span class="ident">position</span>] {
            <span class="kw">let</span> <span class="ident">next</span> <span class="op">=</span> <span class="ident">State</span> { <span class="ident">cost</span>: <span class="ident">cost</span> <span class="op">+</span> <span class="ident">edge</span>.<span class="ident">cost</span>, <span class="ident">position</span>: <span class="ident">edge</span>.<span class="ident">node</span> };

            <span class="comment">// If so, add it to the frontier and continue</span>
            <span class="kw">if</span> <span class="ident">next</span>.<span class="ident">cost</span> <span class="op">&lt;</span> <span class="ident">dist</span>[<span class="ident">next</span>.<span class="ident">position</span>] {
                <span class="ident">heap</span>.<span class="ident">push</span>(<span class="ident">next</span>);
                <span class="comment">// Relaxation, we have now found a better way</span>
                <span class="ident">dist</span>[<span class="ident">next</span>.<span class="ident">position</span>] <span class="op">=</span> <span class="ident">next</span>.<span class="ident">cost</span>;
            }
        }
    }

    <span class="comment">// Goal not reachable</span>
    <span class="prelude-val">None</span>
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="comment">// This is the directed graph we&#39;re going to use.</span>
    <span class="comment">// The node numbers correspond to the different states,</span>
    <span class="comment">// and the edge weights symbolize the cost of moving</span>
    <span class="comment">// from one node to another.</span>
    <span class="comment">// Note that the edges are one-way.</span>
    <span class="comment">//</span>
    <span class="comment">//                  7</span>
    <span class="comment">//          +-----------------+</span>
    <span class="comment">//          |                 |</span>
    <span class="comment">//          v   1        2    |  2</span>
    <span class="comment">//          0 -----&gt; 1 -----&gt; 3 ---&gt; 4</span>
    <span class="comment">//          |        ^        ^      ^</span>
    <span class="comment">//          |        | 1      |      |</span>
    <span class="comment">//          |        |        | 3    | 1</span>
    <span class="comment">//          +------&gt; 2 -------+      |</span>
    <span class="comment">//           10      |               |</span>
    <span class="comment">//                   +---------------+</span>
    <span class="comment">//</span>
    <span class="comment">// The graph is represented as an adjacency list where each index,</span>
    <span class="comment">// corresponding to a node value, has a list of outgoing edges.</span>
    <span class="comment">// Chosen for its efficiency.</span>
    <span class="kw">let</span> <span class="ident">graph</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[
        <span class="comment">// Node 0</span>
        <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">2</span>, <span class="ident">cost</span>: <span class="number">10</span> },
             <span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">1</span>, <span class="ident">cost</span>: <span class="number">1</span> }],
        <span class="comment">// Node 1</span>
        <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">3</span>, <span class="ident">cost</span>: <span class="number">2</span> }],
        <span class="comment">// Node 2</span>
        <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">1</span>, <span class="ident">cost</span>: <span class="number">1</span> },
             <span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">3</span>, <span class="ident">cost</span>: <span class="number">3</span> },
             <span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">4</span>, <span class="ident">cost</span>: <span class="number">1</span> }],
        <span class="comment">// Node 3</span>
        <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">0</span>, <span class="ident">cost</span>: <span class="number">7</span> },
             <span class="ident">Edge</span> { <span class="ident">node</span>: <span class="number">4</span>, <span class="ident">cost</span>: <span class="number">2</span> }],
        <span class="comment">// Node 4</span>
        <span class="macro">vec</span><span class="macro">!</span>[]];

    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">shortest_path</span>(<span class="kw-2">&amp;</span><span class="ident">graph</span>, <span class="number">0</span>, <span class="number">1</span>), <span class="prelude-val">Some</span>(<span class="number">1</span>));
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">shortest_path</span>(<span class="kw-2">&amp;</span><span class="ident">graph</span>, <span class="number">0</span>, <span class="number">3</span>), <span class="prelude-val">Some</span>(<span class="number">3</span>));
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">shortest_path</span>(<span class="kw-2">&amp;</span><span class="ident">graph</span>, <span class="number">3</span>, <span class="number">0</span>), <span class="prelude-val">Some</span>(<span class="number">7</span>));
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">shortest_path</span>(<span class="kw-2">&amp;</span><span class="ident">graph</span>, <span class="number">0</span>, <span class="number">4</span>), <span class="prelude-val">Some</span>(<span class="number">5</span>));
    <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">shortest_path</span>(<span class="kw-2">&amp;</span><span class="ident">graph</span>, <span class="number">4</span>, <span class="number">0</span>), <span class="prelude-val">None</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=extern%20crate%20collections%3B%0Ause%20std%3A%3Acmp%3A%3AOrdering%3B%0Ause%20std%3A%3Acollections%3A%3ABinaryHeap%3B%0Ause%20std%3A%3Ausize%3B%0A%0A%23%5Bderive(Copy%2C%20Clone%2C%20Eq%2C%20PartialEq)%5D%0Astruct%20State%20%7B%0A%20%20%20%20cost%3A%20usize%2C%0A%20%20%20%20position%3A%20usize%2C%0A%7D%0A%0A%2F%2F%20The%20priority%20queue%20depends%20on%20%60Ord%60.%0A%2F%2F%20Explicitly%20implement%20the%20trait%20so%20the%20queue%20becomes%20a%20min-heap%0A%2F%2F%20instead%20of%20a%20max-heap.%0Aimpl%20Ord%20for%20State%20%7B%0A%20%20%20%20fn%20cmp(%26self%2C%20other%3A%20%26State)%20-%3E%20Ordering%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Notice%20that%20the%20we%20flip%20the%20ordering%20on%20costs.%0A%20%20%20%20%20%20%20%20%2F%2F%20In%20case%20of%20a%20tie%20we%20compare%20positions%20-%20this%20step%20is%20necessary%0A%20%20%20%20%20%20%20%20%2F%2F%20to%20make%20implementations%20of%20%60PartialEq%60%20and%20%60Ord%60%20consistent.%0A%20%20%20%20%20%20%20%20other.cost.cmp(%26self.cost)%0A%20%20%20%20%20%20%20%20%20%20%20%20.then_with(%7C%7C%20self.position.cmp(%26other.position))%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20%60PartialOrd%60%20needs%20to%20be%20implemented%20as%20well.%0Aimpl%20PartialOrd%20for%20State%20%7B%0A%20%20%20%20fn%20partial_cmp(%26self%2C%20other%3A%20%26State)%20-%3E%20Option%3COrdering%3E%20%7B%0A%20%20%20%20%20%20%20%20Some(self.cmp(other))%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20Each%20node%20is%20represented%20as%20an%20%60usize%60%2C%20for%20a%20shorter%20implementation.%0Astruct%20Edge%20%7B%0A%20%20%20%20node%3A%20usize%2C%0A%20%20%20%20cost%3A%20usize%2C%0A%7D%0A%0A%2F%2F%20Dijkstra's%20shortest%20path%20algorithm.%0A%0A%2F%2F%20Start%20at%20%60start%60%20and%20use%20%60dist%60%20to%20track%20the%20current%20shortest%20distance%0A%2F%2F%20to%20each%20node.%20This%20implementation%20isn't%20memory-efficient%20as%20it%20may%20leave%20duplicate%0A%2F%2F%20nodes%20in%20the%20queue.%20It%20also%20uses%20%60usize%3A%3AMAX%60%20as%20a%20sentinel%20value%2C%0A%2F%2F%20for%20a%20simpler%20implementation.%0Afn%20shortest_path(adj_list%3A%20%26Vec%3CVec%3CEdge%3E%3E%2C%20start%3A%20usize%2C%20goal%3A%20usize)%20-%3E%20Option%3Cusize%3E%20%7B%0A%20%20%20%20%2F%2F%20dist%5Bnode%5D%20%3D%20current%20shortest%20distance%20from%20%60start%60%20to%20%60node%60%0A%20%20%20%20let%20mut%20dist%3A%20Vec%3C_%3E%20%3D%20(0..adj_list.len()).map(%7C_%7C%20usize%3A%3AMAX).collect()%3B%0A%0A%20%20%20%20let%20mut%20heap%20%3D%20BinaryHeap%3A%3Anew()%3B%0A%0A%20%20%20%20%2F%2F%20We're%20at%20%60start%60%2C%20with%20a%20zero%20cost%0A%20%20%20%20dist%5Bstart%5D%20%3D%200%3B%0A%20%20%20%20heap.push(State%20%7B%20cost%3A%200%2C%20position%3A%20start%20%7D)%3B%0A%0A%20%20%20%20%2F%2F%20Examine%20the%20frontier%20with%20lower%20cost%20nodes%20first%20(min-heap)%0A%20%20%20%20while%20let%20Some(State%20%7B%20cost%2C%20position%20%7D)%20%3D%20heap.pop()%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Alternatively%20we%20could%20have%20continued%20to%20find%20all%20shortest%20paths%0A%20%20%20%20%20%20%20%20if%20position%20%3D%3D%20goal%20%7B%20return%20Some(cost)%3B%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20Important%20as%20we%20may%20have%20already%20found%20a%20better%20way%0A%20%20%20%20%20%20%20%20if%20cost%20%3E%20dist%5Bposition%5D%20%7B%20continue%3B%20%7D%0A%0A%20%20%20%20%20%20%20%20%2F%2F%20For%20each%20node%20we%20can%20reach%2C%20see%20if%20we%20can%20find%20a%20way%20with%0A%20%20%20%20%20%20%20%20%2F%2F%20a%20lower%20cost%20going%20through%20this%20node%0A%20%20%20%20%20%20%20%20for%20edge%20in%20%26adj_list%5Bposition%5D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20let%20next%20%3D%20State%20%7B%20cost%3A%20cost%20%2B%20edge.cost%2C%20position%3A%20edge.node%20%7D%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20so%2C%20add%20it%20to%20the%20frontier%20and%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20next.cost%20%3C%20dist%5Bnext.position%5D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20heap.push(next)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Relaxation%2C%20we%20have%20now%20found%20a%20better%20way%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bnext.position%5D%20%3D%20next.cost%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20Goal%20not%20reachable%0A%20%20%20%20None%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20%2F%2F%20This%20is%20the%20directed%20graph%20we're%20going%20to%20use.%0A%20%20%20%20%2F%2F%20The%20node%20numbers%20correspond%20to%20the%20different%20states%2C%0A%20%20%20%20%2F%2F%20and%20the%20edge%20weights%20symbolize%20the%20cost%20of%20moving%0A%20%20%20%20%2F%2F%20from%20one%20node%20to%20another.%0A%20%20%20%20%2F%2F%20Note%20that%20the%20edges%20are%20one-way.%0A%20%20%20%20%2F%2F%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%207%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%2B-----------------%2B%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20v%20%20%201%20%20%20%20%20%20%20%202%20%20%20%20%7C%20%202%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%200%20-----%3E%201%20-----%3E%203%20---%3E%204%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%5E%20%20%20%20%20%20%20%20%5E%20%20%20%20%20%20%5E%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%7C%201%20%20%20%20%20%20%7C%20%20%20%20%20%20%7C%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%7C%203%20%20%20%20%7C%201%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%2B------%3E%202%20-------%2B%20%20%20%20%20%20%7C%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%2010%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B---------------%2B%0A%20%20%20%20%2F%2F%0A%20%20%20%20%2F%2F%20The%20graph%20is%20represented%20as%20an%20adjacency%20list%20where%20each%20index%2C%0A%20%20%20%20%2F%2F%20corresponding%20to%20a%20node%20value%2C%20has%20a%20list%20of%20outgoing%20edges.%0A%20%20%20%20%2F%2F%20Chosen%20for%20its%20efficiency.%0A%20%20%20%20let%20graph%20%3D%20vec!%5B%0A%20%20%20%20%20%20%20%20%2F%2F%20Node%200%0A%20%20%20%20%20%20%20%20vec!%5BEdge%20%7B%20node%3A%202%2C%20cost%3A%2010%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Edge%20%7B%20node%3A%201%2C%20cost%3A%201%20%7D%5D%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20Node%201%0A%20%20%20%20%20%20%20%20vec!%5BEdge%20%7B%20node%3A%203%2C%20cost%3A%202%20%7D%5D%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20Node%202%0A%20%20%20%20%20%20%20%20vec!%5BEdge%20%7B%20node%3A%201%2C%20cost%3A%201%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Edge%20%7B%20node%3A%203%2C%20cost%3A%203%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Edge%20%7B%20node%3A%204%2C%20cost%3A%201%20%7D%5D%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20Node%203%0A%20%20%20%20%20%20%20%20vec!%5BEdge%20%7B%20node%3A%200%2C%20cost%3A%207%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20Edge%20%7B%20node%3A%204%2C%20cost%3A%202%20%7D%5D%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20Node%204%0A%20%20%20%20%20%20%20%20vec!%5B%5D%5D%3B%0A%0A%20%20%20%20assert_eq!(shortest_path(%26graph%2C%200%2C%201)%2C%20Some(1))%3B%0A%20%20%20%20assert_eq!(shortest_path(%26graph%2C%200%2C%203)%2C%20Some(3))%3B%0A%20%20%20%20assert_eq!(shortest_path(%26graph%2C%203%2C%200)%2C%20Some(7))%3B%0A%20%20%20%20assert_eq!(shortest_path(%26graph%2C%200%2C%204)%2C%20Some(5))%3B%0A%20%20%20%20assert_eq!(shortest_path(%26graph%2C%204%2C%200)%2C%20None)%3B%0A%7D%0A">Run</a></pre>
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
<table>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.BinaryHeap.html"
                                  title='struct collections::binary_heap::BinaryHeap'>BinaryHeap</a></td>
                           <td class='docblock-short'>
                                <p>A priority queue implemented with a binary heap.</p>
                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Drain.html"
                                  title='struct collections::binary_heap::Drain'>Drain</a></td>
                           <td class='docblock-short'>
                                <p>A draining iterator over the elements of a <code>BinaryHeap</code>.</p>
                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.IntoIter.html"
                                  title='struct collections::binary_heap::IntoIter'>IntoIter</a></td>
                           <td class='docblock-short'>
                                <p>An owning iterator over the elements of a <code>BinaryHeap</code>.</p>
                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.Iter.html"
                                  title='struct collections::binary_heap::Iter'>Iter</a></td>
                           <td class='docblock-short'>
                                <p>An iterator over the elements of a <code>BinaryHeap</code>.</p>
                           </td>
                       </tr>
                       <tr class=' module-item'>
                           <td><a class="struct" href="struct.PeekMut.html"
                                  title='struct collections::binary_heap::PeekMut'>PeekMut</a></td>
                           <td class='docblock-short'>
                                <p>Structure wrapping a mutable reference to the greatest item on a
<code>BinaryHeap</code>.</p>
                           </td>
                       </tr>
                       <tr class='unstable module-item'>
                           <td><a class="struct" href="struct.BinaryHeapPlace.html"
                                  title='struct collections::binary_heap::BinaryHeapPlace'>BinaryHeapPlace</a></td>
                           <td class='docblock-short'>
                               [<div class='stab unstable'>Experimental</div>] 
                           </td>
                       </tr></table></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <aside id="help" class="hidden">
        <div>
            <h1 class="hidden">Help</h1>

            <div class="shortcuts">
                <h2>Keyboard Shortcuts</h2>

                <dl>
                    <dt>?</dt>
                    <dd>Show this help dialog</dd>
                    <dt>S</dt>
                    <dd>Focus the search field</dd>
                    <dt>&larrb;</dt>
                    <dd>Move up in search results</dd>
                    <dt>&rarrb;</dt>
                    <dd>Move down in search results</dd>
                    <dt>&#9166;</dt>
                    <dd>Go to active search result</dd>
                    <dt>+</dt>
                    <dd>Collapse/expand all sections</dd>
                </dl>
            </div>

            <div class="infos">
                <h2>Search Tricks</h2>

                <p>
                    Prefix searches with a type followed by a colon (e.g.
                    <code>fn:</code>) to restrict the search to a given type.
                </p>

                <p>
                    Accepted types are: <code>fn</code>, <code>mod</code>,
                    <code>struct</code>, <code>enum</code>,
                    <code>trait</code>, <code>type</code>, <code>macro</code>,
                    and <code>const</code>.
                </p>

                <p>
                    Search functions by type signature (e.g.
                    <code>vec -> usize</code> or <code>* -> vec</code>)
                </p>
            </div>
        </div>
    </aside>

    

    <script>
        window.rootPath = "../../";
        window.currentCrate = "collections";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>