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

  • SEARCH
  • Home
  • 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 3605106
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:04:57+00:00 2026-05-18T21:04:57+00:00

So I guess this is a classical question for somebody with MSC in CS.

  • 0

So I guess this is a classical question for somebody with MSC in CS.

I have N element and I have the distances as well. Let’s say I have 3 elements with the following distances. It is symmetric, so

A -> B == B -> A

It looks like a matrix:

   A,  B,  C,  
A  0, 10,  20 
B 10,  0,  30
C 20, 30,   0

My question would be:

  • how can I store this efficiently(what data structure)
  • what is the most efficient way to get a linked list where the sum of distance is minimal

In this case the best is

B -> A -> C = 30 which equals to C -> A -> B

other case:

A -> B -> C = 40 which equals to C -> B -> A

I had the impression that BFS might be suitable for this. Link to documentation in English is good, even books on Amazon…

  • 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-18T21:04:58+00:00Added an answer on May 18, 2026 at 9:04 pm

    The ideal solution for your data structure is an adjacency list.

    An adjacency list is simply a list of objects (representing vertices on your graph). Each object has a list containing all the vertices that it has an adjacent edge to and the corresponding weight.

    In ruby, a simple implementation might looking something like:

    vertices = {}
    a = Vertex.new
    b = Vertex.new
    
    a.add(b, 10)
    b.add(a, 10)
    
    vertices[a] = a
    vertices[b] = b
    

    The algorithm to find the shortest, weighted path is called Dijkstra’s.

    If you would like to get the shortest path after running the algorithm, you can do a traceback. This is done by storing the (optimal) parent of each node as you reach it. In order to do this, you could have a hash for each visited node which maps to the node which lead to it with the least cost.

    Once you have finished the algorithm, your recursive traceback may look something like this:

    def traceback(parent, start, node, path)
      if(start == node)
         (path + start.to_s).reverse
      else
         path += node.to_s + " "
         traceback(parent, start, parent[node], path)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.