Repository navigation
Expand file tree
/
Copy pathGraph.java
More file actions
106 lines (86 loc) · 2.26 KB
/
Copy pathGraph.java
File metadata and controls
106 lines (86 loc) · 2.26 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
package edp;
import java.util.ArrayList;
/**
*
* @author Alvaro Berrocal Martin - URJC
*/
public class Graph {
private int [][] adjacent;
private int nodes;
private boolean empty;
final private int MAX =100000;
public Graph (int nodes){
this.nodes= nodes;
adjacent = new int [nodes][nodes];
for (int i=0; i<nodes; i++){
for (int j=0; j<nodes; j++){
adjacent [i][j] = MAX;
}
}
empty=true;
}
public int[][] getAdjacent() {
return adjacent;
}
public int getNodes() {
return nodes;
}
public void setAdjacent(int[][] adjacent) {
this.adjacent = adjacent;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public void setNodes(int nodes) {
this.nodes = nodes;
}
public boolean isEmpty(){
boolean empty= false;
for (int i=0; i<nodes; i++){
for (int j=0; j<nodes; j++){
empty = adjacent [i][j]==0;
if (!empty)
break;
}
if (!empty)
break;
}
return empty;
}
public int[] neighbors(int v){
ArrayList <Integer> l= new ArrayList <>();
for (int i =0; i<nodes ;i++){
int aux =adjacent [v][i];
if (aux!=MAX)
l.add(i);
}
int [] n = new int [l.size()];
for (int i=0 ; i< l.size();i++){
n[i]=l.get(i);
}
return n;
}
public int getWeight(int v1, int v2){
return adjacent [v1][v2];
}
public int[][] copyMatrix (){
int [][] copy = new int [adjacent.length][adjacent.length];
for (int i =0; i<adjacent.length; i++){
for(int j=0; j< adjacent.length;j++){
copy[i][j]= adjacent [i][j];
}
}
return copy;
}
@Override
public String toString() {
String s ="";
for (int i =0; i<adjacent.length; i++){
for(int j=0; j< adjacent.length;j++){
s= s+adjacent[i][j]+" |";
}
s= s+ '\n';
}
return s;
}
}