-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cc
More file actions
52 lines (40 loc) · 855 Bytes
/
dijkstra.cc
File metadata and controls
52 lines (40 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include "dijkstra.h"
#include "Graph.h"
#include "NodeSet.h"
using std::cout;
using std::endl;
void dijkstra(Node* n)
{
n->setValue(0);
NodeSet stack;
stack.add(n);
// print_neighbors(n);
while( !stack.isEmpty() )
{
auto node = stack.removeMin();
process_neighbors(node, stack);
}
}
void process_neighbors(Node* node, NodeSet& stack)
{
for (auto neighbor : node->getEdges())
{
Node* dest = neighbor.getDestination();
int length = neighbor.getLength();
int value = node->getValue();
int sum = length + value;
if( sum < dest->getValue() )
{
dest->setValue(sum);
stack.add(dest);
}
}
}
void print_neighbors(Node* n)
{
std::cout << "neighbors to: " << *n << " is\n";
for(auto de : n->getEdges()){
cout << " -" << de << endl;
}
}