-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphe.cpp
More file actions
125 lines (102 loc) · 3.26 KB
/
Graphe.cpp
File metadata and controls
125 lines (102 loc) · 3.26 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Graphe.hpp"
Graphe::Graphe(){
}
Graphe::Graphe(vector<Noeud*> sommets, vector<Edge*> aretes) {
noeuds = sommets;
edges = aretes;
}
Graphe::Graphe(Graphe g, vector<int> ids_sommets, vector<Edge*> aretes) {
for (int id : ids_sommets) {
Noeud* n = g.getNoeud(id);
if(n != nullptr) {
noeuds.push_back(n);
}
}
edges = aretes;
}
void Graphe::addNoeud(Noeud* newN){
bool exite = false;
for(Noeud* n : noeuds){
if(n->getName() == newN->getName()){
exite = true;
}
}
if(!exite){
noeuds.push_back(newN);
}else{
cout << "il exite deja" << endl;
}
}
vector<int> Graphe::DistAleatoires(int k, int nb_edge, int n, int seed) {
if (k >= n) {
throw invalid_argument("k doit etre strictement inférieur a n");
}
vector<int> valeurs;
valeurs.reserve(nb_edge);
for (int i = 0; i < nb_edge; i++) {
valeurs.push_back(i);
}
mt19937 g(seed);
shuffle(valeurs.begin(), valeurs.end(), g);
valeurs.resize(k);
return valeurs;
}
void Graphe::closeRoad(int k,int nb_edge, int seed){
for(int e : DistAleatoires(k, nb_edge, noeuds.size(), seed)){
edges[e]->close = true;
}
}
void Graphe::addEdge(Noeud* n1, Noeud* n2, double value){
Edge* e = new Edge(n1, n2, value);
edges.push_back(e);
n1->neighboringEdges.push_back(e);
n2->neighboringEdges.push_back(e);
}
void Graphe::display(string path = "output.png"){
GVC_t* gvc = gvContext();
Agraph_t* g = agopen((char*)"g", Agundirected, 0);
agattr(g, AGRAPH, (char*)"overlap", (char*)"false");
agattr(g, AGRAPH, (char*)"splines", (char*)"true");
vector<Agnode_t*> graphviz_nodes;
for(Noeud* n : noeuds) {
string str = "Node" + to_string(n->getName());
Agnode_t* node = agnode(g, (char*)str.c_str(), 1);
graphviz_nodes.push_back(node);
}
int edge_counter = 0;
for(Edge* e : edges) {
vector<Noeud*> links = e->getLinks();
int idx1 = -1, idx2 = -1;
for(size_t i = 0; i < noeuds.size(); i++) {
if(noeuds[i] == links[0]) idx1 = i;
if(noeuds[i] == links[1]) idx2 = i;
}
if(idx1 >= 0 && idx2 >= 0) {
string edge_id = "edge_" + to_string(edge_counter++);
Agedge_t* edge = agedge(g, graphviz_nodes[idx1], graphviz_nodes[idx2], (char*)edge_id.c_str(), 1);
string value_str = to_string(e->getValue());
agsafeset(edge, (char*)"label", (char*)value_str.c_str(), (char*)"");
if(e->close)
agsafeset(edge, (char*)"color", (char*)"red", (char*)"");
}
}
gvLayout(gvc, g, "neato");
gvRenderFilename(gvc, g, "png", path.c_str());
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
}
void Graphe::closeParticularRoad(int e) {
edges[e]->close = true;
}
Noeud* Graphe::getNoeud(int nb){
return noeuds[nb];
}
Edge* Graphe::getEdge(int n1, int n2){
for(Edge* edge : edges){
if((edge->getLinks()[0]->getName() == n1 && edge->getLinks()[1]->getName() == n2) ||(edge->getLinks()[1]->getName() == n1 && edge->getLinks()[0]->getName() == n2)){
return edge;
}
}
return nullptr;
}