-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiGraphAdjList.h
More file actions
68 lines (56 loc) · 1.21 KB
/
Copy pathDiGraphAdjList.h
File metadata and controls
68 lines (56 loc) · 1.21 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
#ifndef __DIGRAPH_ADJ_LIST_H__
#define __DIGRAPH_ADJ_LIST_H__
#include "DiGraph.h"
using namespace std;
namespace ds {
/**
* Directed graph implementation using **adjacent list**
*/
class DiGraphAdjList : public DiGraph {
private:
int V; // Number of vertices
VList<int> *adjList; // Array of lists indexed by vertex id
public:
/**
* Create empty directed graph with V vertices
*
* @param _V number of vertices
*/
explicit DiGraphAdjList(int _V) {
V = _V;
adjList = new VList<int>[V];
}
/**
* Destroy the DiGraphAdjList object
*/
~DiGraphAdjList() { delete[] adjList; }
/**
* @return number of vertices
*/
int v() const { return V; }
/**
* @return number of edges
*/
int e() const {
int sum = 0;
for (int i = 0; i < v(); i++) {
sum += adjList[i].size();
}
return sum;
}
/**
* @param v node id
* @return a list of nodes adjacent to v
*/
VList<int> adj(int v) const {
if (v >= V)
return VList<int>();
return adjList[v];
}
void addEdge(int v1, int v2);
void delEdge(int v1, int v2);
bool hasEdge(int v1, int v2) const;
bool hasCycle() const;
};
} // namespace ds
#endif // __DIGRAPH_ADJ_LIST_H__