-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListGraph.h
More file actions
172 lines (160 loc) · 2.68 KB
/
ListGraph.h
File metadata and controls
172 lines (160 loc) · 2.68 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
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include <cstdlib>
struct Node
{
int n;
Node *next;
};
template <typename T>class ListGraph
{
private:
Node **adj = nullptr; // head of the adj list
int MaxN;
T* data;
public:
void ListDfs(int, bool*);
int listDistance(int, int);
ListGraph(std::string, T*); // constructor
~ListGraph(); //destructor
bool connected(); //check if graph is connected
void print();
};
template<typename T>
void ListGraph<T>::print()
{
Node *p;
for (int i = 0; i < MaxN; ++i)
{
p = adj[i];
std::cout << i << ": ";
while (p)
{
std::cout << p->n << " ";
p = p->next;
}
std::cout << endl;
}
for (int i = 0; i < MaxN; ++i)
{
std::cout << "Data: " << data[i] << endl;
}
}
template <typename T>
ListGraph<T>::ListGraph(std::string fname, T* dat)
{
ifstream in(fname, ios::binary);
in.read((char*)&MaxN, sizeof(int));
data = new T[MaxN];
adj = new Node*[MaxN];
for (int i = 0; i < MaxN; ++i)
{
adj[i] = nullptr;
data[i] = dat[i];
}
int k = 0;
for (int i = 0; i < MaxN; i++)
{
for (int j = 0; j < MaxN; j++)
{
in.read((char*)&k, sizeof(int));
if (k == 1)
{
Node* p = new Node;
p->n = j;
p->next = adj[i];
adj[i] = p;
}
}
}
in.close();
}
template <typename T>
int ListGraph<T>::listDistance(int u, int v)
{
if (u == v) return 0;
if (u < 0 || u>MaxN || v < 0 || v > MaxN) { return -1; } //wrong value of vertex
stack <int> s;
bool *visited = new bool[MaxN];
int *prev = new int[MaxN];
for (int i = 0; i < MaxN; ++i)
{
visited[i] = false;
prev[i] = -1;
}
s.push(u);
int w = 0; //vertex from the stack
Node* p = nullptr;
while (!s.empty())
{
w = s.top();
s.pop();
visited[w] = true;
p = adj[w];
while (p)
{
if (!visited[p->n])
{
s.push(p->n);
prev[p->n] = w;
}
if (p->n == v) break;
p = p->next;
}
if (p) break;
}
if (s.empty()) { return -1; }// vertexes are not connected
int n = 0;
w = v;
while (w != u)
{
++n;
w = prev[w];
}
return n;
}
template <typename T>
void ListGraph<T>::ListDfs(int u, bool *visited)
{
visited[u] = true;
for (Node* p = adj[u]; p; p = p->next)
{
if (!visited[p->n]) { ListDfs(p->n, visited); }
}
}
template <typename T>
bool ListGraph<T>::connected()
{
bool *visited = new bool[MaxN];
for (int i = 0; i < MaxN; ++i) { visited[i] = false; }
int c = 0;
for (int i = 0; i < MaxN; ++i)
{
if (!visited[i])
{
c++;
ListDfs(i, visited);
}
}
if (c > 1) { return false; }
return true;
}
template<typename T>
ListGraph<T>:: ~ListGraph()
{
Node* p, *t;
for (int i = 0; i < MaxN; ++i)
{
p = adj[i];
while (p)
{
t = p;
p = p->next;
delete t;
}
}
delete[] adj;
}