-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
105 lines (70 loc) · 1.72 KB
/
Copy pathGraph.h
File metadata and controls
105 lines (70 loc) · 1.72 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
#ifndef _GRAPH_H
#define _GRAPH_H_
#include "dynArray.h"
#include "SLinkedList.h"
#include "Queue.h"
typedef unsigned int uint;
using namespace std;
template <class TYPE>
class Graph{
struct vertex{
TYPE data;
Vector<vertex*> edges;
vertex(const TYPE& data) : data(data){}
bool AddEdge(vertex* destination)
{
if (std::find(edges.begin(), edges.end(), destination) != edges.end())
return false;
edges.push_back(destination);
return true;
}
bool pepitojuanito(const vertex* dst, Vector<const vertex*>& visited_nodes) const
{
if (dst == this)
return true;
visited_nodes.push_back(this);
/* for (const List<vertex*>::node* item = links.front_node(); item; item = item->next())
{
if (visited_nodes.find(item->data) == visited_nodes.size())
{
if (item->data->pepitojuanito(dst, visited_nodes) == true)
return true;
}
}
*/
for (int i = 0; i < edges.size(); i++)
if (std::find(visited_nodes.begin(), visited_nodes.end(), edges[i]) != edges.end())
edges[i]->pepitojuanito(dst, visited_nodes);
return false;
}
};
public:
Vector<vertex*> vertices;
Graph(){}
~Graph(){}
public:
vertex* push(const TYPE& data)
{
vertex new_vertex = new vertex(data);
vertices.push_back(new_vertex);
return new_vertex;
}
uint size() const
{
return vertices.size();
}
bool empty() const
{
return vertices.empty();
}
void clear()
{
vertices.clear();
}
bool is_reachable(const vertex* src, const vertex* dst) const
{
Vector<const vertex*> visited_nodes(size());
return src->pepitojuanito(dst, visited_nodes);
}
};
#endif