-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.h
88 lines (79 loc) · 1.67 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
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <vector>
#include <list>
#include <limits.h>
#include <cassert>
#include <algorithm>
#ifndef _GRAPH_H
#define _GRAPH_H
struct Edge{
int _vertex;
int _weight;
Edge(int vertex, int weight):
_vertex(vertex), _weight(weight){
}
};
class Graph {
private:
/*
* Adjacency List Data structure
*/
std::vector < std::list<Edge> > _m;
const int _NUM_NODES;
const int _NUM_EDGES;
public:
int num_nodes() const;
int num_edges() const;
Graph(int NUM_NODES, int NUM_EDGES);
void insert(int x, int y, int weight);
std::list<Edge>& operator [] (int x);
void print();
};
/**
* Constructor:
* Require number of nodes, and number of edges
*/
Graph::Graph(int NUM_NODES, int NUM_EDGES)
: _m(NUM_NODES+1),
_NUM_NODES(NUM_NODES),
_NUM_EDGES(NUM_EDGES) {
}
/**
* @return: number of nodes;
*/
int Graph::num_nodes() const{
return _NUM_NODES;
}
/**
* @return: total number of edges:
*/
int Graph::num_edges() const{
return _NUM_EDGES;
}
/**
* Insert an edge.
*/
void Graph::insert(int x, int y, int weight) {
Edge node(y, weight);
_m[x].push_back(node );
}
/**
* @return: the edges from node,
* @param: x node
*/
std::list<Edge>& Graph::operator [] (int x){
return _m[ x ];
}
/**
* Print matrix containers
*/
void Graph::print() {
for (unsigned int i = 0; i < _m.size(); ++i) {
std::list<Edge> x = _m[i];
std::list<Edge>::iterator iterator;
for (iterator = x.begin(); iterator != x.end(); ++iterator) {
Edge node = *iterator;
std::cout << i << " " << node._vertex << " " << node._weight << std::endl;
}
}
}
#endif