-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphADT
More file actions
146 lines (115 loc) · 3.29 KB
/
GraphADT
File metadata and controls
146 lines (115 loc) · 3.29 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
// OOP_Assignment1_GraphADT.cpp
// Chung-thuy Pham
#include "pch.h"
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
//template <class type>
class Graph {
// No need to declare private variables.
// All about 'function'alities, not data.
public:
//virtual bool adjacent(type x, type y) = 0; // Is there a path from x to y
//virtual vector<type> neighbors(type x) = 0; // Return a vector containing the nodes have an edge from x to elements in the vector
virtual void addEdge(int src, int dest) = 0;
virtual void deleteEdge(int src, int dest) = 0;
virtual bool adjacent(int x, int y) = 0;
};
template <class type>
class AdjList : public Graph
{
private:
int vertices;
list<type> *adj;
public:
AdjList(int size)
{
vertices = size;
this->vertices = vertices;
adj = new list<type>[vertices];
}
void addEdge(type x, type y) {
adj[x].push_back(y); // Add y to x's list
}
void deleteEdge(type x, type y) {
adj[x].erase(y);
}
void printList(type x) {
for (list<int>::iterator it = adj[x].begin(); it != adj[x].end(); ++it)
cout << *it << ' ';
}
};
template <class type>
class AdjMatrix : public Graph
{
private:
int vertices; // Nodes
int rowCount, colCount;
int** ary; // Look up what ** does. Mayone one pointer to col, one to row (?)
//int* ary = new int[aSize]; how to do 1D dynamic array
void initArytoZero()
{
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++)
ary[r][c] = 0;
}
}
public:
// Constructor
AdjMatrix(int size) {
vertices = size;
rowCount = colCount = vertices;
ary = new type*[rowCount]; // Look up what this syntax is actually doing
for (int i = 0; i < rowCount; i++)
ary[i] = new int[colCount]; // Look up what this syntax is actually doing
initArytoZero();
}
void addEdge(type x, type y) {
ary[x][y] = 1;
}
void deleteEdge(type x, type y) {
ary[x][y] = 0;
}
bool adjacent(type x, type y) {
bool flag;
if (ary[x][y] == 1)
flag = true;
/*else if (ary[y][x] == 1) //Add this if graph is undirected
flag = true;*/
else
flag = false;
return flag;
}
void printMatrix() { // for debug
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
cout << ary[r][c] << " ";
}
cout << endl;
}
}
};
int main() {
AdjMatrix<int> mat1(5);
mat1.addEdge(0, 2);
mat1.addEdge(0, 4);
mat1.addEdge(1, 0);
mat1.addEdge(3, 1);
mat1.addEdge(4, 3);
if (mat1.adjacent(4, 0)) // for debug
cout << "Adjacent" << endl;
else
cout << "Not adjacent" << endl;
mat1.printMatrix();
/* AdjList<int> list1(5);
list1.addEdge(0, 2);
list1.addEdge(0, 4);
list1.addEdge(1, 0);
list1.addEdge(3, 1);
list1.addEdge(4, 3);
list1.printList(4);*/
return 0;
}