014
20.05.2009, 05:24 Uhr
xyz
|
Danke es hat funktioniert.
In der Klasse GraphNode() in graph.cpp exestistiert eine Funktion distance() die Distanzen zwischen zwei Punkten berechnen kann. Leider weiss ich nicht ob diese Funktion in diese Klasse gehört oder nicht und wie man die Ergebnisse in eine Distanzmatrix speichert?
Hier ist der aktualisierte Code: main.cpp
Code: |
#include "graph.h" #include <iostream> #include <fstream> #include <vector>
using namespace std;
struct s_node { int label; double x, y, z; };
istream &operator>>(istream &in, s_node &n) { return in >> n.label >> n.x >> n.y >> n.z; }
int main() { //make an empty graph, and an empty temporary graph node Graph *graph = new Graph(); s_node s_nodeTmp;
ifstream in("template.txt"); //construct the nodes while(in >> s_nodeTmp) { //GraphNode *tempNode = new GraphNode(s_nodeTmp.label, GraphNode tempNode (s_nodeTmp.label, s_nodeTmp.x, s_nodeTmp.y, s_nodeTmp.z); graph->addNode(tempNode); //delete tempNode; } //construct the adjency for (size_t i = 0; i < graph->getNumNodes(); i++) { for (size_t j = 0; j < graph->getNumNodes(); j++) { if (graph->getNodeAt(i).getLabel() != graph->getNodeAt(j).getLabel()){ graph->getNodeAt(i).addAdjacentNode(graph->getNodeAt(j).getLabel()); } } }
//print the graph graph->print(); delete graph; return 0; }
|
graph.h
Code: |
#ifndef GRAPH_H_ #define GRAPH_H_
#include <iostream> #include <string> #include <vector>
using namespace std;
class GraphNode { private: vector<int> adjacencyList; //list of indices of nodes connected to this node int label; double x,y,z; //x,y,z as in a point in 3-d space.
public: GraphNode(int label, double x, double y, double z);
//get and set functions vector<int> getAdjacencyList(); size_t getNumAdjacentNodes();
void setLabel(int label); void setX(double x); void setY(double y); void setZ(double z);
int getLabel(); double getX(); double getY(); double getZ();
//utility functions void addAdjacentNode(int label); void print(); void distance(); };
class Graph { private: vector<GraphNode> nodes; //an array of nodes to represent the graph vector<vector<double> > distance_matrix; vector<vector<int> > connects;
public: //get functions size_t getNumNodes();
//utility functions void print(); void addNode(GraphNode node); GraphNode& getNodeAt(size_t index); void setNodeAt(size_t index, GraphNode node);
};
#endif /* GRAPH_H_ */
|
graph.cpp
Code: |
#include "graph.h"
using namespace std;
GraphNode::GraphNode(int label, double x, double y, double z){ setLabel(label); setX(x); setY(y); setZ(z); }
vector<int> GraphNode::getAdjacencyList() { //returns the complete adjacency list //return (*adjacencyList); return adjacencyList; }
size_t GraphNode::getNumAdjacentNodes() { //returns the number of adjacent nodes return adjacencyList.size(); }
void GraphNode::setLabel(int label){this->label = label;} void GraphNode::setX(double x){this->x = x;} void GraphNode::setY(double y){this->y = y;} void GraphNode::setZ(double z){this->z = z;}
int GraphNode::getLabel(){return label;} double GraphNode::getX(){return x;} double GraphNode::getY(){return y;} double GraphNode::getZ(){return z;}
void GraphNode::addAdjacentNode(int label){ adjacencyList.push_back(label); }
void GraphNode::print() { cout << "\t--- Node " << label << " ---"<<endl; cout << "Coordinate:\t\t" << getX() << ", " << getY() << ", " << getZ() << " " << endl; cout << "Adjacent Nodes:\t"; for (size_t count = 0; count < adjacencyList.size(); count++) { cout << adjacencyList[count]; if (count != adjacencyList.size() - 1) { cout << ", "; } } cout << endl; }
void GraphNode::distance(){ double x_p, y_p, z_p;
for(int i= 1; i <= nodeList.size(); ++i) { for(int j = 1; j <= nodeList.size(); ++j) { if (i == j) { distance_matrix[i][j] = 0; } else { x_p = nodeList[i].getX() - nodeList[j].getX(); y_p = nodeList[i].getY() - nodeList[j].getY(); z_p = nodeList[i].getZ() - nodeList[j].getZ(); this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p ); } } } }
size_t Graph::getNumNodes() { //returns the number of nodes in the graph return nodes.size(); }
void Graph::addNode(GraphNode node) { nodes.push_back(node); }
GraphNode& Graph::getNodeAt(size_t index) { //if ((index < numNodes) && (index >= 0)) if ((index < nodes.size()) && (index >= 0)) { return nodes[index]; } else { //invalid index specified // return GraphNode(); //return an 'empty' node } }
void Graph::print() { for (size_t count = 0; count < nodes.size(); count++) { nodes[count].print(); cout << endl << endl; } }
void Graph::setNodeAt(size_t index, GraphNode node) { //assignments are only allowed into existing locations if ((index < nodes.size()) && (index >= 0)) { nodes[index] = node; } }
|
Die Ausgabe vom Programm sieht wie folgt aus:
Code: |
--- Node 39 --- Coordinate: 18.378, 59.466, 13.185 Adjacent Nodes: 40, 41, 43, 44, 45, 48, 49
--- Node 40 --- Coordinate: 14.27, 54.556, 18.227 Adjacent Nodes: 39, 41, 43, 44, 45, 48, 49
--- Node 41 --- Coordinate: 30.119, 51.333, 33.732 Adjacent Nodes: 39, 40, 43, 44, 45, 48, 49
--- Node 43 --- Coordinate: 26.326, 51.1, 21.154 Adjacent Nodes: 39, 40, 41, 44, 45, 48, 49
--- Node 44 --- Coordinate: 23.764, 55.05, 18.338 Adjacent Nodes: 39, 40, 41, 43, 45, 48, 49
--- Node 45 --- Coordinate: 17.697, 75.884, 26.178 Adjacent Nodes: 39, 40, 41, 43, 44, 48, 49
--- Node 48 --- Coordinate: 22.351, 66.269, 28.727 Adjacent Nodes: 39, 40, 41, 43, 44, 45, 49
--- Node 49 --- Coordinate: 19.98, 63.046, 33.535 Adjacent Nodes: 39, 40, 41, 43, 44, 45, 48
|
Wie bekommt man distance() zum laufen, so dass man die Distanzen in eine Distanzmatrix speichern kann (z.B Node 49 zu 39, Node 49 zu 40, ....)? Dieser Post wurde am 20.05.2009 um 05:27 Uhr von xyz editiert. |