
November 11th, 2012, 03:47 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 13 m 54 sec
Reputation Power: 0
|
|
|
Graph Implementation in c++
I have a a vector<vector<string>> which holds the value if Nodes and edges.
i.e
1,2,1 // here 1 is starting node, 2 is ending node and 1 is weight
1,3,4
....
Code:
vector<vector<string>> inputdata;
total_node =4;
So far I created a 3 class, Node class, Edge Class and Graph class
Code:
class Node {
public:
Node(string NodeId,string NodeName)
{
_nodeId = NodeId;
}
string GetNodeId()
{
return _nodeId;
}
void addAdjacentNode(Node *startNode, Node *dstNode, int cost)
{
Edge edge(startNode,dstNode,cost);
_neighborEdge.push_back(edge);
}
private:
string _nodeId;
vector <Edge> _neighborEdge;
}
class Edge {
public:
Edge(Node *startNode, Node *endNode, int weight)
{
originatingNodeId = startNode;
destinationNodeId = endNode;
cost = weight;
}
private:
Node *originatingNodeId;
Node *destinationNodeId;
int cost;
}
class Graph {
public:
void addNode(Node *node)
{
graph.push_back(node);
}
private:
vector<Node*> graph;
}
I am not sure, how should I make use of the class in the main program to create a graph .
How should I be calling my graph class to create a graph. Do I need to add any methods to any of my class to make it work.
Appreciate your help.
|