-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1et2.cpp
More file actions
188 lines (153 loc) · 5.03 KB
/
Question1et2.cpp
File metadata and controls
188 lines (153 loc) · 5.03 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
// Classe pour les graphes avec des sommets de type int
class GraphInt {
private:
std::unordered_map<int, std::vector<std::pair<int, int>>> adj;
std::unordered_map<int, bool> visite;
public:
void addsommet(int s) {
// Ajout d'un sommet, s'il n'existe pas déjà
if (adj.find(s) == adj.end()) { //Documentation de unorderedmap
adj[s] = {};
visite[s] = false;
}
}
void addarete(int origine, int arrivee, int poids) {
adj[origine].push_back({arrivee, poids});
}
void addEdge(int origine, int arrivee, int poids) {
addsommet(origine);
addsommet(arrivee);
addarete(origine, arrivee, poids);
}
void AfficherSommet(int s) const {
std::cout << s;
}
void Affichergraph() const {
std::cout << "{";
bool second = true;
for (const auto& paire : adj) {
if (!second){
std::cout << ", ";
}
AfficherSommet(paire.first);
std::cout << ": [";
bool first = true;
for (const auto& arete : paire.second) {
if (!first) {
std::cout << ", ";
}
AfficherSommet(arete.first);
std::cout << "(" << arete.second << ")";
first = false;
}
std::cout << "]" ;
second = false;
}
std::cout << "}" << std::endl;
}
void dfs(int sommet) {
if (visite[sommet]) return; // Si déjà visité, on arrête
visite[sommet] = true;
std::cout << "Visité: ";
AfficherSommet(sommet);
std::cout << std::endl;
for (const auto& arete : adj[sommet]) {
std::cout << "Edge: ";
AfficherSommet(sommet);
std::cout << " -> ";
AfficherSommet(arete.first);
std::cout << " (Poids: " << arete.second << ")" << std::endl;
dfs(arete.first); // Appel récursif
}
}
};
// Classe pour les graphes avec des sommets de type string
class GraphString {
private:
std::unordered_map<std::string, std::vector<std::pair<std::string, int>>> adj;
std::unordered_map<std::string, bool> visite;
public:
void addsommet(const std::string& s) {
if (adj.find(s) == adj.end()) {
adj[s] = {};
visite[s] = false;
}
}
void addarete(const std::string& origine, const std::string& arrivee, int poids) {
adj[origine].push_back({arrivee, poids});
}
void addEdge(const std::string& origine, const std::string& arrivee, int poids) {
addsommet(origine);
addsommet(arrivee);
addarete(origine, arrivee, poids);
}
void AfficherSommet(const std::string& sommet) const {
std::cout << sommet;
}
void Affichergraph() const {
std::cout << "{";
bool second = true;
for (const auto& paire : adj) {
if (!second){
std::cout << ", ";
}
AfficherSommet(paire.first);
std::cout << ": [";
bool first = true;
for (const auto& arete : paire.second) {
if (!first) {
std::cout << ", ";
}
AfficherSommet(arete.first);
std::cout << "(" << arete.second << ")";
first = false;
}
std::cout << "]" ;
second = false;
}
std::cout << "}" << std::endl;
}
void dfs(const std::string& sommet) {
if (visite[sommet]) return;
visite[sommet] = true;
std::cout << "Visité: ";
AfficherSommet(sommet);
std::cout << std::endl;
for (const auto& arete : adj[sommet]) {
std::cout << "Edge: ";
AfficherSommet(sommet);
std::cout << " -> ";
AfficherSommet(arete.first);
std::cout << " (Poids: " << arete.second << ")" << std::endl;
dfs(arete.first);
}
}
};
int main() {
// Utilisation du graphe avec des sommets de type int
GraphInt gInt;
gInt.addEdge(1, 2, 10);
gInt.addEdge(1, 3, 20);
gInt.addEdge(2, 3, 30);
gInt.addEdge(3, 4, 40);
std::cout << "Graph avec des sommets de type int:" << std::endl;
gInt.Affichergraph();
std::cout << "\nParcours en profondeur depuis le sommet 1:" << std::endl;
gInt.dfs(1);
// Utilisation du graphe avec des sommets de type string
GraphString gString;
gString.addEdge("Paris", "Lyon", 100);
gString.addEdge("Paris", "Marseille", 150);
gString.addEdge("Lyon", "Nice", 80);
gString.addEdge("Marseille", "Nice", 70);
gString.addEdge("Nice", "Paris", 200);
std::cout << "\nGraph avec des sommets de type string:" << std::endl;
gString.Affichergraph();
std::cout << "\nParcours en profondeur depuis 'Paris':" << std::endl;
gString.dfs("Paris");
return 0;
}