我正在研究Dijkstra的算法以寻找最短路径。我在add_edge函数上初始化图形的两个节点之间的权重时遇到了问题。因此,我在使用我的dijkstra函数时遇到了问题。我也不允许使用矢量。 以下是我到目前为止的代码:
#include <iostream>
#include <limits.h>
using namespace std;
class ListNode;
class Node {
public:
int visited, distance;
char label;
Node* prev;
Node* next;
ListNode* adjacent;
//node constructor
Node(char label) {
this->label = label;
this->visited = 0;
this->distance=0;
this->next = 0;
this->prev = 0;
this->adjacent = 0;
}
};
class ListNode {
public:
Node * datum;
int weight;
ListNode* next;
ListNode* prev;
//Listnode constructor
ListNode(Node* datum) {
this->datum = datum;
this->prev = 0;
this->next = 0;
this->weight=0;
}
};
class Graph {
public:
Node * vertices;
Node* visited;
ListNode* head;
ListNode* tail;
//Graph constructor
Graph() {
this->head = 0;
this->tail = 0;
this->vertices = 0;
this->visited = 0;
}
//adjaceny list destructor_helper
void destructorAdjacent_helper(ListNode* ln) {
if (ln == 0)
return;
else {
destructorAdjacent_helper(ln->next);
delete ln;
}
}
//node destructor_helper
void destructorNodes_helper(Node* temp) {
if (temp == 0)
return;
else {
destructorNodes_helper(temp->next);
destructorAdjacent_helper(temp->adjacent);
delete temp;
}
}
//Graph destructor
~Graph() {
destructorNodes_helper(this->vertices);
}
//add node to the graph
int add_node(char label) {
//every label must be unique
for (Node* temp = this->vertices; temp != 0; temp = temp->next) {
if (temp->label == label)
return 0;
}
//after uniqueness test add node to the graph
Node* newNode = new Node(label);
if (this->vertices != 0) {
this->vertices->prev = newNode;
}
newNode->next = this->vertices;
this->vertices = newNode;
return 1;
}
//add edge from and to the given nodes
int add_edge(char from, char to, int weight) {
Node *temp, *start, *end;
//check the start node
for (temp = this->vertices; temp != 0; temp = temp->next) {
if (temp->label == from)
break; //goes out of the loop after start node is found
}
if (temp == 0)
return 0;
else
start = temp;
//check the end node
for (temp = this->vertices; temp != 0; temp = temp->next) {
if (temp->label == to)
break; //breaks from the loop after end node is found
}
if (temp == 0)
return 0;
else
end = temp;
ListNode* endNode = new ListNode(end);
if (start->adjacent != 0) {
start->adjacent->prev = endNode;
}
endNode->next = start->adjacent;
start->adjacent = endNode;
//????????????
//how do we insert the weight between from and to nodes ???
return 1;
}
//push a node to the stack or queue as necessary
void push(Node* node) {
ListNode* newNode = new ListNode(node);
if (this->head == 0) {
this->head = newNode;
this->tail = newNode;
}
else {
this->head->prev = newNode;
newNode->next = this->head;
this->head = newNode;
}
}
//pops the tail node from the queue
Node* queue_pop() {
if (this->head == 0) {
return 0;
}
else {
Node* retVal = this->tail->datum;
ListNode* temp = this->tail;
this->tail = this->tail->prev;
if (this->tail != 0) {
this->tail->next = 0;
}
else {
this->head = 0;
}
delete temp;
return retVal;
}
}
void visit(Node* node) {
//remove from vertices list
if (node->prev != 0) {
node->prev->next = node->next;
}
if (node->next != 0) {
node->next->prev = node->prev;
}
if (this->vertices == node) {
this->vertices = node->next;
}
//add to visited list
if (this->visited != 0) {
this->visited->prev = node;
}
node->next = this->visited;
node->prev = 0;
this->visited = node;
//mark as visited
node->visited = 1;
}
void dijkstra(char src){
Node*temp = this->vertices;
ListNode* LTemp;
for(;temp!=0; temp=temp->next){
temp->distance = INT_MAX;
temp->visited=0;
}
src->distance=0;
push(src);
while(this->head!=0){
temp=queue_pop();
if (!temp->visited) {
//mark as visited
visit(temp);
for (LTemp = temp->adjacent; LTemp != 0; LTemp = LTemp->next) {
if (!LTemp->datum->visited) {
push(LTemp->datum);
}
}
}
//push top of vertices if queue is empty and vertices is not
if (!this->head && this->vertices) {
push(this->vertices);
}
//algorithm to follow:
for each neighbor u of v:
//not sure how to get length(v,u)???
alt := dist[v] + length(v, u)
if alt < dist[u]:
dist[u] := alt
}
}
};
int main(){
//create a new graph
Graph* G = new Graph();
//add nodes to the graph
G->add_node('A');
G->add_node('B');
G->add_node('C');
G->add_node('D');
G->add_node('E');
G->add_node('F');
//add edges to the nodes
G->add_edge('C', 'B', 3);
G->add_edge('C', 'E', 5);
G->add_edge('C', 'D', 6);
G->add_edge('E', 'C', 5);
G->add_edge('E', 'D', 2);
G->add_edge('D', 'C', 6);
G->add_edge('D', 'E', 2);
G->add_edge('B', 'C', 3);
G->add_edge('B', 'A', 7);
G->add_edge('A', 'B', 7);
G->add_edge('B', 'D', 4);
G->add_edge('D', 'B', 4);
G->add_edge('E', 'F', 1);
G->add_edge('F', 'E', 1);
G->dijkstra('C');
//free the dynamically allocated memory
delete G;
return 0;
}