473,770 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grill My Dijkstra

implemented Dijkstra's algorithm as follows. plz pour on the negative
criticism cuz I know I have a ton to learn.

oh, before you flame me regarding the language, my class was told to do
this in C# but, what can I say, I just like you guys better than the
crowd in the C# newsgroup. ;o)
using System;

namespace Dijkstra
{
public class Algorithm
{
private const int refArrayRows = 10;
private const int refArrayColumns = 10;
/* this implementation of Dijkstra's Algorithm assumes a 10 x 10
reference matrix of labels is provided */

private const int startNode = 1;
private const int endNode = 10;
/* a second assumption is point one is our starting or "vantage"
point
and point ten is our end point */

private const int nodes = 10;

private const int labels = 10;
// edge values

private const int resultColumns = 2;
/* each node (first column) in the determined shortest path has one
predecessor (second column) */

private const int infinity = 10000;
// value 10,000 represents no edge / link between nodes

private static int [,] refMatrix = new int[nodes, labels];
private static int [] distArray = new int [labels];
private static int [] prevArray = new int [nodes];
private static bool [] markedArray = new bool [nodes];
private static bool [] unmarkedArray = new bool [nodes];

private Algorithm()
{
}

public static void ImportRefMatrix ()
{
string l_Filename = "..\\..\\Matrix .DAT";
refMatrix = Import.Contruct Matrix(l_Filena me);
}

public static void InitializeArray s()
{
int i = 0;

for( ; i < labels; ++i)
{
distArray[i] = infinity;
// no path exists

}

for(i = 0; i < nodes; ++i)
{
prevArray[i] = 0;
// no nodes have predecessors

}

for(i = 0; i < nodes; ++i)
{
markedArray[i] = false;
unmarkedArray[i] = true;
}

markedArray[startNode - 1] = true;
unmarkedArray[startNode - 1] = false;
// starting point considered marked

// - 1 often used when dealing with array indexes (which begin at 0)

}

public static void Implement()
{

while(CountMark edVertices() < nodes)
{
int closestVertex = SelectUnmarkedV ertexClosestToS ource(startNode );
markedArray[closestVertex - 1] = true;
unmarkedArray[closestVertex - 1] = false;

for(int i = 0; i < labels; ++i)
{
if(refMatrix[closestVertex - 1, i] < infinity)
// for each edge adjacent to closestVertex

{
if(refMatrix[startNode - 1, i] > refMatrix[startNode - 1,
closestVertex - 1] +
refMatrix[closestVertex - 1, i])
/* if distance from starting point to adjacent node exceeds
distance from
starting point to closestVertex plus distance from closestVertex
to adjacent
node */

{
refMatrix[startNode - 1, i] = refMatrix[startNode - 1,
closestVertex - 1] +
refMatrix[closestVertex - 1, i];
// update distance grid

prevArray[i] = closestVertex - 1;
}
}
}

}

}

public static int CountMarkedVert ices()
{
int counter = 0;

for(int i = 0; i < nodes; ++i)
{
if(markedArray[i] == true) ++counter;
}

return counter;
}

public static int SelectUnmarkedV ertexClosestToS ource(int sourceNode)
{
int shortestDistanc eToSource = infinity;
int closestVertex = 0;

for(int i = 0; i < nodes; ++i)
{
if(unmarkedArra y[i] == true)
{
if(refMatrix[sourceNode - 1, i] < shortestDistanc eToSource)
{
shortestDistanc eToSource = refMatrix[sourceNode - 1, i];
closestVertex = i;
}
}
}

return ++closestVertex ;
}

public static void ShowResults()
{
Console.Write(" \nThe following chart shows all nodes on the
left.\n\nTheir precedessors " +
"are listed in the right column.\n\n'Pre v' refers to 'previous' or
'preceding' node." +
"\n\n\n\n") ;
ShowArray("Prev ");
}

public static void ShowArray(strin g arrayName)
{

switch(arrayNam e)
{
case "Prev":
Console.Write(" Prev ");
break;
case "Marked":
Console.Write(" Marked ");
break;
case "Unmarked":
Console.Write(" Unmarked ");
break;
default:
Console.Write(" Unknown array name.\n\n");
break;
}

Console.Write(" Array\n--------------\n\n");

switch(arrayNam e)
{
case "Prev":

for(int i = 0; i < nodes; ++i)
{
Console.Write(" Node {0}\t\t{1}\n", i + 1, prevArray[i] + 1);
}

break;
case "Marked":

for(int i = 0; i < nodes; ++i)
{
Console.Write(" Node {0}\t\t{1}\n", i + 1, markedArray[i]);
}

break;
case "Unmarked":

for(int i = 0; i < nodes; ++i)
{
Console.Write(" Node {0}\t\t{1}\n", i + 1, unmarkedArray[i]);
}

break;
default:
break;
}

Console.Write(" \n\n");
}
}
}

Nov 7 '05 #1
3 6242

If you mark one, remember the "number of marked" as a member variable,
forcing you not to iterate through all markedArray[] in
CountMarkedVert ices() in Implement() -> will gain some speed.
I don't know about memory allocation in C# - go to c# group for that.
We cannot give hints about that, we only can give you conceptual
hints. A C# group should be your first choice.

If you like us better than them, we can easily change this ;)
-Gernot
Nov 7 '05 #2
A_*********@hot mail.com wrote:
implemented Dijkstra's algorithm as follows. plz pour on the negative
criticism cuz I know I have a ton to learn.

oh, before you flame me regarding the language, my class was told to do
this in C# but, what can I say, I just like you guys better than the
crowd in the C# newsgroup. ;o)

[snip]

According to the FAQ, your question is off-topic here because on-topic
posts are "answerable by looking into the C++ language definition as
determined by the ISO/ANSI C++ Standard document, and by planned
extensions and adjustments"
(http://www.parashift.com/c++-faq-lit....html#faq-5.9).

IOW, this group is explicitly limited to the C++ language (hence the
name comp.lang.c++). Your post is neither in the C++ language nor
really concerned with language issues (C++ or C#) to begin with. Thus
we cannot answer your question.

We would, however, be happy to critique your use of the *language* (but
not the algorithm) if you re-post your code in C++ rather than C#.

Cheers! --M

Nov 7 '05 #3
A_*********@hot mail.com wrote:
implemented Dijkstra's algorithm as follows. plz pour on the negative
criticism cuz I know I have a ton to learn.

oh, before you flame me regarding the language, my class was told to do
this in C# but, what can I say, I just like you guys better than the
crowd in the C# newsgroup. ;o)


If it's the *algorithm* you want to be critiqued on, you may be better
off in comp.programmin g than either the C++ or C# groups. If it's your
*implementation * for which you seek criticism, you're clearly off-base
in a newsgroup for a language that many of us probably don't even know.

--
Mike Smith
Nov 7 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4196
by: Ricardo Batista | last post by:
I need that someone help me. I need to program the dijkstra algorithm by object oriented. I'll send my code. #!/usr/bin/env python # -*- encoding: latin -*- NIL =
6
5888
by: ThanhVu Nguyen | last post by:
Hi all, I need recommendation for a very fast shortest path algorithm. The edges are all directed, positive weights. Dijkstra shortest path will solve it just fine but the if the graph is not parse then it takes about O(N^2) where N is the # of vertices, too much for large graphs. Furthermore, I don't need to know the all the path from a start point to every other single vertex as Dijkstra would provide. Just the shortest path from a...
5
2206
by: A_StClaire_ | last post by:
thoughts or criticism anyone? using System; namespace Dijkstra { public class Algorithm { private const int nodes = 10;
1
16127
by: arlef | last post by:
Can somebody please explain and provide pseudocode for the Dijkstra algorithm? I'm trying to implement the Dijkstra shortest path algorithm. However, I'm finding it extremely difficult to understand. I've a node class that hold the node name, and the x,y coordinate. I've an edge class the takes two(2) nodes, from and to, and a name for the edge. I'm using only simple arrays to store the nodes and the edges, no priority
9
4517
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 tried adding some outputs to the console throughout the "Dijkstra" function but wasn't able to...
8
4132
by: abhradwip | last post by:
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........... ...
1
5121
Ganon11
by: Ganon11 | last post by:
Hey guys, I'm back, and with another FUN question! My latest homework asks this question: "Suppose all the edge weights in a graph are integers between 1 and |E|. How fast can Dijkstra's algorithm be implemented?" The only thing I could think of is somehow involving bucket sort - since the edges are bounded, we can use bucket sort to achieve an O(n) sorting time - but I'm not sure how I can use the sorted edge weights to help me at...
2
4662
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)
1
14206
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
0
9602
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3987
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
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.