Advertisement
RoshHoul

graph

Oct 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. class node {
  9. public:
  10. typedef pair<int, node*> ve;
  11. vector<ve> adj;
  12. string name;
  13.  
  14. node (string s) : name(s) {}
  15. };
  16.  
  17.  
  18. class Graph {
  19. public:
  20. typedef map<string, node *> vmap;
  21. vmap map;
  22. void addVertex(const string& name);
  23. void addEdge(const string& from, const string& to, double cost);
  24. void printNodes();
  25. };
  26.  
  27. void Graph::addVertex(const string &name) {
  28. vmap::iterator it = map.find(name);
  29. if (it == map.end()) {
  30. node *n;
  31. n = new node(name);
  32. map[name] = n;
  33. return;
  34. }
  35.  
  36. cout << "existing node" << endl;
  37. }
  38.  
  39. void Graph::addEdge(const string& from, const string& to, double cost) {
  40. node *f = map.find(from)->second;
  41. node *t = map.find(to)->second;
  42. pair<int, node *> edge = make_pair(cost, t);
  43. f->adj.push_back(edge);
  44. }
  45.  
  46. void Graph::printNodes() {
  47. for (vmap::iterator it = map.begin(); it!= map.end(); it++) {
  48. cout << "Key: " << it->first;
  49. for (int i = 0; i < it->second->adj.size(); i++ ) {
  50. cout << " with edges: " << it->second->adj[i];
  51. }
  52. cout << endl;
  53. }
  54. }
  55.  
  56. int main() {
  57. Graph graph;
  58. string input;
  59. for (int i = 0; i < 5; i++) {
  60.  
  61. cin >> input;
  62. graph.addVertex(input);
  63. }
  64.  
  65. graph.addEdge("nqma", "takuv", 0);
  66. graph.addEdge("nqma", "key", 0);
  67. graph.addEdge("nqma", "ei", 0);
  68. graph.addEdge("ei", "tupak", 0);
  69.  
  70.  
  71. graph.printNodes();
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement