-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
200 lines (156 loc) · 4.42 KB
/
Graph.java
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import Queue.LinkedUnbndQueue;
import Queue.UnboundedQueueInterface;
public class Graph<T> implements WeightedGraphInterface<T>{
private AdjecencyLinkedList<T>[] graph;
//private AdjecencyLinkedList<Integer> adjencyNodes;
private T[] vertices;
private int sizeGraph,
numVertices;
private boolean[] marks;
public Graph(int sizeGraph) {
this.graph = new AdjecencyLinkedList[sizeGraph];
this.sizeGraph = sizeGraph;
this.vertices = (T[]) new Object[sizeGraph];
this.numVertices = 0;
this.marks= new boolean[this.sizeGraph];
clearMarks();
}
public boolean isEmpty() {
return (this.numVertices == 0);
}
public boolean isFull() {
return (this.numVertices == this.sizeGraph);
}
public void addVertex(T vertex) {
if(isFull()){
throw new RuntimeException();
} else{
this.vertices[this.numVertices] = vertex;
} this.numVertices ++;
}
public void addVertex(T vertex, int index) {
if(isFull()){
throw new RuntimeException();
} else{
this.vertices[index] = vertex;
} this.numVertices ++;
}
public boolean hasVertex(T vertex){
for(int i = 0; i < this.numVertices; i++){
if(this.vertices[i].equals(vertex)){
return true;
}
} return false;
}
public void addEdge(T fromVertex, T toVertex, int weight) {
int start = indexIs(fromVertex);
int last = indexIs(toVertex);
AdjecencyLinkedList<Integer> adjencyNode = new AdjecencyLinkedList(last, weight);
AdjecencyLinkedList<Integer> currentNode = (AdjecencyLinkedList<Integer>) this.graph[start];
if(this.graph[start] == null){
this.graph[start] = (AdjecencyLinkedList<T>) adjencyNode;
} else{
while(currentNode.getLink() != null){
currentNode = currentNode.getLink();
} currentNode.setLink(adjencyNode);
}
}
public int indexIs(T vertex){
int index = 0;
while(!(vertex.equals(this.vertices[index]))) {
index ++;
} return index;
}
public int weightIs(T fromVertex, T toVertex) {
int start = indexIs(fromVertex);
int last = indexIs(toVertex);
AdjecencyLinkedList<Integer> first= (AdjecencyLinkedList<Integer>) this.graph[start];
if(first == null){
return -1;
}
if(first.getEdge() == last){
return first.getInfo();
}
while(first.getLink() != null){
first = first.getLink();
if(first.getEdge() == last){
return first.getInfo();
}
} return -1;
}
public UnboundedQueueInterface<T> getToVertices(T vertex) {
int index = indexIs(vertex);
LinkedUnbndQueue<T> queue = new LinkedUnbndQueue<T>();
AdjecencyLinkedList<Integer> first = (AdjecencyLinkedList<Integer>) this.graph[index];
if(first == null){
return queue;
} queue.enqueue(this.vertices[first.getEdge()]);
while (first.getLink() != null){
first = first.getLink();
queue.enqueue(this.vertices[first.getEdge()]);
} return queue;
}
public void clearMarks() {
for(int i = 0; i < this.sizeGraph; i++){
this.marks[i] = false;
}
}
public void markVertex(T vertex) {
int index = indexIs(vertex);
this.marks[index] = true;
}
public boolean isMarked(T vertex) {
int index = indexIs(vertex);
return this.marks[index];
}
public T getUnmarked() {
for(int i = 0; i < this.numVertices; i++){
if(!isMarked(this.vertices[i])){
return this.vertices[i];
}
} return null;
}
public String toString(){
String g = "Graph: \n";
System.out.println(this.graph[0].getEdge()+"SDFGHJK");
for(int i = 0; i < this.numVertices; i++){
g = g + i + " " + this.vertices[i] + " to " + (this.graph[i].getLink().getEdge()) + "\n";
}
return g;
}
public String printQueue(T vertex){
String queue = "Queue: ";
LinkedUnbndQueue<T> start = (LinkedUnbndQueue<T>) getToVertices(vertex);
while(!start.isEmpty()){
queue += "( " + start.dequeue() + " )";
}
return queue;
}
public boolean breadthPath(T startVertex, T endVertex){
UnboundedQueueInterface<T> queue = new LinkedUnbndQueue<>();
UnboundedQueueInterface<T> vertexQueue = new LinkedUnbndQueue<>();
boolean found = false;
T vertex;
T element;
clearMarks();
queue.enqueue(startVertex);
do{
vertex = (T) queue.dequeue();
if(vertex==endVertex){
found=true;
}else{
if(!isMarked(vertex)){
markVertex(vertex);
vertexQueue = getToVertices(vertex);
while(!vertexQueue.isEmpty()){
element=(T) vertexQueue.dequeue();
if(!isMarked(element)){
queue.enqueue(element);
}
}
}
}
}while(!queue.isEmpty()&&!found);
return found;
}
}