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 8715259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:48:16+00:00 2026-06-13T05:48:16+00:00

Like graph in form of a list : 1 : 2 -> 3 2

  • 0

Like graph in form of a list :

1 : 2 -> 3
2 : 3 -> 4 -> 1
3 : 2 -> 1 -> 4
4 : 3 -> 2 

whew 1 : 2 -> 3 means that node 1 is connected to nodes 2 and 3.

So the output should be each node list sorted :

1 : 2 -> 3
2 : 1 -> 3 -> 4
3 : 1 -> 2 -> 4
4 : 2 -> 3 

So, this can be done in n * O(log n) time by sorting each list, but what is the most optimal algorithm for this problem ?

  • 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-06-13T05:48:18+00:00Added an answer on June 13, 2026 at 5:48 am

    Sorting all separate lists would be easiest way to get what you want. However, if you have n nodes, you have to sort n lists. Each list could have n-1 entries. Sorting 1 list of n-1 entries would have complexity O(n*log(n)). Your total complexity would be O(n²*log(n)).

    You could try to go under that by sorting your lists sequentially and exploiting that information. From your example i assume that your graph is undirected, which allows for the following optimization.

    An example first:

    1. You start by sorting the first list which would here yield 2->3.
      Then your first list would be done. You could then add ‘1’ to the
      lists of node 2 and 3 (as 1 will appear in their lists as first
      item). This would give you the start of those lists. Then you move
      on to the list of node 2.
    2. Seeing as you already know the start of it
      (1), you could skip all that node during sorting. You could do a
      quick pass through your linked list and remove 1 from the set. Then
      you sort the rest (which would give 3->4) and append it to the 1 you
      already had. As with 1, you now have the full sorted list of 2 and
      you can add ‘2’ to the list of 3 and 4.
    3. You then continue for 3, do a quick pass to remove all nodes you already know, and sort the
      rest. This would give you 4 and you append that to what you already have to get 1->2->4. Add 3 to the list of node 4.
    4. The list of node 4 is already done as there are no nodes in the list with id >4.

    More formally:

    initialize the final sorted list of every node to null;
    for(i=1;i<=nrNodes;i++){
        remove all nodes with id<i from linked list;
        A:=sort remainder of list
        append A to the final sorted list of i;
        for (every node n in A){
            append i to final sorted list of node n
        }
    }
    

    This should be faster than sequentially sorting all the lists as the lists to be sorted are smaller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help with search string encoding. Simple request like this: https://graph.facebook.com/search?q=max&type=user works pretty good,
I would like a webpage that constantly updates a graph with new data as
I have an undirected graph on matris by vertex adjacency relations like that; /*
I am drawing a line on a control on my Windows form like this:
I have a parent form first.cs like this public partial class first : Form
I have code that generates a data-driven SVG graph and I'd like to display
If I have a grid-like graph and a set of nodes like [A,B,J,K]. Grid
I have a django template that looks like this: <head> <script src=http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js type=text/javascript></script> <script
I have two classes which look like this in barebones form: @interface GraphNode {
I have to represent graphically an oriented graph like in the image below. alt

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.