If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

With this table, basically all nodes will have all other 9 nodes as neighbours. In this case, it is not possible to divide them into levels like a tree right?

I'm suppose to list the top 10 solutions to this distance table and display the best solution.

My idea to solving this problem is assuming A is the starting node. I'll compare all the other 9 neighbours of A and take the shortest edge, then i put the 2 nodes into the path list and compare the neighbours of the 2nd node, and so on..until i get a list of shortest paths through all 10 nodes and back to A. Is there any problem with this implementation?

This is my assignment and i'm not asking for solutions here. I would appreciate if u guys could help me with d concepts..thx guys..

If I understand your algorithm you are using what is termed a "greedy" approach. This is fine, but it does not garantee the best solution. For instance, the best path may start out with a very long edge, but as a whole the path is the shortest. In order to be garanteed to find the best possible solution, you will need to search all possible paths (however there are possible optimizations). Because cycles are not allowed in an optimal path, the easiest solution to this problem would probably be a depth-first search. i.e. assume A is the starting point then find the best path with A-B as the first edge then B-C the second edge, and so on. Then go back to A-C and C-B and so forth until you have traversed all possible permutations. There are several implementations of this algorithm, but ultimately to be garanteed the best solution, you will need to traverse the entire tree.

A nice optimization to perform is to stop transversing a path once it gets longer than your shortest path.

If i have 10 cities. That means that i have 9*8*7*6*5*4*3*2*1 = 362880 solutions rite?

Yes 9! is the number of possible solutions.

However I think you have some confusion about BFS or maybe i just misread your post. BFS will exhaust every path to every node that it hasn't already visited, so you can't just take take the shortest path off one node, thus eliminating all other paths, this is no longer a BFS search if your do.

In your algorithm you should have a queue (to keep track of the next node to make the "current" node), an array (to keep track of which nodes have been visited), and an adjacency list (to keep track of the structure). If you don't know what an adjancey list is it is simply an array of linked lists.

This puesedo code is quoted from Richard Johnsonbaugh and Marcus Schaefer's book entitled "Algorithms" (p. 183).

the objects in your adjancey list are simply nodes that hold information about the city they represent and have a link to another node (thus a linked list).

Now, with all that said, this is the base you use to buid an algorithm that finds the shortest paths. Like evilich said you seem to have a greedy approach, which is fine, but may not always give you the optimal answer. However, there is a greedy algorithm, that always finds the shortest path from a start to end vertex. It's name is Dijkstra's Algorithm. But for your case I think there needs to be a different approach.

Since you have to compute a top 10 list, it might be best to just run the BFS and then have an algorithm that computers every single possible path from the adjancey list, and as its computing these paths it places them in an array of size 10 based on their total cost (the first 10 computations will go into the array no matter what). If the cost of the path is not greater than the last position in the array, then you know it should be added somewhere and the rest should get bumped back. The only problem with this approach is in worst case it will take O(n3) time (n time to compute all paths, n time to place the value to the front of the array, n time to shift the array, cubic, not good!). But you can eliveate this by using a smart search algorithm on your array that holds the top 10. Personally, I would use the last method I suggest (run the BFS, and then use the adjacency list to compute the value of all paths, keeping an array of top 10 as you do so). With a proper algorithm to parse your top 10 array, I think the full process could be ran in a O(n lg n) time.

Thx alot customwoodtek. It seems u have really put it some time to reply my thread. Appreciate it alot...

Question:
For java, how do i implement this 'queue'? using Arrays? I haven't learned linked lists yet, so i'm sort of stuck with arrays at this moment...

I'm currently experimenting and developing my algorithm for the exhaustive method. Alot of problems with the logic and loops. My lecturer has made it 'easy' for us by setting the starting point at A (instead of any points from A to J). When i have some codes working, i'll post it here and u guys can take a looks...

Thx alot customwoodtek. It seems u have really put it some time to reply my thread. Appreciate it alot...

Question:
For java, how do i implement this 'queue'? using Arrays? I haven't learned linked lists yet, so i'm sort of stuck with arrays at this moment...

I'm currently experimenting and developing my algorithm for the exhaustive method. Alot of problems with the logic and loops. My lecturer has made it 'easy' for us by setting the starting point at A (instead of any points from A to J). When i have some codes working, i'll post it here and u guys can take a looks...

Thx alot guys...

Don't take this personally, but if you do not understand the implementation of basic data structures such as a queue and linked lists than i'm afraid this problem might be over your head. This is not your fault but rather sounds like your school's/teacher's fault. I take it you are in some type of algorithms class, this class should have a prerequsite Data Structures class or the teacher should be teaching you these data structures as they are needed to implement the algorithm.

this shouldn't discourage you thought becaue luckily these data structures are simple in concept and also are already implemented in the Collections framework in java's standard libraries (however you should also know how to implement them yourself as it will help you better understand them.

You can find an already implemented queue in java's Collection framework, more specifically java.util.concurrent.ArrayBlockingQueue

As for a linked list, here is a quick breakdown. A linked list is a lot like an array. It has "areas" or "boxes" of storage where you place your data. Arrays are good for times when you know the location of an object or piece of data and you want to access it in constant time, but it is bad for insertions and deletions when there can be no gaps in your data (you essentially have to "shift" all the data, which takes linear time). Linked lists are good for inserting and deleting in constant time, but bad when trying to access an item (beacause unless you keep a pointer to each node or "box" you have to parse the list everytime to locate an item). Anyways, a linked list is a list of nodes. In a singly linked list each node has a pointer to the next node in the list, and you will have a head pointer that always points to the front of this list. Also the node will hold a pointer to an object that contains the data you whish to store. Here is a simple example:

You have a bunch of cities you whish to store in a linked list. First you would crreate a cities class that holds relevant information about the city. In your case this might be the name, its adjacent cities, and the distance to them. You would then create a node class that stores a pointer to another node and a pointer to your city object. You would then create a class called LinkedList that has a pointer to the first node in the llist, and a method to create a new node (which would probably consist of calling the city constructore, and then calling the node constructor passing it the city object as a parameter.

This is all easy to do but fournately the nice java developers already implement a linked list for you (java.util.LinkedList). So now you just need to create a city class and create a linked list of cities. In j2se 1.5 that would go something like this

If you get an error abaout the < > symbols you are using j2se 1.4.2 or previous, just remove them, and any time you pull something out of the list you have to cast it.

I could show you a lot more, but I figured this should be enough for you to chew for now. If this feels beyond you for some reason then scrap everything i'm saying and ask your professor for some help. If you think your ok feel free to ask more questions.

I might be jumping into the middle of this, but I'll give a quick explaination maybe it will help.

For the BFS you get a work queue of things that you are processing for simplicity my example will using strings of letters, i.e. the path A-B-C will be represented by the string "ABC". You start by adding your initial path to the queue in this case it will be "A". Then you perform a loop which continues until the queue is not empty. In the loop you remove the first element, and, if the path is complete, i.e. all the cities have been visited, then you compute the distance, compare it to your shortest and update the shortest if necessary. Otherwise you loop over the cities which have not been visited and append each to the end of the string and put it on the queue. Pseudo code might look like this.

Code:

add "A" to queue
while queue is not empty {
let current = first element of queue (this should also remove that element)
if the length of current is the total length then {
compute the distance to travel the path
if the distance is smaller than the current shortest path then
set this path to the current shortest path
}
else {
for each city (all of them) {
if the city has not been visited in the string then
append city to path and add it to the queue
}
}
}
when you are done, shortest will be your shortest path.

You start by adding your initial path to the queue in this case it will be "A".

r u saying that in the beginning the queue will only contain 1 element, the starting node "A"? if so, where r all rest of of the nodes?

Then you perform a loop which continues until the queue is not empty.

Since the line 'add "A" to queue' is outside the while loop, wont the queue b never empty?

Code:

else {
for each city (all of them) {
if the city has not been visited in the string then
append city to path and add it to the queue
}
}
}

If lets say the sample contains A-B-C-D. A is my starting node, with BCD unvisited. If i code it like above, i will get the path ABCD right? but how about other paths like ACDB etc? because, after ABCD, C would already b labeled as visited, so how would it detect paths like ACDB?