# implementation of Dijkstra algorithm using file handling

Page 1 of 1

## 1 Replies - 238 Views - Last Post: 04 December 2017 - 06:22 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=408002&amp;s=d82f615ebbac582df8dba080982c063d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 george mely

Reputation: 0
• Posts: 2
• Joined: 12-November 17

# implementation of Dijkstra algorithm using file handling

Posted 04 December 2017 - 05:37 AM

hi i am implementing a Dijkstra algorithm but some how i suffer from a unknow error using dev+
unknow error occur
```

#include <iostream.h>

int minDistance(int dist[], bool sptSet[])
{

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

void printPath(int parent[], int j)
{

if (parent[j] == -1)
return;

printPath(parent, parent[j]);

printf("%d ", j);
}

int printSolution(int dist[], int n, int parent[])
{
int src = 0;
printf("Vertex\t Distance\tPath");
for (int i = 1; i < V; i++)
{
printf("\n%d -> %d \t\t %d\t\t%d ", src, i, dist[i], src);
printPath(parent, i);
}
return 0;
}

void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
int parent[V];
for (int i = 0; i < V; i++)
{
parent[0] = -1;
dist[i] = INT_MAX;
sptSet[i] = false;
}

dist[src] = 0;

for (int count = 0; count < V - 1; count++)
{

int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] &&
dist[u] + graph[u][v] < dist[v])
{
parent[v] = u;
dist[v] = dist[u] + graph[u][v];
}
}
printSolution(dist, V, parent);
}

class xyNodes
{
public:
int xaxis;
int yaxis;
};

static int distancecalcuation(int x1, int y1, int x2, int y2)
{
int x = x1 - x2;
int y = y1 - y2;
int dist;

dist = pow(x, 2) + pow(y, 2);
dist = sqrt(dist);

return dist;
}

int main()
{

double v01, v03, v12, v14, v24, v23, v25, v35, v45, v05;
xyNodes zero, one, Two, Four, Three, Five;
zero.xaxis = 1000; zero.yaxis = 2400; one.xaxis = 2800; one.yaxis = 3000; Two.xaxis = 2400; Two.yaxis = 2500; Three.xaxis = 4000; Three.yaxis = 0; Four.xaxis = 4500; Four.yaxis = 3800; Five.xaxis = 6000; Five.yaxis = 1500;
xyNodes aryxy[6];
std::string strWords, input;

std::ifstream file("sample.txt");
std::vector<std::string> wordsVectorArray;
int aryindex = 0;
while (std::getline(file, strWords))
{
if (strWords.find("0") & strWords.find("1"))
{
v01 = distancecalcuation(zero.xaxis, zero.yaxis, one.xaxis, one.yaxis);

}
if (strWords.find("0") & strWords.find("3"))
{
v03 = distancecalcuation(zero.xaxis, zero.yaxis, Three.xaxis, Three.yaxis);
}
if (strWords.find("1") & strWords.find("2"))
{
v12 = distancecalcuation(one.xaxis, one.yaxis, Two.xaxis, Two.yaxis);

}
if (strWords.find("1") & strWords.find("4"))
{
v14 = distancecalcuation(one.xaxis, one.yaxis, Four.xaxis, Four.yaxis);
}
if (strWords.find("2") & strWords.find("4"))
{
v24 = distancecalcuation(Two.xaxis, Two.yaxis, Four.xaxis, Four.yaxis);
}
if (strWords.find("2") & strWords.find("3"))
{
v23 = distancecalcuation(Two.xaxis, Two.yaxis, Three.xaxis, Three.yaxis);
}
if (strWords.find("2") & strWords.find("5"))
{
v25 = distancecalcuation(Two.xaxis, Two.yaxis, Five.xaxis, Five.yaxis);
}
if (strWords.find("3") & strWords.find("5"))
{
v35 = distancecalcuation(Three.xaxis, Three.yaxis, Five.xaxis, Five.yaxis);
}
if (strWords.find("4") & strWords.find("5"))
{
v45 = distancecalcuation(Four.xaxis, Four.yaxis, Five.xaxis, Five.yaxis);
}
if (strWords.find("0") & strWords.find("5"))
{
v05 = distancecalcuation(zero.xaxis, zero.yaxis, Five.xaxis, Five.yaxis);
}

}
file.close();
file.clear();

int graph[V][V] = {
{ 14, 0, 18, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 27, 0,5 4, 0, 0, 12 },
{ 0, 0, 17, 0, 19, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 41, 0, 10, 0, 12, 0, 0 },
{ 0, 0, 0, 1, 0, 12, 0, 11, 16 },
{ 8, 1, 0, 0, 0, 0, 11, 0, 7 },
{ 0, 0, 12, 0, 0, 0, 61, 17, 0 }
};

dijkstra(graph, 0);

system("pause");
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: implementation of Dijkstra algorithm using file handling

### #2 jimblumberg

Reputation: 5420
• Posts: 16,899
• Joined: 25-December 09

## Re: implementation of Dijkstra algorithm using file handling

Posted 04 December 2017 - 06:22 AM

Your code seems to have quite a few errors, what "unknown error" are you talking about?

Jim