Repository navigation
Expand file tree
/
Copy pathgraph.cpp
More file actions
166 lines (140 loc) · 3.19 KB
/
Copy pathgraph.cpp
File metadata and controls
166 lines (140 loc) · 3.19 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
#include <iostream>
#include "graph.h"
#include "colorize.h"
using namespace std;
/* Note that the following graph structure is already declared in graph.h
as extern variables.
These are definitions.
*/
int V[100];
int E[100][100];
int Vsize=0;
int Esize[100];
int stackV[100];
int stackE[100][100];
int stackPtr = 0;
int stackEsize[100];
/* End of Global datastructures that are declared in graph.h */
void eraseVertex(int index) {
for(int i=index; i<Vsize-1; ++i) {
V[i] = V[i+1];
}
Vsize = Vsize - 1;
}
void eraseEdge(int index) {
/* Should logically by i=index to Vsize-1 but global Vsize already altered above.
This is a cheap trick. :)
*/
for(int i=index; i<Vsize; ++i) {
for(int j=0; j<Esize[i+1]; ++j) {
E[i][j] = E[i+1][j];
}
Esize[i] = Esize[i+1];
}
}
void eraseSingleEdge(int index, int jndex) {
for(int i=jndex; i<Esize[index]-1; ++i) {
E[index][i] = E[index][i+1];
}
Esize[index] = Esize[index] - 1;
}
/* Checks if an Element ele exists in an Array arr[] whose size is asize */
bool exists(int ele, int asize, int arr[]) {
int i;
for(i=0; i<asize; ++i) {
if(arr[i] == ele) return 1;
}
return 0;
}
/* Pops the top G(v,e) set from stack to graph */
void pop() {
int tos = stackPtr - 1;
int v = stackV[tos];
int esize = stackEsize[tos];
int e[esize];
for(int i=0; i<esize; ++i) {
e[i] = stackE[tos][i];
}
insertVertex(v,esize,e);
stackPtr = stackPtr - 1;
}
/* Returns the position of a vertex v in the V[] data struct */
int getIndex(int v) {
int Index = -1;
for(int i = 0; i < Vsize; ++i) {
if(v==V[i]) Index = i;
}
return Index;
}
/*Inserts a Vertex v into V[], and its edges e[] into E[][]*/
void insertVertex(int v, int esize, int e[]) {
int i, currV;
if(Vsize == 0) {
Vsize = 1;
V[0] = v;
for(i=0; i<esize; ++i) {
E[0][i] = e[i];
}
Esize[0] = esize;
}
else {
currV = Vsize;
Vsize = Vsize + 1;
V[currV] = v;
for(i=0; i<esize; ++i) {
E[currV][i] = e[i];
}
Esize[currV] = esize;
for(i=0; i<Vsize; ++i) {
if((!(exists(v,Esize[i],E[i])))&&(exists(V[i],esize,e)))
{
E[i][Esize[i]] = v;
Esize[i] = Esize[i] + 1;
}
}
}
//printGraph();
}
/*Deletes a vertex with value v*/
void deleteVertex(int v) {
int DeletionIndex = -1;
for(int i = 0; i < Vsize; ++i) {
if(v==V[i]) DeletionIndex = i;
}
if(DeletionIndex == -1) cout<<"\nVERTEX NOT FOUND\n";
else {
/*Saves onto the Stack before proceding */
stackV[stackPtr] = V[DeletionIndex];
stackEsize[stackPtr] = Esize[DeletionIndex];
for(int i=0; i<Esize[DeletionIndex]; ++i)
stackE[stackPtr][i] = E[DeletionIndex][i];
stackPtr = stackPtr + 1;
/* erases V[DeletionIndex] */
eraseVertex(DeletionIndex);
/* erases E[DeletionIndex][] */
eraseEdge(DeletionIndex);
/* Any v occurence in the Edges of other Vertices?
Checks and deletes */
for(int i=0; i<Vsize; ++i) {
for(int j=0; j<Esize[i]; ++j) {
if(v==E[i][j]) {
eraseSingleEdge(i,j);
break;
}
}
}
}
}
/*Prints the Interference Graph*/
void printGraph() {
int i, j;
cout<<"\nVERTEX \tEDGES \tCOLOR";
for(i=0; i<Vsize; ++i) {
cout<<"\n"<<V[i]<<"\t";
for(j=0; j<Esize[i]; ++j) {
cout<<E[i][j];
}
cout<<"\t"<<Vcolor[i];
}
cout<<"\n";
}