-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.hpp
97 lines (81 loc) · 2.04 KB
/
graph.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
class vertex {
//int index;
//std::vector<vertex> neighbours;
public:
std::string name;
vertex();
~vertex(){};
/* data */
};
class edge{
public:
int weight;
std::string data;
edge();
~edge(){};
};
class graph {
public:
std::map<std::string,std::vector<edge>> Graph;
graph(){};
~graph(){};
void addVertex(std::string nameNode);
void addVertex (vertex nameNode);
void addEdge(vertex a,vertex b);
vertex createVertex(std::string nameNode);
void printGraph();
void BFS(vertex start);
};
void graph::addVertex(std::string nameNode) {
edge *dummy = new edge();
Graph[nameNode].push_back(*dummy);
}
// overloaded addvertex method
void graph::addVertex(vertex nameNode) {
edge *dummy = new edge();
Graph[nameNode.name].push_back(*dummy);
}
vertex graph::createVertex(std::string nameNode){
vertex *temp = new vertex();
temp->name = nameNode;
return *temp;
}
void graph::addEdge(vertex a,vertex b){
edge *temp = new edge();
temp->weight = 0;
temp->data = b.name;
Graph[a.name].push_back(*temp);
}
void graph::printGraph(){
typedef std::map<std::string, std::vector<edge>>::const_iterator MapIterator;
typedef std::vector<edge>::const_iterator ListIterator;
for (MapIterator iter = Graph.begin(); iter != Graph.end(); iter++){
std::cout << "Key: " << iter->first << std::endl << "Values:" << std::endl;
for (ListIterator list_iter = iter->second.begin()+1; list_iter != iter->second.end(); list_iter++)
std::cout << " " << (list_iter->data) << std::endl;
}
}
// Work in Progress
void graph::BFS(vertex start){
std::cout << start.name;
std::set<std::string> visited;
visited.insert(start.name);
typedef std::map<std::string, std::vector<edge>>::const_iterator MapIterator;
typedef std::vector<edge>::const_iterator ListIterator;
}
vertex::vertex(){
name = "";
}
edge::edge(){
weight = 0;
data = "";
//std::cout << "Error here! \n";
}
#endif