Repository navigation
Expand file tree
/
Copy pathGraphAdyList.java
More file actions
159 lines (137 loc) · 3.15 KB
/
Copy pathGraphAdyList.java
File metadata and controls
159 lines (137 loc) · 3.15 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
package Graphs;
/**
*
* @author Wilson Soto
* @since 20.06.16
*
*/
public class GraphAdyList implements Graph {
boolean diGraph;
int maxVertex;
int numVertex;
List listAdy [];
public GraphAdyList (boolean d) {
maxVertex = numVertex = 0;
diGraph = d;
}
public GraphAdyList (int n, boolean d) {
diGraph = d;
maxVertex = n;
numVertex = 0;
listAdy = new List[n];
}
public void insertVertex(int n) {
if (n > maxVertex - numVertex)
System.out.println ("Error");
else {
for (int i = numVertex; i < numVertex + n; i++)
listAdy[i]= new List();
numVertex += n;
}
}
public void deleteVertex(int v) {
if (v > listAdy.length)
System.out.println("Error");
else {
listAdy[v].firstNode = null;
for (int i=0; i<listAdy.length; i++)
if (existEdge(i, v))
deleteEdge(i,v);
}
}
public void insertEdge(int i, int j) {
if (i>=numVertex && j>=numVertex)
System.out.println("Error");
else {
listAdy[i].add(listAdy[i].size, j);
if (!diGraph)
listAdy[j].add(listAdy[j].size, i);
}
}
public void deleteEdge(int i, int j) {
if (i>= numVertex && j>=numVertex)
System.out.println("Error");
else {
listAdy[i].remove (listAdy[i].indexOf(j));
if (!diGraph)
if (listAdy[j].indexOf(i)!=-1)
listAdy[j].remove (listAdy[j].indexOf(i));
}
}
public boolean isEmpty() {
for (int i=0; i<listAdy.length; i++)
if (listAdy[i]!=null)
return false;
return true;
}
public boolean existEdge(int i, int j) {
if (listAdy[i]==null)
return false;
ListNode aux = listAdy[i].firstNode;
while (aux!=null) {
if (aux.element.equals(j))
return true;
aux = aux.next;
}
return false;
}
public int degreeIn(int v) {
int dgIn = 0;
for (int i = 0; i < numVertex; i++)
if (i != v)
if (listAdy[i].indexOf(v)!=-1)
dgIn++;
return dgIn;
}
public int degreeOut(int i) {
int dgOut = 0;
ListNode aux = listAdy[i].firstNode;
while (aux != null){
dgOut++;
aux = aux.next;
}
return dgOut;
}
public int incidence(int i) {
if (!diGraph)
return degreeIn (i);
else return degreeIn (i) + degreeOut (i);
}
public int size() {
int size = 0;
for (int i=0; i<numVertex; i++)
size += numElementos (listAdy[i]);
if (!diGraph)
size = size / 2;
return size;
}
public int numElementos (List lista) {
ListNode aux = lista.firstNode;
int resul = 0;
while (aux != null) {
resul += 1;
aux = aux.next;
}
return resul;
}
public void printGraph () {
System.out.println("Number maximum of vertices: " + maxVertex + "\n");
System.out.println("the graph has " + numVertex + " vertex: \n");
for (int i = 0; i < numVertex; i++) {
System.out.print ("Vertex " + i + ": ");
toString (listAdy [i]);
}
}
public void toString (List lista) {
ListNode aux;
aux = lista.firstNode;
String str = "";
while (aux != null) {
str += aux.element +", ";
aux = aux.next;
}
if (str.length()>1)
str = str.substring(0, str.length()-2);
System.out.println ("["+str+"]");
}
}