Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4550416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:29:36+00:00 2026-05-21T16:29:36+00:00

I’ve been struggling to get a Prüfer sequence algorithm implemented in Clojure, primarily as

  • 0

I’ve been struggling to get a Prüfer sequence algorithm implemented in Clojure, primarily as a mental exercise. While what I have works correctly, I was wondering if there’s a more concise way of removing something from map and a collection of vectors, for example if I wanted to remove all 5’s in the following:

{1 [2 3], 2 [1 4], 3 [1 5], 4 [2], 5 [3]}

to be left with:

{1 [2 3], 2 [1 4], 3 [1], 4 [2]}

I suspect the structure I have is… suboptimal… but it was the simplest way I could think of to represent edge sets. Any thoughts much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:29:37+00:00Added an answer on May 21, 2026 at 4:29 pm

    TLDR: Use sets instead of vectors in this case.

    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 x–y there exists a map entry from x to a vector containing y and an entry from y to a vector containing x. To remove a node z and all its edges, this invariant has to be maintained by both removing the map entry that has z as its key and by removing z from the vectors of all other entries.

    The first operation – to remove a map entry given its key – is simple. The map without the entry is given by the expression (dissoc m 5), where m is the map in your example and 5 is the node you wish to remove from the graph.

    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 remove-from-vector), we can do 1) in a number of ways (here are three examples):

    (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)))
    

    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.

    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:

    {1 #{2 3}, 2 #{1 4}, 3 #{1 5}, 4 #{2}, 5 #{3}}
    

    For sets the operation corresponding to our remove-from-vector above is disj – the reverse of conj. The final solution with the new representation can be implemented like this:

    (defn remove-node [graph node]
      (into {} (for [[k v] (dissoc graph node)]
                 [k (disj v node)])))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm looking for suggestions for debugging... If you view this site in Firefox or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.