Register | Sign In


Understanding through Discussion


EvC Forum active members: 65 (9164 total)
4 online now:
Newest Member: ChatGPT
Post Volume: Total: 916,913 Year: 4,170/9,624 Month: 1,041/974 Week: 368/286 Day: 11/13 Hour: 1/1


Thread  Details

Email This Thread
Newer Topic | Older Topic
  
Author Topic:   sorting problem
Rrhain
Member
Posts: 6351
From: San Diego, CA, USA
Joined: 05-03-2003


Message 5 of 11 (72468)
12-12-2003 4:01 AM
Reply to: Message 1 by matheo
12-11-2003 2:25 AM


Sounds like somebody is in an Algorithms class and has a final exam coming up.
You haven't even touched upon the possibility that some starting points are unsolvable. For example, if you have 100 songs but only two artists, one of which has made 75 of them and the other has made 25, then there is no way to sort them the way you want.
How might one determine if the problem has a solution at all? No, I'm not going to tell you how to do it...you have to do your own homework.
Once you have that down, you might be inspired about how to design an algorithm that would generate a list. Notice the size of the set of songs for each individual artist. How would one interleave one set into the others? Here's a hint: Think of a checkerboard.
This will certainly give you a possible sorting, but it won't give you all possible sortings. Off the top of my head, I can't think of a single algorithm that would generate all possible sortings, but that doesn't mean one doesn't exist.
------------------
Rrhain
WWJD? JWRTFM!

This message is a reply to:
 Message 1 by matheo, posted 12-11-2003 2:25 AM matheo has not replied

Replies to this message:
 Message 7 by Rrhain, posted 12-22-2003 12:59 PM Rrhain has not replied

  
Rrhain
Member
Posts: 6351
From: San Diego, CA, USA
Joined: 05-03-2003


Message 7 of 11 (74676)
12-22-2003 12:59 PM
Reply to: Message 5 by Rrhain
12-12-2003 4:01 AM


Here's the algorithm I had in mind:
First, if the total number of songs is even, no one artist can have more than half of the songs. If the total number of songs is odd, then no one artist can have more than one-half-plus-one of the songs. This is because is if all you have is one other artist, you're going to do an every-other solution and you need to have enough of the other artist to fit between all the original artist's entries.
Now, imagine a gigantic chessboard with as many squares as you need in both directions. Going down the left-hand column, enter the titles for the artist that has the most songs on the squares. Thus, the last song of the most common artist is on the nth square of the leftmost column.
Now, go back to the top, but move one column over. Take the next most numerous artist and start entering its entries on the squares going down the second column. If you run out of entries before reaching the nth square in the second column, take the next most numerous artist and keep going until you reach the nth square of the second column. At that point, go back to the top and continue entering down the third column.
Keep doing this until you run out of songs to enter. To get your sorted list, simply start at the very first square (uppermost left) and read across the rows.
We know this will work because the most numerous artist has, at most half-plus-one songs. Thus, the second column has at most n items, none of which are the first artist and at least n-1 items, meaning that every row has at least two entries except the very last one and thus the final transversal will always have at least one entry that isn't the first artist before wrapping around.
This recurses for all the other columns. Even though we may "wrap around" the bottom of one column to start at the top of the next, we will never catch up to have two entries by the same artist side-by-side horizontally because each artist has fewer than n songs and the column length is n.
And since there is guaranteed a second column and no row has the same artist side-by-side, transversing the grid horizontally will guarantee that you never hit the same artist twice in a row.
Now, there is an improvement one could make to this algorithm if one were programming it. Does it really matter that other than the choice of the most numerous artist for the first column that we choose the rest of the artists in descending order of size? No. Since all of the remaining artists are no larger than the most numerous, we can put them into the grid in any order once we have chosen the most numerous in order to set the column size to that cardinality. We never have to worry about overlap from column to column.
------------------
Rrhain
WWJD? JWRTFM!

This message is a reply to:
 Message 5 by Rrhain, posted 12-12-2003 4:01 AM Rrhain has not replied

Replies to this message:
 Message 10 by Peter, posted 12-23-2003 4:29 AM Rrhain has not replied

  
Newer Topic | Older Topic
Jump to:


Copyright 2001-2023 by EvC Forum, All Rights Reserved

™ Version 4.2
Innovative software from Qwixotic © 2024