-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
75 lines (68 loc) · 1.93 KB
/
graph.h
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
/* Klasa SP jest grafem, ktory modeluje np. spolecznosc. Wierzcholki sa napisami (np. nazwiska osob), krawedzie skierowane modeluja relacje pomiedzy osobami. Implementacja metod:
- dodawanie wierzcholka
- usuwanie wierzcholkow i wszystkich krawedzi
- dodawanie krawedzi miedzy osobami
- laczenie wierzcholkow (i scalanie przy tym krawedzi) */
#pragma once
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class SP
{
map<const char*, vector<const char*>> graph;
public:
SP()
{};
void add_vertex(const char* v);
void del_vertex(const char* v);
void add_edge(const char* p1, const char* p2);
void connect_vertex(const char* p1, const char* p2);
void write()
{
for (map<const char*, vector<const char*>>::iterator it = graph.begin(); it != graph.end(); ++it)
{
cout << it->first << " => ";
for (vector<const char*>::iterator it_vector = it->second.begin(); it_vector != it->second.end(); it_vector++)
cout << *it_vector << ", ";
cout << endl;
}
}
};
void SP::add_vertex(const char* v)
{
pair<map<const char*, vector<const char*>>::iterator, bool> ret;
ret = graph.insert({ v, vector<const char*>() });
if (ret.second == false)
cout << "element already exists" << endl;
}
void SP::del_vertex(const char* v)
{
if (graph.find(v) != graph.end())
{
graph[v].clear();
graph.erase(v);
}
else
cout << "element doesn't exist" << endl;
}
void SP::add_edge(const char* p1, const char* p2)
{
if (graph.find(p1) != graph.end())
{
if (find(graph[p1].begin(), graph[p1].end(), p2) == graph[p1].end())
graph[p1].push_back(p2);
else
cout << "edge already exist" << endl;
}
else
cout << "vertex doesn't exist" << endl;
}
void SP::connect_vertex(const char* p1, const char* p2)
{
for (vector<const char*>::iterator it = graph[p2].begin(); it != graph[p2].end(); it++)
if (find(graph[p1].begin(), graph[p1].end(), *it) == graph[p1].end())
graph[p1].push_back(*it);
graph[p2].clear();
graph.erase(p2);
}