-Ivan-
13-07-2013, 10:48
Sto implementando questa classe:
typedef Graph<GraphNode, GraphEdge> GraphType;
template <class graph_type, class node_type, class edge_type>
class DijkstraSearch
{
public:
typedef node_type Node;
typedef edge_type Edge;
typedef graph_type Graph;
private:
//the graph the algorithm is runned on
const Graph& graph;
//stores the closest node to compute in the next step
int nextClosestNode;
//Search Path Tree built by the algorithm
std::vector<const Edge*> spt;
//cost to reach each node from the source (the index inside the vector is the index of the node)
std::vector<double> costToThisNode;
//vector containing parent edges leading to the node connected to the spt but not added to the spt yet
std::vector<const Edge*> searchFrontier;
//source and target for the search
int source;
int target;
void Search();
public:
DijkstraSearch( const Graph& p_Graph,
int p_Source,
int p_Target = -1)
:
graph(p_Graph),
spt(p_Graph.NumNodes()), //init with the number of nodes in the graph
searchFrontier(p_Graph.NumNodes()), //init with the number of nodes in the graph
costToThisNode(p_Graph.NumNodes()), //init with the number of nodes in the graph
source(p_Source),
target(p_Target)
{
//std::cout<<"p_Source: "<<p_Source<<std::endl;
//std::cout<<"p_Target: "<<p_Target<<std::endl;
//p_Graph.PrintGraph();
std::cout<<"searchfrontier size: "<<p_Graph.NumNodes()<<std::endl;
//std::cout<<"costToThisNode size: "<<costToThisNode.size()<<std::endl;
Search(); //run the search when instantiated
}
std::list<int> GetPathToTarget()const;
};
Ma dentro al costruttore p_Graph sembra non essere valorizzato, infatti poi NumNodes() mi restituisce 0 ed i vari vettori non sono inizializzati e fanno crashare il programma.
Il main è questo:
int _tmain(int argc, _TCHAR* argv[])
{
//istantiate a graph
GraphType *graph = new GraphType(true);
//graph->PopulateRandom(5, 8);
graph->CreateTestGraph();
//graph->PrintGraph();
//test dijkstra algorithm
DijkstraSearch<GraphType, GraphNode, GraphEdge> *dijkstraSearch = new DijkstraSearch<GraphType, GraphNode, GraphEdge>( graph, 0, 2);
//print the resulting SPT
dijkstraSearch->GetPathToTarget();
delete graph;
system("pause");
return 0;
}
Il grafo che creo nel main è costruito bene, se decommmento la riga che fa PrintGraph mi stampa esattamente il grafo di test che voglio. E' un errore sulla variabile di tipo reference? Ho letto che occorre che siano inizializzate al momento della dichiarazione ma poi ho visto anche che lo si può fare nel costruttore (spero di non sbagliarmi).
typedef Graph<GraphNode, GraphEdge> GraphType;
template <class graph_type, class node_type, class edge_type>
class DijkstraSearch
{
public:
typedef node_type Node;
typedef edge_type Edge;
typedef graph_type Graph;
private:
//the graph the algorithm is runned on
const Graph& graph;
//stores the closest node to compute in the next step
int nextClosestNode;
//Search Path Tree built by the algorithm
std::vector<const Edge*> spt;
//cost to reach each node from the source (the index inside the vector is the index of the node)
std::vector<double> costToThisNode;
//vector containing parent edges leading to the node connected to the spt but not added to the spt yet
std::vector<const Edge*> searchFrontier;
//source and target for the search
int source;
int target;
void Search();
public:
DijkstraSearch( const Graph& p_Graph,
int p_Source,
int p_Target = -1)
:
graph(p_Graph),
spt(p_Graph.NumNodes()), //init with the number of nodes in the graph
searchFrontier(p_Graph.NumNodes()), //init with the number of nodes in the graph
costToThisNode(p_Graph.NumNodes()), //init with the number of nodes in the graph
source(p_Source),
target(p_Target)
{
//std::cout<<"p_Source: "<<p_Source<<std::endl;
//std::cout<<"p_Target: "<<p_Target<<std::endl;
//p_Graph.PrintGraph();
std::cout<<"searchfrontier size: "<<p_Graph.NumNodes()<<std::endl;
//std::cout<<"costToThisNode size: "<<costToThisNode.size()<<std::endl;
Search(); //run the search when instantiated
}
std::list<int> GetPathToTarget()const;
};
Ma dentro al costruttore p_Graph sembra non essere valorizzato, infatti poi NumNodes() mi restituisce 0 ed i vari vettori non sono inizializzati e fanno crashare il programma.
Il main è questo:
int _tmain(int argc, _TCHAR* argv[])
{
//istantiate a graph
GraphType *graph = new GraphType(true);
//graph->PopulateRandom(5, 8);
graph->CreateTestGraph();
//graph->PrintGraph();
//test dijkstra algorithm
DijkstraSearch<GraphType, GraphNode, GraphEdge> *dijkstraSearch = new DijkstraSearch<GraphType, GraphNode, GraphEdge>( graph, 0, 2);
//print the resulting SPT
dijkstraSearch->GetPathToTarget();
delete graph;
system("pause");
return 0;
}
Il grafo che creo nel main è costruito bene, se decommmento la riga che fa PrintGraph mi stampa esattamente il grafo di test che voglio. E' un errore sulla variabile di tipo reference? Ho letto che occorre che siano inizializzate al momento della dichiarazione ma poi ho visto anche che lo si può fare nel costruttore (spero di non sbagliarmi).