-
Notifications
You must be signed in to change notification settings - Fork 0
/
WGraph.java
158 lines (134 loc) · 4.6 KB
/
WGraph.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
import java.io.*;
import java.util.*;
class Edge{
public int[] nodes = new int[2];
public Integer weight;
Edge(int i, int j, int w){
this.nodes[0] = i;
this.nodes[1] = j;
this.weight = w;
}
}
public class WGraph{
private ArrayList<Edge> edges = new ArrayList<Edge>();
private ArrayList<Integer> nodes = new ArrayList<Integer>();
private int nb_nodes = 0;
private Integer source = 0;
private Integer destination =0;
WGraph() {
}
WGraph(WGraph graph) {
for(Edge e:graph.edges){
this.addEdge(new Edge(e.nodes[0],e.nodes[1],e.weight));
}
this.source = graph.source;
this.destination = graph.destination;
}
WGraph(String file) throws RuntimeException {
try {
Scanner f = new Scanner(new File(file));
String[] ln = f.nextLine().split("\\s+");
this.source = Integer.parseInt(ln[0]);
this.destination = Integer.parseInt(ln[1]);
int number_nodes = Integer.parseInt(f.nextLine());
while (f.hasNext()){
String[] line = f.nextLine().split("\\s+");
if (line.length != 3){
continue;
}
int i = Integer.parseInt(line[0]);
int j = Integer.parseInt(line[1]);
int w = Integer.parseInt(line[2]);
Edge e = new Edge(i, j, w);
this.addEdge(e);
}
f.close();
/*Sanity checks*/
if (number_nodes != this.nb_nodes){
throw new RuntimeException("There are " + this.nb_nodes + " nodes while the file specifies " + number_nodes);
}
for (int i = 0; i < this.nodes.size(); i++){
if ((this.nodes.get(i) >= this.nb_nodes) || (this.nodes.get(i) < 0)){
throw new RuntimeException("The node " + this.nodes.get(i) + " is outside the range of admissible values, between 0 and " + this.nb_nodes + "-1");
}
}
if(!this.nodes.contains(source)){
throw new RuntimeException("The source must be one of the nodes");
}
if(!this.nodes.contains(destination)){
throw new RuntimeException("The destination must be one of the nodes");
}
}
catch (FileNotFoundException e){
System.out.println("File not found!");
System.exit(1);
}
}
public void addEdge(Edge e) throws RuntimeException{
int n1 = e.nodes[0];
int n2 = e.nodes[1];
if (this.nodes.indexOf(n1) >= 0 && this.nodes.indexOf(n2) >= 0){
for (int z = 0; z < this.edges.size(); z++){
int[] n = this.edges.get(z).nodes;
if ((n1 == n[0] && n2 == n[1])){
throw new RuntimeException("The edge (" + n1 + ", " + n2 + ") already exists");
}
}
}
if (this.nodes.indexOf(n1) == -1){
this.nodes.add(n1);
this.nb_nodes += 1;
}
if (this.nodes.indexOf(n2) == -1){
this.nodes.add(n2);
this.nb_nodes += 1;
}
this.edges.add(e);
}
public Edge getEdge(Integer node1, Integer node2){
for(Edge e:edges){
if(e.nodes[0]==node1 && e.nodes[1]==node2){
return e;
}
}
return null;
}
public int getSource(){
return this.source;
}
public int getDestination(){
return this.destination;
}
public void setEdge(Integer node1, Integer node2, int weight){
for(Edge e:edges){
if(e.nodes[0]==node1 && e.nodes[1]==node2){
e.weight=weight;
}
}
}
public ArrayList<Edge> listOfEdgesSorted(){
ArrayList<Edge> edges = new ArrayList<Edge>(this.edges);
Collections.sort(edges, new Comparator<Edge>() {
public int compare(Edge e1, Edge e2)
{
return e2.weight.compareTo(e1.weight);
}
});
return edges;
}
public ArrayList<Edge> getEdges(){
return this.edges;
}
public int getNbNodes(){
return this.nb_nodes;
}
public String toString(){
String out = Integer.toString(this.source)+ " " + Integer.toString(this.destination)+"\n";
out += Integer.toString(this.nb_nodes);
for (int i = 0; i < this.edges.size(); i++){
Edge e = edges.get(i);
out += "\n" + e.nodes[0] + " " + e.nodes[1] + " " + e.weight;
}
return out;
}
}