I want to write a program which will find the shortest path between n no. of cities using dijkstra's algorithm ...... but could not do it..... i have written a program which will give the shortest path between finite no. of cities....... plz help me out....... i would also like to know for the states & city names should i use a database.....if so how???? Im giving the prog which ive written...
plz help me with the source code........... -
#include <stdio.h>
-
#include <limits.h>
-
#include <assert.h>
-
#include <conio.h>
-
-
typedef enum {false, true} bool;
-
typedef char *string;
-
-
-
#define oo UINT_MAX
-
-
typedef unsigned int value;
-
-
typedef enum { KOLKATA, MUMBAI, CHENNAI, BANGALORE, PUNE, DELHI, BHUBANESWAR, HYDERBAAD, nonexistent} vertex;
-
-
const vertex first_vertex = KOLKATA;
-
const vertex last_vertex = HYDERBAAD;
-
-
#define no_vertices 8
-
-
-
-
string name[] = {"KOLKATA","MUMBAI","CHENNAI","BANGALORE","PUNE","DELHI", "BHUBANESWAR", "HYDERBAAD"};
-
-
-
-
-
value weight[no_vertices][no_vertices] =
-
{
-
/* KOLKATA MUMBAI CHENNAI BANGALORE PUNE DELHI BHUBANESWAR HYDERBAAD */
-
{ 0, 339, 2555, oo, oo, oo, oo, oo}, /* KOLKATA */
-
{ oo, 0, 337, 1843, oo, oo, oo, oo}, /* MUMBAI */
-
{ 2555, 337, 0, 1743, 1233, oo, oo, oo}, /* CHENNAI */
-
{ oo, 1843, 1743, 0, 802, oo, 849, oo}, /* BANGALORE */
-
{ oo, oo, 1233, 802, 0, 1387, oo, 1120}, /* PUNE */
-
{ oo, oo, oo, oo, 1387, 0, 142, 1099}, /* DELHI */
-
{ oo, oo, oo, 849, oo, 142, 0, 1205}, /* BHUBANESWAR */
-
{ oo, oo, oo, oo, 1120, 1099, 1205, 0} /* HYDERBAAD */
-
};
-
-
-
-
-
value D[no_vertices];
-
bool tight[no_vertices];
-
vertex predecessor[no_vertices];
-
-
-
-
vertex minimal_nontight() {
-
-
vertex j, u;
-
-
for (j = first_vertex; j <= last_vertex; j++)
-
if (!tight[j])
-
break;
-
-
assert(j <= last_vertex);
-
-
u = j;
-
-
-
-
-
-
for (j++; j <= last_vertex; j++)
-
if (!tight[j] && D[j] < D[u])
-
u = j;
-
-
-
-
return u;
-
}
-
-
-
-
bool successor(vertex u, vertex z) {
-
return (weight[u][z] != oo && u != z);
-
}
-
-
-
-
-
void dijkstra(vertex s) {
-
-
vertex z, u;
-
int i;
-
-
D[s] = 0;
-
-
for (z = first_vertex; z <= last_vertex; z++) {
-
-
if (z != s)
-
D[z] = oo;
-
-
tight[z] = false;
-
predecessor[z] = nonexistent;
-
}
-
-
for (i = 0; i < no_vertices; i++) {
-
-
u = minimal_nontight();
-
tight[u] = true;
-
-
-
-
if (D[u] == oo)
-
continue; /* to next iteration ofthe for loop */
-
-
for (z = first_vertex; z <= last_vertex; z++)
-
if (successor(u,z) && !tight[z] && D[u] + weight[u][z] < D[z]) {
-
D[z] = D[u] + weight[u][z]; /* Shortcut found. */
-
predecessor[z] = u;
-
}
-
}
-
}
-
-
-
-
-
#define stack_limit 10000 /* Arbitrary choice. Has to be big enough to
-
accomodate the largest path. */
-
-
vertex stack[stack_limit];
-
unsigned int sp = 0; /* Stack pointer. */
-
-
void push(vertex u) {
-
assert(sp < stack_limit); /* We abort if the limit is exceeded. This
-
will happen if a path with more
-
vertices than stack_limit is found. */
-
stack[sp] = u;
-
sp++;
-
}
-
-
bool stack_empty() {
-
return (sp == 0);
-
}
-
-
vertex pop() {
-
assert(!stack_empty()); /* We abort if the stack is empty. This will
-
happen if this program has a bug. */
-
sp--;
-
return stack[sp];
-
}
-
-
/* End of stack digression and back to printing paths. */
-
-
-
void print_shortest_path(vertex origin, vertex destination) {
-
-
vertex v;
-
-
assert(origin != nonexistent && destination != nonexistent);
-
-
dijkstra(origin);
-
-
printf("The shortest path from %s to %s is:\n\n",
-
name[origin], name[destination]);
-
-
for (v = destination; v != origin; v = predecessor[v])
-
if (v == nonexistent) {
-
printf("nonexistent (because the graph is disconnected).\n");
-
return;
-
}
-
else
-
push(v);
-
-
push(origin);
-
-
while (!stack_empty())
-
printf(" %s",name[pop()]);
-
-
printf(".\n\n");
-
}
-
-
-
/* We now run an example. */
-
-
int main() {
-
clrscr();
-
print_shortest_path(KOLKATA,PUNE);
-
getch();
-
return 0; /* Return gracefully to the caller of the program
-
(provided the assertions above haven't failed). */
-
}
8 4108 sicarie 4,677
Recognized Expert Moderator Specialist
So what are you having trouble with? Are you getting any errors, and if so, what are they? Or are you just not getting a correct answer, and what inputs are you using?
no.....actually i have done this program for only 8 cities....& have defined the aadjacent matrix within the code...... im getting the output correctly ....but if i want this program for n number of cities, then what to do?
DeMan 1,806
Top Contributor
It may be tedious, but you could use linked lists instead of arrays, otherwise, if there are n cities (and if you don't know what n is) they must come from somwhere (eg std input), you can count them as there input (storing them temporarily in a linked list), and then create matrices the size you like afterward....
Alternatively (assuming you're allowed to get rid of the matrix), you create a graph. A graph is like a linked list, but can go in more than 2 directions (so it contains a linked list of neighbors, say). Any Node stores only the cities it Neighbors, along with the weight for that particular city. This would also modify your code for Djikstra's alg, but it would probably be more efficient, because you only need to try the routes that are possible (that is, you know that a tight node will always be in the list of the current node).
Hope this is helpful, if not (I know I can be confusing) please post again....
thanx buddy.......... ......for your help...but the thing is that its little bit confusing.....
let me tell you the problem.....
suppose there is a chart like this Source Destination
State City Area State City Area
now i have to find the shortest path from same city but different area....or different city & area..... like a flight journey...
buddy this is my main project...& i have to submit it on monday
If u can Plz help me with the source code......im very confused....... plz help me
DeMan 1,806
Top Contributor
You can find the details of Djikstra's algorithm here
in other words....
Take the Node along the shortest path from the curerent Node (this is why we stored the weights in the Node) and add him to an "active Nodes" storing also the total weight to get this far. Choose the next shortest total path (which may be the shortest node at your new node, or the second shortest at your old one)....Continu e to do this until you arrive at your destination
i am getting u ......... but to build this algo..is a very tough task.......that s yyy im requesting u to plz provide me with the code.....for n no. of cities.
- ahem -
you learning how to build tough algorithms yourself is what its all about. if you dont really want to learn how to program, you should not be doing it.
DeMan 1,806
Top Contributor
As Roonie said, we can offer help, but ultimately you still have to do the work. Perhaps if you can explain where the difficulty lies, we may be able to help you through it.
Alternatively, if you can attempt the problem (and find it doesn't compile, it gives unsexpected output etc), we may be able to help you fix some of the problems.
Maybe to begibn with, you should consider what sort of methods you might need and what they would do. You should also consider Data Structures you may need and how you would go about im[plementing them.
Then you can try to convert the algorithm on the net (or my english version) into some form of pseudocode to work out what you are doing.....
Hope this helps
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Der Andere |
last post by:
Do you know of an efficient implementation (priority queues) of Dijkstra's
algorithm for calculating the shortest distances between vertices in a
graph?
Cheers,
Matthias
--
Für emails Anweisung in der Adresse befolgen
|
by: A_StClaire_ |
last post by:
hi,
I am trying to do this thing in C# and was wondering if it's customary
for the algorithm to backtrack. is its purpose only to find the
shortest path from a to z, or does one normally implement it backward
(i.e., z to a) after going forward?
obviously we are assuming edges may not be equal when reversing
directions (e.g., takes...
|
by: Ook |
last post by:
This is probably a bit OT, as I'm not looking for a c++ implementaiton of
Dijkstra's algorithm, rather I'm just trying to understand it (is there a
better place then here to ask this question?). I've reviewed several sites
and the faq (no, I'm not asking you to do my homework for me) and the
examples don't give me the information I need to...
|
by: Josh Zenker |
last post by:
I've been working on an implementation of Dijkstra's algorithm on and
off for the past few days. I thought I was close to being finished,
but clearly I've done something wrong. I'm not a very experienced
programmer, so please be patient.
My code compiles in g++ without any errors or warnings. Unfortunately
it segfaults when it runs. I...
|
by: momotaro |
last post by:
am having a final tomorrow am not yet done with dijkstra!!!
plz help this is my code waht is wrong with it!
void Dijkstra(char *root, char *destination, GraphNode *head)
{
GraphNode *Root, *dest, *GraphNodeWalker, *vertmin;
ListNode *ListNodeWalker;
int flag;
| |
by: victorporton |
last post by:
D.K. is traveling from City A to City B. He can stop at some designated spots only.
I am trying to use Dijkstra’s algorithm to determine the “spot-to-spot” path that will get D.K. from City A to the City B in the minimum amount of time.
The input in my program is an integer n and the 2D coordinates of n spots.
Some assumptions have been...
|
by: shashankbs |
last post by:
Given a topology and a certain node X, find the shortest path tree
with X as the root.
* Input: a topology file similar to the following (which is a three-node
ring)
--------------------------------------------
Nodes: (3) // there are three nodes (node 0, 1 and 2)
Edges: (3)
|
by: Glenton |
last post by:
Hi All
Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm:
#!/usr/bin/env python
#This is meant to solve a maze with Dijkstra's algorithm
from numpy import inf
from copy import copy
|
by: Greg Jackson |
last post by:
Hello,
I am having some trouble in working out the distance of each node from the starting node in my Dijkstra Algorithm code. Here is my code with the section i'm stuck on in bold:
infinity = 1000000
invalid_node = -1
startNode = 0
class Node:
distFromSource = infinity
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |