Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>TLDR: Use sets instead of vectors in this case.</strong> </p> <p>First, to repeat the representation you have chosen: An undirected graph is represented as a map from nodes (represented by numbers) to vectors of nodes. For each edge <em>x–y</em> there exists a map entry from <em>x</em> to a vector containing <em>y</em> and an entry from <em>y</em> to a vector containing <em>x</em>. To remove a node <em>z</em> and all its edges, this invariant has to be maintained by both removing the map entry that has <em>z</em> as its key and by removing <em>z</em> from the vectors of all other entries.</p> <p>The first operation – to remove a map entry given its key – is simple. The map without the entry is given by the expression <code>(dissoc m 5)</code>, where m is the map in your example and 5 is the node you wish to remove from the graph.</p> <p>The second operation – to remove an element from all the vectors that are the values of a map – is a compound operation consisting of 1) do something for all values of a map 2) remove an element from a vector. Assuming 2) is solved (let's call it <code>remove-from-vector</code>), we can do 1) in a number of ways (here are three examples):</p> <pre><code>(into {} (for [[k v] m] [k (remove-from-vector v z)])) (into {} (map (fn [[k v]] [k (remove-from-vector v z)]) m)) (zipmap (keys m) (map #(remove-from-vector % z) (vals m))) </code></pre> <p>Problem 2) raises some questions since there is no built-in Clojure function that removes an element in the middle of a vector. The reason is that it is not something that a vector can do efficiently. Imagine that you have a vector of 10000 elements and you want to remove the one with index 5000. To construct the new vector, 4999 elements has to be conj'ed to the end of the subvector containing indices 0 to 4999. Clearly, we would be better off with a datastructure that handles removal of an arbitrary element in a better way.</p> <p>To address this issue, we can redesign the representation. There is in fact a datastructure that handles removal better: the set. If a set contains 10000 values with evenly distributed hash values, the height of its internal tree is 3. This means that to remove an element of the set requires 3 internal steps rather than something in the order of magnitude of 10000. If we use sets instead of vectors, the example map looks like this:</p> <pre><code>{1 #{2 3}, 2 #{1 4}, 3 #{1 5}, 4 #{2}, 5 #{3}} </code></pre> <p>For sets the operation corresponding to our <code>remove-from-vector</code> above is <code>disj</code> – the reverse of <code>conj</code>. The final solution with the new representation can be implemented like this:</p> <pre><code>(defn remove-node [graph node] (into {} (for [[k v] (dissoc graph node)] [k (disj v node)]))) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload