first commit
This commit is contained in:
37
.clang-format
Normal file
37
.clang-format
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
|
||||||
|
AlignAfterOpenBracket: DontAlign
|
||||||
|
BreakConstructorInitializers: AfterColon
|
||||||
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
|
PointerAlignment: Left
|
||||||
|
SortIncludes: true
|
||||||
|
SpacesBeforeTrailingComments: 2
|
||||||
|
UseTab: Always
|
||||||
|
MaxEmptyLinesToKeep: 5
|
||||||
|
|
||||||
|
TabWidth: 4
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentCaseLabels: true
|
||||||
|
|
||||||
|
ColumnLimit: 135
|
||||||
|
AlwaysBreakTemplateDeclarations: Yes
|
||||||
|
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BraceWrapping:
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: false
|
||||||
|
AfterEnum: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Xmake cache
|
||||||
|
.xmake/
|
||||||
|
build/
|
||||||
|
|
||||||
|
# MacOS Cache
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
|
||||||
16
.vscode/compile_commands.json
vendored
Normal file
16
.vscode/compile_commands.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"directory": "/home/simon/Programmation/Graphe",
|
||||||
|
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-Wall", "-O0", "-std=c++20", "-Ibuild/.gens/Graphe/linux/x86_64/debug/platform/windows/idl", "-o", "build/.objs/Graphe/linux/x86_64/debug/src/Algo.cpp.o", "src/Algo.cpp"],
|
||||||
|
"file": "src/Algo.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/simon/Programmation/Graphe",
|
||||||
|
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-Wall", "-O0", "-std=c++20", "-Ibuild/.gens/Graphe/linux/x86_64/debug/platform/windows/idl", "-o", "build/.objs/Graphe/linux/x86_64/debug/src/Graphe.cpp.o", "src/Graphe.cpp"],
|
||||||
|
"file": "src/Graphe.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/simon/Programmation/Graphe",
|
||||||
|
"arguments": ["/usr/bin/gcc", "-c", "-m64", "-g", "-Wall", "-O0", "-std=c++20", "-Ibuild/.gens/Graphe/linux/x86_64/debug/platform/windows/idl", "-o", "build/.objs/Graphe/linux/x86_64/debug/src/main.cpp.o", "src/main.cpp"],
|
||||||
|
"file": "src/main.cpp"
|
||||||
|
}]
|
||||||
53
.vscode/settings.json
vendored
Normal file
53
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"string": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"span": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"typeinfo": "cpp"
|
||||||
|
},
|
||||||
|
"C_Cpp.errorSquiggles": "disabled"
|
||||||
|
}
|
||||||
23
cours-representation.gra
Normal file
23
cours-representation.gra
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
NAME: cours-representation.gra
|
||||||
|
COMMENT: graphe du cours illustrant la représentation d'un graphe en machine
|
||||||
|
ORIENTED: true
|
||||||
|
NB_VERTICES: 5
|
||||||
|
NB_VALUES_BY_VERTEX: 1
|
||||||
|
NB_EDGES: 7
|
||||||
|
NB_VALUES_BY_EDGE: 2
|
||||||
|
|
||||||
|
VERTICES [id name values[]]:
|
||||||
|
0 a 2
|
||||||
|
1 b 3
|
||||||
|
2 c 6
|
||||||
|
3 d 7
|
||||||
|
4 e 1
|
||||||
|
|
||||||
|
EDGES [idInitialVertex idFinalVertex values[]]:
|
||||||
|
0 1 3 8
|
||||||
|
0 2 2 7
|
||||||
|
0 3 3 3
|
||||||
|
1 3 2 5
|
||||||
|
1 4 1 6
|
||||||
|
2 3 1 4
|
||||||
|
4 3 3 9
|
||||||
64545
graphe-communes.gra
Normal file
64545
graphe-communes.gra
Normal file
File diff suppressed because it is too large
Load Diff
64545
graphe-nonoriente-01.gra
Normal file
64545
graphe-nonoriente-01.gra
Normal file
File diff suppressed because it is too large
Load Diff
40
graphe-oriente-01.gra
Normal file
40
graphe-oriente-01.gra
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
NAME: graphe-oriente-01.gra
|
||||||
|
COMMENT: graphe de test
|
||||||
|
ORIENTED: true
|
||||||
|
NB_VERTICES: 12
|
||||||
|
NB_VALUES_BY_VERTEX: 0
|
||||||
|
NB_EDGES: 17
|
||||||
|
NB_VALUES_BY_EDGE: 0
|
||||||
|
|
||||||
|
VERTICES [id name values[]]:
|
||||||
|
0 a
|
||||||
|
1 b
|
||||||
|
2 c
|
||||||
|
3 d
|
||||||
|
4 e
|
||||||
|
5 f
|
||||||
|
6 g
|
||||||
|
7 h
|
||||||
|
8 i
|
||||||
|
9 j
|
||||||
|
10 k
|
||||||
|
11 l
|
||||||
|
|
||||||
|
EDGES [idInitialVertex idFinalVertex values[]]:
|
||||||
|
0 1 7
|
||||||
|
0 5 1
|
||||||
|
0 6 4
|
||||||
|
1 6 3
|
||||||
|
2 6 2
|
||||||
|
3 9 1
|
||||||
|
5 1 2
|
||||||
|
5 6 2
|
||||||
|
5 10 3
|
||||||
|
6 7 1
|
||||||
|
7 2 3
|
||||||
|
7 8 4
|
||||||
|
8 3 2
|
||||||
|
8 11 1
|
||||||
|
9 4 4
|
||||||
|
9 8 3
|
||||||
|
10 6 6
|
||||||
200
src/Algo.cpp
Normal file
200
src/Algo.cpp
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
#include "Algo.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
static constexpr int I_INFINITY = std::numeric_limits<int>::max();
|
||||||
|
static constexpr float F_INFINITY = std::numeric_limits<float>::infinity();
|
||||||
|
|
||||||
|
|
||||||
|
std::map<Vertex*, int> PlusCourtCheminNbArcs(Graphe& g, Vertex* s) {
|
||||||
|
std::vector<Vertex*> f;
|
||||||
|
|
||||||
|
std::map<Vertex*, int> longueurs;
|
||||||
|
std::map<Vertex*, bool> atteints;
|
||||||
|
|
||||||
|
for (Vertex& v : g.getVertecies()) {
|
||||||
|
longueurs.insert({&v, I_INFINITY});
|
||||||
|
atteints.insert({&v, false});
|
||||||
|
}
|
||||||
|
|
||||||
|
atteints[s] = true;
|
||||||
|
longueurs[s] = 0;
|
||||||
|
|
||||||
|
f.push_back(s);
|
||||||
|
|
||||||
|
while (!f.empty()) {
|
||||||
|
Vertex* v = f.back();
|
||||||
|
|
||||||
|
for (Edge* edge : v->m_Edges) {
|
||||||
|
Vertex* y = edge->m_End;
|
||||||
|
if (atteints[y] == true)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
atteints[y] = true;
|
||||||
|
longueurs[y] = longueurs[v] + 1;
|
||||||
|
f.push_back(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
f.erase(std::remove(f.begin(), f.end(), v));
|
||||||
|
}
|
||||||
|
|
||||||
|
return longueurs;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<Vertex*, Path> PlusCourtCheminsNbArcs(Graphe& g, Vertex* s) {
|
||||||
|
std::vector<Vertex*> f;
|
||||||
|
|
||||||
|
std::map<Vertex*, bool> atteints;
|
||||||
|
std::map<Vertex*, Vertex*> parents;
|
||||||
|
|
||||||
|
for (Vertex& v : g.getVertecies()) {
|
||||||
|
atteints.insert({&v, false});
|
||||||
|
}
|
||||||
|
|
||||||
|
atteints[s] = true;
|
||||||
|
|
||||||
|
f.push_back(s);
|
||||||
|
|
||||||
|
while (!f.empty()) {
|
||||||
|
Vertex* v = f.back();
|
||||||
|
|
||||||
|
for (Edge* edge : v->m_Edges) {
|
||||||
|
Vertex* y = edge->m_End;
|
||||||
|
if (atteints[y] == true)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
atteints[y] = true;
|
||||||
|
parents[y] = v;
|
||||||
|
f.push_back(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
f.erase(std::remove(f.begin(), f.end(), v));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::map<Vertex*, Path> chemins;
|
||||||
|
|
||||||
|
for (Vertex& v : g.getVertecies()) {
|
||||||
|
Path path;
|
||||||
|
|
||||||
|
path.push_back(&v);
|
||||||
|
|
||||||
|
auto parent_it = parents.find(&v);
|
||||||
|
|
||||||
|
while (parent_it != parents.end()) {
|
||||||
|
Vertex* parent = parent_it->second;
|
||||||
|
path.push_back(parent);
|
||||||
|
parent_it = parents.find(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::reverse(path.begin(), path.end());
|
||||||
|
|
||||||
|
chemins.insert({&v, path});
|
||||||
|
}
|
||||||
|
|
||||||
|
return chemins;
|
||||||
|
}
|
||||||
|
|
||||||
|
Path PlusCourtChemin(Graphe& g, Vertex* s, Vertex* arrivee) {
|
||||||
|
std::vector<Vertex*> f;
|
||||||
|
|
||||||
|
std::map<Vertex*, bool> atteints;
|
||||||
|
std::map<Vertex*, Vertex*> parents;
|
||||||
|
|
||||||
|
for (Vertex& v : g.getVertecies()) {
|
||||||
|
atteints.insert({&v, false});
|
||||||
|
}
|
||||||
|
|
||||||
|
atteints[s] = true;
|
||||||
|
|
||||||
|
f.push_back(s);
|
||||||
|
|
||||||
|
bool trouve = false;
|
||||||
|
|
||||||
|
while (!f.empty() && !trouve) {
|
||||||
|
Vertex* v = f.back();
|
||||||
|
|
||||||
|
for (Edge* edge : v->m_Edges) {
|
||||||
|
Vertex* y = edge->m_End;
|
||||||
|
if (atteints[y] == true)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
atteints[y] = true;
|
||||||
|
parents[y] = v;
|
||||||
|
f.push_back(y);
|
||||||
|
|
||||||
|
if (y == arrivee) {
|
||||||
|
trouve = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f.erase(std::remove(f.begin(), f.end(), v));
|
||||||
|
}
|
||||||
|
|
||||||
|
Path path;
|
||||||
|
path.push_back(arrivee);
|
||||||
|
|
||||||
|
auto parent_it = parents.find(arrivee);
|
||||||
|
|
||||||
|
while (parent_it != parents.end()) {
|
||||||
|
Vertex* parent = parent_it->second;
|
||||||
|
path.push_back(parent);
|
||||||
|
parent_it = parents.find(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::reverse(path.begin(), path.end());
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DjikstraData {
|
||||||
|
std::map<Vertex*, int> d;
|
||||||
|
std::map<Vertex*, Vertex*> p;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::map<Vertex*, float> PlusCourtCheminDjikstra(Graphe& g, Vertex* s) {
|
||||||
|
std::vector<Vertex*> z;
|
||||||
|
|
||||||
|
std::map<Vertex*, float> distances;
|
||||||
|
std::map<Vertex*, Vertex*> prev;
|
||||||
|
|
||||||
|
for (Vertex& v : g.getVertecies()) {
|
||||||
|
distances.insert({&v, F_INFINITY});
|
||||||
|
z.push_back(&v);
|
||||||
|
}
|
||||||
|
|
||||||
|
distances[s] = 0;
|
||||||
|
|
||||||
|
while (!z.empty()) {
|
||||||
|
Vertex* u = z.front();
|
||||||
|
float minimum = distances[u];
|
||||||
|
|
||||||
|
for (Vertex* vertex : z) {
|
||||||
|
float distance = distances[vertex];
|
||||||
|
if (distance < minimum) {
|
||||||
|
u = vertex;
|
||||||
|
minimum = distance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
z.erase(std::remove(z.begin(), z.end(), u));
|
||||||
|
|
||||||
|
for (Edge& e : g.getEdges()) {
|
||||||
|
if (e.m_Start != u)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float alt = distances[u] + e.m_Values[0];
|
||||||
|
|
||||||
|
Vertex* v = e.m_End;
|
||||||
|
|
||||||
|
if (alt < distances[v]) {
|
||||||
|
distances[v] = alt;
|
||||||
|
// prev[v] = u
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return distances;
|
||||||
|
}
|
||||||
11
src/Algo.h
Normal file
11
src/Algo.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Graphe.h"
|
||||||
|
|
||||||
|
std::map<Vertex*, int> PlusCourtCheminNbArcs(Graphe& g, Vertex* depart);
|
||||||
|
|
||||||
|
std::map<Vertex*, Path> PlusCourtCheminsNbArcs(Graphe& g, Vertex* depart);
|
||||||
|
|
||||||
|
Path PlusCourtChemin(Graphe& g, Vertex* depart, Vertex* arrivee);
|
||||||
|
|
||||||
|
std::map<Vertex*, float> PlusCourtCheminDjikstra(Graphe& g, Vertex* s);
|
||||||
145
src/Graphe.cpp
Normal file
145
src/Graphe.cpp
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
#include "Graphe.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <regex>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Graphe& g) {
|
||||||
|
os << "Graphe: " << g.file_name << "\n";
|
||||||
|
os << "Comment: " << g.comment << "\n";
|
||||||
|
os << "Directed: " << (g.directed ? "true" : "false") << "\n";
|
||||||
|
os << "Number of vertices: " << g.nb_vertices << "\n";
|
||||||
|
os << "Number of edges: " << g.nb_edges << "\n";
|
||||||
|
os << "Vertices:\n";
|
||||||
|
for (const auto& vertex : g.m_Vertecies) {
|
||||||
|
os << " ID: " << vertex.m_Name << ", Values: " << vertex.m_Values << "\n";
|
||||||
|
}
|
||||||
|
os << "Edges:\n";
|
||||||
|
for (const auto& edge : g.m_Edges) {
|
||||||
|
os << " Start: " << edge.m_Start->m_Name
|
||||||
|
<< ", End: " << edge.m_End->m_Name
|
||||||
|
<< ", Values: " << edge.m_Values << "\n";
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Graphe::Graphe(const string& fileName) {
|
||||||
|
// load the graph from the file
|
||||||
|
ifstream file(fileName);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
// cerr << "Error opening file: " << fileName << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
string line;
|
||||||
|
bool isSettingsSection = true;
|
||||||
|
bool verticesSection = false;
|
||||||
|
bool edgesSection = false;
|
||||||
|
while (getline(file, line)) {
|
||||||
|
if (line.empty()) {
|
||||||
|
if (isSettingsSection) {
|
||||||
|
isSettingsSection = false;
|
||||||
|
verticesSection = true;
|
||||||
|
// cout << "Settings section ended, now reading vertices." << endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (verticesSection) {
|
||||||
|
verticesSection = false;
|
||||||
|
edgesSection = true;
|
||||||
|
// cout << "Vertices section ended, now reading edges." << endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isSettingsSection) {
|
||||||
|
// cout << "Settings :" << line << endl;
|
||||||
|
regex settingRegex(R"(^([A-Z_]+):\s([\w\d\.]+)$)");
|
||||||
|
smatch match;
|
||||||
|
if (regex_match(line, match, settingRegex)) {
|
||||||
|
string key = match[1].str();
|
||||||
|
string value = match[2].str();
|
||||||
|
if (key == "NAME") {
|
||||||
|
file_name = value;
|
||||||
|
} else if (key == "COMMENT") {
|
||||||
|
comment = value;
|
||||||
|
} else if (key == "ORIENTED") {
|
||||||
|
directed = (value == "true");
|
||||||
|
} else if (key == "NB_VERTICES") {
|
||||||
|
nb_vertices = stoi(value);
|
||||||
|
} else if (key == "NB_EDGES") {
|
||||||
|
nb_edges = stoi(value);
|
||||||
|
} else if (key == "NB_VALUES_BY_VERTEX") {
|
||||||
|
nb_values_by_vertex = stoi(value);
|
||||||
|
} else if (key == "NB_VALUES_BY_EDGE") {
|
||||||
|
nb_values_by_edge = stoi(value);
|
||||||
|
} else {
|
||||||
|
cerr << "Unknown setting: " << key << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Vertecies.reserve(nb_vertices);
|
||||||
|
m_Edges.reserve(nb_edges);
|
||||||
|
|
||||||
|
if(verticesSection) {
|
||||||
|
// cout << "Reading vertices : " << line << endl;
|
||||||
|
if(line.starts_with("VER")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
regex vertexRegex(R"(^(\d+)\s(\w+)\s?(.*)$)");
|
||||||
|
smatch match;
|
||||||
|
if (regex_match(line, match, vertexRegex)) {
|
||||||
|
Vertex v;
|
||||||
|
int id = stoi(match[1].str());
|
||||||
|
v.m_Name = match[2].str();
|
||||||
|
string valuesStr = match[3].str();
|
||||||
|
regex valueRegex(R"((\d+))");
|
||||||
|
auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex);
|
||||||
|
auto valuesEnd = sregex_iterator();
|
||||||
|
for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) {
|
||||||
|
v.m_Values.push_back(stof((*i).str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Vertecies.push_back(v);
|
||||||
|
m_VertexMap[v.m_Name] = &m_Vertecies[id];
|
||||||
|
} else {
|
||||||
|
cerr << "Invalid vertex line: " << line << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (edgesSection) {
|
||||||
|
// cout << "Reading edges : " << line << endl;
|
||||||
|
if(line.starts_with("EDG")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
regex edgeRegex(R"(^(\d+)\s(\d+)\s?(.*)$)");
|
||||||
|
smatch match;
|
||||||
|
if (regex_match(line, match, edgeRegex)) {
|
||||||
|
Edge e;
|
||||||
|
e.m_Start = &m_Vertecies[stoi(match[1].str())];
|
||||||
|
e.m_End = &m_Vertecies[stoi(match[2].str())];
|
||||||
|
string valuesStr = match[3].str();
|
||||||
|
regex valueRegex(R"((\d+))");
|
||||||
|
auto valuesBegin = sregex_iterator(valuesStr.begin(), valuesStr.end(), valueRegex);
|
||||||
|
auto valuesEnd = sregex_iterator();
|
||||||
|
for (sregex_iterator i = valuesBegin; i != valuesEnd; ++i) {
|
||||||
|
e.m_Values.push_back(stof((*i).str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t edgeID = m_Edges.size();
|
||||||
|
m_Edges.push_back(e);
|
||||||
|
e.m_Start->m_Edges.push_back(&m_Edges[edgeID]);
|
||||||
|
if (!directed) {
|
||||||
|
Edge reverseEdge;
|
||||||
|
reverseEdge.m_Start = e.m_End;
|
||||||
|
reverseEdge.m_End = e.m_Start;
|
||||||
|
reverseEdge.m_Values = e.m_Values;
|
||||||
|
std::size_t index = m_Edges.size();
|
||||||
|
m_Edges.push_back(reverseEdge);
|
||||||
|
reverseEdge.m_Start->m_Edges.push_back(&m_Edges[index]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cerr << "Invalid edge line: " << line << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
86
src/Graphe.h
Normal file
86
src/Graphe.h
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
struct Edge;
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
std::vector<float> m_Values;
|
||||||
|
std::vector<Edge*> m_Edges;
|
||||||
|
|
||||||
|
std::size_t getWeight() {
|
||||||
|
return m_Edges.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string m_Name;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Edge {
|
||||||
|
Vertex* m_Start;
|
||||||
|
Vertex* m_End;
|
||||||
|
std::vector<float> m_Values;
|
||||||
|
};
|
||||||
|
|
||||||
|
using VertexMap = std::map<std::string, Vertex*>;
|
||||||
|
|
||||||
|
using Path = std::vector<Vertex*>;
|
||||||
|
|
||||||
|
class Graphe {
|
||||||
|
private:
|
||||||
|
VertexMap m_VertexMap;
|
||||||
|
std::vector<Vertex> m_Vertecies;
|
||||||
|
std::vector<Edge> m_Edges;
|
||||||
|
|
||||||
|
std::string file_name;
|
||||||
|
std::string comment;
|
||||||
|
bool directed;
|
||||||
|
int nb_vertices;
|
||||||
|
int nb_edges;
|
||||||
|
int nb_values_by_vertex;
|
||||||
|
int nb_values_by_edge;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Graphe(const std::string& fileName);
|
||||||
|
|
||||||
|
std::vector<Vertex>& getVertecies() {
|
||||||
|
return m_Vertecies;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertex* getVertex(const std::string& name) {
|
||||||
|
return m_VertexMap.at(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Edge>& getEdges() {
|
||||||
|
return m_Edges;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const Graphe& graphe);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, const Path& p) {
|
||||||
|
os << "[";
|
||||||
|
for (size_t i = 0; i < p.size(); i++) {
|
||||||
|
os << p[i]->m_Name;
|
||||||
|
if (i < p.size() - 1) {
|
||||||
|
os << ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return os << "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& os, const std::vector<T>& value) {
|
||||||
|
os << "[";
|
||||||
|
for (size_t i = 0; i < value.size(); i++)
|
||||||
|
{
|
||||||
|
os << value[i];
|
||||||
|
if (i < value.size() - 1) {
|
||||||
|
os << ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os << "]";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
15
src/PrettyPrint.h
Normal file
15
src/PrettyPrint.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void printYellow(const std::string& text) {
|
||||||
|
std::cout << "\033[33m" << text << "\033[0m";
|
||||||
|
}
|
||||||
|
void printRed(const std::string& text) {
|
||||||
|
std::cout << "\033[31m" << text << "\033[0m";
|
||||||
|
}
|
||||||
|
void printGreen(const std::string& text) {
|
||||||
|
std::cout << "\033[32m" << text << "\033[0m";
|
||||||
|
}
|
||||||
|
void printBlue(const std::string& text) {
|
||||||
|
std::cout << "\033[34m" << text << "\033[0m";
|
||||||
|
}
|
||||||
33
src/main.cpp
Normal file
33
src/main.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "Algo.h"
|
||||||
|
#include "Graphe.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
Graphe graphe("graphe-oriente-01.gra");
|
||||||
|
std::cout << graphe << std::endl;
|
||||||
|
auto result = PlusCourtCheminNbArcs(graphe, graphe.getVertex("a"));
|
||||||
|
|
||||||
|
std::cout << "Résultat :\n";
|
||||||
|
for (auto& [vertex, longueur] : result) {
|
||||||
|
std::cout << vertex->m_Name << " " << longueur << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result2 = PlusCourtCheminsNbArcs(graphe, graphe.getVertex("a"));
|
||||||
|
|
||||||
|
std::cout << "Résultat :\n";
|
||||||
|
for (auto& [vertex, chemin] : result2) {
|
||||||
|
std::cout << vertex->m_Name << " " << chemin << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result3 = PlusCourtChemin(graphe, graphe.getVertex("a"), graphe.getVertex("e"));
|
||||||
|
|
||||||
|
std::cout << "Résultat :\n";
|
||||||
|
std::cout << result3 << std::endl;
|
||||||
|
|
||||||
|
auto result4 = PlusCourtCheminDjikstra(graphe, graphe.getVertex("a"));
|
||||||
|
std::cout << "Résultat :\n";
|
||||||
|
for (auto& [vertex, longueur] : result4) {
|
||||||
|
std::cout << vertex->m_Name << " " << longueur << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
80
xmake.lua
Normal file
80
xmake.lua
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
add_rules("mode.debug", "mode.release")
|
||||||
|
|
||||||
|
set_languages("c++20")
|
||||||
|
|
||||||
|
set_warnings("all")
|
||||||
|
|
||||||
|
target("Graphe")
|
||||||
|
set_kind("binary")
|
||||||
|
add_files("src/*.cpp")
|
||||||
|
set_rundir(".")
|
||||||
|
|
||||||
|
--
|
||||||
|
-- If you want to known more usage about xmake, please see https://xmake.io
|
||||||
|
--
|
||||||
|
-- ## FAQ
|
||||||
|
--
|
||||||
|
-- You can enter the project directory firstly before building project.
|
||||||
|
--
|
||||||
|
-- $ cd projectdir
|
||||||
|
--
|
||||||
|
-- 1. How to build project?
|
||||||
|
--
|
||||||
|
-- $ xmake
|
||||||
|
--
|
||||||
|
-- 2. How to configure project?
|
||||||
|
--
|
||||||
|
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
||||||
|
--
|
||||||
|
-- 3. Where is the build output directory?
|
||||||
|
--
|
||||||
|
-- The default output directory is `./build` and you can configure the output directory.
|
||||||
|
--
|
||||||
|
-- $ xmake f -o outputdir
|
||||||
|
-- $ xmake
|
||||||
|
--
|
||||||
|
-- 4. How to run and debug target after building project?
|
||||||
|
--
|
||||||
|
-- $ xmake run [targetname]
|
||||||
|
-- $ xmake run -d [targetname]
|
||||||
|
--
|
||||||
|
-- 5. How to install target to the system directory or other output directory?
|
||||||
|
--
|
||||||
|
-- $ xmake install
|
||||||
|
-- $ xmake install -o installdir
|
||||||
|
--
|
||||||
|
-- 6. Add some frequently-used compilation flags in xmake.lua
|
||||||
|
--
|
||||||
|
-- @code
|
||||||
|
-- -- add debug and release modes
|
||||||
|
-- add_rules("mode.debug", "mode.release")
|
||||||
|
--
|
||||||
|
-- -- add macro definition
|
||||||
|
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
||||||
|
--
|
||||||
|
-- -- set warning all as error
|
||||||
|
-- set_warnings("all", "error")
|
||||||
|
--
|
||||||
|
-- -- set language: c99, c++11
|
||||||
|
-- set_languages("c99", "c++11")
|
||||||
|
--
|
||||||
|
-- -- set optimization: none, faster, fastest, smallest
|
||||||
|
-- set_optimize("fastest")
|
||||||
|
--
|
||||||
|
-- -- add include search directories
|
||||||
|
-- add_includedirs("/usr/include", "/usr/local/include")
|
||||||
|
--
|
||||||
|
-- -- add link libraries and search directories
|
||||||
|
-- add_links("tbox")
|
||||||
|
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
||||||
|
--
|
||||||
|
-- -- add system link libraries
|
||||||
|
-- add_syslinks("z", "pthread")
|
||||||
|
--
|
||||||
|
-- -- add compilation and link flags
|
||||||
|
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
||||||
|
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
||||||
|
--
|
||||||
|
-- @endcode
|
||||||
|
--
|
||||||
|
|
||||||
Reference in New Issue
Block a user