-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigraph.h
63 lines (47 loc) · 1.76 KB
/
digraph.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
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <set>
using namespace std;
/*
Represents a graph using an adjacency list representation.
Vertices are assumed to be integers.
*/
class Digraph {
public:
// No constructor or destructor are necessary this time.
// A new instance will be an empty graph with no nodes.
// add a vertex, does nothing if it already exists
void addVertex(int v);
// adds an edge, creating the vertices if they do not exist
// if the edge already existed, does nothing
void addEdge(int u, int v);
// returns true if and only if v is a vertex in the graph
bool isVertex(int v);
// returns true if and only if (u,v) is an edge in the graph
// will certainly return false if neither vertex is in the graph
bool isEdge(int u, int v);
// returns a const iterator to the neighbours of v
unordered_set<int>::const_iterator neighbours(int v) const;
// returns a const iterator to the end of v's neighour set
unordered_set<int>::const_iterator endIterator(int v) const;
// return the number of outgoing neighbours of v
int numNeighbours(int v);
// returns the number of nodes
int size();
// return a vector with all vertices
vector<int> vertices();
// returns true if 'walk' represents a walk on this graph
// A walk is a sequence of vertices (perhaps with repeated vertices)
// v0, v1, . . . , vk where (vi,vi+1) is an edge for each 0 <= i < k.
// the length of a walk is the number of edges traversed
bool isWalk(vector<int> walk);
// returns true if 'path' represents a path on this graph
// a path is a walk with no repeated vertices
bool isPath(vector<int> path);
private:
unordered_map<int, unordered_set<int>> nbrs;
};
#endif